Position and P & L
TT .NET SDK allows you to obtain a snapshot of your positions and P&L at any point after the API has successfully synchronized the order books by calling either the TTAPI.GetPositionSnapshot() or TTAPI.GetPositionSnapshots() methods.
The former will return a single Position object for the AccountKey and InstrumentKey that are provided. The latter returns a collection of Position objects for all unique AccountKey / InstrumentKey pairs that you have at least one fill. For example,
private TTAPI m_api;
public void ttNetApiInitHandler(TTAPI api, ApiCreationException ex)
{
if (ex == null)
{
Console.WriteLine("TT.NET SDK INITIALIZED");
// Authenticate your credentials
m_api = api;
m_api.TTAPIStatusUpdate += new EventHandler<TTAPIStatusUpdateEventArgs>(m_api_TTAPIStatusUpdate);
m_api.OrderbookSynced += m_api_OrderbookSynced;
m_api.Start();
}
else if (ex.IsRecoverable)
{
// Initialization failed but retry is in progress...
}
else
{
Console.WriteLine("TT.NET SDK Initialization Failed: {0}", ex.Message);
Dispose();
}
}
private void m_api_OrderbookSynced(object sender, EventArgs e)
{
IList<Position> lists = m_api.GetPositionSnapshots();
}
Note: You must also set the TTAPIOptions.EnablePositions property to true.
You can also subscribe for position and P&L updates by registering for TTAPI.ProfitLossChanged events as follows.
private TTAPI m_api;
public void ttNetApiInitHandler(TTAPI api, ApiCreationException ex)
{
if (ex == null)
{
Console.WriteLine("TT.NET SDK INITIALIZED");
// Authenticate your credentials
m_api = api;
m_api.TTAPIStatusUpdate += new EventHandler<TTAPIStatusUpdateEventArgs>(m_api_TTAPIStatusUpdate);
m_api.OrderbookSynced += m_api_OrderbookSynced;
m_api.ProfitLossChanged += new EventHandler<ProfitLossChangedEventArgs>(m_api_ProfitLossChanged);
m_api.Start();
}
else if (ex.IsRecoverable)
{
// Initialization failed but retry is in progress...
}
else
{
Console.WriteLine("TT.NET SDK Initialization Failed: {0}", ex.Message);
Dispose();
}
}
private void m_api_ProfitLossChanged(object sender, ProfitLossChangedEventArgs e)
{
// …
}