Good afternoon, thank you very much for the time spent reading the post. I want to apply daily rules to some assets, for example SPY, I understand that it is possible using the default framework through the OnData method but I understand that the simulation of the order will be done every day. Using the consolidated information and Consolidate methodology, I can simulate the order for example in minutes and continue to apply the rules with a handler. However, despite the rules being applied daily using the MarketOrder method, the results are not the same, there is another way to simulate the order in different time frames or it is only possible with consolidated? Thanks a lot.
Derek Melchin
Hi Julio,
We can use a Scheduled Event to initiate our trading at whatever interval we'd like
self.Schedule.On(self.DateRules.EveryDay(self.symbol), self.TimeRules.Every(timedelta(minutes=30)), self.trade)
With the Scheduled Event set, we just need to define the method it's calling. For example:
def trade(self): # Place order logic here if not (self.CurrentSlice.ContainsKey(self.symbol) and self.CurrentSlice[self.symbol] is not None): return self.MarketOrder(self.symbol, 1)
See the attached backtest for reference.
Best,
Derek Melchin
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.
Julio Cesar Franco
Hi Derek!
Thank you so much for your answer.Â
I'm not sure if with the Schedule method I can solve the issue. I need to test intrabar orders so basically I could get stop orders and limit orders filled in a realistic way because if I use daily bars, and I place a stop order for the next bar the order in the real world could be filled within the next bar before it closes.Â
http://help.tradestation.com/10_00/eng/tradestationhelp/subsystems/spr_topics/report/look_inside_bar_back_testing_strategy_performance_report_.htm
Best
Derek Melchin
Hi Julio,
If we aim to issue orders daily, we can place a Scheduled Event at the market open. The default fill model fills the SL and TP orders using the data resolution. When we subscribe to minute-resolution data for a given symbol, the SL and TP orders for that symbol will be filled with minute-resolution. For further assistance, attach a backtest which demonstrates the issue.
Best,
Derek Melchin
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.
Julio Cesar Franco
Hi Derek,
Again, thank you very much for responding.
Let's take an example, suppose I have a set of daily rules, I can simulate orders using daily data and the OnData method, or I can use daily consolidators with minute data, As the rules apply at the end of the day both backtesting should be the same but They are not :( Why? How is the best way to simulate intraday orders or for example use daily rules but with Stop Orders in minutes?
Â
Best,
Julio
import clr clr.AddReference("System") clr.AddReference("QuantConnect.Algorithm") clr.AddReference("QuantConnect.Indicators") clr.AddReference("QuantConnect.Common") from System import * from QuantConnect import * from QuantConnect.Algorithm import * from QuantConnect.Indicators import * class Example(QCAlgorithm): def Initialize(self): self.SetStartDate(2019, 1, 1) self.SetEndDate(2020, 1, 1) self.SetCash(1000000) self.ticker = "SPY" self.AddEquity(self.ticker, Resolution.Minute) self.symbol = self.Securities[self.ticker].Symbol self.daywindow = RollingWindow[float](3) self.Consolidate(self.symbol, Resolution.Daily, self.handler) def handler(self, consolidate): self.daywindow.Add(consolidate.Close) if not self.daywindow.IsReady: return if self.daywindow[0] > self.daywindow[1] > self.daywindow[2]: self.MarketOrder(self.symbol, 1) def OnData(self, data): pass
Â
Derek Melchin
Hi Julio,
There are a few reasons why the results differ between the two approaches when using the algorithm above:
If we are making trading decisions off of the daily returns as shown in the code above, we can subscribe the RateOfChange indicator to daily data but still trade with minute resolution. See the attached backtest for an example. It subscribes to minute data, but the indicators are updated with daily bars. To place the orders daily, we use a Scheduled Event. To extend the algorithm to use SL orders, refer to this thread.
Best,
Derek Melchin
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.
Julio Cesar Franco
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!