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
AMit
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!