Connect to OEC OEC Application Programming Interface

Getting Started: connect to OEC

Hello, OEC!

Let's create an instance of OEC API and connect it to OEC.

First of all, we need to specify UUID. This is a string that assigned to an application/developer by our Customer Service to identify the application. Because we are building an example, let's use common UUID for all samples in OEC: 9e61a8bc-0a31-4542-ad85-33ebab0e4e86.

As the most of methods of OECAPI, Connect(String, Int32, String, String, Boolean) method is asynchronous one. We don't know was our connection connected to OEC server or not on the next line of source code. So, we need to hook OnLoginComplete, OnLoginFailed and OnDisconnected events.

C#
                    using System;

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

		static void Main(string[] args)
		{
			Console.WriteLine("Hello, OEC!");
			oecapi = new OEC.API.OECClient();
			oecapi.UUID = "9e61a8bc-0a31-4542-ad85-33ebab0e4e86";
			oecapi.Connect("api.gainfutures.com", 9200, "vic", "vic", true);
			Console.WriteLine("Connecting...");
			oecapi.OnLoginComplete += new OEC.API.OnLoginCompleteEvent(oecapi_OnLoginComplete);
			oecapi.OnLoginFailed += new OEC.API.OnLoginFailedEvent(oecapi_OnLoginFailed);
			oecapi.OnDisconnected += new OEC.API.OnDisconnectedEvent(oecapi_OnDisconnected);
			Console.WriteLine("Press Ctrl-C to exit");
			System.Windows.Forms.Application.Run();
		}
		
		static void oecapi_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);
		}

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

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

OECAPI is based on Windows messages inside. That's why we need to call Application.Run() to initiate message pump in the thread of console application. Please note: message pump should be initiated in the same thread where the instance of OECClient was created. All events will be raised in the context of this thread. Moreover, OECAPI is not thread-safe: we need to make sure our application doesn't try to access it from another thread

OnLoginComplete is our starting point to OECAPI functionality after successful connect: connection is established, all mandatory information have been loaded. For example, we can extend it with displaying of the first available account and ES basecontract

C#
		static void oecapi_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);
			DisplayContract(oecapi.BaseContracts["ES"]);
		}

		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("\tCash: {0:c}", totalBalance.Cash);
			Console.WriteLine("\tOpen P/L: {0:c}", totalBalance.OpenPnL);
			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 DisplayContract(OEC.API.BaseContract baseContract)
		{
			Console.WriteLine("BaseContract: {0} - {1}", baseContract.Symbol, baseContract.Name);
			Console.WriteLine("\tCurrency: {0}", baseContract.Currency.Name);
			Console.WriteLine("\tExchange: {0}", baseContract.Exchange.Name);
			Console.WriteLine("\tKind: {0}", baseContract.ContractKind);
			Console.WriteLine("\tType: {0}", baseContract.Type);
			Console.WriteLine("\tGroup: {0}", baseContract.ContractGroup.Name);
			Console.WriteLine("\tNormal trading session: {0}-{1}, day close: {2}", baseContract.StartTime, baseContract.StopTime, baseContract.DayClose);
			Console.WriteLine("\tPrice Format: {0}", OEC.API.Strings.GetPriceMask(baseContract.PriceFormat, 0));
		}
See Also

Reference

Other Resources