My first post here, so hello to everyone:)
I'm trying to learn backtesting in LEAN + Python. Maybe you could answer some of my questions:
1. I searched through the forum but couldn't find a good explanation how to backtest using a weekly or monthly resolution. I've tried to use Consolidators to merge daily bars into monthly bars and a monthly Scheduler to do actions based on those consolidated monthly bars, but it doesn't work well. Here is my code:
from AlgorithmImports import *
class MyProject1(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2021, 1, 1)
self.SetEndDate(2021, 4, 1)
self.Schedule.On(self.DateRules.MonthStart(), self.TimeRules.At(1,0), self.Monthly)
self.monthlyBar = {}
for ticker in ['AAPL']:
self.AddEquity(ticker, Resolution.Daily)
indicator = MonthlyBar(self)
self.monthlyBar[ticker] = indicator
tbc = TradeBarConsolidator(Calendar.Monthly)
self.RegisterIndicator(ticker, indicator, tbc)
def Monthly(self):
bar = self.monthlyBar['AAPL'].Bar
self.Log(f"Monthly:: time={self.Time} bar={bar}")
def OnData(self, data):
bar = data['AAPL']
self.Log(f"OnData:: time={self.Time} bar={bar}")
class MonthlyBar:
def __init__(self, algo):
self.Algo = algo
self.Value = None
self.IsReady = True
def Update(self, bar):
self.Algo.Log(f"Update:: time={bar.EndTime} algotime={self.Algo.Time} bar={bar}")
self.Bar = bar
self.Value = bar.Close
return True
The output log is:
20220222 14:48:05.943 TRACE:: Log: OnData:: time=2021-02-27 00:00:00 bar=AAPL: O: 122.61 H: 124.85 L: 121.2 C: 121.26 V: 149612830
20220222 14:48:05.943 TRACE:: Log: Monthly:: time=2021-03-01 01:00:00 bar=AAPL: O: 133.3607 H: 146.0318 L: 126.1914 C: 131.7631 V: 2087459953
20220222 14:48:05.943 TRACE:: Log: Update:: time=2021-03-01 00:00:00 algotime=2021-03-02 00:00:00 bar=AAPL: O: 133.4506 H: 137.875 L: 118.38 C: 121.26 V: 1727742132
20220222 14:48:05.943 TRACE:: Log: OnData:: time=2021-03-02 00:00:00 bar=AAPL: O: 123.78 H: 127.93 L: 122.79 C: 127.79 V: 110211547
The order of operation is not something I would expect - first we have the daily bar for the last day of Feb (OK), but then the monthly scheduler is called, and my indicator (consolidator based) is called last. So my scheduler does NOT have access to the latest monthly bar.
I'd expect it to be: last daily bar, then consolidator for February, and lastly the scheduler to make orders for March based on Feb data.
2. Is there some setting to do something about the dates in OnData? Right now it's a little awkward - a bar from Friday has a date from Saturday and 00:00:00 hour. I'd rather have Friday date. Hour and minutes don't matter as I'm working on a daily resolution.
Fred Painchaud
Hi Midra,
I recommend having a careful read of
A daily bar for Friday gets fed into OnData after the day is over, so Saturday 00:00:00.
An hourly bar for, say, 10h00, gets fed into Ondata at 11h00 that day…
Your monthly scheduler is a schedule even that happens on the first of each month.
I would recommend using something like this if you want to consolidate days into months:
Fred
Midra
OK, I think I managed to solve it based on this thread:
https://www.quantconnect.com/forum/discussion/5483/consolidation-several-sets-of-symbols/p1
I'll leave it here, maybe it'll be useful for someone. The first line in Monthly() is essential to make it work:
And as for the time in self.Time I understand how it works. I'm just saying that it's a little awkward to have Friday data under Saturday date.
Also in Consolidators you get Time and EndTime variables, and in this case Time is the start time, unlike elsewhere in the framework where it's the ending time. To make it consistent, Time in Consolidator bar should be the ending time, and instead of EndTime variable you should have StartTime.
Midra
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!