Back

Scheduled code runs on the same day over and over

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:006653 | 19:32:58: 100 of 5006654 | 19:32:58: 200 of 5006655 | 19:32:58: 300 of 5006656 | 19:32:58: 400 of 5006657 | 19:32:58: 500 of 5006658 | 19:32:58: New Day6659 | 19:32:58: 2018-01-03 13:00:006660 | 19:32:58: 100 of 5006661 | 19:32:58: 200 of 5006662 | 19:32:58: 300 of 5006663 | 19:32:58: 400 of 5006664 | 19:32:58: 500 of 5006665 | 19:32:58: New Day6666 | 19:32:58: 2018-01-03 13:00:006667 | 19:32:58: 100 of 5006668 | 19:32:58: 200 of 5006669 | 19:32:58: 300 of 5006670 | 19:32:58: 400 of 5006671 | 19:32:58: 500 of 5006672 | 19:32:58: New Day6673 | 19:32:58: 2018-01-03 13:00:006674 | 19:32:58: 100 of 5006675 | 19:32:58: 200 of 5006676 | 19:32:58: 300 of 5006677 | 19:32:58: 400 of 5006678 | 19:32:58: 500 of 5006679 | 19:32:58: New Day6680 | 19:32:58: 2018-01-03 13:00:006681 | 19:32:58: 100 of 5006682 | 19:32:58: 200 of 5006683 | 19:32:58: 300 of 5006684 | 19:32:58: 400 of 5006685 | 19:32:58: 500 of 5006686 | 19:32:58: New Day6687 | 19:32:58: 2018-01-03 13:00:006688 | 19:32:58: 100 of 5006689 | 19:32:58: 200 of 5006690 | 19:32:58: 300 of 5006691 | 19:32:58: 400 of 5006692 | 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 ---------------



 

Update Backtest







0

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.


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.

1

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. 

0

Thank you both for your answers, clearly I should become more familiar with the documentation.

 

0

Update Backtest





0

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.


Loading...

This discussion is closed