Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Order a Stock, then place a limit order ASAP
API

Hey!

I am having some trouble.

My goal is to purchase a stock, and as soon as I do that, to place a limit order for it at 2% above it's current price.

The below code doesn't seem to work.

My thought is that when I place the buy order, it isn't completing by the time I place the sell order, however, I'm not sure how to handle that or wait for that event.

Thanks for your help!

# This is what interactive brokers charges us  
set_commission(commission.PerShare(cost=0.0075, min_trade_cost=1.00))

# Put any initialization logic here.  The context object will be passed to  
# the other methods in your algorithm.  
def initialize(context):  
    # I want this to run every trading day, a couple minutes after we start trading  
    schedule_function(  
        func=myDailyTradingFunction,  
        date_rule=date_rules.every_day(),  
        time_rule=time_rules.market_open(minutes=60),  
        half_days=False  
    )

def myDailyTradingFunction(context,data):  
    # These are the stocks I want to trade between  
    oilUp = symbol('UWTI')  
    oilDown = symbol('DWTI')  
    # Figure out which stock to buy  
    stockToBuy = oilDown  
    # Place the Buy order  
    buyOrderId = order_percent(stockToBuy, 1)  
    # Place the Sell order  
    limitPrice = data[stockToBuy].price*1.02  
    stopPrice = data[stockToBuy].price*0.99  
    order_percent(stockToBuy, -1, style=StopLimitOrder(limit_price=limitPrice,stop_price=stopPrice))  
    return True  
5 responses

With schedule_function, this is easy.

Split myDailyTradingFunction into two functions: myBuy and mySell. Schedule myBuy to run some minutes after market open (eg. 60), then schedule mySell to run one minute later. myBuy should decide what to buy, place the buy order, and calculate limitPrice and stopPrice; mySell should just place the stop limit order.

To make it possible for the two functions to pass data, make local variables members of context, ie. prefix each local variable name with context., so eg. stockToBuy will become context.stockToBuy, and so on.

That sounds like it would work, thanks.

However, There could be a point at which the order hasn't completed yet. Is there anyway to see outstanding orders that are not completed or events that get trigged once an order is completed?

Could I run a while command and sleep for 30 seconds until the order is completed; as a dirty trick?

Thanks again!

You can use get_open_orders to see if your order has been filled. But, with schedule_function, you only get one chance per day to sell. You could schedule mySell not 1 minute after myBuy, but a little later. Or let mySell look at buyOrderId, and cancel the order if it hasn't been filled at all (it knows how many shares have been filled), place stop-limit if filled in full, or, if filled in part, perhaps place the stop-limit order for that many shares and cancel the rest of the buy order. Or schedule mySell several times after myBuy, each time placing a stop-limit order on the number of shares that have been filled and not "sold" yet. Or do all of the above.

Sleeping will do you no good. Each call to handle_data (and, I imagine, also your scheduled function) has a 50-second time limit, and an exception is thrown if it's exceeded. On the other hand, orders are reported as filled (or not) only in the next minute.

@Andre

I know I am a little late here, but I am trying to do the same thing as David was trying to do. However, when I wrote a function to sell my securities at a designated stop/limit price, the command is alway ignored, and the securities are never sold, even though I know that they have exceed the price at which I want the limit order to be executed.