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
Alethea Lin
Hi Nicolas,
Both orders are filled because the data feed resolution is set to daily. Here you have two limit orders at 1.49 and 1.41, and the daily bar is most likely wide enough such that it contains both prices. Therefore, before you could cancel the other limit order, the algo would have already filled the other limit order since the price conditions have been met.
We suggest changing the data resolution to 'minute', which would result in a much narrower data bar, and this issue should disappear.
Hope this helps! Thanks for your support!
Nicolas Ferrari
Hello Alethea, thanks for your clear response. I have changed the resolution and definetlty the behaviour was better. So I think that you are right about the resolution.
Thanks
Nicolás!
Nicolas Ferrari
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 QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!