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...

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

  1. Create instance of SyncAPIConnector class with given server (demo in this case):
  2. SyncAPIConnector connector = new SyncAPIConnector(Servers.DEMO);
  3. Login to the server using your own credentials:
  4. Credentials credentials = new Credentials(123456, "yourpassword");
    APICommandFactory.ExecuteLoginCommand(connector, credentials);
  5. Connect to streaming:
  6. 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.

  1. Create callback function for ticks:
  2. void Streaming_TickRecordReceived(StreamingTickRecord tickRecord)
    {
        Console.WriteLine("Got tick: " + tickRecord.ToString());
        // do something with tick record here
    }
  3. Add freshly created callback for TickRecordReceived event (in the main function):
  4. connector.Streaming.TickRecordReceived += Streaming_TickRecordReceived;
  5. Subscribe for ticks (in the main function):
  6. connector.Streaming.SubscribePrice("EURUSD");
  7. Repeat the procedure for trades:
  8. 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();
  9. ...and balance:
  10. 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.