Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Creating slippage models which need to be calibrated?

Hi all,

Is there a way to create slippage models which must be calibrated per-stock? I saw https://www.quantopian.com/posts/custom-slippage-modeling-transaction-costs-for-algorithmic-strategies , but it looks like that slippage model must be created in initialize(), which is long before we have any price data, or have selected our universe.

I am envisaging setting my universe, pulling down daily price history to estimate the average daily volume and daily volatility, and then plugging that into a standard square-root or ^3/5 slippage model, so from what I understand, this would require being able to update the slippage model with new data in before_trading_start, and still, it would require price data in there.

Is this possible?

Simon.

2 responses

Hi Simon,

At one point, I showed how globals could be used to get data into the slippage model (see below). I haven't looked at it in awhile, so I don't know if it makes any sense. In any case, the globals worked, so maybe the idea would be handy. Accessing the globals in before_trading_start would be a way of configuring the slippage model.

Credit to Anony Mole for writing the original custom slippage model used as a basis for the code below.

Grant

priorOpen = None  
priorClose = None

def initialize(context):  
    context.stocks = [sid(8554),sid(33652)]  
    set_slippage(TradeAtTheCloseSlippageModel(priorOpen,priorClose,1.0))  
    set_commission(commission.PerTrade(cost=0.0))  
def handle_data(context, data):  
    for stock in context.stocks:  
        order(stock,1)  
    global priorOpen  
    global priorClose  
    priorOpen = {}  
    priorClose = {}  
    for sid in data:  
        priorOpen[sid] = data[sid].open_price  
        priorClose[sid] = data[sid].close_price  
        print sid.symbol+' prior open/close: '+str(priorOpen[sid])+'/'+str(priorClose[sid])  
########################################################  
# Slippage model to trade at the prior close or at a fraction of the prior open - close range.  
class TradeAtTheCloseSlippageModel(slippage.SlippageModel):  
    '''Class for slippage model to allow trading at the prior close  
       or at a fraction of the prior open to close range.  
    '''  
    # Constructor, self and fraction of the prior open to close range to add (subtract)  
    # from the prior open to model executions more optimistically  
    def __init__(self, priorOpen,priorClose,fractionOfOpenCloseRange):

        # Store the percent of prior open - close range to take as the execution price  
        self.priorOpen = priorOpen  
        self.priorClose = priorClose  
        self.fractionOfOpenCloseRange = fractionOfOpenCloseRange

    def process_order(self, trade_bar, order):  
        openPrice = priorOpen[trade_bar.sid]  
        closePrice = priorClose[trade_bar.sid]  
        ocRange = closePrice - openPrice  
        ocRange = ocRange * self.fractionOfOpenCloseRange  
        if (ocRange != 0.0):  
            targetExecutionPrice = closePrice - ocRange  
        else:  
            targetExecutionPrice = closePrice  
        log.info('\nOrder:{0} open:{1} close:{2} exec:{3} side:{4}'.format(  
            trade_bar.sid.symbol, openPrice, closePrice, targetExecutionPrice, order.direction))

        # Create the transaction using the new price we've calculated.  
        return slippage.create_transaction(  
            trade_bar,  
            order,  
            targetExecutionPrice,  
            order.amount  
        )  

Yeah that might do the trick, thanks!