Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Max Order Count Error?

Hello,

I have placed a trading guard, set max order count, to max of 3 trades per day. But when I back-test it, it gives me a runtime error of trading control violation.

def initialize(context):  
    context.aapl= sid(45570)  
    set_max_order_count(3)  
    set_commission(commission.PerShare(cost=0.0000, min_trade_cost=0))

def handle_data(context,data):  
    hist= data.history(context.aapl,'price',20, '1m')  
    log.info(hist.head())  
    sma_20= hist.mean()  
    sma_10= hist[-10:].mean()  


    open_orders= get_open_orders()  


    if sma_10>1.02*sma_20:  
        order_target_percent(context.aapl,.025)  
    elif sma_10<1.05*sma_20:  
        order_target_percent(context.aapl,0)  

    record(leverage=context.account.leverage)  

I've attached the code I used. Any input will be helpful!

1 response

The problem is you are trying to place more than 3 orders a day (which you set as a constraint using 'set_max_order_count(3)'). You probably realized that though. This constraint function simply throws an error, and by itself, doesn't keep you from trading more than 3 times a day. It just causes an error which unhandled will stop the program. If you don't want the program to halt then you will need to handle the error. Something like this.

def initialize(context):  
    set_max_order_count(3)  


def handle_data(context,data):  
    try:  
        order(symbol('AAPL'), 1)  
    except:  
        # put any logic here that you want to do if the order limit was reached.

See attached algo for a simple example. Look at the logs to see whats happening.