API Support Forum
OEC API > API Support > Not able to connect with API
Author Topic: Not able to connect with API
(7 messages, Page 1 of 1)
Moderators: VPfau
BGoyal9136
Posts: 27
Joined: Sep 21, 2017


Posted: Mar 29, 2020 @ 11:07 PM             Msg. 1 of 7
Hi Team,

I hope you guys doing great.
I am using GF client in my application . I am using below code to connect with GF. but still getting connection error .

_gfClient.Connection.Aggregate.Disconnect();
_gfClient.Connection.Aggregate.Connect(
new ConnectionContextBuilder()
.WithUserName(uid)
.WithPassword(pass)
.WithUUID(UUID)
.WithPort(9210)
.WithHost(GFApis)
.WithForceLogin(true)
.Build());


Can you please check this. where I am doing wrong .or why I am getting connection error.

Note : when I am using my credentials with advance example with GF api .I am able to login.


Thanks
Kapil Goyal
CMicciche902
Posts: 348
Joined:


Posted: Mar 30, 2020 @ 05:21 PM             Msg. 2 of 7
What is the message provided by server? I saw successful connection in our logs. Did you type credentials incorrectly previously?
Chris M
BGoyal9136
Posts: 27
Joined: Sep 21, 2017


Posted: Mar 31, 2020 @ 06:53 AM             Msg. 3 of 7
Hi CMicciche902,

Thanks for your reply.
when I am using advanceExample with my credential I am getting successful connection. but when I am using Gain Future api with my application code. still I am getting FailedReason as ConnectionError in LoginFailed method.

for your reference Below are the code I am using :


using GF.Api;
using GF.Api.Connection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace WpfApp1
{
class Class1
{

private static GF.Api.IGFClient gfClient;

public static void Main(String[] args)
{
gfClient = GF.Api.Impl.GFApi.CreateClient();

var runner = new GF.Api.Threading.GFClientRunner(gfClient);
runner.Start();

Console.WriteLine("Connecting...");

gfClient.Connection.Aggregate.LoginCompleted += OnLoginComplete;
gfClient.Connection.Aggregate.LoginFailed += onLogonFailed;
gfClient.Connection.Aggregate.Disconnected += OnDisconnected;
gfClient.Logging.ErrorOccurred += (client, e) => Console.WriteLine($"{e.Exception.Message}");


gfClient.Connection.Aggregate.Disconnect();
gfClient.Connection.Aggregate.Connect(
new GF.Api.Connection.ConnectionContextBuilder()
.WithUserName("BGoyal9136")
.WithPassword("Test1234!")
.WithUUID("9e61a8bc-0a31-4542-ad85-33ebab0e4e86")
.WithPort(9210)
.WithHost("api.gainfutures.com")
.WithForceLogin(true)
.Build());


var timer = new System.Timers.Timer { Interval = TimeSpan.FromSeconds(2).TotalMilliseconds };
timer.Elapsed += (_, __) =>
{
// The timer callback is on a different thread than the GFClientRunner, so we must delegate to the runner thread
gfClient.Threading.Invoke(() =>
{
if (!gfClient.Connection.Aggregate.IsConnected)
return;

var account = gfClient.Accounts.Get().First();
GF.Api.Balances.IBalance totalBalance = account.TotalBalance;

Console.WriteLine($"Account: {account.Spec}");
Console.WriteLine($"\tNetLiq: {totalBalance.NetLiq:c}");
Console.WriteLine($"\tCash: {totalBalance.Cash:c}");
Console.WriteLine($"\tOpen P/L: {totalBalance.OpenPnL:c}");
Console.WriteLine($"\tTotal P/L: {totalBalance.RealizedPnL + totalBalance.OpenPnL:c}");
Console.WriteLine($"\tInitial Margin: {totalBalance.InitialMargin:c}");
Console.WriteLine($"\tNet Options Value: {totalBalance.LongCallOptionsValue + totalBalance.LongPutOptionsValue + totalBalance.ShortCallOptionsValue + totalBalance.ShortPutOptionsValue:c}");
Console.WriteLine($"Average Positions: {account.AvgPositions.Count}");
Console.WriteLine($"Orders: {gfClient.Orders.Get().Count}, last one: {(gfClient.Orders.Get().Count > 0 ? gfClient.Orders.Get().Last().ToString() : string.Empty)}");
Console.WriteLine();
});
};

Console.WriteLine("Press any key to quit");
//Console.ReadKey();

timer.Stop();
runner.Stop();
}


public static void OnDisconnected(IGFClient client, DisconnectedEventArgs e)
{
Console.WriteLine("Login Disconnected: " + e.Message);


}

public static void onLogonFailed(IGFClient client, LoginFailedEventArgs e)
{

Console.WriteLine("Login Failed: " + e.FailReason);
}

public static void OnLoginComplete(IGFClient client, LoginCompleteEventArgs e) {
Console.WriteLine("Login Success ");
}

}





}




Thanks
Kapil Goyal
CMicciche902
Posts: 348
Joined:


Posted: Mar 31, 2020 @ 04:06 PM             Msg. 4 of 7
Does this issue occur with latest API version 4.0.3.28, latest can be found at https://www.nuget.org/packages/GFAPI/
Chris M
BGoyal9136
Posts: 27
Joined: Sep 21, 2017


Posted: Mar 31, 2020 @ 11:09 PM             Msg. 5 of 7
Hi,

I am using GF 4.0.3.5 api.

Thanks
Kapil Goyal
BGoyal9136
Posts: 27
Joined: Sep 21, 2017


Posted: Mar 31, 2020 @ 11:20 PM             Msg. 6 of 7
Hi ,

I have tried with both api verisons :
GF 4.0.3.5
GF 4.0.3.28
but still getting same error :

Connection Error

Thanks
Kapil Goyal
JSmith5611
Posts: 187
Joined:


Posted: Jun 11, 2020 @ 12:22 PM             Msg. 7 of 7
I just created a new .net framework console application using .net 4.6.2. Added GFAPI 4.0.3.33 via nuget, and copied the code posted here.

After uncommenting out the readkey line, and starting the timer, it works just fine for me.

I then tested with every version of the GFAPI available on nuget:
4.0.3.33 - Works
4.0.3.28 - Works
4.0.3.16 - Works
4.0.3.5 - Fails with your description.

Please make sure you update your reference to GFAPI through nuget.
Jason Smith