Hi, I am new to quant connect and was trying my first simple algorithm backtesting code (it is very simple code), shown below
# region imports
from AlgorithmImports import *
# endregion
class FormalFluorescentYellowArmadillo(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2024, 1, 1) # Set Start Date
self.SetEndDate(2025, 1, 1) # Set End Date
self.SetCash(100000) # Set Strategy Cash
spy = self.AddEquity("SPY", Resolution.Daily)
# self.AddForex, self.AddFuture...
spy.SetDataNormalizationMode(DataNormalizationMode.Raw)
self.spy = spy.Symbol
self.SetBenchmark("SPY")
self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin)
self.entryPrice = 0
self.period = timedelta(31)
self.nextEntryTime = self.Time
def OnData(self, data):
#if not self.spy in data:
if not data.contains_key(self.spy):
return
price = data.Bars[self.spy].Close
#price = data[self.spy].Close
# price = self.Securities[self.spy].Close
if not self.Portfolio.Invested:
if self.nextEntryTime <= self.Time:
self.SetHoldings(self.spy, 1)
# self.MarketOrder(self.spy, int(self.Portfolio.Cash / price) )
self.Log("BUY SPY @" + str(price))
self.entryPrice = price
elif self.entryPrice * 1.1 < price or self.entryPrice * 0.90 > price:
self.Liquidate()
self.Log("SELL SPY @" + str(price))
self.nextEntryTime = self.Time + self.period
However when I built and run backtesting, it pops runtime error shown below:
Runtime Error: 'SPY' wasn't found in the TradeBars object, likely because there was no-data at this moment in time and it wasn't possible to fillforward historical data. Please check the data exists before accessing it with data.ContainsKey("SPY")
at OnData
price = data.Bars[self.spy].Close
~~~~~~~~~^^^^^^^^^^
in main.py: line 29
It looks it is unhappy about line 29, for unknown reason.
would you mind help me?
Ahmed
Mia Alissi
Here's the revised version of Ahmed's algorithm with corrections:
This version should resolve any runtime errors and improve the algorithm's functionality.
Sources: Opening Range Breakout for Stocks in Play, Momentum Effect In Commodities Futures, Gold Market Timing, Momentum And State Of Market Filters, Asset Growth Effect
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.
Avi Sharma
Facing similar issue as explained above after trying the proposed resolution, can someone recommend alternative solution ?
Ahmed Khidre
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!