Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
New algorithian! The backtest takes too much time and there is too much order cancellation!

import numpy as np
import quantopian.algorithm as algo
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.filters import QTradableStocksUS

def initialize(context):
context.crm = sid(26401)
context.panw = sid(43202)

def handle_data(context, data):

x = data.history(context.crm, 'price', 253, '1d')  
y = data.history(context.panw, 'price', 253, '1d')  
diff=y-1.9469*x  
#print(type(x),type(y),type(diff))  
#log.info(diff.head())  
#print('difftest',diff)  
mean=diff.mean()  
#print('mean',mean)  
std=diff.std()  
#print(type(mean),type(std))  

for i in diff:  
    if i > (mean + std):  
        order_target_percent(context.crm,0.5)  
        order_target_percent(context.panw,-0.5)  
    elif i == mean:  
        order_target_percent(context.crm,0)  
        order_target_percent(context.panw,-0)  
    elif i < (mean - std):  
        order_target_percent(context.panw,0.5)  
        order_target_percent(context.crm,-0.5)  
3 responses

The backtesting itself does takes very long time to load and hence I can not properly show your the result. Hence you could just copy the code that I've displayed here and see yourself.
In the result what it shows it just loads tons of lines in the log stating this:
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for 28096 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for 28096 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for 28096 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for 28096 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for 28096 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for 28096 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for 28096 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for 28096 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for 28096 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for 28096 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for 28096 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for 28096 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for 28096 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.
2018-07-13 04:00 WARN Your order for -40050 shares of CRM failed to fill by the end of day and was canceled.

This is what it shows when you trying to run the backtest and it just takes too much time. Is there anything else that could improve the timing of running this backtest or how it could basically improved in terms of timing to run the backtest itself. Thanks!

There you go

Dear Jamie,

Massive Thanks!