
from quantopian.pipeline import Pipeline, CustomFilter
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline.factors import Latest
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.data.psychsignal import aggregated_twitter_withretweets_stocktwits as st
from sklearn.preprocessing import StandardScaler
#from quantopian.pipeline.data.psychsignal import stocktwits as st
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.svm import SVR
from sklearn.neighbors import KNeighborsRegressor 
#Note that Quantopian uses sklearn v.16.1 - ie MLPRegressor is not available
from quantopian.pipeline.filters import Q500US
from quantopian.pipeline.filters import Q1500US
import numpy as np
from odo import odo 
import pandas as pd
#Basic model that trades based on recent stock twit and twitter sentiment data
def initialize(context): 
   
    context.days_collection=10 #Number of days to collect data before building model (initially used 14)
    context.delLen=0
    context.y_portVal=0
    context.performances=[]
    context.yest_feats=[]
    context.Y_securities=[]
    context.Cs=[]
    context.Os=[]
    context.refit=False
    context.results=[[0],[0]]
    context.yest_res=[]
    context.long_securities=[]
    context.long_matrixResults=[]
    context.long_outcomes=[]
    context.last_predictions=[]
    context.securities_in_results = []
    context.sentimentMultiplier=3.5 #Number of times more bull messages than bear required to make it past scree 
    #context.model = RandomForestRegressor()
    #context.model = RandomForestRegressor(warm_start=True) #******Make sure this model is a regressor*****
    context.model = GradientBoostingRegressor(warm_start=True)
    #context.model = GradientBoostingRegressor()
    context.scaler = StandardScaler()
    #context.model = KNeighborsRegressor()
    #context.model2 = RandomForestRegressor()
    #context.model2 = RandomForestRegressor(warm_start=True)
    #context.model = SVR(kernel='rbf', C=1e3, gamma=0.1)
    #context.model2 = SVR(kernel='linear', C=1e3, gamma=0.1)
    #context.model.n_estimators = 20 #Original val 500
    #context.model.min_samples_leaf = 200 #Original val 100
    context.ran = 0 #Used to build initial model (and subsequent secondary models - every context.days_collection days)
    context.ran2=0
    
    #schedule_function(sellAll, date_rules.every_day(), time_rules.market_close(minutes=5)) #IF we are training our model on a target that has a result (open - close) percent change, we will match this more closely by selling all our securities 5 minutes before market close
    
    schedule_function(build_model,date_rules.every_day(),time_rules.market_open()) 
    schedule_function(trade, date_rules.every_day(), time_rules.market_open())
    schedule_function(calc_performance, date_rules.every_day(), time_rules.market_open())
    set_commission(commission.PerShare(cost=0.0, min_trade_cost=0.0))