There are enum objects in QC/Lean that I would like to know how to print. For instance, I would like to get the OrderEvent.Status Enum name. Here's the public OrderStatus Enum members:
public enum OrderStatus : System.Enum MemberDescriptionCanceledOrder cancelled before it was filledCancelPendingOrder waiting for confirmation of cancellationFilledCompleted, Filled, In Market Order.InvalidOrder invalidated before it hit the market (e.g. insufficient capital)..NewNew order pre-submission to the order processor.NoneNo Order State YetPartiallyFilledPartially filled, In Market Order.SubmittedOrder submitted to the marketI've tried the following to see the status of an order event:
def OnOrderEvent(self, order_event):
self.Log(str(order_event.Status)) # <-- Output: 3
What is returned is an integer, probably representing the index for a member element. I would imagine there is some way to pass that into a Enum object to get the name (e.g. self.OrderStatus[order_event.Status]). But I have had no luck finding anything like that.
How might one go about accessing the Enum members given an index?
Alexandre Catarino
I would advice you to create a dictionary with the information you found (public enum OrderStatus) and use it to get the enum name:
self.status_dict = { 0: 'New', 1: 'Submitted', 2: 'PartiallyFilled', 3 : 'Filled', 4: 'Canceled', 6: 'None', 7: 'Invalid', 8: 'CancelPending' } def OnOrderEvent(self, order_event): self.Log(self.status_dict[order_event.Status]) # <-- Output: Filled
James Candan
I find it odd that we would have to 1) assume the numerical order, and 2) recreate the dict. The enum for OrderStatus is public. Is there not some way to import it, like with color?
'''Import the .NET Common Language Runtime (CLR)''' from clr import AddReference AddReference("System.Drawing") from System.Drawing import * class MyColorTester(QCAlgorithm): def Initialize(self): self.Debug("Green: " + str(Color.Green))
Jared Broad
Ironically Color isn't a true enum; its a static list of properties in a class. The str method is probably just showing the name of the property.
Enums are always numerical but yes its not great having to know their numbers. This is why we put the numbers into the docs though =) I've pingged PythonNet and we'll see if we can get better enum string display support.
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.
James Candan
ok. thanks for the work-around then.
"This is why we put the numbers into the docs though"
Sorry, where is this documented? Looking at the following documentation on OrderStatus does not list the corresponding numbers.
James Candan
Okay, so I can assume the order in the documentation is correct, and went with that.
There are several enum properties I would like to see to better understand what's going on in my algorithm, and I have to hard-code each:
def get_order_status_name(self, index): return { 0: 'New', 1: 'Submitted', 2: 'PartiallyFilled', 3: 'Filled', 4: 'Canceled', 6: 'None', 7: 'Invalid', 8: 'CancelPending' }[index] def get_order_direction_name(self, index): return { 0: 'Buy', 1: 'Hold', 2: 'Sell', }[index] def get_order_type_name(self, index): return { 0: 'LIMIT', 1: 'MARKET', 2: 'MARKETIFTOUCHED', 3: 'STOP', 4: 'STOPLOSS', 5: 'TAKEPROFIT', 6: 'TRAILINGSTOPLOSS' }[index] . . . order = self.Transactions.GetOrderById(order_event.OrderId) self.Debug("{}: A {} order to {} was {} with quantity {}".format( self.Time, self.get_order_type_name(order.Type), self.get_order_direction_name(order.Direction), self.get_order_status_name(order_event.Status), order.Quantity ))
I hope we hear back soon from PythonNet for a solution. :)
James Candan
Curious if there's any update on this from PythonNet?
James Candan
I see that the Lean API has changed, what was only 8 enum options for OrderStatus above, is now 12 (https://www.quantconnect.com/lean/documentation/topic5609.html). We really need a way to get the String name associated with enum objects. Jared Broad, do please look into your last comment about PythonNet, it would be truly helpful to have this.
Nick Abram
James Candan, did you ever get an answer about how this enum maps the integers to the order status strings?Â
Jasper van Merle
I'm not James, but I think I can help here. You can find the enum to integer mapping for order statuses in Lean in Common/Orders/OrderTypes.cs:87. Also, if you know the type of the enum, you can convert from int to string using Enum.Format (this also works in Python). For example, if order is of type QuantConnect.Orders.OrderTicket, you can get the order's status as a string using Enum.Format(OrderStatus, order.Status, "g").
Sebastian Lueneburg
My workaround for the missing helper method in System.Enum. Define in your utils.py or anywhere:
I use it for logging. Don't redefine the dict anywhere. Breaks DRY and sooner or later your logging
James Candan
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!