API Support Forum
User Profile

Viewing User Profile for: RAnde


About

Sep 05, 2017 09:01 AM

Jul 08, 2020 04:51 AM

Feb 03, 2021 08:13 PM



Post Statistics
RAnde has contributed to 20 posts out of 5677 total posts (0.35%) in 2633 days (0.00 posts per day).

20 most recent posts:

API Support » Getting the "ConnectionError" when connecting to the Gain Futures server Jul 08, 2020 @ 04:51 AM (Total replies: 9)

Hi Ruscak,

I want to change my application design in which I will use the same login session for multiple operations of various requests. It's kind of connection pooling. When a request is made, the GFClient instance (unique per user) will be put in the pool, and its state is set to "Used". If there is another request made simultaneously by same user, it will pick up the corresponding GFClient in the pool which is already logged in for the first request.
Basically, that means that multiple requests from the same user can be processed in the same login session (for that user). This will avoid the bottleneck when I have to process requests sequentially per user as you mentioned above.

But I have a concern. GF API works asynchronously, so I have to subscribe to callback events to handle the request's result. And for now, the callback event only processes one request at a time. I'm not sure that GF API works properly in case there are multiple requests made in the same login session and the subscribed callback events must handle all of them.
Is the GF API designed to work like that? Is there any unexpected exception If I apply that new design?

Thanks in advance

API Support » Getting the "ConnectionError" when connecting to the Gain Futures server Jun 30, 2020 @ 05:24 AM (Total replies: 9)

As I mentioned in previous posts, I already disconnect and dispose the GFClient instance at the end of request. I also use the "force login" option when building the connection context.

I'm disconnecting one time before connecting to GF server (as recommended in GF examples), and one more time at the end of request. Does this cause any thing wrong?
I make this question because the issue frequency decreases explicitly after I remove the disconnecting at the end of request (just disconnect before calling GFClient.Connection.Aggrerate.Connect())

You can see details in the code below

private void ConnectToServer(IClientRunner clientRunner, RequestData requestData)
{
// ReSharper disable once InconsistentNaming
var applicationUUID = GetApplicationUUID(requestData);

var connectionContextBuilder = new ConnectionContextBuilder()
.WithUserName(requestData.UserName)
.WithPassword(requestData.Password)
.WithHost(requestData.Url)
.WithPort(GainCapitalFuturesConfiguration.Default.OrderServerPort)
.WithUUID(applicationUUID)
.WithForceLogin(true)
.Build();

// connect to GF server
clientRunner.Client.Connection.Aggregate.Disconnect();
clientRunner.Client.Connection.Aggregate.Connect(connectionContextBuilder);
}

public void Dispose()
{
if (_runner != null)
{
_runner.Dispose();
_runner = null;
}

if (Client == null)
return;

Disconnect();
Client = null;
}

private void Disconnect()
{
if (Client.Connection.Aggregate.IsConnected || Client.Connection.Aggregate.IsConnecting)
Client.Connection.Aggregate.Disconnect();
}


I gonna try the "one Client-per-user" approach combined with the "re-connect" strategy.
One question, should I start and stop the client runner (stop calling advance()) within a request for the "one Client-per-user" OR just start the client runner one time when it is initiated (I still connect and disconnect client inside a request, but keep the client runner running)?

Thanks Ruscak
Edited by RAnde on Jun 30, 2020 05:35 AM

API Support » Getting the "ConnectionError" when connecting to the Gain Futures server Jun 29, 2020 @ 05:12 AM (Total replies: 9)

Hi Ruscak,

#1 - It looks to me that, in your code, after calling Aggregate.Connect the code does not wait for LoginComplete to fire before calling CreateSignal. Aggregate.Connect is not synchronous.

I know that GF API works as an asynchronous approach. Actually, my CreateSignal method will make the current thread to wait until there is a corresponding signal raised in LoginComplete event indicating that the connection process is completed. I only continue to execute my request (call SendOrder, Lookup symbol...) after that process.

Because my application works as synchronous. Every request (get account information, place order...) needs a result returned. So that's how I deal with your asynchronous API.
The point is, in some places I call my request, my code does not connect to GF successfully. The Disconnected and LoginFailed events are raised instead of LoginComplete.
Because my approach has worked, so I think the problem is not from here. If you think it's from my process which I deal with the asynchronous GF API, would you please specify what is wrong here?

#2 - Creating a client per-request is not a use case we've designed for. Typically we expect a single instance that runs for the life of the application, which may connect and disconnect many times, even for different users. Your use case might work, but I can't be sure.

2.1 My application allows multiple users, who have a specific GF Account, work on it. For now, a GF Client will be created and connected to GF server with user's credential every time the user makes a request with their account. And that GF Client instance will be disconnected and disposed within the request.
There may are a lot of users who make requests at the same time. And according to your API documentation, GF API does not support multi-threading. If I use only one single instance of GF Client for the whole application, how can I deal with this requirement?
User cannot wait because the single GF Client instance has been working for another.

2.2 By the way, I have tried to use one single instance of GF Client as your suggestion. It has worked, but it is not stable. In some places during my process, the issue still happens.
When the issue happens, I have tried to re-connect again, sometimes it's connected successfully, sometimes not. The log as below

// The log when the issue happens
GF client disconnected. Message: ConnectionError. Reason: LoginFailure
Login Failed. Reason: ConnectionError

// The log when re-connecting
Login Failed. Reason: ConnectionError

As you can see, the Disconnected event is not even raised when I re-connect. So I don't know exactly the rule of the issue.

#3. One clue I have about this issue. In the place where the issue happens, it takes more time to create an instance of GF Client than where the issue does not occur. Usually, it takes around 10 seconds to create GF Client. But in my process where the issue happens, it takes more than 1 minute (even 2 mins).

I'm getting stuck with this issue several days. I really want to find out the root cause. I'm ready to provide any detail that you need to address this issue.

Thanks a lot.
Edited by RAnde on Jun 29, 2020 05:16 AM

API Support » Getting the "ConnectionError" when connecting to the Gain Futures server Jun 26, 2020 @ 05:42 AM (Total replies: 9)

Hi Ruscak,

1. Do you mean the login request?
I implement a class that encapsulates things which deal with GF. When I make a request from my appliction to GF, the work-flow in that class is as below
- Create GF Client -> create GFCLientRunner and start -> Subscribe events -> Connect to GF server -> process the request (submit order, get position information...) -> dispose GF Client.

Here are the relevant methods

private void CreateClientRunnerAndConnect(RequestData requestData)
{
// Create GFClient and its runner
_clientRunner = _clientRunner.WithClient(GFApi.CreateClient());
_clientRunner.Start();

// Subscribe general events of GF Client
SubscribeEvents(_clientRunner);

ConnectToServer(_clientRunner, requestData);

CreateSignal(requestData, GainCapitalFutureOperationType.ConnectServer);
}

private void ConnectToServer(IClientRunner clientRunner, RequestData requestData)
{
// ReSharper disable once InconsistentNaming
var applicationUUID = GetApplicationUUID(requestData);

var connectionContextBuilder = new ConnectionContextBuilder()
.WithUserName(requestData.UserName)
.WithPassword(requestData.Password)
.WithHost(requestData.Url)
.WithPort(GainCapitalFuturesConfiguration.Default.OrderServerPort)
.WithUUID(applicationUUID)
.WithForceLogin(true)
.Build();

// connect to GF server
clientRunner.Client.Connection.Aggregate.Disconnect();
clientRunner.Client.Connection.Aggregate.Connect(connectionContextBuilder);
}



Methods on ClientRunner

public ClientRunner WithClient(IGFClient client)
{
_runner = new GFClientRunner(client);
Client = client;
return this;
}

public void Start()
{
if(_runner == null)
throw new InvalidOperationException("GFClientRunner is not initialized");

_runner.Start();
}

Is there anything wrong with those codes?

2. The problem may be thread timing. If you're not on the GFAPI thread, you should call Connect inside GF.Threading.Invoke
I see that the GFClientRunner will create a new thread to call Advance(). It may not be on the same thread with my connection process. So I put the connection process into GFClient.Thread.Invoke(), but it does not work. The same issue happened.

3. On the other hand, if the GFAPI thread is being hogged by a complex process, then Advance won't be called frequently enough to successfully log in.
In my process, there are a lot of Tasks which are executed in newly Thread (by Task.Run()) before the GF request is called. Also, I'm using the async/await approach to call between services/functions.

Does this somehow cause a side-effect when working with GF API?
I have tried to call the GF request in place that the process does not call Task.Run() much. And it has worked.

Note that, when the issue happened, I have only called the GF request first time. So it means the problem is not from the sharing existing GFClient (as you can see the code above, I encapsulate the whole GF request process inside a class. Another request in my process will create a new instance of that class and work with another GFClient).

I'm getting stuck and have no any clue. Do you have any other suggestion?
And please check your server log and give me any information which relates to the issue.

Thanks a lot.

API Support » Getting the "ConnectionError" when connecting to the Gain Futures server Jun 25, 2020 @ 05:54 AM (Total replies: 9)

Hi team,

I'm getting the "ConnectionError" in OnDisconnected event after connecting to the GF server. This issue only occurs when I call the GF request inside a complex process.
The code has worked fine and this issue does not happen if I call the request separatedly.

I made a search and see that there is an issue about connection in the old version of GF API. But I'm using the latest one 4.0.3.33 from NuGet.

Is this a known issue from GF? Or there is something wrong that I did cause the problem?

I followed the example on GF documentation and the code has been working.
Any suggestion to address this issue is appreciated.

Thanks.

API Support » "ContractID Invalid" error returned in Order submission Jun 04, 2020 @ 03:35 AM (Total replies: 4)

Hi Evgeny,

You are correct. I'm using two different IGFClient objects for the processes of symbol lookup and order draft sending.
So the client in the order draft sending did not know the Contact that is from the symbol lookup.

I have moved the symbol lookup inside the order draft sending process so that they can use the same client. And the problem disappears.

Thank you very much for helping.
Edited by RAnde on Jun 04, 2020 03:36 AM

API Support » "ContractID Invalid" error returned in Order submission Jun 02, 2020 @ 09:59 PM (Total replies: 4)

Hi Evgeny,

I'm getting contract by using the api of IGFCLient.Contracts.Lookup.ByCriteria. And then I use the ContractID of that contract to create the order draft for the order placing. The process is same as your example.
Please look at the code details as below

The code of symbol lookup

private void ProcessLookupSymbolRequest(RequestData requestData)
{
var symbolLookupRequestBuilder =
GainCapitalFuturesUtility.CreateSymbolLookupRequestBuilder(requestData.PostData, 50);

_clientRunner.Client.Contracts.Lookup.ByCriteria(symbolLookupRequestBuilder.Build());

GainCapitalFuturesDataStorage.AddContracts(requestData.PostData, new List());
}

private void OnSymbolLookupReceived(IGFClient client, SymbolLookupEventArgs e)
{
if (e.Contracts.Any())
{
var newContracts = e.Contracts.ToList();
GainCapitalFuturesDataStorage.AddContracts(_currentRequestData.PostData, newContracts);
}
}


Create OrderDraft

private static OrderDraft CreateNewOrderDraft(GainCapitalFuturesOrder gainFuturesOrder, AccountID accountId, IContract contract)
{
var orderDraft = new OrderDraftBuilder()
.WithAccountID(accountId)
.WithContractID(contract.ID)
.WithSide(gainFuturesOrder.Side)
.WithOrderType(gainFuturesOrder.Type)
.WithPrice(gainFuturesOrder.Price)
.WithPrice2(gainFuturesOrder.Price2)
.WithQuantity((int) gainFuturesOrder.Quantity)
.WithFlags(gainFuturesOrder.Flag);

if (gainFuturesOrder.Type != OrderType.TrailingStopLoss &&
gainFuturesOrder.Type != OrderType.TrailingStopLimit)
return orderDraft.Build();

var delta = GetTrailingStopOrderDelta(gainFuturesOrder, contract);
var referencePrice = gainFuturesOrder.Reference > 0
? gainFuturesOrder.Reference
: contract.CurrentPrice.LastPrice;
var trailingStopData = new TrailingStopData(referencePrice, delta);
orderDraft.WithTrailingStop(trailingStopData);

return orderDraft.Build();
}


Place order

private void ProcessSingleOrderSubmissionRequest(RequestData requestData)
{
var gainFuturesOrder = JsonConvert.DeserializeObject(requestData.PostData);
var orderDraft = gainFuturesOrder.ToOrderDraft(_clientRunner.Client, requestData);
var submittedOrder = _clientRunner.Client.Orders.SendOrder(orderDraft);
}


And I'm using GF API version 4.0.3.33

Thanks Evgeny for helping.

API Support » "ContractID Invalid" error returned in Order submission Jun 02, 2020 @ 03:59 AM (Total replies: 4)

I got the error as below when calling the SendOrder api to submit an order.
"Invalid OrderDraft: ContractID (277300774, electronic) is invalid"

Note that I submit the order with symbol of ESM20. And I get the correct Contract for ESM20 after using the symbol lookup api. Also, I have tried with another symbol (such as GCLN20), but the same error returned.

Is there anyone known this error? And how should I do to solve it?
Edited by RAnde on Jun 02, 2020 04:13 AM

API Support » How to open a sub account under the API account Dec 26, 2017 @ 08:45 PM (Total replies: 1)

Hi,
I'm working with the OEC link API and I have an API account. I want to create a sub account under that API account.
But I could not find out any link for this on the account manager section.

How can I do this? Would you please give me the link for this?

API Support » Order was rejected after submitting Nov 15, 2017 @ 04:44 AM (Total replies: 0)

Hello,

I submit a Market order with valid values. I sent the order to your server successfully. But it was rejected immediately after that. I received the following exception on the Trader Developer Tool

"Order was rejected...Buy 2 ESZ7 MKT at 11/15/2017 4:16:21 PM (Order breaks limit)"
"System Error: Risk violation:.....Max order quantity = 0"

I still submitted order successfully with same values before. So I don't know the reason.
Would you please explain why I get this error?

API Support » Callback event for modifying Order Nov 07, 2017 @ 04:12 AM (Total replies: 0)

I have tried to modify a working order by using method OECClient.ModifyOrder().
I need to get the ID of new modified order for my client order. But I don't know how to do that.

After modifying, the event "OnOrderConfirmed" is not raised. I have tried with event "OnCommandUpdated". But order which is returned in this event is the old (unmodified) order.

Is there any callback event which returns new order that is created for the modification? If not, please show me the way that I can get this.
Edited by RAnde on Nov 07, 2017 04:12 AM

API Support » One Cancel Other order was rejected Nov 06, 2017 @ 10:06 PM (Total replies: 2)

Thanks Chris! I have one more question.

I have tried to submit OCO with 2 Buy orders, one for Limit and one for Stop. The current last price of symbol is 2589.
I submitted with LimitPrice = 2591 and StopPrice = 2588. And these two orders were rejected with exception "Invalid price combination".
Then I submitted successfully with LimitPrice = 2588 and StopPrice = 2591. The current last price of symbol is still 2589.

It means that the price validation for Limit and Stop orders on your API is as following
- For Limit order
+ If Order.Side is Buy, Limit Price must be less than the current last price of symbol
+ If Order.Side is Sell, Limit Price must be greater than the current last price of symbol
- For Stop order
+ If Order.Side is Buy, Stop Price must be greater than the current last price of symbol
+ If Order.Side is Sell, Stop Price must be less than the current last price of symbol

Is my understanding correct?
Edited by RAnde on Nov 06, 2017 10:07 PM

API Support » One Cancel Other order was rejected Nov 06, 2017 @ 04:09 AM (Total replies: 2)

Hello,

I submitted One Cancel Other order successfully by using method SendOCOOrders. But orders of the One Cancel Other order were rejected after that. And I received the following message on OEC Trader Developer application
"Order was reject...(invalid order type)"

My One Cancel Other order includes 2 two Buy order. One has order type of Market, the order has order type of Limit.

Would you please explain why my order was rejected?
Edited by RAnde on Nov 06, 2017 04:10 AM

API Support » Get Order Details Nov 03, 2017 @ 05:06 AM (Total replies: 2)

Thanks Chris!

API Support » Get Order Details Nov 02, 2017 @ 02:04 AM (Total replies: 2)

I need to get the order information such as status, filled quantity...
But I don't see any API method which returns the Order Details by OrderID.

Does API support any method for that purpose? Or is there any method which returns the list of Order by OrderState (such as Completed order, Working order, Expired order...)?

If not, please show me how I can get the details of a specified order
Edited by RAnde on Nov 02, 2017 02:16 AM

API Support » Placing Trailing Stop Order Nov 02, 2017 @ 01:52 AM (Total replies: 5)

Hi ETrifonov

I understood about Reference and Delta. But I still don't understand about OrderDraft.Price.
According to your answer, Trailing Stop order can work well with Reference and Delta.
Why does it need OrderDraft.Price here? What is OrderDraft.Price used for?

And thank you one again for helping me.

API Support » Placed Order ID Nov 01, 2017 @ 11:58 PM (Total replies: 2)

Hi SRuscak

Thank you very much!
As you suspect, I'm getting OrderID from the Order returned by OECClient.SendOrder.
I will try with OECClient.OnOrderConfirmed event.

API Support » Placed Order ID Nov 01, 2017 @ 12:02 AM (Total replies: 2)

Hello
I have tried to place an order to the developer server "api.gainfutures.com". But when I get the order object returned, the property ID is always -1.
Is that a API bug? Or it is the Gain Capital API design?

Because I need to save the placed order ID into my client order. And I will use that value for getting the order detail. So it doesn't make sense when all orders has ID of -1.

In OEC Trader Developer tool, In the section of "Working Orders", I see that Order has a column called "Order #". It seems like that column is the ID number of Order. If so, how can I get that column from API?
Note that that column is not included in the order returned after I placed an order.

API Support » Placing Trailing Stop Order Oct 31, 2017 @ 09:41 PM (Total replies: 5)

Hi ETrifonov

Thank you very much for replying my question. There is some points that I don't understand in your answer yet.

#1 Depending on order side (buy or sell), order will be modified to (+-Delta) price on market price become outside of (Reference +- Delta)

It means that the Trailing Stop Price of order will be modified by (+/-) Delta when market price becomes outside of (Reference +- Delta).

#1 - Is my understanding correct?

#1.1 - If so, in this case, Delta is the trailing stop distance. And depending on Delta and the current market price, system can calculate the trailing stop price. So why we have to input OrderDraft.Price here?

#1.2 - If my understanding is correct, that Delta is the trailing stop distance, so why is Delta set to 0 in your example? The trailing stop distance (Delta) is 0, so the trailing stop price cannot be modified by it. Right?
Edited by RAnde on Oct 31, 2017 09:41 PM

API Support » Placing Trailing Stop Order Oct 31, 2017 @ 05:31 AM (Total replies: 5)

I have tried to place a Trailing Stop Order. But I got an exception "Invalid order part: ExtendedData" from OrderDraft.SendOrder. I see that I have to use the method "OrderDraft.SetTSData" to set values for properties "Reference" and "Delta".

#1 - Would you please explain how these properties is used in the Trailing Stop order process?

In my application, the Trailing Stop order has a property that is used for the trailing stop distance. The system will calculate the stop price of order based on the last trade price of symbol and that trailing stop distance.
E.g: If I place a trailing stop order for ESZ7, the currency trade price is 50. And I put the trailing stop distance is 5. So the trailing stop price will be 45.

#2 - What properties of OrderDraft should I pass the trailing stop distance to? Is that OrderDraft.Price, Reference or Delta?

I'm using your API in my application. And I'm getting stuck with Trailing Stop order. I didn't see any information that resolve my problem in your documentation. Please answer my question.