I am getting an error printed in my log. This algo did work but things became deprecated and I tried to update it but cannot figure out what I am doing wrong. The error is as follows:
The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC, LinearSVC, NuSVC
from sklearn.ensemble import RandomForestClassifier
from sklearn import preprocessing
from collections import Counter
import numpy as np
def initialize(context):
context.stocks = symbols('XLY', # XLY Consumer Discrectionary SPDR Fund
'XLF', # XLF Financial SPDR Fund
'XLK', # XLK Technology SPDR Fund
'XLE', # XLE Energy SPDR Fund
'XLV', # XLV Health Care SPRD Fund
'XLI', # XLI Industrial SPDR Fund
'XLP', # XLP Consumer Staples SPDR Fund
'XLB', # XLB Materials SPDR Fund
'XLU') # XLU Utilities SPRD Fund
context.historical_bars = 100 #recalls last 100 bars
context.feature_window = 10 #slices of 10 day windows for each slice training occurs. if price goes up buy if price goes down sell
def handle_data(context, data):
#prices = history(bar_count = context.historical_bars, frequency ='1d', field='price') "deprecated"
prices = data.history(context.stocks, 'price', context.historical_bars, frequency = '1d')
#todays price and 99 before is stored in pandas data frame of prices.
for stock in context.stocks:
try:
#ma1 = data[stock].mavg(50) "deprecated"
price_hist = data.history(context.stocks, 'price', bar_count=50, frequency='1d')
ma1 = price_hist.mean()
# ma2 = data[stock].mavg(200) "depricated"
price_hist = data.history(context.stocks, 'price', bar_count=200, frequency='1d')
ma2 = price_hist.mean()
start_bar = context.feature_window #has to start at 10 to get last 9 points.
price_list = prices[stock].tolist() #column in pandas dataframe is converted to list
X = [] #features #is empty list that is populated by the last 10 days of prices
y = [] #labels
#feature sets^
bar = start_bar
#feature creation
while bar < len(price_list)-1: #lists start at 0 not 1
try:
end_price = price_list[bar+1]
begin_price = price_list[bar]
#says that the last price in the list is what wer'e interested in.
#was the subsequent price a rise or fall ex. [1,2,3,4,5,6,7,8,9,10,???]
pricing_list = [] #actual list of prices
xx = 0 #counts us through
for _ in range(context.feature_window):
price = price_list[bar-(context.feature_window-xx)]
#take current bar were on and starts list
pricing_list.append(price)
xx += 1
features = np.around(np.diff(pricing_list) / pricing_list[:-1] * 100.0, 1) #1=limits to 1 dec
#gets the percent change of stocks
#print(features)
if end_price > begin_price: #with this set of features and % chagnes the ending price from the point of investment was higher
label = 1
else:
label = -1
bar += 1
X.append(features)
y.append(label)
#feature creation
except Exception as e:
bar += 1 #prevent infinite while loop
print(('feature creation',str(e)))
clf1 = RandomForestClassifier() #10 = how many classifiers to use
clf2 = LinearSVC()
clf3 = NuSVC()
clf4 = LogisticRegression()
last_prices = price_list[-context.feature_window:]
current_features = np.around(np.diff(last_prices) / last_prices[:-1] * 100.0, 1)
X.append(current_features)
X = preprocessing.scale(X) #sets all features between 1 and -1
current_features = X[-1]
X = X[:-1] #up to -1st but not -1st
clf1.fit(X,y) #here are the features and labels that apply.
clf2.fit(X,y)
clf3.fit(X,y)
clf4.fit(X,y)
p1 = clf1.predict(current_features) [0]
p2 = clf2.predict(current_features) [0]
p3 = clf3.predict(current_features) [0]
p4 = clf4.predict(current_features) [0]
if Counter([p1,p2,p3,p4]).most_common(1)[0][1] >= 4: #votes says clasifiers must agree
p = Counter([p1,p2,p3,p4]).most_common(1)[0][0] #prediction = like tuple, (number)[occurances][prediction]
else:
p = 0 #nothing will hapen if if isnt satisfied
print(('prediciton',p))
if p == 1 and ma1 > ma2:
order_target_percent(stock,0.11)
elif p == -1 and ma1 < ma2:
order_target_percent(stock,-0.11)
except Exception as e:
print(str(e))
record('Leverage',context.account.leverage)