Back to Community
Leaving Quantopian

I am leaving this community.

Most of what I got in a month of trying to produce a working algorithm where various sorts of syntax and run-time errors.

Please delete my account.

14 responses

My code usually works without errors. I suggest you work on your Python coding skills, carefully read the documentation of the Quantopian API, and write really simple algorithms to develop experience. You might consider getting a tutor in both Python and Quantopian, if it helps.

Having said that, Quantopian error messages could and should be more informative than "Sorry - something went wrong."

I agree that there are things that can be improved upon but I think all in all, Q is doing a great job developing the system and cultivating the community. Keep it up!

He's missing out. There is so much information on this website, and on the Internet!

Why are you making a public display of your whining and terrible coding skills?

Check out sentdex on youtube he's a great place to start learning how to use Quantopian

I agree with Ari Schaffel, The sentdex videos are a great, easy and very fast path to successfully code your own algorithms.

Cameron, I did it to please people like yourself.

Many thanks to everyone else for their valuable advice.

But just as an example, can someone tell me why the algorithm listed below crashes?


def initialize(context):  
    set_universe(universe.DollarVolumeUniverse(95.0, 100.0))  
    schedule_function(trade,  
                      date_rule=date_rules.month_start(),  
                      time_rule=time_rules.market_open(minutes=30))  

def handle_data(context, data):  
    pass


def trade(context, data):  
    p = history(300, '1d', 'price')  
    R = p.pct_change().dropna()  
    x = R.mean()  
    s = R.std()  
    ranks = x + s*s/2

    lower, upper = ranks.quantile([.05, .95])  
    shorts = ranks[ranks <= lower]  
    longs = ranks[ranks >= upper]  
    nlong = len(longs)  
    nshort = len(shorts)  
    for stock in data:  
        if get_open_orders(sid = stock):  
            continue  
        try:  
            if stock in shorts.index:  
                order_target_percent(stock, -0.5/nshort)  
            elif stock in longs.index:  
                order_target_percent(stock, 0.5/nlong)  
            else:  
                order_target(stock, 0)  
        except:  
            log.warn("[Failed Order] stock = %s"%stock.symbol)  
    record(leverage = context.account.leverage,  
           exposure = context.account.net_leverage)  
    record(nlong = nlong, nshort = nshort)  

Tim,
I spent about 10 minutes looking into this. Here's what I've found.

1) The error occurs in your trade() function. A little googling didn't immediately tell me what the problem was from inspecting the function call that is failing (the pandas quantile method). There was no quick fix.
2) It looked like the error you hit doesn't happen immediately. So the first thing I did was put in some logging in the trade() function to see upon what date the algo was hitting the error. I put the logging right before the line that throws the error. The logging showed me that you hit the error in April 2012 (in my backtest) when I started the backtest in 2011.
3) So I used the debugger to put a break in at the logging line, and adjusted the dates of my backtest to just before your algo hits the error. Here's info on the debugger: https://www.quantopian.com/posts/new-feature-debugging-in-the-ide
4) When I ran the debugger, the first time I examined the series 'ranks', it looked normal and nice. (3/1/2012). I could execute the quantiles method properly.
5) The next month through the algo, I examined the same series and this time, rather than having a number for each member of the series, ranks had a "nan" value. Now we're getting somewhere.
6) If I try the quantile method, this errors out.
7) Why is the series full of nan's? Well, in the debugger, I could examine each of the building blocks for 'ranks' and it seemed like s (based on the std() method) seems to be the cause of all the nans.
8) Why is that? Why is s being set to all nans? I dunno, I don't have time to dig into it further at this point in the day. I'll leave that as an exercise for the reader to dig further.

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.

Thank you, Josh, for your time and this extensive analysis.

Tim Vidmar,

Your algo works if you remove the .dropna()

change

R = p.pct_change().dropna()

to

R = p.pct_change()

I don't know why using pct_change() values in conjunction with std() breaks, but it does. If anyone figures out exactly why this happens please let me know ^_^ (This issue has drove me crazy for quit a while lol)

Clone Algorithm
2
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
# Backtest ID: 55cbd16b0112890c5d580edc
There was a runtime error.

Lucky Bomb,

I don't know how to thank you, I would almost certainly never have figured that out by myself. Cheers, mate!

The algo now indeed works, but the results are lousy. :-)

Tim and Lucky, the reason the algo fails is because of the dropna(), the default behavior of that function is to drop rows where there is at least 1 NaN value. That makes sense for the first row after calculating the returns, but for the other rows if any stock has NaNs, it will drop the data point for the other stocks as well. Say a stock IPOs and is immediately heavily traded (e.g. FB), it will show up in the dollar volume universe, but there will be no trailing history for it. In that case .dropna() will wipe out all of the data prior to the new stocks IPO.

To fix the issue you can specify an axis, use .dropna(axis=1) which will drop columns that have NaN values rather than the rows. I'd suggest using it on the initial price frame from history. Then getting returns and using .dropna() will work fine.

Does this clarify what was causing the algorithm to error??

Cheers,
David

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
# Backtest ID: 55ce059b09e2370c66d9d06b
There was a runtime error.

Many thanks, David,

for you explanation. I am afraid it is quite beyond me, but I think I can still use your solution as a recipe.

Regards,

Tim