quantopian / zipline

Zipline, a Pythonic Algorithmic Trading Library
https://www.zipline.io
Apache License 2.0
17.68k stars 4.73k forks source link

Bracket orders / OCO bracket #366

Open femtotrader opened 10 years ago

femtotrader commented 10 years ago

Hello,

it will be nice if Zipline / Quantopian could support bracketed order http://optionstradingbeginner.blogspot.fr/2010/08/bracketed-order.html http://optionstradingbeginner.blogspot.fr/2008/11/types-of-orders-in-trading.html

Bracket OCO http://help.tradestation.com/09_01/tradestationhelp/ob/about_oco_oso_orders.htm

see also this issue: https://github.com/quantopian/zipline/issues/189

Kind regards

femtotrader commented 10 years ago

see https://www.quantopian.com/posts/bracket-orders and maybe also OCA orders (one cancel all) https://www.quantopian.com/posts/oca-orders

femtotrader commented 9 years ago

see http://quant.stackexchange.com/questions/16042/how-to-model-bracket-orders/16077?iemail=1&noredirect=1#16077 and http://stackoverflow.com/questions/10445936/r-quantstrat-orders-cancel-each-other

dalejung commented 9 years ago

@femtotrader for your own stuff, reading through the quantstrat code would be beneficial. I only learned R so I could understand it.

The OCO logic is fairly straight forward iirc. The only gotcha is that if a single bar triggers multiple orders in the orderset. For my stuff, it's modeled like so:

Executor receives price event
    passes price, orderbook to execution_model
    execution_model gathers all possible triggered fills
        if multiple orders from same orderset can be triggered:
            execution_model calls orderset_model that determines the winner
    Executor now has all fills modeled by execution_model. 
    executes orders and Orderbook cancels any orphan orderset orders.

I think what's important is that you don't execute immediately on limit/stop price trigger because you might have multiple triggered orders. You can break the tie randomly, default to adverse outcome, or maybe dip down to more granular data to determine which order was triggered first. If you're working on sub-minutely data, I don't know how often you'd run into the multiple trigger problem.

You can actually unit test your python code against R/quantstrat. http://nbviewer.ipython.org/gist/dalejung/3357531/faber.ipynb is an example of getting the equivalent output from R and python. That's notebook is like 2 years old so I dunno if it'll still work, but more of an example.

femtotrader commented 9 years ago

see https://github.com/quantopian/zipline/issues/189 and https://www.quantopian.com/posts/a-framework-to-manage-bracket-orders-with-sl-tp-and-additional-features-trailingstop-breakeven-expiration-and-advanced-statistics