Hello,
In helping me of the script of Levitikon on OCO orders and the base script MultipleSymbol on GitHub, I try to applicate at this two fusionned code, multiple level of Take-Profit and Quantity foreach.
In the Backtest Below when you delet from line 137 at 146, all is good, orders are well sended and triggered. But if you let the code like it is, this become the chaos. However, I have modified the code to separate relative orders for the level 1 of take-profit and the 2. I don't see really where i'am wrong.
Derek Melchin
Hi Gmamuze Cht,
The reason the algorithm is not working as expected is because of the logic in OnOrderEvent. As soon as both sets of OCO orders are submitted, if one of the take-profit or stop-loss levels are hit in either set, all the standing orders are cancelled. Instead, we should check which OCO set the filled (or partially-filled) order is from and cancel the remaining orders in that set only. To do this, we need to add a helper method to the OCO classes to determine which one is responsible for managing a given order ID.
public bool InOrders(int orderId) { foreach (var orderTicket in this.OrderTicketsUp1) if (orderTicket.OrderId == orderId) return true; return false; }
In addition to the proper cancelling of orders, the algorithm also runs into issues when both the take-profit and stop-loss levels are hit in a single bar. To avoid this, we can increase the resolution of our data, as was done in the backtest below. See the attached files for the full solution.
Best,
Derek Melchin
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.
Gmamuze Cht
Hello,
I have cloned your Algo Derek and I encountered an interesting issue, in the backtest below (cloned from your), I just change consolidator on 240 minutes for get 4h candle.
In this configuration as example in the orders resume, the 2020-03-02 at 8h00 , StopMarket and Limit order are filled with a difference filled date equal to 9 days.
if you have 5 minutes to give me, thank you!
https://imgur.com/nBUPtYo
Derek Melchin
Hi Gmamuze,
When we increase the bar period this much, a new position with OCO orders is opened up before the algorithm had a chance to cancel the TP & SL orders of the previous OCO set. This is because the `ProfitLossOrdersUp1` and `ProfitLossOrdersUp2` properties get overwritten. We can resolve this by storing our `OneCancelsOtherTicketSetUp` objects in a list. This way, we just loop through the OCO sets until we find the one that corresponds to the recently-filled order and cancel the remaining TP/SL orders in that set. See the attached backtest for reference.
Going forward, this algorithm could be improved by moving the position sizing of the TP and SL orders out of OnData and into OnOrderEvent. Doing so enables us to set our TP and SL price levels based off the actual fill price of the entry order instead of what we predict it to be in OnData. Additionally, the `OneCancelsOtherTicketSetUp1` and `OneCancelsOtherTicketSetUp2` classes should be merged together to keep the code dry.
Best,
Derek Melchin
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.
Gmamuze Cht
Hello Derek,
What a wonderfull work lol, with your code i got 0.0042% of error on 1000 trades, and the two trades of the error cause come certainly from an other thing in my code.
I use a timespan of 1h conbined with my signal to send orders (6 orders each times), And the two error cause orders are MarketOrder N°6. I think the engine dont have the time to send 6 differently orders in one time. But this is an other question !
Thanks again !
Gmamuze Cht
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!