Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
HELP! The truth vale of a series is ambiguous. Use a.empty, a.bool()....

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)  

machine learning reference page scikit-learn.org/

1 response

Hi James,

Near the end of the code you posted here you compare ma1 and ma2:

if p == 1 and ma1 > ma2:  
    order_target_percent(stock,0.11)  

ma1 and ma2 are both Series. Comparing them returns a new Series, where each element is a boolean indicating the result of comparing the corresponding elements of ma1 and ma2. Meanwhile, the if expects a boolean, not a Series.

The reason that ma1 and ma2 are both Series is that they are the result of using the .mean() method on a DataFrame (2d array). And you got a DataFrame from data.history because you are passing in context.stocks, when you really just want the data for stock.

So the underlying problem is that you are querying for the history of all the stocks in your list when you just want the history of each one, one at a time. This error will be fixed if you change the first argument of both instances of data.history to stock.

Thanks for posting this question. If you have another specific issue with code like this one in the future, I would recommend that you ask our support team directly by using Learn & Support > Contact Support on the top menu bar of the site.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.