Back to Community
Truth value of a series
import talib

def initialize(context):  
    context.nvda = sid(19725)

def handle_data(context, data):  
    hist = data.history(context.nvda, ['price', 'high', 'low', 'close'], 50, '1d')  
    sma_50 = hist.mean()  
    sma_20 = hist[-20:].mean()  
    willr = talib.WILLR(hist['high'], hist['low'], hist['close'], timeperiod=14)  
    WILLR = willr[-1]  
    WILLR = -WILLR  
    log.info(type(WILLR))  

    open_order = get_open_orders()  
    if WILLR > 80 and sma_50 > sma_20:  
        if context.nvda not in open_order:  
            order_percent(context.nvda, 0.3)  
    elif WILLR < 20 and sma_20 > sma_50:  
        if context.nvda not in open_order:  
            order_percent(context.nvda, -0.3)  
    record(levarage = context.account.leverage)  

I am getting the error on line "if WILLR > 80 and sma_50 > sma_20:" saying that "Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()". Can anyone explain me why?

EDIT: Nevermind , got it. It seems that i forgot to put that it is hist['price'] to do the mean.

3 responses

The variables 'sma_50' and 'sma_20' are both series. One cannot generally test the truth value of a series. That is why the error message is recommending Use a.empty, a.bool(), a.item(), a.any() or a.all().

The reason those are series is because of the statement

      hist = data.history(context.nvda, ['price', 'high', 'low', 'close'], 50, '1d')  

When the data.history method is provided a single asset but multiple fields (as in this case) the result is a pandas dataframe. When taking the mean of a dataframe one gets the mean of every column and NOT a simple scaler. Perhaps try the following instead (which will return a pandas series).

      hist = data.history(context.nvda, 'price', 50, '1d')  

Hope that helps.

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.

Speed of history was tested using ['price', 'high', 'low', 'close'] vs each of those separately one at a time, once a day over 9 years.
Photo finish.

2019-11-08 06:31 timing:135 INFO avg 0.007745 lo 0.004089  hi 0.045839  history_indexing_into_dataframe  
2019-11-08 06:31 timing:135 INFO avg 0.007095 lo 0.003759  hi 0.043659  history_separate_calls  
Clone Algorithm
1
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# for history speed test comparison at https://www.quantopian.com/posts/truth-value-of-a-series
'''
2010-01-05 06:31 timing:120 INFO 0.0223 avg 0.0223 lo 0.0223  hi 0.02230  history_separate_calls new max
2010-01-05 06:32 timing:120 INFO 0.0066 avg 0.0066 lo 0.0066  hi 0.00658  history_indexing_into_dataframe new max
2010-01-11 06:32 timing:120 INFO 0.0074 avg 0.0066 lo 0.0064  hi 0.00739  history_indexing_into_dataframe new max
2010-01-12 06:32 timing:120 INFO 0.0121 avg 0.0076 lo 0.0064  hi 0.01211  history_indexing_into_dataframe new max
2010-01-13 06:32 timing:120 INFO 0.0189 avg 0.0092 lo 0.0064  hi 0.01888  history_indexing_into_dataframe new max
2010-01-14 06:32 timing:120 INFO 0.0331 avg 0.0122 lo 0.0064  hi 0.03311  history_indexing_into_dataframe new max
2010-01-27 06:31 timing:120 INFO 0.0223 avg 0.0096 lo 0.0050  hi 0.02233  history_separate_calls new max
2010-05-04 06:31 timing:120 INFO 0.0241 avg 0.0074 lo 0.0039  hi 0.02408  history_separate_calls new max
2010-07-26 06:32 timing:120 INFO 0.0349 avg 0.0085 lo 0.0043  hi 0.03489  history_indexing_into_dataframe new max
2010-08-31 06:31 timing:120 INFO 0.0258 avg 0.0077 lo 0.0039  hi 0.02584  history_separate_calls new max
2010-09-01 06:31 timing:120 INFO 0.0266 avg 0.0078 lo 0.0039  hi 0.02655  history_separate_calls new max
2011-11-08 06:31 timing:120 INFO 0.0437 avg 0.0080 lo 0.0039  hi 0.04366  history_separate_calls new max
2014-01-09 06:32 timing:120 INFO 0.0458 avg 0.0087 lo 0.0042  hi 0.04584  history_indexing_into_dataframe new max
2019-11-08 06:31 timing:133 INFO Timings by highs descending:
2019-11-08 06:31 timing:135 INFO avg 0.007745 lo 0.004089  hi 0.045839  history_indexing_into_dataframe
2019-11-08 06:31 timing:135 INFO avg 0.007095 lo 0.003759  hi 0.043659  history_separate_calls
2019-11-08 06:31 timing:139 INFO 
Runtime 0 hr 02 min    End: 2019-11-09 20:38 US/Eastern
'''

def initialize(context):
    context.nvda = sid(19725)

    # Whichever call is first will suffer a bit from being first
    schedule_function(history_separate_calls, date_rules.every_day(), time_rules.market_open(minutes=1))
    schedule_function(history_indexing,       date_rules.every_day(), time_rules.market_open(minutes=2))

def history_indexing(context, data):                 # ['price', 'high', 'low', 'close'] all at one time
    #             id reset
    #                   \/        time to zero for this id
    #                   \/        \/
    timing(context, 'history_indexing_into_dataframe', 'reset')           # Reset time to zero, says to start.

    hst   = data.history(context.nvda, ['price', 'high', 'low', 'close'], 50, '1d')
    '''
        DataFrame:
                                    close    high     low   price
        2012-05-17 00:00:00+00:00  12.650  12.910  12.650  12.650
        2012-05-18 00:00:00+00:00  12.090  12.740  12.020  12.090
        2012-05-21 00:00:00+00:00  12.295  12.350  12.015  12.295
    '''

    highs  = hst.high
    lows   = hst.low
    closes = hst.close
    prices = hst.price
    '''
        prices: Series
        Timestamp('2012-05-17 00:00:00+0000', tz='UTC', offset='C'): 12.65
        Timestamp('2012-05-18 00:00:00+0000', tz='UTC', offset='C'): 12.09
        Timestamp('2012-05-21 00:00:00+0000', tz='UTC', offset='C'): 12.295
        [...]
    '''

    timing(context, 'history_indexing_into_dataframe') # -----|           # Time since reset, does the logging.
    #                                                     /\
    #                                                     No reset this time

    # Since the variables are only used once currently, this is just neutralizing the build error that would occur.
    highs=highs ; lows=lows ; closes=closes ; prices=prices

def history_separate_calls(context, data):            # One at a time, like 'price'
    #             id reset
    #                   \/        time to zero for this id
    #                   \/        \/
    timing(context, 'history_separate_calls', 'reset')                    # Reset time to zero, says to start.

    highs    = data.history(context.nvda,  'high', 50, '1d')
    lows     = data.history(context.nvda,   'low', 50, '1d')
    closes   = data.history(context.nvda, 'close', 50, '1d')
    prices   = data.history(context.nvda, 'price', 50, '1d')

    '''
        prices: Series
        Timestamp('2012-05-17 00:00:00+0000', tz='UTC', offset='C'): 12.65
        Timestamp('2012-05-18 00:00:00+0000', tz='UTC', offset='C'): 12.09
        Timestamp('2012-05-21 00:00:00+0000', tz='UTC', offset='C'): 12.295
        [...]
    '''

    timing(context, 'history_separate_calls') # -----|                    # Time since reset, does the logging.
    #                                            /\
    #                                            No reset this time

    # Since the variables are only used once currently, this is just neutralizing the build error that would occur.
    highs=highs ; lows=lows ; closes=closes ; prices=prices



def timing(c, ident, reset=None):  # You set the id to any string you wish
    '''   https://www.quantopian.com/posts/timing-code
    Timing code to determine elapsed time for any given section of code.
    Log timing between any 'reset' and its pair like timing(context, 'history1')
    Time is in seconds, incoming 'c' is context
    How to:
        Start:   timing(context, 'some string', 'reset')
        End:     timing(context, 'some string')
          ... and the function def timing(), those are all you need.
    There can be multiple instances with different ids ('some string', whatever you set).
    The string identifier keeps them separate.
    '''
    from pytz     import timezone as _tz
    from datetime import datetime as _dt
    import time as _t         # Python will only bother doing once, makes this portable.
                              #   Move to top of algo for better efficiency.
    t = _t.time()
    if 'timer' not in c:      # Also move these, to initialize() for better efficiency.
        c.timer   = {         # Dictionary to keep track of these timings.
            'log_level': 1,   # -1   Off
                              #  0   Only summary at end of run
                              #  1   Each new maximum
                              #  2   Everything always
                              #  3   Only if beyond a certain threshold. Use warn_threshold.
            # Use with (3) log_level to only log if elapsed is above this seconds value.
            'warn_threshold': 1.5,
            'elapsed_start' : _t.time(),
            'end_dt'        : get_environment('end'),
            'ids'           : {},
            'last_day_summary_done': 0,
        }
    if ident not in c.timer['ids']:
        c.timer['ids'][ident] = {   # Initialization of any new id string
            'point': t,
            'hi'   : 0,
            'lo'   : 1000,
            'avg'  : [0, 0],
        }
        return
    if c.timer['log_level'] == -1: return
    if reset:
        c.timer['ids'][ident]['point'] = t    # Point-in-time, acting as zero.
        return
    elapsed     = t - c.timer['ids'][ident]['point']      # Elapsed time
    avg, num    = c.timer['ids'][ident]['avg']            # Tracking average
    avg_new     = ((avg * num) + elapsed) / (num + 1)   # Calc new average
    new_max_str = 'new max' if elapsed > c.timer['ids'][ident]['hi'] else ''
    c.timer['ids'][ident]['avg'] = [avg_new, num + 1]     # New values
    c.timer['ids'][ident]['hi']  = max(elapsed, c.timer['ids'][ident]['hi'])
    c.timer['ids'][ident]['lo']  = min(elapsed, c.timer['ids'][ident]['lo'])
    if (c.timer['log_level'] == 1 and new_max_str) or c.timer['log_level'] == 2 or (c.timer['log_level'] == 3 and elapsed > c.timer['warn_threshold']):
      log.info('{} avg {} lo {}  hi {}  {} {}'.format('%.4f' % elapsed, '%.4f' % avg_new, '%.4f' % c.timer['ids'][ident]['lo'], '%.5f' % c.timer['ids'][ident]['hi'], ident, new_max_str))

    # Summary on last day once, sorted by highest high value first.
    # If using schedule_function(), backtest last day/time may need to match for this to execute.
    if get_datetime().date() == c.timer['end_dt'].date():   # Log summary on last day
        if c.timer['last_day_summary_done']: return         # Only once on last day
        id_list = []
        for i in c.timer['ids']:
            hi = '%.6f' % c.timer['ids'][i]['hi']
            lo = '%.6f' % c.timer['ids'][i]['lo']
            av = '%.6f' % c.timer['ids'][i]['avg'][0]
            id_list.append( (hi, lo, av, i) )
        list_sort = sorted(id_list, reverse=True)
        log.info('Timings by highs descending:')
        for ls in list_sort:
            log.info('avg {} lo {}  hi {}  {}'.format(ls[2],ls[1],ls[0],ls[3]))
        elapsed = (_t.time() - c.timer['elapsed_start']) / 60  # minutes
        log.info( '\nRuntime {} hr {} min    End: {} {}'.format(
            int(elapsed / 60), '%02d' % (elapsed % 60),
            _dt.now(_tz('US/Eastern')).strftime('%Y-%m-%d %H:%M'), 'US/Eastern'
        ))
        c.timer['last_day_summary_done'] = True
        
        
        
There was a runtime error.

@Blue Thank you for testing that. I've always wondered how much faster it is to fetch all the data at once as a dataframe or one at a time as separate series. Looks like not much at all (if any). It does sometimes make one's code easier to read fetching each data field separately. In the case of the original algo, fetching each separately may have avoided the confusion with sma_50 and sma_20 being series objects and not scalers?