Hi,
the title says it all.
In one of my strategies the buy and sell actions happen within the OnData() function. I would like to force them to happen between a certain timeframe.
So far I had success doing it but only with hours, with a fragment of code like this one, (this is c#);
public override void OnData(Slice data)
{
if (!Portfolio.Invested)
{
if (Time.Hour>Convert.ToDouble(GetParameter("start trading hour")) && Time.Hour<Convert.ToDouble(GetParameter("end trading hour"))){
foreach (var universe in UniverseManager.Values) {
So basically let's say that Time..Hour>10 and Time.Hour<15 works well in my strategy, the problem is that I want also to use minutes. And if I want to use minutes, to, let's say, trade only between 10:20 and 15:10, it doesn't work (obviously) and I couldn't find a better solution.
Thanks
Emiliano Fraticelli
this way does not work either....
Maybe the only way is scheduling functions...
Emiliano Fraticelli
public override void OnData(Slice data) { TradeBars bars = data.Bars; var dateNow = DateTime.Now; var date = new DateTime(dateNow.Year, dateNow.Month, dateNow.Day, 9, 45, 0); //////////////////////////// SPY BLOCK /////////////////////////////////////////////////// if (data.Bars.ContainsKey("SPY") && Portfolio["SPY"].Invested==false && DateTime.Compare(Time, date)>0){ if (bars["SPY"].Price > highOfTheDaySpy && _sma_SPY.Current.Value!= 0){
One more attempt..but doesn't work
Derek Melchin
Hi Emiliano,
If we want to include minutes, we can use Scheduled Events to set a flag.
def Initialize(self): # ... self.Schedule.On(self.DateRules.EveryDay(symbol), self.TimeRules.At(start_hour, start_min), self.start) self.Schedule.On(self.DateRules.EveryDay(symbol), self.TimeRules.At(end_hour, end_min), self.end) def start(self): self.trade = True def end(self): self.trade = False
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.
Emiliano Fraticelli
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!