Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How can I limit the number of orders in a given day without stoping the algorithm?

I'm trying to fine tune the "sample record variables" by adding another indicator, but sometimes the parameter happens many times in a single day, and since I'm using commission of $10/trade it drives me to -99%. How could I add a counter, or a "last order" parameter to compare it in the if statement. I tried adding a counter (context.d) but it did not help me since it does not reset daily, also I added a """ and tradeday != to time """ but it said it was invalid. Is there any way to program the algorithm so it does not do more that X trades a day? set_max_order_count works but stops the algorithm all together. Could someone show me a way to add a statement like do not buy if the last transaction (buy or sell) was within 1% of the current price. Thanks in Advance

if (short > long) and (context.price > short) and cash > context.price:
order(context.stock, +cash/context.price)
log.info("Buying %s" % (context.stock))
log.info(context.price)
log.info(share)
log.info(tradeday)
context.d = context.d +1
log.info("# %s" % (context.d))

elif (short < mid) and (context.price < short) and share > context.min_notional:  
    order(context.stock, -share)  
    log.info("Selling %s" % (context.stock))  
    log.info(context.price)  
    log.info(share)  
    log.info(tradeday)  
    context.d = context.d +1  
    log.info("# %s" % (context.d))  
2 responses

Hi Lucas,

Thanks for sharing your problem! There are a couple of ways to go about doing this. The first way is to use 'set_max_order_size' and put a try/except block around your order statement like so:

try:  
    order(sid(24), 100)  
except:  
    print "No More Orders For Today  

This way you no longer have the trading guards stopping your algorithm mid-run. The problem with this however is that if you have two orders for 60 shares, it will only execute the first 60 and won't execute the last 40 (assuming the max number of shares you specified is 100).

The way to alleviate that problem is to use this solution (which works in minutely mode):

import numpy as np  
import talib


def initialize(context):  
    context.sid = sid(24)  
    context.max_order_size = 100  
    context.total_ordered = 0  
    context.day = 0

# Will be called on every trade event for the securities you specify.  
def handle_data(context, data):  
    #: At the start of each day, reset the number of total ordered stocks  
    if get_datetime().day != context.day:  
        context.total_ordered = 0  
        context.day = get_datetime().day  
    if context.total_ordered < context.max_order_size:  
        #: Define order size  
        order_size = 94  
        #: If order size is too big calculate a smaller order size  
        if order_size + context.total_ordered > context.max_order_size:  
            order_size = context.max_order_size - context.total_ordered

        #: Use the smaller order size  
        order(context.sid, order_size)  
        context.total_ordered += order_size  

Let me know if you need any more help

Seong

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.

Thank you very much!!!
I ended up using this :

buy = (short > mid) and context.date != day and cash > context.price

context.date = none
day = get_datetime().day
if buy:
order(context.stock, +cash/context.price)
context.date = day

and it seems to work, I do not want to have a max or min of stock since I want to use all the cash. At least it does not drive me to zero.