An in-depth look at the Price class
TT .NET SDK Price class provides users with the ability to compare and manipulate prices in a variety of formats. It exposes:
TT .NET SDK Price class provides users with the ability to compare and manipulate prices in a variety of formats. It exposes:
The Price class overloads common arithmetic operators that allow you to perform arithmetic on prices in a variety of formats.
For example, assume that the variable inst represents a valid instance for the Eurex FGBL-Jun20 futures contract. You could use the overloaded ++ operator to increase the price by one tick.
The result of this code snippet would be:
Because these operators return a Price object, you can create expressions such as:
The expression (p1 + 1) calls the +(Price price, int tick) operator. Because this expression also returns a Price object, it then calls the *(Price price, int tick) operator to complete the evaluation.
The Price class overloads common comparison operators that allow you to compare prices with one another in a variety of formats.
For example, assume that the variable inst represents a valid InstrumentDetails instance for the Eurex FGBL-Mar20 futures contract. You could use the overloaded == operator to indicate whether two prices are equal:
In this example, the price on the left-hand-side (lhs) of the comparison operator is a Price object and the price on the right-hand-side (rhs) of the comparison operator is in ticks format (an integer). Therefore, TT .NET SDK calls:
which produces the following result:
After you get a Price object that is both valid and tradable, you can use the following methods to extract the price in different formats.
| Desired format | Price class method |
|---|---|
| Points | ToDecimal() |
| Ticks | ToTicks() |
| TT Display | ToString() |
| Full price (in contract currency) | ToPrimaryCurrency() |
| Full price (in trader’s currency) | ToNativeCurrency() |
| Full price (in specific currency) | ToCurrency() |
For example, assume that the variable inst represents a valid InstrumentDetails instance for the Eurex FGBL-Dec20 futures contract. You could use these methods as follows to display the price in the various formats:
The result of this code snippet would be:
The Price class has no defined constructors; instead, it provides a series of static methods that return an instance of a Price class:
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):
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.
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.
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.
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:
Suppose you modified the previous example to use rounding, as follows:
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:
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.