trophy

Long Only, ETF Alpha Streams Competition with $27,500 Prize Pool

Back

Problem with including latest price

As I know, the history request does not include the latest/current price bar until it closes in the end of market and I want to add it to the previous data whenever i called.

My strategy is to buy 7-days low SPY at 15mins before market close. Also , at that time, the price should be above sma200.

Below is my first code but i got the error. Could someone help, please. Thanks!

Error:

BacktestingRealTimeHandler.Run(): There was an error in a scheduled event SPY: EveryDay: SPY: 30 min before MarketClose. The error was AttributeError : 'NoneType' object has no attribute 'Close'

from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from QuantConnect.Data.Market import TradeBar
from datetime import datetime, timedelta
import decimal

class DaysAlgorithm(QCAlgorithm):

def Initialize(self):

self.SetStartDate(2003,1,1)
self.SetEndDate(2019,4,1)
self.SetCash(10000)
self.spy = self.AddEquity("SPY", Resolution.Daily)

#consolidator = TradeBarConsolidator(timedelta(1))
#consolidator.DataConsolidated += self.OnDailyData
#self.SubscriptionManager.AddConsolidator("SPY", consolidator)
self.daily = RollingWindow[TradeBar](2)
self.window = RollingWindow[TradeBar](2)
self.spy.SetDataNormalizationMode(DataNormalizationMode.Raw)
self.leverage = 1
self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.BeforeMarketClose("SPY", 30), Action(self.Rebalance))

self.sma = self.SMA(self.spy.Symbol, 200, Resolution.Daily)
self.sma.Updated += self.SmaUpdated
self.smaWin = RollingWindow[IndicatorDataPoint](5)

def SmaUpdated(self, sender, updated):
self.smaWin.Add(updated)

def OnData(self, data):

self.window.Add(data["SPY"])

if not (self.window.IsReady and self.smaWin.IsReady): return

def Rebalance(self):

close = self.History(self.spy.Symbol, 6, Resolution.Daily)['close']
holdings = self.Portfolio[self.spy.Symbol].Quantity
currBar = self.window[0].Close
currSma = self.smaWin[0].Value

if not self.Portfolio.Invested and currBar > currSma and currBar < min(close):
self.SetHoldings(self.spy.Symbol, 1)
elif currBar < currSma or currBar > max(close):
self.SetHoldings(self.spy.Symbol, 0)

 

 

Update Backtest







0

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.


Hi KY,

There was a dividend event on Nov 15th 2014 of SPY. Therefore that data entry does not contain a tradebar and caused this error. To prevent it, you could put a None check at the top of OnData() method:

if not data.Bars.ContainsKey("SPY"): return

 

0

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.


Update Backtest





0

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.


Loading...

This discussion is closed