STREAMING IN .NET WRAPPER
In this short tutorial you will learn how to asynchronously receive data using streaming in .NET wrappers.
Commands used in this tutorial:
- login,
- getTickPrices,
- getTrades,
- getBalance,
- stopBalance.
Before you start...
- You may complete First steps with xAPI, C# and Visual Studio tutorial, which describes some basic acitons you should perform to start working with the wrapper,
- The wrapper itself, available here: download (in this tutorial we will use 2.3 version).
Step 1. Create new application
Create new C# application (the whole process is decribed in First steps with xAPI, C# and Visual Studio)
Step 2. Connect with xAPI server
- Create instance of SyncAPIConnector class with given server (demo in this case):
- Login to the server using your own credentials:
- Connect to streaming:
SyncAPIConnector connector = new SyncAPIConnector(Servers.DEMO);
Credentials credentials = new Credentials(123456, "yourpassword"); APICommandFactory.ExecuteLoginCommand(connector, credentials);
connector.Streaming.Connect();
Step 3. Create callback functions
After each streaming record arrival an appropriate event occurs. You can subscribe for the event you want with your own callback function.
All events generated by the streaming connector are listed below:
- TickRecordReceived,
- TradeRecordReceived,
- TradeStatusRecordReceived,
- BalanceRecordReceived,
- ProfitRecordReceived.
In this tutorial we will subscribe for ticks, trades and balance.
- Create callback function for ticks:
- Add freshly created callback for TickRecordReceived event (in the main function):
- Subscribe for ticks (in the main function):
- Repeat the procedure for trades:
- ...and balance:
void Streaming_TickRecordReceived(StreamingTickRecord tickRecord) { Console.WriteLine("Got tick: " + tickRecord.ToString()); // do something with tick record here }
connector.Streaming.TickRecordReceived += Streaming_TickRecordReceived;
connector.Streaming.SubscribePrice("EURUSD");
void Streaming_TradeRecordReceived(StreamingTradeRecord tradeRecord) { Console.WriteLine("Got trade: " + tradeRecord.ToString()); // do something with trade record here }
connector.Streaming.TradeRecordReceived += Streaming_TradeRecordReceived;
connector.Streaming.SubscribeTrades();
void Streaming_BalanceRecordReceived(StreamingBalanceRecord balanceRecord) { Console.WriteLine("Got balance: " + balanceRecord.ToString()); // do something with balance record here }
connector.Streaming.BalanceRecordReceived += Streaming_BalanceRecordReceived;
connector.Streaming.SubscribeBalance();
Now your streaming is ready. You should receive records in callback functions.
Step 4. Stop streaming
To unsubscribe only single records (like ticks or trads) call unsubscribe method, like:
connector.Streaming.UnsubscribeBalance();
To stop all streaming values coming in call Disconnect() method:
connector.Streaming.Disconnect();
Remember to call Connect() if you want to use streaming again.