Overall Statistics
Total Orders
8347
Average Win
0.46%
Average Loss
-0.74%
Compounding Annual Return
19.472%
Drawdown
77.100%
Expectancy
0.135
Start Equity
60000
End Equity
5111710.69
Net Profit
8419.518%
Sharpe Ratio
0.579
Sortino Ratio
0.677
Probabilistic Sharpe Ratio
1.107%
Loss Rate
30%
Win Rate
70%
Profit-Loss Ratio
0.62
Alpha
0.098
Beta
1.062
Annual Standard Deviation
0.249
Annual Variance
0.062
Information Ratio
0.551
Tracking Error
0.183
Treynor Ratio
0.136
Total Fees
$13860.40
Estimated Strategy Capacity
$3000000.00
Lowest Capacity Asset
EMCG R735QTJ8XC9X
Portfolio Turnover
4.45%
#region imports
from AlgorithmImports import *
#endregion

# Your New Python File
class SymbolData():
    def __init__(self) -> None:
        self._price: List[float] = []
        
    def update_price(self, price: float) -> None:
        self._price.insert(0, price)

    def get_performance(self) -> float:
        return self._price[0] / list(self._price)[-1] - 1

    def reset(self) -> None:
        self._price = self._price[:1]

    def is_ready(self) -> bool:
        return len(self._price) > 1

# Custom fee model
class CustomFeeModel(FeeModel):
    def GetOrderFee(self, parameters: OrderFeeParameters) -> OrderFee:
        fee: float = parameters.Security.Price * parameters.Order.AbsoluteQuantity * 0.00005
        return OrderFee(CashAmount(fee, "USD"))
# region imports
from AlgorithmImports import *
import data_tools
from dateutil.relativedelta import relativedelta
from typing import List, Dict, Set
from pandas.core.frame import DataFrame
from pandas.core.series import Series
# endregion

class MetatronYTDHigh(QCAlgorithm):

    _notional_value: int = 60_000
    _trade_exec_minute_offset: int = 15

    # pick max sector percentage of total traded stock count
    _sector_max_percentage: float = 0.20

    _top_count: int = 10
    _week_period: int = 51

    _minute_resolution: bool = True

    def initialize(self) -> None:
        self.set_start_date(2000, 1, 1)
        self.set_cash(self._notional_value)

        # universe count by market cap
        self._fundamental_count: int = 500
        self._fundamental_sorting_key = lambda x: x.market_cap

        self.universe_settings.leverage = 5
        self._tickers_to_ignore: List[str] = ['EGAN', 'CR', 'PATH', 'ILIF', 'CRW']
        self._exchange_codes: List[str] = ['NYS', 'NAS', 'ASE']    

        self._data: Dict[Symbol, data_tools.SymbolData] = {}
        self._sector_manager: Dict[int, int] = {}
        self._last_selection: List[Symbol] = []

        resolution = Resolution.MINUTE if self._minute_resolution else Resolution.DAILY

        market: Symbol = self.add_equity('SPY', resolution).Symbol

        self._selection_flag: bool = False
        self._trade_flag: bool = False
        self.universe_settings.resolution = resolution
        self.add_universe(self.fundamental_selection_function)
        self.settings.minimum_order_margin_portfolio_percentage = 0.
        self.settings.daily_precise_end_time = False

        self.schedule.on(self.date_rules.every_day(market),
                        self.time_rules.before_market_close(market),
                        self.selection)

        if self._minute_resolution:
            self.schedule.on(self.date_rules.every_day(market),
                            self.time_rules.before_market_close(market, self._trade_exec_minute_offset),
                            self.rebalance)

        self._counter: int = 0
        self._current_week: int = -1
        self._curr_year: int = -1

    def on_securities_changed(self, changes: SecurityChanges) -> None:
        for security in changes.added_securities:
            security.set_fee_model(data_tools.CustomFeeModel())

    def fundamental_selection_function(self, fundamental: List[Fundamental]) -> List[Symbol]:
        if not self._selection_flag:
            return Universe.UNCHANGED
        if not self._minute_resolution:
            self._selection_flag = False

        # update data
        for stock in fundamental:
            symbol: Symbol = stock.symbol
            
            if symbol in self._data:
                self._data[symbol].update_price(stock.adjusted_price)

        selected: List[Fundamental] = [
            x for x in fundamental 
            if x.SecurityReference.ExchangeId in self._exchange_codes
            and x.Market == 'usa'
            and x.symbol.value not in self._tickers_to_ignore
        ]
        if len(selected) > self._fundamental_count:
            selected = [x for x in sorted(selected, key=self._fundamental_sorting_key, reverse=True)[:self._fundamental_count]]

        # price warmup
        for stock in selected:
            symbol: Symbol = stock.symbol
            
            if stock.asset_classification.morningstar_sector_code not in self._sector_manager:
                self._sector_manager[stock.asset_classification.morningstar_sector_code] = self._top_count * self._sector_max_percentage

            if symbol not in self._data:
                self._data[symbol] = data_tools.SymbolData()

                lookback: int = self.time.isocalendar().week - 1 if self.time.isocalendar().week != 1 else self.time.isocalendar().week + self._week_period
                history: DataFrame = self.history(symbol, start=self.time.date() - relativedelta(weeks=lookback), end=self.time.date())

                if history.empty:
                    self.log(f"Not enough data for {symbol} yet.")
                    continue
                data: Series = history.loc[symbol]
                data_periodically: Series = data.groupby(pd.Grouper(freq='W')).first()
                for time, row in data_periodically.iterrows():
                    self._data[symbol].update_price(row.close)

        self._last_selection = [x.symbol for x in selected if self._data[x.symbol].is_ready()]

        if not self._minute_resolution:
            self._trade_flag = True

        return self._last_selection

    def on_data(self, slice: Slice) -> None:
        # order execution
        if not self._trade_flag:
            return
        self._trade_flag = False

        stock_performance: Dict[Symbol, float] = {symbol: self._data[symbol].get_performance() for symbol in self._last_selection if self._data[symbol].is_ready()}

        long: List[Symbol] = []

        # sort and divide
        if len(stock_performance) >= self._top_count:
            sorted_stocks: List[Symbol] = sorted(stock_performance, key=stock_performance.get, reverse=True)
            for symbol in sorted_stocks:
                sector_code: str = self.securities[symbol].fundamentals.asset_classification.morningstar_sector_code
                if sector_code == 0:
                    self.log(f'sector code missing for ticker: {symbol.value}')
                    continue
                if len(long) >= self._top_count:
                    break
                if self._sector_manager[sector_code] == 0:
                    continue
                long.append(symbol)
                self._sector_manager[sector_code] -= 1

        # trade execution
        targets: List[PortfolioTarget] = []
        for symbol in long:
            if slice.contains_key(symbol) and slice[symbol]:
                if slice[symbol].value != 0:
                    targets.append(PortfolioTarget(symbol, 1 / len(long)))
        
        self.set_holdings(targets, True)
        self._last_selection.clear()

        for sector, _ in self._sector_manager.items():
            self._sector_manager[sector] = self._top_count * self._sector_max_percentage

    def selection(self) -> None:
        if self.time.isocalendar().week == self._current_week:
            return
        self._current_week = self.time.isocalendar().week
        self._counter += 1
        if self._counter == 2:
            self._selection_flag = True
            self._counter = 0

        # reset price data
        if self.time.year != self._curr_year:
            self._curr_year = self.time.year
            for symbol, symbol_data in self._data.items():
                symbol_data.reset()

    def rebalance(self) -> None:
        if self._selection_flag:
            self._trade_flag = True
            self._selection_flag = False