Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Question on Linear regression

Hi all, I am new to python and what's wrong with my below code inside the "my rebalance"?

from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline import Pipeline  
from quantopian.pipeline import CustomFactor  
from quantopian.pipeline.data.builtin import USEquityPricing  
from quantopian.pipeline.factors import AverageDollarVolume, SimpleMovingAverage, RSI  
from quantopian.pipeline import CustomFactor  
import talib  
import math  
import numpy as np  
import pandas as pd  
import statsmodels.api as sm

def make_pipeline():  
    pipe = Pipeline()  
    return pipe

def before_trading_start(context, data):  
    context.output = pipeline_output('my_pipeline')  
    context.security_listF = {}  
    context.vix = context.output["VixClose"].iloc[0]  
    context.can_trade = 0  
    context.security_list = [symbol('MMM'),symbol('AXP'),symbol('AAPL'),symbol('BA'),symbol('CAT'),symbol('CVX'),symbol('CSCO'),symbol('KO'),symbol('DIS'),symbol('DD'),symbol('XOM'),symbol('GE'),symbol('GS'),symbol('HD'),symbol('IBM'),symbol('INTC'),symbol('JNJ'),symbol('JPM'),symbol('MCD'),symbol('MRK'),symbol('MSFT'),symbol('NKE'),symbol('PFE'),symbol('PG'),symbol('TRV'),symbol('UTX'),symbol('UNH'),symbol('VZ'),symbol('V'),symbol('WMT')]  
def my_rebalance(context,data):

     high1 = data.history(context.security_list,'high',20,'1d')  
     close1 = data.history(context.security_list,'close',20,'1d')  
     ma = close1.mean()  
     highest1 = max(high1)  
     y1 = close1 - highest1 - ma  
     high2 = data.history(context.security_list,'high',21,'1d')[:-1]  
     close2 = data.history(context.security_list,'close',21,'1d')[:-1]  
     ma2 = close2.mean()  
     highest2 = max(high2)  
     y2 = close2 -highest2 - ma2  
     val1 = sm.OLS(y1, 20).fit.params[0]  
     val2 = sm.OLS(y2, 20).fit.params[0]  
8 responses

This may help.

import statsmodels.api as sm  
import talib

def initialize(context):  
    schedule_function(record_slope, date_rules.week_start(), time_rules.market_open(minutes = 65)) 

def record_slope(context,data):  
    stock, ma, lr = symbol('MMM'), 20, 20

    C = data.history(stock, 'close', ma + lr,'1d')  
    sma = talib.SMA(C, ma)  
    y = C[-lr:] - sma[-lr:]  
    slope = sm.OLS(y, sm.add_constant(range(-len(y) + 1, 1))).fit().params[1]

    record(slope = slope)  

Thanks Vladimir. You helped me a lot. But I have find some strange thing:

def initialize(context):  
    schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open(minutes=1))  
    schedule_function(my_record_vars, date_rules.every_day(), time_rules.market_open())  
    attach_pipeline(make_pipeline(), 'my_pipeline')  
    context.vix_hist = {}  
    context.vl = 18  
    context.vs = 26  
    context.trail = {}  
    context.security_list = [symbol('AXP'), symbol('AAPL')]

def my_rebalance(context,data):

    ma = 40  
    lr = 20  
    lm = 21  
    C = data.history(context.security_list, 'close', ma, "1d")  
    H = data.history(context.security_list, 'high', ma, "1d")  
    L = data.history(context.security_list, 'low', ma, "1d")  
    print H  

in the log, the High value is correct on or before 7 Nov but not correct afterwards

Try with :

 schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_close())  

i have changed the time rules to time_rules.market_close(), but the problem is still there.

https://na.cx/i/dyWPCiJ.png

Hi Vladimir, I can see your reply in the email, but cant find it here.

anyway, please change the ma to 40 and check the log on 8 Nov and 9 Nov

from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline import Pipeline  
from quantopian.pipeline import CustomFactor  
from quantopian.pipeline.data.builtin import USEquityPricing  
from quantopian.pipeline.factors import AverageDollarVolume, SimpleMovingAverage, RSI  
from quantopian.pipeline.data import morningstar  
from brokers.ib import IBExchange  
from quantopian.pipeline.data.quandl import cboe_vix as vix  
from quantopian.pipeline import CustomFactor  
import talib  
import math  
import numpy as np  
import pandas as pd  
import statsmodels.api as sm

stocks, ma = symbols('AXP', 'AAPL',), 40 

def initialize(context):  
    schedule_function(record_at_close, date_rules.every_day(), time_rules.market_close())  

def record_at_close(context, data):  
    H = data.history(stocks, 'high', ma, '1d')  
    print (H)  

More likely the price difference is the result of the adjustment to dividend (0.73) paid on Nov 08, 2018.

Oh, thanks again Vladimir. All make sense now.