As a dry run at taking an algo live, I took a (only very slightly modified) bootcamp solution and tried to launch it on to Alpaca paper trading.
class OvernightBeatdown(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2017, 11, 1)
self.SetEndDate(2018, 7, 1)
self.SetCash(100000)
self.AddEquity("TSLA", Resolution.Minute)
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.BeforeMarketClose("TSLA", 0), self.ClosingBar)
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen("TSLA", 1), self.OpeningBar)
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen("TSLA", 45), self.ClosePositions)
self.action = 0
self.window = RollingWindow[TradeBar](2)
# Warmup for 5K bars ("periods") of your security resolution
self.SetWarmUp(5000)
#1. Create a manual Standard Deviation indicator to track recent volatility
self.volatility = StandardDeviation("TSLA", 60)
def OnData(self, data):
if data["TSLA"] is not None:
#2. Update our standard deviation indicator manually with algorithm time and TSLA's close price
self.volatility.Update(self.Time, data["TSLA"].Close)
def OpeningBar(self):
if "TSLA" in self.CurrentSlice.Bars:
self.window.Add(self.CurrentSlice["TSLA"])
#3. Use IsReady to check if both volatility and the window are ready, if not ready 'return'
if not self.window.IsReady or not self.volatility.IsReady:
return
delta = self.window[0].Open - self.window[1].Close
#4. Save an approximation of standard deviations to our deviations variable by dividing delta by the current volatility value:
# Normally this is delta from the mean, but we'll approximate it with current value for this lesson.
deviations = delta / self.volatility.Current.Value
#5. SetHoldings to 100% TSLA if deviations is less than -3 standard deviations from the mean:
if deviations < -3:
self.SetHoldings("TSLA", 1)
self.action = self.action + 1
self.Plot("Action", "Opertunity", self.action)
self.Debug("Opening Price Collected")
def ClosePositions(self):
self.Liquidate()
self.Debug("Holdings Liquidated")
def ClosingBar(self):
self.window.Add(self.CurrentSlice["TSLA"])
self.Debug("Closeing Price Collected")
It backtests fine, what when it goes live, I get this error at the first EOD:
at QuantConnect.Ex: tendedDictionary`1[T].get_Item (System.String ticker) [0x00020] in <4fc3bcd66016475d97c581dc9193f674>:0
at (wrapper manage: d-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflecti: on.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <b0e1ad7573a24fd5a9f2af9595e677e7>:0
2020-06-15 20:00:04 : Runtime Error: LiveTradingRealTimeHandler.Run(): There was an error in a scheduled event EveryDay: TSLA: 0 min before MarketClose. The error was KeyNotFoundException : 'TSLA' wasn't found in the Slice 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("TSLA")
at QuantConnect.Da: ta.Slice.get_Item (QuantConnect.Symbol symbol) [0x00028] in <4fc3bcd66016475d97c581dc9193f674>:0
at QuantConnect.Ex: tendedDictionary`1[T].get_Item (System.String ticker) [0x00020] in <4fc3bcd66016475d97c581dc9193f674>:0
at (wrapper manage: d-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflecti: on.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <b0e1ad7573a24fd5a9f2af9595e677e7>:0 : StackTrace: System.Exception: LiveTradingRealTimeHandler.Run(): There was an error in a scheduled event EveryDay: TSLA: 0 min before MarketClose. The error was KeyNotFoundException : 'TSLA' wasn't found in the Slice 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("TSLA")
at QuantConnect.Da: ta.Slice.get_Item (QuantConnect.Symbol symbol) [0x00028] in <4fc3bcd66016475d97c581dc9193f674>:0
at QuantConnect.Ex: tendedDictionary`1[T].get_Item (System.String ticker) [0x00020] in <4fc3bcd66016475d97c581dc9193f674>:0
at (wrapper manage: d-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflecti: on.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <b0e1ad7573a24fd5a9f2af9595e677e7>:0
Is there a different way that I should be acessing the price at the close of regular trading hours?
Shile Wen
Hi T. Dalton Combs,
The error is caused by line 27, try changing that line from if data["TSLA"] is not None to if "TSLA" in data. This error is caused because "TSLA" may not always be a key in data when data for "TSLA" isn't available. Since this is for live trading, I won't be able to attach a backtest.
Best,
Shile Wen
T. Dalton Combs
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!