I open an order as follows:
MainOrder=self.MarketOrder(symbol=symbol,quantity=order_size,tag='main', synchronous=False)
Note tag='main'. Then:
def OnOrderEvent(self, orderEvent: OrderEvent) -> None:
tag = orderEvent.Ticket.Tag
self.Log(f"<{tag}>")
outputs this:
tag=<main - Oanda Fill Event>
tag should be “main” not “main - Oanda Fill Event”
How is this happening? Looking through the forum posts I saw a thread from 2020 saying the order functions don't support named parameters. But I have seen later posts that show examples with “tag=xxxxx”.
I am using tags to keep track of my main, stoploss, and take-profit orders. I understand QC does not support ‘bracket orders’ - orders with integrated SL & TP - so I have to manage them separately.
I am open to other ideas for how to manage this.
Mia Alissi
Based on the information provided in the extracts, it seems that the
tag
parameter in theMarketOrder
function is not supported as a named parameter. This was mentioned in a forum discussion from August 2020. The correct syntax to include a tag for aMarketOrder
would beself.MarketOrder(ticker/symbol, quantity, False, tag string)
.However, it's important to note that the
tag
parameter is not used to differentiate between different types of orders (like main, stop loss, or take profit orders). Instead, it's used to add a note or comment to the order.If you want to manage different types of orders separately, you might want to consider using different variables to store the order tickets for each type of order. For example, you could use
mainOrderTicket
,stopLossOrderTicket
, andtakeProfitOrderTicket
to store the order tickets for the main order, stop loss order, and take profit order respectively.Here's an example of how you could do this:
Then, in the
OnOrderEvent
method, you can check theOrderId
of theorderEvent
against theOrderId
of each order ticket to determine which type of order has been filled:This way, you can keep track of each type of order separately and handle them accordingly in your algorithm.
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.
Lee Allen
2 problems with Mia's answer.
First, ‘she’ is referring to the same 2020 post that says named parameters are not supported, but there have been subsequent posts recommending named parameters. And my testing indicates the Tag normally does work when specified as a named parameter.
Second: I agree that would be the most straightforward way to code it - having separate variables (or dictionary items) to keep track of the main, SL, TP orders. But I am trying to build a robust system that runs correctly across broker reconnects and even program restarts. So by using tags I can always infer the system state from examining the open orders, and incoming order events.
Mia Alissi
I apologize for the confusion. If your testing indicates that the
tag
parameter works as a named parameter, then you should continue using it as such. The information in the extracts might be outdated or incorrect.As for your second point, using tags to manage your orders across broker reconnects and program restarts is indeed a good strategy. However, it seems there's an issue with the tag being appended with additional information like "- Oanda Fill Event". This behavior is not explicitly documented in the provided extracts.
One possible workaround could be to parse the tag in your
OnOrderEvent
method to extract the original tag. Here's an example:This code splits the tag string at ' - ' and takes the first part, which should be your original tag.
However, this is just a workaround and might not work in all cases. It would be best to contact QuantConnect support for a more definitive solution to this issue. You can reach them at support@quantconnect.com.
As for the issue of order tags not being restored upon algorithm restart, it seems to be a known issue based on a Github issue from the extracts. A potential solution suggested in the comments of the issue is to pull the order information from cloud storage, match by brokerage id, and attempt to map the order tags back to the order. However, this solution has not been implemented yet. You might want to follow this issue for updates or contact QuantConnect support for more information.
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.
Lee Allen
Sorry one more thing. Everything worked fine in testing. It is only since I deployed my program Live I am having this problem. It's almost certainly related to connecting to Oanda vs. the LEAN engine ‘simulating’ Oanda. And since I am only seeing the problem in Live trading it's very difficult to experiment to narrow this down.
Lee Allen
"As for the issue of order tags not being restored upon algorithm restart, it seems to be a known issue based on a Github issue from the extracts."
Ah, that changes everything. Now there is no reason for me to use tags at all. I will implement your previous suggestion.
Mia Alissi
I'm glad to hear that you found the information useful. Implementing separate variables for each type of order (main, stop loss, take profit) can indeed be a good strategy for managing your orders. Here's the example code again for your reference:
And in the
OnOrderEvent
method:This way, you can keep track of each type of order separately and handle them accordingly in your algorithm. If you have any more questions or run into any issues, feel free to ask.
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.
Lee Allen
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!