Hello,
I have begin to Program with your fxcor2 api and i have a small Problem to download hystorical data in vb.net.
When i delete in the ontablesupdate the code, then i can download the hystorical data without Problems.
can you help me?
here is my code, the Sub to download Hystorical dat is on the end of this post:
Friend Class ResponseListener
Implements IO2GResponseListener
Private mView As Form1
Private mSession As O2GSession
Private mInstrument As String
Private mInstrument1 As String
Private mRequestID As String
Private mEvent As New Object()
Private mError As String
Private mReader As O2GMarketDataSnapshotResponseReader
Dim mythread As New Threading.Thread(AddressOf onTablesUpdates)
Public Sub New(ByVal session As O2GSession, ByVal instrument As String, ByVal view As Form1)
mView = view
mSession = session
mInstrument = instrument
mInstrument1 = instrument
End Sub
Public Sub setInstrument(ByVal instrument As String)
mInstrument = instrument
End Sub
Public Sub onRequestCompleted(ByVal requestID As String, ByVal response As O2GResponse) Implements IO2GResponseListener.onRequestCompleted
If response.Type = O2GResponseType.GetOffers Then
printOffers(response)
ElseIf response.Type = O2GResponseType.MarketDataSnapshot Then
If response.Type <> fxcore2.O2GResponseType.MarketDataSnapshot Then
mError = "Incorrect response"
Else
Dim factory As O2GResponseReaderFactory = mSession.getResponseReaderFactory()
mReader = factory.createMarketDataSnapshotReader(response)
End If
SyncLock mEvent
Monitor.PulseAll(mEvent)
End SyncLock
End If
End Sub
Public Sub onRequestFailed(ByVal requestID As String, ByVal [error] As String) Implements IO2GResponseListener.onRequestFailed
End Sub
Public Sub onTablesUpdates(ByVal response As O2GResponse) Implements IO2GResponseListener.onTablesUpdates
If response.Type = O2GResponseType.GetOffers OrElse response.Type = O2GResponseType.TablesUpdates Then
printOffers(response)
End If
End Sub
Private Sub printOffers(ByRef response As O2GResponse)
Dim readerFactory As O2GResponseReaderFactory
readerFactory = mSession.getResponseReaderFactory()
Dim mi5 As Integer
If readerFactory IsNot Nothing Then
Dim Reader As O2GOffersTableResponseReader = readerFactory.createOffersTableReader(response)
Dim i As Integer
For i = 0 To Reader.Count - 1
Dim OfferRow As O2GOfferRow = Reader.getRow(i)
mi5 = suchinst(OfferRow.Instrument.ToString)
mView.UpdatePriceInfo(OfferRow.Bid, OfferRow.Ask, mi5)
Next i
End If
End Sub
Private Function suchinst(ByVal inst As String)
Dim xi1, xi2, xi3 As Integer
xi1 = mView.mInstrument1.Length - 1
For xi2 = 0 To xi1
If mView.mInstrument1(xi2) = inst Then
xi3 = xi2
Exit For
Else
xi3 = Nothing
End If
Next
Return xi3
End Function
Public ReadOnly Property Reader() As O2GMarketDataSnapshotResponseReader
Get
Return mReader
End Get
End Property
Public Sub setRequest(ByVal RequestID As String)
mRequestID = RequestID
End Sub
Public Sub [get](ByVal instrument As String, ByVal tf As String, ByVal [from] As Date, ByVal [to] As Date)
Dim tfo As O2GTimeframe
Dim factory As O2GRequestFactory = mSession.getRequestFactory()
Dim timeframes As O2GTimeframeCollection = factory.Timeframes
tfo = timeframes(tf)
If tfo Is Nothing Then
Throw New Exception("ResponseListener: time frame is incorrect")
End If
Dim request As O2GRequest = factory.createMarketDataSnapshotRequestInstrument(instrument, tfo, "300")
factory.fillMarketDataSnapshotRequestTime(request, [from], [to], False)
mError = Nothing
mReader = Nothing
mRequestID = request.RequestID
mSession.subscribeResponse(Me)
Try
mSession.sendRequest(request)
Do
SyncLock mEvent
Monitor.Wait(mEvent, 250)
End SyncLock
If mError Is Nothing AndAlso mReader Is Nothing Then
Continue Do
End If
If mError IsNot Nothing Then
Throw New Exception("ResponseListener:" & mError)
End If
Exit Do
Loop
Catch e As Exception
Console.WriteLine("Exception in getAccounts()." & vbLf & vbTab & " " + e.Message)
Finally
mSession.unsubscribeResponse(Me)
End Try
End Sub
End Class
Friend Class AccountBalance
Private mView As Form1
Dim mRequestID
Dim mSession
Public Sub getAccounts(session As O2GSession, responseListener As ResponseListener)
Try
Dim loginRules As O2GLoginRules = session.getLoginRules()
' Check if Accounts table is loaded automatically
If loginRules IsNot Nothing AndAlso loginRules.isTableLoadedByDefault(O2GTableType.Accounts) Then
' If table is loaded, use getTableRefreshResponse method
Dim accountsResponse As O2GResponse = loginRules.getTableRefreshResponse(O2GTableType.Accounts)
Dim responseFactory As O2GResponseReaderFactory = session.getResponseReaderFactory()
If responseFactory IsNot Nothing Then
Dim accountsReader As O2GAccountsTableResponseReader = responseFactory.createAccountsTableReader(accountsResponse)
For i As Integer = 0 To accountsReader.Count - 1
Dim account As O2GAccountRow = accountsReader.getRow(i)
Form1.UpdateBalanceInfo(account.Balance, account.UsedMargin)
Next
End If
Else
' If table is not loaded, use createRefreshTableRequest method
Dim requestFactory As O2GRequestFactory = session.getRequestFactory()
If requestFactory IsNot Nothing Then
Dim request As O2GRequest = requestFactory.createRefreshTableRequest(O2GTableType.Accounts)
responseListener.setRequest(request.RequestID)
session.sendRequest(request)
Thread.Sleep(1000)
End If
End If
Catch e As Exception
Console.WriteLine("Exception in getAccounts()." & vbLf & vbTab & " " + e.Message)
End Try
End Sub
' Implementation of IO2GResponseListener interface public method onRequestCompleted
Public Sub onRequestCompleted(requestID As [String], response As O2GResponse)
If requestID.Equals(mRequestID) Then
Dim readerFactory As O2GResponseReaderFactory = mSession.getResponseReaderFactory()
If readerFactory IsNot Nothing Then
Dim reader As O2GAccountsTableResponseReader = readerFactory.createAccountsTableReader(response)
For i As Integer = 0 To reader.Count - 1
Dim account As O2GAccountRow = reader.getRow(i)
Form1.UpdateBalanceInfo(account.Balance, account.UsedMargin)
Next
End If
End If
End Sub
' Implementation of IO2GResponseListener interface public method onTablesUpdates
Public Sub onTablesUpdates(response As O2GResponse)
Dim factory As O2GResponseReaderFactory = mSession.getResponseReaderFactory()
If factory IsNot Nothing Then
Dim updatesReader As O2GTablesUpdatesReader = factory.createTablesUpdatesReader(response)
For i As Integer = 0 To updatesReader.Count - 1
Dim tableType As O2GTableType = updatesReader.getUpdateTable(i)
If tableType = O2GTableType.Accounts Then
Dim account As O2GAccountRow = updatesReader.getAccountRow(i)
Form1.UpdateBalanceInfo(account.Balance, account.UsedMargin)
End If
Next
End If
End Sub
End Class
Friend Class marketsnapshot
Private mView As Form1
Dim mresponseListener As ResponseListener
Dim mRequestID
Dim strInstrument As String
Dim strTimeFrame As String
Dim dtFrom As Date
Dim dtTo As Date
' Implementation of IO2GResponseListener interface public method onRequestCompleted
Public Sub test(ByVal mSession As Object)
Dim lv As ListViewItem
Dim x As String
Dim l() As String
strInstrument = "EUR/USD"
strTimeFrame = "m5"
dtFrom = Convert.ToDateTime("4/3/2014")
dtTo = Convert.ToDateTime("4/4/2014")
Dim mresponseListener As New ResponseListener(mSession, strInstrument, mView)
mresponseListener.get(strInstrument, strTimeFrame, dtFrom, dtTo)
Dim reader As O2GMarketDataSnapshotResponseReader = mresponseListener.Reader
For i As Integer = 0 To reader.Count - 1
x = reader.getBidLow(i) & ";" & reader.getBidOpen(i) & ";;" & x
Next i
l = Split(x, ";;")
Form1.UpdateHystoryDataInfo(l)
End Sub
I have begin to Program with your fxcor2 api and i have a small Problem to download hystorical data in vb.net.
When i delete in the ontablesupdate the code, then i can download the hystorical data without Problems.
can you help me?
here is my code, the Sub to download Hystorical dat is on the end of this post:
Friend Class ResponseListener
Implements IO2GResponseListener
Private mView As Form1
Private mSession As O2GSession
Private mInstrument As String
Private mInstrument1 As String
Private mRequestID As String
Private mEvent As New Object()
Private mError As String
Private mReader As O2GMarketDataSnapshotResponseReader
Dim mythread As New Threading.Thread(AddressOf onTablesUpdates)
Public Sub New(ByVal session As O2GSession, ByVal instrument As String, ByVal view As Form1)
mView = view
mSession = session
mInstrument = instrument
mInstrument1 = instrument
End Sub
Public Sub setInstrument(ByVal instrument As String)
mInstrument = instrument
End Sub
Public Sub onRequestCompleted(ByVal requestID As String, ByVal response As O2GResponse) Implements IO2GResponseListener.onRequestCompleted
If response.Type = O2GResponseType.GetOffers Then
printOffers(response)
ElseIf response.Type = O2GResponseType.MarketDataSnapshot Then
If response.Type <> fxcore2.O2GResponseType.MarketDataSnapshot Then
mError = "Incorrect response"
Else
Dim factory As O2GResponseReaderFactory = mSession.getResponseReaderFactory()
mReader = factory.createMarketDataSnapshotReader(response)
End If
SyncLock mEvent
Monitor.PulseAll(mEvent)
End SyncLock
End If
End Sub
Public Sub onRequestFailed(ByVal requestID As String, ByVal [error] As String) Implements IO2GResponseListener.onRequestFailed
End Sub
Public Sub onTablesUpdates(ByVal response As O2GResponse) Implements IO2GResponseListener.onTablesUpdates
If response.Type = O2GResponseType.GetOffers OrElse response.Type = O2GResponseType.TablesUpdates Then
printOffers(response)
End If
End Sub
Private Sub printOffers(ByRef response As O2GResponse)
Dim readerFactory As O2GResponseReaderFactory
readerFactory = mSession.getResponseReaderFactory()
Dim mi5 As Integer
If readerFactory IsNot Nothing Then
Dim Reader As O2GOffersTableResponseReader = readerFactory.createOffersTableReader(response)
Dim i As Integer
For i = 0 To Reader.Count - 1
Dim OfferRow As O2GOfferRow = Reader.getRow(i)
mi5 = suchinst(OfferRow.Instrument.ToString)
mView.UpdatePriceInfo(OfferRow.Bid, OfferRow.Ask, mi5)
Next i
End If
End Sub
Private Function suchinst(ByVal inst As String)
Dim xi1, xi2, xi3 As Integer
xi1 = mView.mInstrument1.Length - 1
For xi2 = 0 To xi1
If mView.mInstrument1(xi2) = inst Then
xi3 = xi2
Exit For
Else
xi3 = Nothing
End If
Next
Return xi3
End Function
Public ReadOnly Property Reader() As O2GMarketDataSnapshotResponseReader
Get
Return mReader
End Get
End Property
Public Sub setRequest(ByVal RequestID As String)
mRequestID = RequestID
End Sub
Public Sub [get](ByVal instrument As String, ByVal tf As String, ByVal [from] As Date, ByVal [to] As Date)
Dim tfo As O2GTimeframe
Dim factory As O2GRequestFactory = mSession.getRequestFactory()
Dim timeframes As O2GTimeframeCollection = factory.Timeframes
tfo = timeframes(tf)
If tfo Is Nothing Then
Throw New Exception("ResponseListener: time frame is incorrect")
End If
Dim request As O2GRequest = factory.createMarketDataSnapshotRequestInstrument(instrument, tfo, "300")
factory.fillMarketDataSnapshotRequestTime(request, [from], [to], False)
mError = Nothing
mReader = Nothing
mRequestID = request.RequestID
mSession.subscribeResponse(Me)
Try
mSession.sendRequest(request)
Do
SyncLock mEvent
Monitor.Wait(mEvent, 250)
End SyncLock
If mError Is Nothing AndAlso mReader Is Nothing Then
Continue Do
End If
If mError IsNot Nothing Then
Throw New Exception("ResponseListener:" & mError)
End If
Exit Do
Loop
Catch e As Exception
Console.WriteLine("Exception in getAccounts()." & vbLf & vbTab & " " + e.Message)
Finally
mSession.unsubscribeResponse(Me)
End Try
End Sub
End Class
Friend Class AccountBalance
Private mView As Form1
Dim mRequestID
Dim mSession
Public Sub getAccounts(session As O2GSession, responseListener As ResponseListener)
Try
Dim loginRules As O2GLoginRules = session.getLoginRules()
' Check if Accounts table is loaded automatically
If loginRules IsNot Nothing AndAlso loginRules.isTableLoadedByDefault(O2GTableType.Accounts) Then
' If table is loaded, use getTableRefreshResponse method
Dim accountsResponse As O2GResponse = loginRules.getTableRefreshResponse(O2GTableType.Accounts)
Dim responseFactory As O2GResponseReaderFactory = session.getResponseReaderFactory()
If responseFactory IsNot Nothing Then
Dim accountsReader As O2GAccountsTableResponseReader = responseFactory.createAccountsTableReader(accountsResponse)
For i As Integer = 0 To accountsReader.Count - 1
Dim account As O2GAccountRow = accountsReader.getRow(i)
Form1.UpdateBalanceInfo(account.Balance, account.UsedMargin)
Next
End If
Else
' If table is not loaded, use createRefreshTableRequest method
Dim requestFactory As O2GRequestFactory = session.getRequestFactory()
If requestFactory IsNot Nothing Then
Dim request As O2GRequest = requestFactory.createRefreshTableRequest(O2GTableType.Accounts)
responseListener.setRequest(request.RequestID)
session.sendRequest(request)
Thread.Sleep(1000)
End If
End If
Catch e As Exception
Console.WriteLine("Exception in getAccounts()." & vbLf & vbTab & " " + e.Message)
End Try
End Sub
' Implementation of IO2GResponseListener interface public method onRequestCompleted
Public Sub onRequestCompleted(requestID As [String], response As O2GResponse)
If requestID.Equals(mRequestID) Then
Dim readerFactory As O2GResponseReaderFactory = mSession.getResponseReaderFactory()
If readerFactory IsNot Nothing Then
Dim reader As O2GAccountsTableResponseReader = readerFactory.createAccountsTableReader(response)
For i As Integer = 0 To reader.Count - 1
Dim account As O2GAccountRow = reader.getRow(i)
Form1.UpdateBalanceInfo(account.Balance, account.UsedMargin)
Next
End If
End If
End Sub
' Implementation of IO2GResponseListener interface public method onTablesUpdates
Public Sub onTablesUpdates(response As O2GResponse)
Dim factory As O2GResponseReaderFactory = mSession.getResponseReaderFactory()
If factory IsNot Nothing Then
Dim updatesReader As O2GTablesUpdatesReader = factory.createTablesUpdatesReader(response)
For i As Integer = 0 To updatesReader.Count - 1
Dim tableType As O2GTableType = updatesReader.getUpdateTable(i)
If tableType = O2GTableType.Accounts Then
Dim account As O2GAccountRow = updatesReader.getAccountRow(i)
Form1.UpdateBalanceInfo(account.Balance, account.UsedMargin)
End If
Next
End If
End Sub
End Class
Friend Class marketsnapshot
Private mView As Form1
Dim mresponseListener As ResponseListener
Dim mRequestID
Dim strInstrument As String
Dim strTimeFrame As String
Dim dtFrom As Date
Dim dtTo As Date
' Implementation of IO2GResponseListener interface public method onRequestCompleted
Public Sub test(ByVal mSession As Object)
Dim lv As ListViewItem
Dim x As String
Dim l() As String
strInstrument = "EUR/USD"
strTimeFrame = "m5"
dtFrom = Convert.ToDateTime("4/3/2014")
dtTo = Convert.ToDateTime("4/4/2014")
Dim mresponseListener As New ResponseListener(mSession, strInstrument, mView)
mresponseListener.get(strInstrument, strTimeFrame, dtFrom, dtTo)
Dim reader As O2GMarketDataSnapshotResponseReader = mresponseListener.Reader
For i As Integer = 0 To reader.Count - 1
x = reader.getBidLow(i) & ";" & reader.getBidOpen(i) & ";;" & x
Next i
l = Split(x, ";;")
Form1.UpdateHystoryDataInfo(l)
End Sub
Aucun commentaire:
Enregistrer un commentaire