| Overall Statistics |
|
Total Trades 304 Average Win 0.31% Average Loss -0.38% Compounding Annual Return -1.620% Drawdown 9.700% Expectancy -0.076 Net Profit -4.490% Sharpe Ratio -0.36 Probabilistic Sharpe Ratio 0.687% Loss Rate 49% Win Rate 51% Profit-Loss Ratio 0.82 Alpha -0.032 Beta 0.116 Annual Standard Deviation 0.043 Annual Variance 0.002 Information Ratio -1.423 Tracking Error 0.115 Treynor Ratio -0.132 Total Fees $616.76 |
class OpeningRangeBreakout(QCAlgorithm):#inheritance of class QCAlgorithm
openingBar = None #variable used while creating consolidated bar for first 30 min to save high,low,close
secondaryBar = None# variabe to save 2nd 30 min bar data
def Initialize(self):
self.SetStartDate(2016, 7, 10) #start date
self.SetEndDate(2019, 5, 2) #end date
self.SetCash(100000)#enter the cah amount
self.AddEquity("SPY", Resolution.Minute)#"spy" equity is added,giving updated high low for a minute
self.Consolidate("SPY", timedelta(minutes=30), self.OnDataConsolidated)
#creating a consolidation by aggregating the 30 1 min bar obtained from resolution.minute
self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.BeforeMarketClose("SPY", 10), self.ClosePositions)
#trigger a method at particular time,i.e closeposition(mentioned below)
#trigger the sschedule even before 10 min of market close
def OnData(self, data):
#If self.Portfolio.Invested is true, or if the openingBar is None, return
if self.Portfolio.Invested or self.openingBar is None or self.secondaryBar is None:
return
#self.Debug("portfolo:" + str(self.Portfolio.Invested))
#Check if the close price is above the high price, if so go 100% long on SPY
if self.secondaryBar.Close > self.openingBar.High:
self.SetHoldings("SPY", 1)#buy 100 % for spy
self.Debug("time" + str(self.Time))#debug is like print statement,self. time provide the exact time
self.Debug("op time" + str(self.openingBar.EndTime))#provide time at the end of 1st baar, i.e 10am
self.Debug("sectime" + str(self.secondaryBar.EndTime))#provide time at the end of 2nd bar i.e 10:30am
#there is no need for other condition,like if secondaryBar.close<openingBar.low,we are not gonna make any buy in that case.
def OnDataConsolidated(self, bar):#it stores the high low/low/close of bar for the mentioned duration
if bar.Time.hour == 9 and bar.Time.minute == 30:
self.openingBar = bar
if bar.Time.hour == 10 and bar.Time.minute == 0:
self.secondaryBar = bar
def ClosePositions(self):#this method is called by self.scheduled at a given time during end of the day
# Set self.openingBar to None, and liquidate SPY
self.secondaryBar = None
self.openingBar = None#reset bar at the given time of triggerring of self.scheduled
#self.Debug("beforeliquid"+str(self.Portfolio["SPY"].Quantity))
self.Liquidate("SPY")#sell every thing if bought
#self.Debug("afterliquid"+str(self.Portfolio["SPY"].Quantity))