Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Multi-leg order management - Seeking help

The strategy is relatively simple:

Every day, at 9:30 when the market opens, short the stock (from all the US stock universe) that made the biggest (overnight) bullish GAP (today open - yesterday close) if it gapped by at least X% (we can set it to 15%).

The tricky part is the order management part. The order is composed of 3 legs.
The first leg is always opened when the market opens. Second and third legs are fixed in terms of GAP retracement. For example, Market closed yesterday at $10. It opens today at $15. GAP worth $5 which is equal to 50% of yesterday's closing price. So if we set the first leg to be executed at -100% of the GAP retracement, then the first leg will be fixed to $20. (-100% of the GAP = $5 and $15 + $5 = $20.) Same for the third leg.

We also fix Stop Losses and Take Profits in terms of retracement. Each leg has a different SL and TP. Of course, all orders and positions are closed 1 or 2 minutes before market's close.

It would be great if someone could help me coding this strategy or tell me how to do it.

Regards,

Gabriel

4 responses

I said: So if we set the first leg to be executed at -100% of the GAP retracement, then the first leg will be fixed to $20
Please understand this: So if we set the second leg to be executed at -100% of the GAP retracement, then the second leg will be fixed to $20

Hi Gabriel have you written any code yet? If yes, can you post your algo - I can try to help, and you will find many extremely helpful folks in the Q community. If you have not started coding do you have any of the signals being generated outside Q like Excel? If yes, you can import the orders using Fetcher. Below is a shell to import data you can modify for your use.
Best wishes with your strategy, Savio

import pytz  
import pandas as pd  
import datetime

Initialize the timezone:  

EST = pytz.timezone('US/Eastern')

def preview(df):  
    log.info(' \n %s ' % df.head(10))  
    return df

def initialize(context):  
    fetch_csv('insert URL or link to csv', pre_func = preview)

    Initialize slippage settings - I was told to use the default slippage so you might want to do that - volume slippage is not the default  
    set_slippage(slippage.VolumeShareSlippage(volume_limit=.5, price_impact=0))

  these are the IB fees  
set_commission(commission.PerShare(cost=0.005, min_trade_cost=1))

end of day - change minutes to 2 if that is what you prefer  
schedule_function(end_of_day, date_rule = date_rules.every_day(), time_rule = time_rules.market_close(minutes=1))  
def handle_data(context, data):  
    ESTdate = get_datetime().astimezone(EST)  
    for stock in data.fetcher_assets:  
        test for market open by timestamp - you can add any other conditions - data.current will pull information both from fetcher and the minute bars  
        if ESTdate.hour == 9 and ESTdate.minute == 31:  
       Place order for day for example  
             order(stock, shares, style=LimitOrder(limit))  
            get_order(order_id) will give you the order details  
           add the bracket order - profit taker, stop loss condition  
close out positions at the close - note that depending on your slippage settings your order at the close may not be completely filled so you will need to keep that in mind - also read the post on volume discrepancies - you will get quite a bit of insight into Q data - worth noting that open, high, low, close, volume are related to the data in each minute, not the day overall - as far as I know  
def end_of_day(context, data):  
    for stock in context.portfolio.positions:  
        order_target_percent(stock, 0)  

Hi Savio,

Thank you for the code. I am very new to python but I am actually learning it. So I didn't code anything for the moment. Well, I am able to code a very basic algorithm but this one is definitely too ambitious for me I think.

Well, I am not sure to understand what your code stands for. Is it to import an existing backtest from Excel? If yes, the problem is that I can't really backtest this strategy with the trading platform because it just considers too many stocks. That is why I would like to backtest it here on Quantopian.

Regards

Gabriel, look at this excellent post https://www.quantopian.com/posts/simulated-stop-and-limit-orders
This should give you enough code to implement your strategy.
Kind regards
Savio