Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
please help! Runtime exception: ValueError

Hi everybody!

I am new to coding and I really dont understand why and how "Runtime exception: ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()." is connected to my algo.

this is the code

import talib
import numpy as np
import pandas as pd

def initialize(context):
context.jpm = sid(25006)
context.xli = sid(19657)

def handle_data(context, data):

price_jpm = history(100, '1d', 'price')  
price_xli = history(100, '1d', 'price')  

diff_prices = price_jpm - price_xli  

avg_diff_prices = np.mean(diff_prices)  


if price_jpm > avg_diff_prices*1.01:  
    order(context.jpm, 10)

the error statement refers to the if line...

Thank you so much for your patience and help!

4 responses

From what I understand you are trying to compare an array to a single value .
price_jpm is an array of prices containing 100 elements while avg_diff_prices is a single value.
Maybe you should try this:

jpm_last = price_jpm[-1]

if jpm_last > avg_diff_prices*1.01:  
    order(context.jpm,10)  

Hi Matteo,

This can be done in other way as shown below.


def initialize(context):  
    context.jpm = sid(25006)  
    context.xli = sid(19657)

def handle_data(context, data):  
    price_history  = history(100, '1d', 'price')  
    mean = price_history.mean()  
    price_jpm = mean[context.jpm]  
    price_xli = mean[context.xli]  
    diff_prices  =    price_jpm  -   price_xli 

    if price_jpm > diff_prices:  
        order(context.jpm, 10)  

thank you guys! that's exactly what i was looking for!

Yatharth, could you please explain to me what [-1] stands for and when I should used it?

Hi Matteo-

An array can be indexed (get a particular item) by giving the location where the item sits. So price_jpm[0] would be the first item in the prices array. Yatharth is using a feature of python which additionally allows you to index from the end of the array.

jpm_last = price_jpm[-1]  

means "get the element -1 places from the end", or "give me the last element in the array." In this context, you would get the most recent price value.

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.