DO we have any example of open close proce crossover indicator example?
We can do this in Tradingview by below code
tim=input('370')
out1 = security(tickerid, tim, open) #return open price series
out2 = security(tickerid, tim, close) #return open price series
plot(out1,color=red)
plot(out2,color=green)
longCondition = crossover(security(tickerid, tim, close),security(tickerid, tim, open))
if (longCondition)
strategy.entry("long", strategy.long)
shortCondition = crossunder(security(tickerid, tim, close),security(tickerid, tim, open))
if (shortCondition)
strategy.entry("short", strategy.short)
I tried to rewrite same in quantconnect but getitng different result.
def Initialize(self):
'''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''
self.SetStartDate(2018,1,1) #Set Start Date
self.SetEndDate(2018,7,30) #Set End Date
self.SetCash(100000) #Set Strategy Cash
# Find more symbols here: http://quantconnect.com/data
self.AddEquity("BAC")
# define our 30 minute trade bar consolidator. we can
# access the 30 minute bar from the DataConsolidated events
thirtyMinuteConsolidator = TradeBarConsolidator(timedelta(minutes=370))
# attach our event handler. the event handler is a function that will
# be called each time we produce a new consolidated piece of data.
thirtyMinuteConsolidator.DataConsolidated += self.ThirtyMinuteBarHandler
# this call adds our 30 minute consolidator to
# the manager to receive updates from the engine
self.SubscriptionManager.AddConsolidator("BAC", thirtyMinuteConsolidator)
self.__last = None
def OnData(self, data):
'''We need to declare this method'''
pass
def ThirtyMinuteBarHandler(self, sender, consolidated):
'''This is our event handler for our 30 minute trade bar defined above in Initialize(). So each time the
consolidator produces a new 30 minute bar, this function will be called automatically. The 'sender' parameter
will be the instance of the IDataConsolidator that invoked the event, but you'll almost never need that!'''
if self.__last is not None and consolidated.Close >= consolidated.Open and self.__last.Close <= self.__last.Open:
#self.Liquidate("SPY")
#self.Log("{consolidated.Time} >> SPY >> LONG >> 100 >> {self.Portfolio['SPY'].Quantity}")
self.Debug("buying stock for long position SPY" + str(consolidated.Price))
self.Order("BAC", 100)
if self.__last is not None and consolidated.Close <= consolidated.Open and self.__last.Close >= self.__last.Open:
#self.Debug("liquidating stock for long position SPY" )
#self.Liquidate("SPY")
self.Debug("buying stock for short position BAC" + str(consolidated.Price))
self.Order("BAC", -100)
self.__last = consolidated