Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Confused.....How do I do a stop limit order with the order_optimal_portfolio API

I'm in the process of converting my manual order algorithm and replacing it with the order_optimal_portfolio.

I am currently using the style=StopOrder to ensure that I sell under certain pricing conditions.

Any help would be greatly appreciated.

5 responses

order_optimal_portfolio I believe only does market orders.

That seems crazy from a contest perspective. Quantopian completely eliminates the ability to develop creative algorithms for selling at specific price thresholds....

@Steve... I second what you said. I've been struggling to create code that follows Q's requirements. Had some creative algos with good return, good alpha, good beta, etc. Got disqualified from the contest because my leverage dropped below 0.7!? Tried to fix it different ways but can't seem to put this puzzle together. I wonder with so many requirements, how many algos make the cut. (using order_optimal_portfolio).

Good luck
PC

@ Paulo -

One way to force the leverage to 1.0 is to use:

    weights = opt.calculate_optimal_portfolio(  
        objective=objective,  
        constraints=constraints,  
        )  
    weights = weights - weights.mean()  
    weights = weights/weights.abs().sum()  
    objective = opt.TargetWeights(weights)  
    order_optimal_portfolio(  
        objective=objective,  
        constraints=[],  
        )  

Basically, run the optimizer, demean and normalize, and then run again unconstrained. No guarantee you won't get disqualified for other infractions, but at least the leverage problem should be fixed.

@Grant,

Thank you for sharing that code snippet. I tried it and it does seem to control leverage much better. I will have to tweak other parts of my algo because my returns dropped some. However, I did expect to have to rework the logic a bit anyway.