Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Simulated stop & limit orders

Optimize doesn't like to see any stop and limit orders
so here's an example of taking profit or stopping loss using handle_data each minute.

03 06:31 track_orders:110 INFO track_orders active. Headers ...  
03 06:31 track_orders:111 INFO              Shares     Shares  
03 06:31 track_orders:112 INFO Min   Action Order  Sym  Now   at Price   PnL   Stop or Limit   Cash  Id  
03 06:33 opens:32 INFO order 0.5 TOL prc 48.84  limit 48.87  
03 06:33 opens:32 INFO order 0.5 SPY prc 269.00  limit 269.13  
03 06:33 _trac:128 INFO    3   Buy 511 TOL _ at 48.84 limit 48.86                    50000  1e4d  
03 06:33 _trac:128 INFO    3   Buy 92 SPY _ at 269.00 limit 269.13                   50000  8299  
03 06:34 _trac:128 INFO    4      Bot 92 SPY (92) at 269.12 limit 269.13             25228  8299  
03 06:36 _trac:128 INFO    6      Bot 191/511 TOL (191) at 48.78 limit 48.86         15906  1e4d  
03 06:37 _trac:128 INFO    7      Bot all 320/511 TOL (511) at 48.81 limit 48.86     279  1e4d

Profit at minute 64 ...  
03 07:34 handle_data:58 INFO takeprofit 511 TOL cb 48.82  prc 49.33  ratio 1.01  
03 07:34 _trac:128 INFO   64   Sell -511 TOL (511) at 49.33                          279  be63  
03 07:35 _trac:128 INFO   65      Sold -511 TOL _ at 49.38                           25499  be63  
03 12:48 _trac:128 INFO  378   Sell -92 SPY (92) at 270.63                           25499  95e5  
03 12:49 _trac:128 INFO  379      Sold -92 SPY _ at 270.53                           50375  95e5  
04 06:33 opens:32 INFO order 0.5 TOL prc 50.67  limit 50.70  
04 06:33 opens:32 INFO order 0.5 SPY prc 271.32  limit 271.46  
04 06:33 _trac:128 INFO    3   Buy 497 TOL _ at 50.67 limit 50.69                    50375  8e4f  
04 06:33 _trac:128 INFO    3   Buy 92 SPY _ at 271.32 limit 271.45                   50375  dab3  
04 06:34 _trac:128 INFO    4      Bot 92 SPY (92) at 271.37 limit 271.45             25397  dab3  
04 06:36 _trac:128 INFO    6      Bot 497 TOL (497) at 50.67 limit 50.69             201  8e4f

Stop at minute 14 ...  
04 06:44 handle_data:58 INFO stoploss 497 TOL cb 50.70  prc 50.12  ratio 0.99  
04 06:44 _trac:128 INFO   14   Sell -497 TOL (497) at 50.12                          201  3839  
04 06:45 _trac:128 INFO   15      Sold -497 TOL _ at 50.20                           25137  3839  
04 12:48 _trac:128 INFO  378   Sell -92 SPY (92) at 271.70                           25137  f0c5  
04 12:49 _trac:128 INFO  379      Sold -92 SPY _ at 271.64                           50115  f0c5