Price class: initializers
The Price class has no defined constructors; instead, it provides a series of static methods that return an instance of a Price class:
public static Price FromDouble(Fill fill, double price)
public static Price FromDouble(Instrument instrument, double price)
public static Price FromDouble(InstrumentDetails instrumentDetails, double price)
public static Price FromDouble(Order order, double price)
public static Price FromDouble(OrderProfileBase orderProfile, double price)
public static Price FromString(Fill fill, string price)
public static Price FromString(Instrument instrument, string price)
public static Price FromString(InstrumentDetails instrumentDetails, string price)
public static Price FromString(Order order, string price)
public static Price FromString(OrderProfileBase orderProfile, string price)
public static Price FromTick(Fill fill, int price)
public static Price FromTick(Instrument instrument, int price)
public static Price FromTick(InstrumentDetails instrumentDetails, int price)
public static Price FromTick(Order order, int price)
public static Price FromTick(OrderProfileBase orderProfile, int price)
These methods take a price in ticks (int), points (double), or TT Display (string) format. Additionally, these methods require information from the InstrumentDetails object associated with a contract, which you can specify directly or indirectly through the following objects (all of which reference the associated InstrumentDetails):
- Instrument
- Fill
- Order
- OrderProfile
After calling one of these methods, you need to confirm that the Price object was able to correctly interpret the price that you passed to it. The Price class includes the following properties to check the validity of a price: IsValid and IsTradable.
If you provide a price that the Price object cannot interpret, it sets the internal price of the returned Price object to TT_INVALID_PRICE. In this scenario, the IsValid property returns false. For example, assume that the variable inst represents a valid Instrument instance for the CBOT ZB-Mar20 futures contract.
Price p = Price.FromString(inst, "abc");
bool b = p.IsValid;
As "abc" is not a valid price in the TT Display format for this contract, the IsValid property returns false.
If you provide a valid price that does not fall on an allowable trading price, based on the ticking information in the associated InstrumentDetails instance, the IsTradable property returns false. For example, assume that the variable inst represents a valid Instrument instance for the CBOT ZB-Mar20 futures contract.
Price p = Price.FromDouble(inst, 114.03);
bool b = p.IsTradable;
Recall that the Tick Size of CBOT ZB futures is 1/32 (.03125). Because 114.03 points is not an allowable trading price for this contract, the IsTradable property returns false.
To help in situations like this, you could use the overloaded versions of the static initializer methods that take an additional parameter that allow you to specify a rounding convention to use.
public static Price FromDouble(Fill fill, double price, Rounding rounding)
public static Price FromDouble(Instrument instrument, double price, Rounding rounding)
public static Price FromDouble(InstrumentDetails instrumentDetails, double price, Rounding rounding)
public static Price FromDouble(Order order, double price, Rounding rounding)
public static Price FromDouble(OrderProfileBase orderProfile, double price, Rounding rounding)
public static Price FromString(Fill fill, string price, Rounding rounding)
public static Price FromString(Instrument instrument, string price, Rounding rounding)
public static Price FromString(InstrumentDetails instrumentDetails, string price, Rounding rounding)
public static Price FromString(Order order, string price, Rounding rounding)
public static Price FromString(OrderProfileBase orderProfile, string price, Rounding rounding)
public static Price FromTick(Fill fill, int price, Rounding rounding)
public static Price FromTick(Instrument instrument, int price, Rounding rounding)
public static Price FromTick(InstrumentDetails instrumentDetails, int price, Rounding rounding)
public static Price FromTick(Order order, int price, Rounding rounding)
public static Price FromTick(OrderProfileBase orderProfile, int price, Rounding rounding)
The rounding parameter is an enumeration with the following possible values that specifies how to round the price relative to the TT Base Tick Size:
- Up
- Down
- Nearest
- None
Suppose you modified the previous example to use rounding, as follows:
Price p = Price.FromDouble(inst, 114.03, Rounding.Up);
bool b = p.IsTradable;
In this case, the price rounds 114.03 to 114.03125, so the IsTradable property returns true.
You can also round a price relative to the Tick Size by invoking the Round() method for a Price object. This is illustrated by extending the prior example, as follows:
Price p2 = Price.FromDouble(inst,114.007,Rounding.Up);
Price p3 = p2.Round(Rounding.Up);
When the first line of code is executed, the Price class rounds 114.007 to 114.0078125 because the TT Base Tick Size is 1/128 (0.0078125). The IsValid property returns true but the IsTradable property returns false. When the second line of code is executed, the Price class rounds 114.0078125 to 114.03125 because the Tick Size is 1/32 (0.03125). Now, both the IsValid and IsTradable properties return true.
Note that the Round() method does not modify its internal value. Instead, it returns a new Price object with the rounded value.