Multi Example

A couple of topics in the Community seem to be oriented as to how to keep track of orders, especially when several data feeds are in play and also including when multiple orders are working together like in the case of bracket orders

The script below tries to serve as a sample by allowing the user to:

Use 3 data feeds

Use either A single buy order of type Market when usebracket=False or A bracket order set ( usebracket=True In this case how the bracket set is created can be specified Issuing 3 orders (1 parent + 2 children) when rawbracket=True Making a call to buy_bracket when rawbracket=False The main bracket order has an expiry period after valid days (default is 10 )

A position will be entered (or at least attempted to enter) when the weekday matches the enter value defined for each data feed which defaults to [1, 3, 4]

A open position will be exited With a regular close after a holding period. The period is defined individually for each of the data feeds in a list with the parameter hold which defaults to [7, 10, 15] This will trigger the cancelation of the stop side of any bracket order if present (which will automatically cancel the other side) Or if brackets are in use, when either the stop (limit losses) or the limit (take profit) side gets executed (the system will automatically cancel the other side)

Orders are kept in dict which uses the data as the key contains a list per data entry with the orders which are open for each data Once the orders have been issued, the management is done in notify_order



Additionally:

A TestSizer is used which will return different values for buy and sell orders as a test Because only long operations are initiated, only the buy sizing will be returned and the output log will contain no traces of the sell sizing.

A profuse log of operations, positions and order management is created to aid in understanding what’s happening.

Let’s see a sample execution with the default values of usebracket=True and rawbracket=True (to ease the plot, volume will be removed):

$ ./mult-values.py --plot volume=False 2001-01-02 d0 Position 0 2001-01-02 Data d0 OType buy Sizing to 1 2001-01-02 d0 Main 1 Stp 2 Lmt 3 2001-01-02 d1 Position 0 2001-01-02 d2 Position 0 2001-01-03 d0 Order 1 Status Accepted 2001-01-03 d0 Order 2 Status Accepted 2001-01-03 d0 Order 3 Status Accepted 2001-01-03 d0 Order 1 Status Completed -- No longer alive main Ref 2001-01-03 d0 Position 1 2001-01-03 d1 Position 0 2001-01-03 d2 Position 0 2001-01-04 d0 Order 3 Status Completed -- No longer alive limit Ref 2001-01-04 d0 Order 2 Status Canceled -- No longer alive stop Ref ... ... ... 2006-12-27 d0 Order 2036 Status Accepted 2006-12-27 d0 Order 2037 Status Accepted 2006-12-27 d0 Order 2038 Status Accepted 2006-12-27 d0 Position 0 2006-12-27 d1 Position 0 2006-12-27 d2 Position 0 2006-12-28 d0 Position 0 2006-12-28 d1 Position 0 2006-12-28 d2 Position 0 2006-12-29 d0 Position 0 2006-12-29 d1 Position 0 2006-12-29 d2 Position 0

A 2nd execution sets rawbracket=False

$ ./mult-values.py --plot volume=False --strat rawbracket=False

Which outputs exactly the same result, this time having used buy_bracket .

And finally disabling bracket usage:

./mult-values.py --strat usebracket=False --plot volume=False

Conclusion

This should serve as a good example of order management, with multiple data feeds and order sets.

Sample usage

$ ./mult-values.py --help usage: mult-values.py [-h] [--data0 DATA0] [--data1 DATA1] [--data2 DATA2] [--fromdate FROMDATE] [--todate TODATE] [--cerebro kwargs] [--broker kwargs] [--sizer kwargs] [--strat kwargs] [--plot [kwargs]] Multiple Values and Brackets optional arguments: -h, --help show this help message and exit --data0 DATA0 Data0 to read in (default: ../../datas/nvda-1999-2014.txt) --data1 DATA1 Data1 to read in (default: ../../datas/yhoo-1996-2014.txt) --data2 DATA2 Data1 to read in (default: ../../datas/orcl-1995-2014.txt) --fromdate FROMDATE Date[time] in YYYY-MM-DD[THH:MM:SS] format (default: 2001-01-01) --todate TODATE Date[time] in YYYY-MM-DD[THH:MM:SS] format (default: 2007-01-01) --cerebro kwargs kwargs in key=value format (default: ) --broker kwargs kwargs in key=value format (default: ) --sizer kwargs kwargs in key=value format (default: ) --strat kwargs kwargs in key=value format (default: ) --plot [kwargs] kwargs in key=value format (default: )

Sample Code