Working with prices and quantities
Working with the Price class
For efficiency, TT .NET SDK processes and stores prices as integer values. However, you can represent price in different formats, such as Double or String, and in styles such as Ticks. You also might want to perform mathematical calculations with these prices. TT .NET SDK exposes this functionality in the Price object.
The following snippet shows some of the ways you can manipulate prices.
void m_priceSubscription_FieldsUpdated(object sender, FieldsUpdatedEventArgs e)
{
if (e.Error == null)
{
Price lastP = e.Fields.GetLastTradedPriceField().Value;
if ( lastP.IsValid )
{
// Extract the price as a Double
decimal lastAsDecimal = lastP.ToDecimal();
// Extract the price as a String
string lastAsString = lastP.ToString();
// Extract the price in ticks
int lastAsTicks = lastP.ToTicks();
// Move up 1 tick
lastP++;
// Move down 3 ticks
lastP -= 3;
}
}
}
Note
TT .NET SDK guarantees that any Price object returned from a price subscription is a valid object. However, because the underlying value might be invalid or null. To ensure that a price contains a valid value, you should check the Price.IsValid property of the returned object before using its value.
Working with the Quantity class
As quantities can also be expressed in a variety of ways, TT .NET SDK exposes a Quantity class. This class allows you to extract order quantities in Lots and Flow (e.g. Energy contracts). For example:
void m_priceSubscription_FieldsUpdated(object sender, FieldsUpdatedEventArgs e)
{
if (e.Error == null)
{
QuantityField ltq = e.Fields.GetLastTradedQuantityField();
Quantity lastQ = ltq.Value;
if ( lastQ.IsValid )
{
// Extract the quantity in flow
int lastQinFlow = lastQ.ToFlow();
}
}
}
The Quantity class also supports the increment and decrement operators, which are particularly useful when changing the quantity of a contract that trades in flow.
Note
TT .NET SDK guarantees that any Quantity object returned from a price subscription is a valid object. However, because the underlying value might be invalid or null. To ensure that a quantity contains a valid value, you should check the Quantity property of the returned object before using its value.
Other ways to use the Price and Quantity classes
The Price and Quantity classes also contain a series of useful static methods that allow you to create instances directly. For example, assume your application allows users to enter prices (ps) and quantities (qs) as strings. To convert a string representation of a price to a Price class or a quantity string to a Quantity class for a given Instrument (instr), you could use the static Price.FromString (or Quantity.FromString) method as follows.
Price p = Price.FromString( instr, ps );
Quantity q = Quantity.FromString( instr, qs );