Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Maybe trailing stop will crash the trading system

At market open I'd like to check if current price jump 3% higher than yesterday's close, If so, I will set contest.TrailingStop = True And then price is monitored, if price continue to rise another 2%, all short position will be cleared. using the following code.

def handle_data(context,data):  
    if context.TrailingStop == True:  
        current = data.current(context.VXX,'price')  
        exchange_time = get_datetime('US/Eastern')  
        #log.info("realtime {}".format(current))  
        #if exchange_time.date() == context.startDate :  
           # log.info("realtime {}".format(current))  
        if current>context.stop:  
            rebalance(context.sidsLongVol, data, 0.0)  
            return  

And trailing stop monitor will be canceled using contest.TrailingStop = False at market close . During the backtest one day when the trailing stop order is triggered unexpected behavior just happened. The algorithm keeps selling and buying causing a dramatic loss in one day. Check the transactions in the plot [Big loss just happened ][1]

[1]: http://i.imgur.com/Y83boTx.png

The tricky part is when I begin the backtest from 2011/01/01 , the big loss happened.
But if i begin the backtest after 2012, the codes works correctly. I am confused now. I can't imagine what will happen if I use real money

2 responses

After some trial I found out

def handle_data(context,data):  
    if context.TrailingStop == True:  
        current = data.current(context.VXX,'price')  
        exchange_time = get_datetime('US/Eastern')  
        #log.info("realtime {}".format(current))  
        if exchange_time.date() == context.startDate :  
            positions = context.portfolio.positions  
            VolPosition = positions[context.VXX].amount  
            log.info('position {}'.format(VolPosition))  
            #log.info("realtime {}".format(current))  
        if current>context.stop:  
            rebalance(context.sidsLongVol, data, 0.0)  
            context.TrailingStop = False  

will solve the problem

Without seeing the whole backtest, I'm going to guess that the key change was to add context.TrailingStop = False after you place the order to close your position.

There is no guarantee that your order will be filled in the next minute, and your code was set to keep placing an order every minute. Those orders all stacked up, I bet, and then your position got way overloaded in the other direction when they filled. By setting your flag to False you ensured that only a single order would be placed.

You should probably keep digging on what that shows you. Trading in those ETFs can be deadly. It's predictable, until it isn't. I'm guessing you hit the August 24th trading date when ETF pricing fell to pieces for most of an hour.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.