API Support Forum
OEC API > API Support > Account balance
Author Topic: Account balance
(8 messages, Page 1 of 1)
Moderators: VPfau
SOtziger3689
Posts: 4
Joined: May 19, 2015


Posted: May 27, 2015 @ 04:21 PM             Msg. 1 of 8
Dear Support,

I wrote a simple console application to display account information and open positions. The code below is a working example borrowed heavily from the official documentation.

If I end (CTRL + C) and restart the application, the DisplayAccount() information doesn't update. The information only updates after executing a trade.

My objective is to have a console application, which I can run once per day to write the above mentioned information to a text file.

Can I retrieve the current account information only using the OEC.API.OnAccountSummaryChangedEvent or OEC.API.OnBalanceChangedEvent event?

Thanks,
-S


using System;

namespace ConsoleSample
{
class Program
{
static OEC.API.OECClient oecapi;

static void Main(string[] args)
{
oecapi = new OEC.API.OECClient();
oecapi.UUID = "9e61a8bc-0a31-4542-ad85-33ebab0e4e86";
oecapi.Connect("api.openecry.com", 9200, "User", "Password", true);
Console.WriteLine("Connecting...");
oecapi.OnLoginComplete += new OEC.API.OnLoginCompleteEvent(onLoginComplete);
oecapi.OnLoginFailed += new OEC.API.OnLoginFailedEvent(onLoginFailed);
oecapi.OnDisconnected += new OEC.API.OnDisconnectedEvent(onDisconnected);
Console.WriteLine("Press Ctrl-C to exit");
System.Windows.Forms.Application.Run();
}

static void onLoginComplete()
{
Console.WriteLine("OnLoginComplete: CompleteConnected={0}", oecapi.CompleteConnected);
Console.WriteLine("\tAccounts: {0}, orders: {1}, base contracts: {2}", oecapi.Accounts.Count, oecapi.Orders.Count, oecapi.BaseContracts.Count);
DisplayAccount(oecapi.Accounts.First);
DisplayPositions(oecapi.Accounts.First);
}

static void onLoginFailed(OEC.Data.FailReason reason)
{
Console.WriteLine("OnLoginFailed: {0}", reason);
}

static void onDisconnected(bool unexpected)
{
Console.WriteLine("OnDisconnected: {0}", unexpected ? "unexpected" : "expected");
}

private static void DisplayAccount(OEC.API.Account account)
{
Console.WriteLine("Account: {0}", account.Spec);
OEC.API.Balance totalBalance = account.TotalBalance;
Console.WriteLine("\tNetLiq: {0:c}", totalBalance.NetLiq);
Console.WriteLine("\tNetLiquidatingValue: {0:c}", totalBalance.NetLiquidatingValue);
Console.WriteLine("\tCash: {0:c}", totalBalance.Cash);
Console.WriteLine("\tOpen P/L: {0:c}", totalBalance.OpenPnL);
Console.WriteLine("\tSettle P/L: {0:c}", totalBalance.SettlePnL);
Console.WriteLine("\tInitial Margin: {0:c}", totalBalance.InitialMargin);
Console.WriteLine("\tNet Options Value: {0:c}", totalBalance.LongCallOptionsValue + totalBalance.LongPutOptionsValue + totalBalance.ShortCallOptionsValue + totalBalance.ShortPutOptionsValue);
Console.WriteLine("Average Positions: {0}", account.AvgPositions.Count);
}

private static void DisplayPositions(OEC.API.Account account)
{
foreach (OEC.API.Position pos in account.AvgPositions)
{
Console.WriteLine("Average Position. {0}/{1}: Net Pos: {2} @ {3}, Bought: {4}, Sold {5}, Prev Pos: {6} P/L: {7:c}",
account.Spec, pos.Contract.Symbol, pos.Net.Volume, pos.Net.Price, pos.Long.Volume, pos.Short.Volume, pos.Prev.Volume, pos.OTE + pos.Gain);
}
}
}
}
ETrifonov
Posts: 63
Joined:


Posted: May 29, 2015 @ 11:19 AM             Msg. 2 of 8
Hello -S,

Can you please explain what you mean by "information doesn't update"?

You call DisplayAccount() method only once after success login.
And information should update each time you start application and logged in.

Of course you can use OEC.API.OnAccountSummaryChangedEvent or OEC.API.OnBalanceChangedEvent events to obtain necessary updates.

For example, I added to your code subscription to the OEC.API.OnAccountSummaryChangedEvent and now receive updates for account.


class Program
{
static OEC.API.OECClient oecapi;

static void Main(string[] args)
{
oecapi = new OEC.API.OECClient();
oecapi.UUID = "9e61a8bc-0a31-4542-ad85-33ebab0e4e86";
oecapi.Connect("api.openecry.com", 9200, "User", "Pass", true);
Console.WriteLine("Connecting...");

oecapi.OnLoginComplete += new OEC.API.OnLoginCompleteEvent(onLoginComplete);
oecapi.OnLoginFailed += new OEC.API.OnLoginFailedEvent(onLoginFailed);
oecapi.OnDisconnected += new OEC.API.OnDisconnectedEvent(onDisconnected);
oecapi.OnAccountSummaryChanged += oecapi_OnAccountSummaryChanged; // subscibe to account changes

Console.WriteLine("Press Ctrl-C to exit");

System.Windows.Forms.Application.Run();
}

private static int _SummaryLine;
private static int _SummaryLines;
private static Account _MonitoringAccount;

static void oecapi_OnAccountSummaryChanged(OEC.API.Account account, OEC.API.Currency currency)
{
if (account != _MonitoringAccount)
return;

ClearSummary();

Console.CursorTop = _SummaryLine;

UpdateSummary();
}

static void ClearSummary()
{
Console.CursorLeft = 0;
Console.CursorTop = _SummaryLine;

var line = " ".PadRight(Console.BufferWidth, ' ');

for (var i = 0; i < _SummaryLines; i++)
Console.WriteLine(line);
}

static void UpdateSummary()
{
_SummaryLine = Console.CursorTop;

Console.WriteLine();
DisplayAccount(_MonitoringAccount);
Console.WriteLine();
DisplayPositions(_MonitoringAccount);

_SummaryLines = Console.CursorTop - _SummaryLine;
}

static void onLoginComplete()
{
Console.WriteLine("OnLoginComplete: CompleteConnected={0}", oecapi.CompleteConnected);
Console.WriteLine("\tAccounts: {0}, orders: {1}, base contracts: {2}", oecapi.Accounts.Count, oecapi.Orders.Count, oecapi.BaseContracts.Count);

_MonitoringAccount = oecapi.Accounts.First;
UpdateSummary();
}

static void onLoginFailed(OEC.Data.FailReason reason)
{
Console.WriteLine("OnLoginFailed: {0}", reason);
}

static void onDisconnected(bool unexpected)
{
Console.WriteLine("OnDisconnected: {0}", unexpected ? "unexpected" : "expected");
}

private static void DisplayAccount(OEC.API.Account account)
{
Console.WriteLine("Account: {0}", account.Spec);
OEC.API.Balance totalBalance = account.TotalBalance;
Console.WriteLine("\tNetLiq: {0:c}", totalBalance.NetLiq);
Console.WriteLine("\tNetLiquidatingValue: {0:c}", totalBalance.NetLiquidatingValue);
Console.WriteLine("\tCash: {0:c}", totalBalance.Cash);
Console.WriteLine("\tOpen P/L: {0:c}", totalBalance.OpenPnL);
Console.WriteLine("\tSettle P/L: {0:c}", totalBalance.SettlePnL);
Console.WriteLine("\tInitial Margin: {0:c}", totalBalance.InitialMargin);
Console.WriteLine("\tNet Options Value: {0:c}", totalBalance.LongCallOptionsValue + totalBalance.LongPutOptionsValue + totalBalance.ShortCallOptionsValue + totalBalance.ShortPutOptionsValue);
Console.WriteLine("Average Positions: {0}", account.AvgPositions.Count);
}

private static void DisplayPositions(OEC.API.Account account)
{
foreach (OEC.API.Position pos in account.AvgPositions)
{
Console.WriteLine("Average Position. {0}/{1}: Net Pos: {2} @ {3}, Bought: {4}, Sold {5}, Prev Pos: {6} P/L: {7:c}",
account.Spec, pos.Contract.Symbol, pos.Net.Volume, pos.Net.Price, pos.Long.Volume, pos.Short.Volume, pos.Prev.Volume, pos.OTE + pos.Gain);
}
}
}



Evgeny
Edited by ETrifonov on May 29, 2015 at 11:20:42
SOtziger3689
Posts: 4
Joined: May 19, 2015


Posted: May 29, 2015 @ 03:13 PM             Msg. 3 of 8
Dear ETrifonov

Quote:
Can you please explain what you mean by "information doesn't update"?

You call DisplayAccount() method only once after success login.
And information should update each time you start application and logged in.

I mean the information doesn't update each time I start the application and logged in using my example during market hours with open positions. Hence, I start the application, stop it, wait 5 minutes, start again and the information doesn't update. I tried this multiple times and even waited for a few hours.
Edited by SOtziger3689 on May 29, 2015 at 15:15:59
ETrifonov
Posts: 63
Joined:


Posted: May 29, 2015 @ 03:33 PM             Msg. 4 of 8
Simon,

Example you provided should work.
Can you please provide user name you are using to login?
Your sample application output will be helpful too.

Thank you.

Evgeny
SOtziger3689
Posts: 4
Joined: May 19, 2015


Posted: May 29, 2015 @ 03:52 PM             Msg. 5 of 8
Dear Evgeny,

The username is SOtziger3689 and below is the sample application output. Hope this helps!

Thanks


D:\Projects\ConsoleSample\ConsoleSample\bin\Debug>ConsoleSample.exe
Connecting...
Press Ctrl-C to exit
OnLoginComplete: CompleteConnected=True
Accounts: 1, orders: 0, base contracts: 497
Account: API002602
NetLiq: $49'156.25
NetLiquidatingValue: $49'522.50
Cash: $49'303.75
Open P/L: $0.00
Settle P/L: $0.00
Initial Margin: $5'684.00
Net Options Value: $-147.50
Average Positions: 2
Average Position. API002602/ESM5: Net Pos: 1 @ 2120.125, Bought: 0, Sold 0, Prev Pos: 1 P/L: $0.00
Average Position. API002602/OESQ5 P1700: Net Pos: -1 @ 2.95, Bought: 0, Sold 0, Prev Pos: -1 P/L: $0.00
Account Summary Changed. API002602 (USD): P/L = -563.75. Total Net Liq: $48'592.50
Account Summary Changed. API002602 (USD): P/L = -551.25. Total Net Liq: $48'605.00
Account Summary Changed. API002602 (USD): P/L = -563.75. Total Net Liq: $48'592.50
Account Summary Changed. API002602 (USD): P/L = -551.25. Total Net Liq: $48'605.00
Account Summary Changed. API002602 (USD): P/L = -563.75. Total Net Liq: $48'592.50
Account Summary Changed. API002602 (USD): P/L = -551.25. Total Net Liq: $48'605.00
^C
D:\Projects\ConsoleSample\ConsoleSample\bin\Debug>
ETrifonov
Posts: 63
Joined:


Posted: May 29, 2015 @ 04:02 PM             Msg. 6 of 8
Thank you for information.

Sorry but I confused, you said that you do not receive updates but in application output I see Account summary - sample works.

If you do not have similar output each time you start your application then this can be due to communication or login errors.

Regards,
Evgeny.

Evgeny
SOtziger3689
Posts: 4
Joined: May 19, 2015


Posted: May 29, 2015 @ 04:10 PM             Msg. 7 of 8
Ok, then this is probably a misunderstanding from my side how the account summary works. The part below, i.e., the account summary is always the same. After stopping and starting the application, even on the next day. Does it only change after a trade?

Account: API002602
NetLiq: $49'156.25
NetLiquidatingValue: $49'522.50
Cash: $49'303.75
Open P/L: $0.00
Settle P/L: $0.00
Initial Margin: $5'684.00
Net Options Value: $-147.50
ETrifonov
Posts: 63
Joined:


Posted: May 29, 2015 @ 04:31 PM             Msg. 8 of 8
Account summary updates each time when positions updates.
Please try my example and check behavior.

Regards,
Evgeny

Evgeny