Back to Community
Algo works with SPY but no other stock. Error - Runtime exception: IndexError: invalid index to scalar variable.

This is my first algo. Been working on it from scratch since I joined a few days ago. For some reason it only works when I use SPY. I've Googled and gone over this site and have tried multiple things that I found that I thought might work, but so far, nothing has. Would someone be kind enough to give it a quick look over and suggest how this can be fixed to use whatever stock I put in the context.stocks = symbols('XXX'). Preferably at some point I'd like it to pick from multiple stocks in the pool, but right now I'd be happy with just having it work with other stocks beside SPY.

Clone Algorithm
4
Loading...
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
# Add description here
# Put any initialization logic here.  The context object will be passed to
# the other methods in your algorithm.

# Import the libraries we will use here
import talib
import numpy as np
import pandas as pd

# The initialize function is the place to set your tradable universe and define any parameters. 
def initialize(context):
    # This initialize function sets any data or variables that you'll use in
    # your algorithm.  For instance, you'll want to define the security (or 
    # securities) you want to backtest.  You'll also want to define any 
    # parameters or values you're going to use. Put "pass" if empty

    # context.stock = sid(8554) # SPY
    # context.stock = sid(39214) # TQQQ
    # context.stock = sid(41968) # SVXY
    context.stocks = symbols('spy')
    # context.stocks = [sid(24),sid(8554)]
    
    # Set the percent of the account to be invested per stock
    context.long_pct_per_stock = 1.0 / len(context.stocks)
    
    # In the lines max_nominal, we set the maximum and minimum we want our algorithm
    # to go long or short our security.  You don't have to set limits like this
    # when you write an algorithm, but it's good practice.
    context.max_notional = 100000.0
    context.min_notional = -100000.0
   
    set_commission(commission.PerTrade(cost=9.95))
    
    # Set market impact assumptions price impact constant with fixed slippage
    set_slippage(slippage.FixedSlippage(spread=0.00))
    
    # At 11AM ET, which is 1 hour and 30 minutes after market open.
    # schedule_function(func=myfunc, time_rules=time_rules.market_open(hours = 1, minutes = 30))
    schedule_function(
    myfunc,
    date_rules.every_day(),
    time_rules.market_open(minutes=15)
    )
    
    set_benchmark(symbol('spy'))
   
def myfunc(context,data):
  pass

# Will be called on every trade event for the securities you specify. 

def handle_data(context, data):
    # This handle_data function is where the real work is done.  Our data is
    # minute-level tick data, and each minute is called a frame.  This function
    # runs on each frame of the data. # data[sid(X)] holds the trade event data for that security.
    # context.portfolio holds the current portfolio state.
    
    # Load historical data for the stocks
    high = history(30, '1d', 'high')
    low = history(30, '1d', 'low')
    close = history(30, '1d', 'close_price')

    # Iterate over our list of stocks
    for stock in context.stocks:
        current_position = context.portfolio.positions[stock].amount
        slowk, slowd = talib.STOCH(high[stock],
                                   low[stock],
                                   close[stock],
                                   fastk_period=9,
                                   slowk_period=3,
                                   slowk_matype=0,
                                   slowd_period=3,
                                   slowd_matype=0)
        log.info(slowk, slowd)
        # print slowk, slowd
        # record(slowk=slowk[-1], slowd = slowd[-1])
        for talib.STOCH in data:
            slowk = slowk[-1]
            slowd = slowd[-1]
        
    for stock in context.stocks:
        current_position = context.portfolio.positions[stock].amount
        slowk1, slowd1 = talib.STOCH(high[stock],
                                   low[stock],
                                   close[stock],
                                   fastk_period=9,
                                   slowk_period=3,
                                   slowk_matype=0,
                                   slowd_period=3,
                                   slowd_matype=0)
        log.info(slowk1, slowd1)
        # print slowk1, slowd1
        # record(slowk1=slowk1[-2], slowd1 = slowd1[-2])
        
        # get the most recent value
        for talib.STOCH in data:
            slowk1 = slowk1[-2]
            slowd1 = slowd1[-2]
        
        # If either the slowk or slowd are less than 10, the stock is 
        # 'oversold,' a long position is opened if there are no shares
        # in the portfolio.
        
        if (slowk < 50) or (slowd < 50) and current_position <= 0:
            order_target_percent(stock, context.long_pct_per_stock)
        # if (slowk < 50 and slowk1 < slowk) or (slowd < 50 and slowd1 < slowd) and current_position <= 0:
            # order_target_percent(stock, context.long_pct_per_stock)
        
        # If either the slowk or slowd are larger than 90, the stock is 
        # 'overbought' and the position is closed. 
        elif (slowk1 > 80) or (slowd1 > 80) and current_position >= 0:
            order_target(stock, 0)
        
        # elif (slowk > 80 and slowk[-2] > slowk[-1]) or (slowd > 80 and slowd[-2] > slowd[-1]) and current_position >= 0:
            # order_target(stock, 0)
        
        record(slowk = slowk, slowd = slowd, High = 80, Low = 20)
There was a runtime error.
4 responses

It looks like you're off to a great start! I've taken a quick look through your code, and before I can find a solution to your problem, I may need to ask you a question or two so that I can better understand what is going on and what the problem may be.

To start, do you think you could explain to me the logic behind the following block of code (appears lines 77-79 and lines 96-98):

for talib.STOCH in data:
slowk1 = slowk1[-2]
slowd1 = slowd1[-2]

Thanks!

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.

Hello Jamie,
Thanks for your interest.
I am attempting to input code to determine the direction of crossover. I tried with 0 instead of -1 and that didn't work. 0 would be todays date, -1 yesterdays date in my mind. But that didn't work. So my assumption is now todays date is -1 and yesterdays is -2. Thus, if I say I want (slowk: slowktoday) -1 over 30 and -2 (slowk1: slowk yesterday) below 30, then this would tell it that the direction of the line is up and it has just crossed over 30. Therefore I could catch the price action just afterthe bottom just as it is heading upwards. So I made two sets of "if stock in context.stocks", one to calculate for today, the other for yesterday. Does that help?

Try removing your lines that look like this:

for talib.STOCH in data:

I'm not sure that a for loop is appropriate when you are trying to set slowk1 and slowd1 and the line itself doesn't really make sense as a for loop. I cloned your code, removed these lines and it worked for me! Give it a shot and let me know if it works for you as well.

That seems to do the trick. Thanks for the help, much appreciated. If you ever need any help let me know.