API Support Forum
OEC API > API Support > VBA Sample COM Book
Author Topic: VBA Sample COM Book
(4 messages, Page 1 of 1)
Moderators: VPfau
ASykes4318
Posts: 18
Joined:


Posted: Jan 29, 2022 @ 08:11 AM             Msg. 1 of 4
The Auto-Reconnect does not appear to be passed on login and does not work. What changes have to be made to the sample code provided by StoneX to get the reconnect to work properly?



Public WithEvents connection As ServerConnectionApi
Public client As GF_Api_COM.IGFComClient
Private Server As String

Public Sub Disconnect()
If Not connection Is Nothing Then
connection.Disconnect
Rem Set c = Nothing
End If
End Sub

Public Sub Connect(Name As String, Pass As String, Reconnect As Boolean)
On Error GoTo Err

If client Is Nothing Then
Set client = New GFComClient
client.Threading.CreateRunnerFor(client).Start
Set connection = client.connection.Aggregate
Server = "api.gainfutures.com"
End If

Dim Builder As ConnectionContextBuilder
Set Builder = New ConnectionContextBuilder
Builder.WithUserName(Name) _
.WithPassword(Pass) _
.WithUUID("9e61a8bc-0a31-4542-ad85-33ebab0e4e86") _
.WithHost(Server) _
.WithPort (9210)

connection.Connect Builder.Build
Exit Sub
Err:
MsgBox Err.Description
End Sub


Public Function Connected()
If connection Is Nothing Then
Connected = False
Else
Connected = connection.IsConnected
End If
End Function

Private Sub connection_LoginComplete()
On Error GoTo Err
Config.UpdateStatus
AvgPositions.Initialize client
Quotes.Initialize client
Quotes.SubscribeAll True
DOM.Initialize client
Balances.Initialize client
Bars.Initialize client
CompleteOrders.Initialize client
WorkingOrders.Initialize client
Abbreviated.Initialize client
Exit Sub
Err:
MsgBox Err.Description
End Sub

Private Sub connection_LoginFailed(ByVal reason As FailReason)
On Error GoTo Err
Config.LoginFailed reason
Exit Sub
Err:
MsgBox Err.Description
End Sub

Private Sub connection_Disconnected(ByVal reason As DisconnectionReason, ByVal message As String)
On Error GoTo Err
Config.UpdateStatus
AvgPositions.Reset
Balances.Reset
Bars.Reset
CompleteOrders.Reset
WorkingOrders.Reset
Abbreviated.Reset
Exit Sub
Err:
MsgBox Err.Description
End Sub

Public Function FindNearest(Symbol As String) As Contract
Dim bcName As String
If (Len(Contract) <= 3) Or (client Is Nothing) Then
Set FindNearest = Nothing
Else
bcName = Mid(Contract, 1, Len(Contract) - 3)
Dim bc As BaseContract
Set bc = client.contracts.Base.Get_3(bcName)
If Not bc Is Nothing And bc.contracts.Count > 0 Then
Set FindNearest = bc.contracts.GetAt(0)
Else
Set FindNearest = Nothing
End If
End If
End Function
Anthony Sykes
ETrifonov
Posts: 63
Joined:


Posted: Feb 02, 2022 @ 08:10 PM             Msg. 2 of 4
Hello Anthony,

GF API doesn't provide auto reconnect feature out of the box and should be implemented on client side.
Please see documentation sample here: https://gainfutures.com/GFAPI/?topic=html/da57e5d9-0be9-4b5c-a8c8-5e39e11b5c98.htm
Evgeny
ASykes4318
Posts: 18
Joined:


Posted: Feb 03, 2022 @ 06:15 AM             Msg. 3 of 4
Thanks for your reply...we are using the GF_API_COM and the following are not available:
1) GF.Api.Utils.GFApiEventHandler
2) CancellationTokenSource

Can you help me with useful code snippets that work for "GF_API_COM"??
Anthony Sykes
ETrifonov
Posts: 63
Joined:


Posted: Feb 06, 2022 @ 09:55 PM             Msg. 4 of 4
Hello Anthony.

1) You can simply not use GFApiEventHandler - it is not mandatory there.
2) It's in System.Threading namespace.

Below is some snippet you can try.
Also, you can check our COM API samples here: https://bitbucket.org/GainFuturesDev/comapisamples

Private _client As IGFClient
Private _cts As System.Threading.CancellationTokenSource
Private _connectionContext As ConnectionContext

Public Sub Register(ByVal client As IGFClient)
_client = client
AddHandler _client.Connection.Aggregate.LoginCompleted, Sub(gfClient, args) EndReconnect()

AddHandler _client.Connection.Aggregate.LoginFailed, Sub(gfClient, args)
If args.FailReason GF.Api.Values.Users.FailReason.ConnectionError OrElse _cts?.IsCancellationRequested False Then
EndReconnect()
Else
Reconnect()
End If
End Sub

AddHandler _client.Connection.Aggregate.Disconnected, Sub(gfClient, args)
If args.Reason DisconnectionReason.SocketError OrElse _cts?.IsCancellationRequested = False Then
Return
End If

_cts = New System.Threading.CancellationTokenSource()
Reconnect()
End Sub
End Sub

Public Sub Connect(ByVal context As ConnectionContext)
_connectionContext = context
_client.Connection.Aggregate.Connect(context)
End Sub

Private Sub Reconnect()
If _client.Connection.Aggregate.IsClosed Then
_client.Threading.BeginDelayedInvoke(TimeSpan.FromSeconds(1), Sub() _client.Connection.Aggregate.Connect(_connectionContext), _cts.Token)
End If
End Sub

Private Sub EndReconnect()
_cts?.Cancel()

If _client.Connection.Aggregate.IsConnecting Then
_client.Connection.Aggregate.Disconnect()
End If
End Sub
Evgeny

Edited by ETrifonov on Feb 06, 2022 10:00 PM