Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Duplicate Orders on 4 May 2016 for CMG stock only

Hi there,

I've come up with a simple moving average buy signal, and I ran the test for the code just to see if there were any bugs, with the start date on 4 May 2016 and end date 10 May 2016. The stock I chose was CMG, and for some strange reason, it makes a double purchase at the same time 10:14am. My algo sets the order at purchasing close to $10,000 worth of stock, but I end up with close to $20,000.

I have tried this code on a) other days and b) other stocks, and there hasn't been this situation arising. Just wanted to check if there was a unique reason for it that I'm not catching it in my algo, or I need to rework my ordering process.

Thanks for your help :)

def initialize(context):  
    """  
    Called once at the start of the algorithm.  
    """  
    context.security = sid(28016)

def handle_data(context,data):  
    # Create variables for the Fast:9, Medium:25 and Slow:50 tick MAs  
    fast = 9  
    medium = 25  
    slow = 50  
    # Define the price history  
    price_hist_fast = data.history(context.security, 'price', fast, '1m')  
    price_hist_medium = data.history(context.security, 'price', medium, '1m')  
    price_hist_slow = data.history(context.security, 'price', slow, '1m')

    # Define the moving averages and current price  
    mavg_fast = price_hist_fast.mean()  
    mavg_medium = price_hist_medium.mean()  
    mavg_slow = price_hist_slow.mean()  
    current_price = data.current(context.security, 'price')  
    # Rule to enter into the position  
    if context.portfolio.positions[context.security].amount == 0:  
        if mavg_fast > mavg_medium:  
            if mavg_medium > mavg_slow:  
                if current_price > mavg_fast:  
                    try:  
                        order_value(context.security, 10000)  
                    except Exception as e:  
                        print(str(e))  
    record('Leverage', context.account.leverage)  
1 response

Hi Mun Yang,

I've run your algorithm and you're right that there are two orders on consecutive minutes. The reason for that is that your prerequisite for buying more shares is simply that the number currently owned is 0 (as well as your signal). It just so happens that, on the minute where you place the first order for CMG, CMG has zero trading volume. So with our slippage model, no shares actually get bought that minute, but the order is still pending. Then, the next minute, your algorithm sees that no shares are owned yet and places a second order. Both orders are eventually filled completely, leaving you with $20,000 worth of shares instead of $10,000.

One way you can avoid this is by checking for open orders before placing any new orders. I would also recommend that you use order_target_percent to easily put exactly 100% of your money into stocks.

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.