Quantopian IDE backtest is very slow for the code below, even for dates that are 6 months apart. Whats the way out? How can I speed it up?
import numpy as np
import math as math
def initialize(context):
context.security = symbols("AAPL","NFLX")
def getMovingVol(data, security, n):
m=n*420
price_hist = data.history(security, 'price', m ,'1m')
a=price_hist.mean()
diff=price_hist-a
diff_sqr=diff*diff
diff_sqr_sum=math.fsum(diff_sqr)
diff_sqr_sum_avg=diff_sqr_sum/m
diff_sqr_sum_avg_sqrt=math.sqrt(diff_sqr_sum_avg)
return(diff_sqr_sum_avg_sqrt)
def handle_data(context, data):
for security in context.security:
if security in data:
mvvol2 = getMovingVol(data, security, 2) # 2 day mavg
mvvol3 = getMovingVol(data, security, 3) # 2 day mavg
mvvol7 = getMovingVol(data, security, 7) # 1 week mvg
mvvol30 = getMovingVol(data, security, 30) # 1 month mvg
mvvol90 = getMovingVol(data, security, 90) # 1 qtr mvg
record(Vol_2=mvvol2,
Vol_3=mvvol3,
Vol_WK=mvvol7,
Vol_MN=mvvol30,
Vol_QTR=mvvol90)