Hello, 

I have the attached algo, that trades penny stocks after Universe Selection based on conditions of volume and sma short and sma long. The logic of the algo is the folling:

Once the stocks are in the portfolio, the algo send limit orders to sell with profit. If this limit order is not filled and the stocks is on holding for more than 3 days, the algo send a limit order to sell on loss. This logic is in myRebalance() function. 

I used two dictionaries that are self.profitOrders and self.lossOrders to track the limit orders tickets(for take profits or cut loss) and cancel one of them when the other is filled. The logic for cancel orders once the other is filled is in OnOrderEvent.  However, there are times that the profit order is filled, and the loss order keep with Cancel Pending Status but finally is filled too, which is wrong because when the profit order is filled the desire behaviour is that the opposite order(loss order) were close.

I send below an specific example of this  with the stock symbol MSN , on 30/3/2018 where the ticket is cancelled but at the same time, the order is filled. 

Below is a copy of the log text for that specific symbol and the workflow:

Send order to Purchase stock :

2018-03-17 00:00:00 Time: 3/17/2018 4:00:00 AM OrderID: 16 Symbol: MSN Status: Submitted

Limit Order is filled

2018-03-23 00:00:00 Time: 3/23/2018 4:00:00 AM OrderID: 16 Symbol: MSN Status: Filled Quantity: 437 FillPrice: 1.445 USD OrderFee: 2.185 USD

Send limit order to take profit:

2018-03-24 00:00:00 Time: 3/23/2018 1:31:00 PM OrderID: 22 Symbol: MSN Status: Submitted

Because order to take profit is not filled within 3 days after submitted, send a limit order to sell at loss:

2018-03-30 00:00:00 Time: 3/29/2018 1:31:00 PM OrderID: 30 Symbol: MSN Status: Submitted

Now we have two open orders.

The limit order to take profit is filled:

2018-03-30 00:00:00 Time: 3/30/2018 4:00:00 AM OrderID: 22 Symbol: MSN Status: Filled Quantity: -437 FillPrice: 1.49 USD OrderFee: 2.185 USD

Inmediately the limit order to sell at loss is cancelled:

2018-03-30 00:00:00 Time: 3/30/2018 4:00:00 AM OrderID: 30 Symbol: MSN Status: CancelPending

But on the same moment, this order is filled:

2018-03-30 00:00:00 Time: 3/30/2018 4:00:00 AM OrderID: 30 Symbol: MSN Status: Filled Quantity: -437 FillPrice: 1.41 USD OrderFee: 2.185 USD

Thanks for help with this.

Regards,

Nicolás

Author