Back

Print Enum name

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 market

I'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?

Update Backtest








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
1

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.


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))
0

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.

0

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.


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.

https://www.quantconnect.com/lean/docs#topic24346.html
0

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. :)

0

Curious if there's any update on this from PythonNet?

0

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.

0

Update Backtest





0

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.


Loading...

This discussion is closed