The following is a short algo I created. There are two things I need help with.
class CreativeTanGoshawk(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020,5,14) #Set Start Date
self.SetEndDate(2021,5,14) #Set End Date
self.SetCash(100000) # Set Strategy Cash
self.spy = self.AddEquity("TQQQ", Resolution.Daily)
self.window = RollingWindow[TradeBar](2)
self.DefaultOrderProperties.TimeInForce = TimeInForce.Day
def OnData(self, data):
self.window.Add(data["TQQQ"])
if not (self.window.IsReady): return
low = self.window[0].Low
high = self.window[0].High
close = self.window[0].Close
Open = self.window[0].Open
low1 = self.window[1].Low
high1 = self.window[1].High
close1 = self.window[1].Close
if not self.Portfolio.Invested:
if (low < low1) and (high < high1) and (close < Open) and (close < close1):
self.StopMarketOrder("TQQQ", 100, high)
else:
if self.Securities["TQQQ"].Price >= 1 + fill_price:
self.Liquidate("TQQQ")
def OnOrderEvent(self, orderevent):
if orderevent.Status == OrderStatus.Filled:
fill_price = orderevent.FillPrice
self.Debug(fill_price)
1 . I need to change the fill_price (in bold) to actually get the fill price of the order.
2. When I run a backtest, I keep getting order cancelled for every order.
Could someone please help me with this?
Varad Kabade
Hi Rishab Maheshwari,
The above algorithm cannot access fill_price in OnData() method because the variable storing the fill price has scope limited to the method OnOrderEvent. To fix this, whenever our order is filled, we save the price in an algorithm attribute as self.fill_price so it can be accessed by the OnData() method to process various types of orders. We have made the following change to prevent the algorithm from crashing as follows.
Refer to the attached backtest for reference.
Best,
Varad Kabade
Rishab Maheshwari
Hi,
I have attached a copy of the backtest after running this strategy, and I notice that the orders keep getting placed one day after the signal. It is buying at the correct high, but it is placing it the next day. For example, the most recent order was placed on 5/13/21. but the signal was met on 5/12/21, and it should have been placed when the market closes on 5/12/21. Also, in the backtest, all of the orders show as being cancelled even when they should be filled. Could you please advise me on how to fix this.
Varad Kabade
Hi Rishab Maheshwari,
When requesting data for a period(Daily, Hour, Minute), our algorithm receives the data about a particular time after the end time is passed; this prevents us from falling for look-ahead bias. Therefore any order sent from the data based on 5/12/21 will be processed on 5/13/21. Refer to this for further reference. The orders are filled in this version of the algorithm because it's using minute-resolution data. When placing orders with daily data, the orders are filled at the opening auction price of the next bar. Since the latest version of the algorithm using daily data and has set the TimeInForce
this causes the orders to be canceled at 4PM. The orders are never filled because the order is canceled before the next daily bar is passed to OnData. Refer to the attached backtest.
Best,
Varad Kabade
Rishab Maheshwari
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!