Hello,
I'm trying to get some code to run once a day that checks a bunch of pairs and then trades if certain conditions are met (I've presented the key parts to this below).
My issue is that when I run my code I get the output also shown below - as you can see it looks like it's being executed on the same day over and over. Any ideas where I could have gone so wrong?
6652 | 19:32:58:
2018-01-03 13:00:00
6653 | 19:32:58:
100 of 500
6654 | 19:32:58:
200 of 500
6655 | 19:32:58:
300 of 500
6656 | 19:32:58:
400 of 500
6657 | 19:32:58:
500 of 500
6658 | 19:32:58:
New Day
6659 | 19:32:58:
2018-01-03 13:00:00
6660 | 19:32:58:
100 of 500
6661 | 19:32:58:
200 of 500
6662 | 19:32:58:
300 of 500
6663 | 19:32:58:
400 of 500
6664 | 19:32:58:
500 of 500
6665 | 19:32:58:
New Day
6666 | 19:32:58:
2018-01-03 13:00:00
6667 | 19:32:58:
100 of 500
6668 | 19:32:58:
200 of 500
6669 | 19:32:58:
300 of 500
6670 | 19:32:58:
400 of 500
6671 | 19:32:58:
500 of 500
6672 | 19:32:58:
New Day
6673 | 19:32:58:
2018-01-03 13:00:00
6674 | 19:32:58:
100 of 500
6675 | 19:32:58:
200 of 500
6676 | 19:32:58:
300 of 500
6677 | 19:32:58:
400 of 500
6678 | 19:32:58:
500 of 500
6679 | 19:32:58:
New Day
6680 | 19:32:58:
2018-01-03 13:00:00
6681 | 19:32:58:
100 of 500
6682 | 19:32:58:
200 of 500
6683 | 19:32:58:
300 of 500
6684 | 19:32:58:
400 of 500
6685 | 19:32:58:
500 of 500
6686 | 19:32:58:
New Day
6687 | 19:32:58:
2018-01-03 13:00:00
6688 | 19:32:58:
100 of 500
6689 | 19:32:58:
200 of 500
6690 | 19:32:58:
300 of 500
6691 | 19:32:58:
400 of 500
6692 | 19:32:58:
500 of 500
class PairsTradingAlgorithm(QCAlgorithm):
def Initialize(self):
---some parameter stuff here not shown----
tickers = list(set(tickers))
self.symbols = []
for i in tickers:
self.symbols.append(self.AddEquity(i,Resolution.Daily).Symbol)
self.day_run = self.daily_run
self.SetStartDate(2018,1,1)
self.SetEndDate(2019,6,30)
self.lastSlice = None
self.pairs_list = pairs[0:500]
def OnData(self, data):
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
Arguments:
data: Slice object keyed by symbol containing the stock data'''
self.lastSlice = data
scheduled_function = self.day_run
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.At(13, 0), Action(scheduled_function))
def daily_run(self):
data = self.lastSlice
if data != None:
self.Debug('New Day')
self.Debug(str(self.Time))
run = 0
count = 0
for pair in self.pairs_list:
run += 1
count += 1
if count == 100:
self.Debug(str(run) + ' of ' + str(len(self.pairs_list)))
count = 0
if not (data.ContainsKey(pair[0]) and data.ContainsKey(pair[1])): return
### need to check this is the last 60 days
price_x = self.History([pair[0]], 60)['close'].values
price_y = self.History([pair[1]], 60)['close'].values
if len(price_x) < 60: return
------------Some trading logic ---------------
Jason Mark
I don't have the whole picture but generally,
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.At(13, 0), Action(scheduled_function))
is placed in the initialize function since the initialize function is only executed once. You may be using the schedule function wrong. https://www.quantconnect.com/docs/algorithm-reference/scheduled-events Placing it in the OnData function may be causing an infinite loop.
Hao Bin Zhang
You can also use the insert code snippet function so the text is more readable. But as the others have said try adding the schedule function before the Ondata Function. You might also want to specify balance and a brokerage model to make it more realistic.
Ralph West
Thank you both for your answers, clearly I should become more familiar with the documentation.
Ralph West
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!