Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Solved: Abnormal return using order_optimal_portfolio method?

I swapped my old ordering method with order_optimal_portfolio as it is a preferred method, but I got problems.

Here are 3 problems that I have encountered:
1.Abnormal return: Check on the Jan 2017, the return abnormally went up.
2. Abnormal leverage: Also, I have set the opt.MaxGrossExposure(1), so that it won't use leverage(portfolio should always be 1). However, it went up since Jan 2017.
3. Open order: Possible explaination could be its unfilled open orders, but I would assume that opt_portfolio method would automatically adjust for that? That it would not order again if there is open order and close all the open orders on the end of day?

Question: What did I do wrong with my code using order_optimal_portfolio??

Here is the link to my previous post that using the traditional method to order stock, it looks a lot normal:
Trading with Benjamin Graham's formula - Fundamentals and bonds

8 responses

Something has not been correctly accounted for regarding the PVAC symbol in the Quantopian database.

@Vladimir
wow, thanks! I was not expecting that. I have fixed it and also added a filter after learning to check the positions. Filtered out all the stocks, which price is below $10, and the results seemed to be improved. Except that it cannot avoid the drawdown in market down times.

@Vladimir
How did you figure that out? It would be very helpful if we could implement this in algorithms as some sort of sanity check.

@ZT YE
I don't know if that's intentional, but if you define context.weights in initialize and never clear the dictionary, you keep on adding weights to it every time you rebalance. That means you are ordering stocks you wanted years ago even if they are currently not in your buy list.
Here's your latest version with fresh weights for every rebalance

@Tentor Testivis
Good to know!
I thought OPT would auto close open orders. Guess I missed this part.

It's not about open orders, they get cancelled if they coldn't be filled at the end of the day. Your algorithm just kept passing to TargetWeights everything that ever was in your weights plus the weights that were just added, thus opening new orders for all of those weights at each rebalance.

@Tentor

How did you figure that out?

I just checked the position log manually and found that the price of PVAC jumped from $ 0.15 to $ 49.00 12-29-2016 for no reason..
So the problem is not solved.


def Rebalance(context, data):  
    # we calculate the weights here  
    context.weights = {}  
    #weights = {}  
    for sec in context.buy_list:  
        if data.can_trade(sec):  
            context.weights[sec] = 0.99 / len(context.buy_list)  

@Tentor Testivis, You are right. I moved the context.weights = {} down and exactly same results as you had.
Rebalance would clear the dictionary each time it runs.
And no, I didn't put it in initialize on purpose, I was looking at the examples others had and it was there, I didn't think that much.

I ran it again and took out the factor limits stocks under 10 dollars, it performs much more robust, with larger drawdowns and higher return.
And somehow it doesn't pick PVAC anymore. No idea why.