Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Coding - please please help

Hi Quantpians,

I am making my very early steps creating the algorithms, and getting stuck with some very very simple stuff, which drives me completely nuts, so any help would be really really appreciated.

I was trying to build an extremely simply algorithm, not for the sake of its functionality, but just to make sure that the code works, and this is where I got stuck. I have two securities and the idea is to buy those when the weighted average momentum for four periods (month, 3 months, 6 months, year) is positive. Portfolio rebalances once a month.

This is the code I tried (which is very similar to a functional code which I saw in one of the tutorials).

def initialize(context):  
    context.security_list = [sid(24), sid(5061)]  
    schedule_function(handle_data, date_rules.month_start(0), time_rules.market_open(minutes = 30))  
def compute_weights(context, data):  
    hist = data.history(context.security_list, 'close', 240, '1d')  
    m1 = hist.pct_change(20)  
    m3 = hist.pct_change(60)  
    m6 = hist.pct_change(120)  
    m12 = hist.pct_change(240)  
    momentum_13612 = (m1*12 + m3*4 + m6*2 + m12)/4  
    if momentum_13612 > 0:  
        weight = 0.5  
    else:  
        weight = 0

    return weight

def handle_data(context, data):  
    wt = compute_weights(context, data)  
    for security in context.security_list:  
       if data.can_trade(security):  
           order_target_percent(security, wt[security])

The error that I am receiving is the following:

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
There was a runtime error on line 31. ("wt = compute_weights(context, data)")

Can anyone please suggest what I am doing wrong?

Many thanks in advance!

4 responses

The error message gives a clue

ValueError: The truth value of a DataFrame is ambiguous.

"hist" is a Pandas DataFrame, the same goes for all the other computations including "momentum_13612". Sometimes it can help to just print out the data in question to see what's going on there. A DataFrame consists of columns and rows, in this case there are 2 columns for the symbols and 240 rows for the close prices (or pct_changes later on). So the question the error message asks is "which one of the 480 values is supposed to be greater than 0?"

You are interested in the last one, which can be accessed using momentum_13612.iloc[-1]. Now you only have the last row but still 2 columns for the symbols. One way is to iterate over them, attached is a working version of your code.

Generally I wold recommend some basic tutorials for python, more specifically about pandas or finance. I myself had a lot of fun coding along with sentdex

@Tentor Testivis, thank very much indeed - this is sooo helpful!

I've spent a lot of time in the last few weeks learning the basics of Python, even received some online certificate, but haven't got too far with Pandas yet - will make sure to fill this gap in the near future.

Also tried Sentdex lectures - they are very useful, just got a bit too complicated for me as he started using the sentiment dataset, but hopefully it would be easier to follow as a make some progress with the basics.

Thank you very much again - really appreciate your help!

You're welcome.
I don't know if it works for you, but when I started coding I often just typed along without understanding what was happening. Understanding came later through practice and google ;)
But of course, even if you have basic coding skills, it's a different story to become familiar with the way a specific platform works. Just keep experimenting and look through the forum and tutorials here, after a while you get the hang of it.

Yeah, I feel this is where I am right now... For some reason in the IDE environment everything seems to be very foreign, not like the Python I was learning online. Even printing something often ends up being a challenge. It's reassuring to know that this is a normal stage of the learning curve though.