Hi all;
In the boot camp “Algorithmic Trading Using Python #5 - Trading & Orders” it as the following in OnData()
if self.stopMarketTicket is not None and self.Portfolio.Invested:
# move up trailing stop price
if price > self.highestPrice:
self.highestPrice = price
updateFields = UpdateOrderFields()
updateFields.StopPrice = price * 0.95
self.stopMarketTicket.Update(updateFields)
But this leaves the question, how often is this then going to be called? Is it every minute during the day, just on open, just on close, maybe every 5th day?
This is leaving it to when OnData() is called to see how tightly the stop loss is tied to the stock price. Which IMHO is not good, I should be deciding how often to do this. I can do it with a datetime member variable to change it less often, but I need to know when OnData will fire to insure that when I do want to recalculate, OnData will shortly be firing.
thanks - dave
David Thielen
Hi Mak;
Here is a good example. In this example nothing happens for 30 days because the data is not ready on the first OnData call and OnData is not called again for 30 days. This is from boot camp “Algorithmic Trading Using Python #6 - Trading & Orders”
David Thielen
Hi Mak;
Here is another example - from “Algorithmic Trading Using Python #7 - Consolidators and Rolling Windows”. In this code it has:
The above code depends on OnData() being called at exactly 9:31. Is this going to happen because of the "self.AddEquity("SPY", Resolution.Minute)"? And is that the purpose of the resolution in AddEquity?
And… what if due to delays in other methods called, by the time it calls this, it's now 9:32. Can that happen? Or is self.Time set to what the time should be when this is called?
thanks - dave
Vncne
OnData is run every time your algorithm receives new data, so this depends on what resolution your data is subscribed to. Resolution.Minute will make OnData run after every minute closes, Resolution.Hour after every time the hour closes, etc. Alternatively, you could also set up a scheduled event in Initialize() so you can get code to run exactly when you want everyday.
Also, simply warm up your algorithm in Initialize() using self.SetWarmUp(period) so that you don't have to wait 30 days for OnData to run the first time.
David Thielen
Hi Vncne;
Thank you. This is helping me make sense of all this. Finally, I think…
Question 1: Ok, so in a lot of the examples you're trading one stock. And so when the stock updates, that's when it's time to determine if I want to do something. That works great.
But say I have 40 stocks I'm working with, and I'm going to buy the 10 that best fit my criteria. So I need all 10 to update to their latest data (let's say daily and it's after the market closed). What then?
Question 2: Working my way through the boot camp videos, they tend to mention self.SetWarmUp(period) but then not use it. So I was figuring that best practices was not call anything in the methods that could block. Is that wrong? Should I always use self.SetWarmUp(period) so I know my data is all read in?
thanks - dave
Varad Kabade
Hi David Thielen,
The Ondata method is the primary event handler for securities in the Universe; it is called whenever there is data for the subscribed security(trade and non-trade data), so in case of daily resolution, it would be called at midnight when the day has ended and you could iterate through the data to scan for signals and any order placed would be converted to OnMarketOpen order type. We do not need to explicitly check if our indicators are ready we can have an Updated method to receive updates whenever to scan for insights.You can either use self.SetWarmUp or use history request to make your indicators ready. It is always a good practice to check whether an indicator is ready before using it:
Best,
Varad Kabade
David Thielen
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!