def handle_data(context, data): runs every minute (or whatever parameter you set it to) however the problem with requesting data in this function is it requests a ton over and over again.
For example, I need the last 26 periods of data to calculate an EMA with a timeperiod of 26… however the second time the handle_data function runs, I just need to upload 1 period of data, not all 26 rows again.
Think of it like this…my code requests data 1 through 26, and when I need the 27th point of data, it can't just append that to the historic data variable. It just renews by uploading data 2 through 27.
How can I fix this??? If I can make a global variable called 'hist' and store my data in there, and then just append the latest period every single time that is the most efficient and ideal way to do this.
Hello Kevin,
You can store all the data for subsequent runs of the function handle_data in the context variable passed to it.
You can store previous data as follows:
context.prev_data = last_window # last window will keep 1 to 26
It can be retrieved in subsequent runs. Do tell me if further clarification is needed.
Hi Akshay, this is exactly what I intended to do this is amazing thank you!
However, one follow up question…how can I append data to this list? As it seems like the way I am doing it isn't working.
def handle_data(context, data):
start_time = time.time()
print('def_handle started at:',start_time - start_time)
sTime = get_datetime('US/Eastern')
#sTime is the IB server time, get_datetime() is the build-in function to obtain IB server time
print('context run is:', context.run_once)
if context.run_once is False:
hist = data.history(context.security,'close', 27 , '1m' )
#First we calculate the 26 and 12 day exponential moving averages
calculation_time = time.time()
print('calculations finished:',calculation_time-start_time)
context.run_once = True
context.prev_data = hist
elif context.run_once is True:
hist = data.history(context.security, 'close', 1, '1m')
context.prev_data.append(hist)
Nevermind I got it! just had to change the last line to: context.prev_data = context.prev_data.append(hist)