import quantopian.algorithm as algo
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.tree import DecisionTreeClassifier
from sklearn.feature_selection import RFECV
import numpy as np
import pandas as pd
LEVERAGE=4.0 # this is not relaly a leverage factor. Since we hold stocks only from noon to close, we may want
# to leverage a bit.
def initialize(context):
set_commission(commission.PerShare(cost=0, min_trade_cost=0))
set_slippage(slippage.FixedSlippage(spread=0))
# Rebalance every day, 1 hour after market open.
algo.schedule_function(
open_spy,
algo.date_rules.every_day(),
algo.time_rules.market_open(hours=3, minutes=1),
half_days=False
)
algo.schedule_function(
capture_price,
algo.date_rules.every_day(),
algo.time_rules.market_open(),
half_days=False
)
algo.schedule_function(
capture_price,
algo.date_rules.every_day(),
algo.time_rules.market_open(hours=1),
half_days=False
)
algo.schedule_function(
capture_price,
algo.date_rules.every_day(),
algo.time_rules.market_open(hours=2),
half_days=False
)
algo.schedule_function(
capture_price,
algo.date_rules.every_day(),
algo.time_rules.market_open(hours=3),
half_days=False
)
algo.schedule_function(
close_spy,
algo.date_rules.every_day(),
algo.time_rules.market_close(minutes=5),
half_days=False
)
context.spy = sid(8554)
context.X = dict()
context.y = dict()
context.daily_data = dict()
context.y_d = dict()
context.assets = [sid(3149), sid(35920), sid(4922), sid(8347), sid(3766), sid(5061), sid(698), sid(1267), sid(23112), sid(679), sid(1900), sid(3951), sid(4151), sid(4707), sid(20088), sid(25006), sid(3496), sid(2190), sid(5029), sid(5923), sid(5938), sid(21839), sid(8229), sid(7792), sid(7883)]
for a in context.assets:
context.X[a] = []
context.y[a] = []
context.daily_data[a] = []
context.y_d[a] = 0
set_benchmark(context.spy)
def capture_price(context, data):
for a in context.assets:
context.daily_data[a].append(data.current(a, 'price'))
def open_spy(context, data):
for a in context.assets:
if len(context.daily_data[a]) != 5:
continue
X_d = (np.array(context.daily_data[a])/context.daily_data[a][0])[1:]
context.X[a].append(X_d)
context.y[a].append(context.y_d[a])
y_train = np.array(context.y[a])[1:]
X_train = np.array(context.X[a])[:-1]
X_live = np.array(context.X[a])[-1:]
if X_train.shape[0] > 50:
# model = GaussianNB()
model = LogisticRegression(C=5.0)
############# feature reduction##
selector = RFECV(model, 1, 2)
selector.fit(X_train, y_train)
if selector.score(X_train, y_train) < 0.50:
continue
model = selector
#################################
# model = SVC(kernel='rbf', C=10, gamma='auto', class_weight='auto')
# model.fit(X_train, y_train)
# if model.score(X_train, y_train) < 0.50:
# continue
if model.predict(X_live)[0] == 1:
order_target_percent(a, LEVERAGE/len(context.assets))
else:
order_target_percent(a, -LEVERAGE/len(context.assets))
def close_spy(context, data):
# record(leverage=context.account.leverage)
for a in context.assets:
c_price = data.current(a, 'price')
context.y_d[a] = 1 if c_price - context.daily_data[a][-1] > 0 else -1
context.daily_data[a] = [c_price]
order_target_percent(a, 0.0)