Overall Statistics |
Total Trades 11 Average Win 0% Average Loss 0% Compounding Annual Return 36.569% Drawdown 0.500% Expectancy 0 Net Profit 2.741% Sharpe Ratio 6.071 Probabilistic Sharpe Ratio 96.567% Loss Rate 0% Win Rate 0% Profit-Loss Ratio 0 Alpha 0.122 Beta 0.238 Annual Standard Deviation 0.041 Annual Variance 0.002 Information Ratio -3.381 Tracking Error 0.082 Treynor Ratio 1.035 Total Fees $11.00 Estimated Strategy Capacity $20000000.00 Lowest Capacity Asset IBM R735QTJ8XC9X |
# region imports from AlgorithmImports import * import pandas as pd import numpy as np from datetime import time, datetime, timedelta # endregion class MyAlgorithm(QCAlgorithm): def Initialize(self): self.SetStartDate(2021, 3, 15) # Set Start Date self.SetEndDate(2021, 4, 15) self.SetCash(100000) # Set Strategy Cash self.symbolData = {} self.ticker_list = ['AAPL', 'IBM', 'DIS', 'MSFT'] for ticker in self.ticker_list: symbol = self.AddEquity(ticker).Symbol self.symbolData[symbol] = SymbolData(self, symbol) self.invest = False self.sell = True self.liquidate = True ''' self.potential_buy = False self.indicator_week = False self.indicator_day = 0 self.indicatorHigh = 0 self.indicatorWeekHigh = 0 ''' self.potential_buy = {} self.indicator_week = {} self.indicator_day = {} self.indicatorHigh = {} self.indicatorWeekHigh = {} for symbol, data in self.symbolData.items(): # define a consolidator to consolidate data for this symbol on the requested period dailyConsolidator = TradeBarConsolidator(timedelta(days=1)) weeklyConsolidator = TradeBarConsolidator(Calendar.Weekly) # write up our consolidator to update the indicator dailyConsolidator.DataConsolidated += self.OnTwoDayBar weeklyConsolidator.DataConsolidated += self.OnTwoWeekBar # we need to add this consolidator so it gets auto updates self.SubscriptionManager.AddConsolidator(data.symbol, dailyConsolidator) self.SubscriptionManager.AddConsolidator(data.symbol, weeklyConsolidator) def OnTwoDayBar(self, sender, bar): self.symbolData[bar.Symbol].barWindow.Add(bar) self.barWindow = self.symbolData[bar.Symbol].barWindow if not self.barWindow.IsReady: return indicatorBar = self.barWindow[0] previousBar = self.barWindow[1] self.indicatorHigh[bar.Symbol] = indicatorBar.High indicatorLow = indicatorBar.Low previousHigh = previousBar.High previousLow = previousBar.Low # Inside Bar if (self.indicatorHigh[bar.Symbol] < previousHigh) and (indicatorLow > previousLow): self.indicator_day[bar.Symbol] = self.Time.day self.potential_buy[bar.Symbol] = True # 2Down if (self.indicatorHigh[bar.Symbol] < previousHigh) and (indicatorLow < previousLow): self.indicator_day[bar.Symbol] = self.Time.day self.potential_buy[bar.Symbol] = True def OnTwoWeekBar(self, sender, bar): self.symbolData[bar.Symbol].weekBarWindow.Add(bar) self.barWindow = self.symbolData[bar.Symbol].weekBarWindow if not self.barWindow.IsReady: return indicatorBar = self.barWindow[0] previousBar = self.barWindow[1] self.indicatorWeekHigh[bar.Symbol] = indicatorBar.High indicatorLow = indicatorBar.Low previousHigh = previousBar.High previousLow = previousBar.Low # Inside Bar if (self.indicatorWeekHigh[bar.Symbol] < previousHigh) and (indicatorLow > previousLow): self.indicator_week[bar.Symbol] = True self.Debug(str(self.indicator_week) + str(bar.Symbol.Value)) # 2Down if (self.indicatorHigh[bar.Symbol] < previousHigh) and (indicatorLow < previousLow): self.indicator_week[bar.Symbol] = True def OnData(self, data): for symbol in self.symbolData.keys(): symboldata = self.symbolData[symbol] if symboldata.IsReady(): profit = self.Portfolio[symbol].UnrealizedProfitPercent symbol_price = self.Securities[symbol].Price if (self.indicator_day[symbol] == self.Time.day) and (symbol_price > self.indicatorHigh[symbol]) and self.potential_buy[symbol]: shares_to_buy = int(self.Portfolio.Cash / data[symbol].Open) self.Debug(symbol) self.MarketOrder(symbol, (shares_to_buy*0.1)) self.potential_buy[symbol] = False if symbol in self.indicatorWeekHigh is True: if symbol in self.indicator_week is True: if (symbol_price > self.indicatorWeekHigh[symbol]) and self.indicator_week[symbol]: shares_to_buy = int(self.Portfolio.Cash / data[symbol].Open) self.Debug(symbol) self.MarketOrder(symbol, (shares_to_buy*0.1)) self.potential_buy[symbol] = False class SymbolData: def __init__(self, algorithm, symbol): self.algorithm = algorithm self.symbol = symbol self.barWindow = RollingWindow[TradeBar](2) self.weekBarWindow = RollingWindow[TradeBar](2) def IsReady(self): return self.barWindow.IsReady and self.weekBarWindow.IsReady