GETTING CURRENT SYMBOL PRICE USING STREAMING

In this tutorial you will learn how to obtain current price for a chosen symbol in real time. The code will be written in C#, but it is not complicated and could be easily rewritten to any other language.

The whole code used in this tutorial is available here:

DOWNLOAD CODE

Commands used in this tutorial:

  • getSymbol,
  • getTickPrices(streaming)

Before you start...

  • To complete this tutorial, you need C# wrapper (available here: developers.xstore.pro/api/wrappers) or be able to use all commands listed above by yourself,
  • We assume that you have at least basic knowledge of xAPI and you are able to establish a connection and login to the server.

Step 1. Init streaming

  1. Create new connection and log in to the chosen server using your credentials.
  2. Establish a streaming connection.
  3. Subscribe to tick prices for chosen symbol, in our case it will be EURUSD.
  4. // Connect to server
    SyncAPIConnector connector = new SyncAPIConnector(serverData);
    Console.WriteLine("Connected to the server");
    // Login to server
    Credentials credentials = new Credentials(userId, password, "", "YOUR APP NAME");
    LoginResponse loginResponse = APICommandFactory.ExecuteLoginCommand(connector, credentials, true);
    connector.Streaming.Connect();
    connector.Streaming.SubscribePrice("EURUSD");

Step 2. Create callback function

  1. Create a callback function with single parameter of type StreamingTickRecord.
  2. static void Streaming_TickRecordReceived(StreamingTickRecord tickRecord)
    {
        if (tickRecord.Level == 0)
            Console.WriteLine("Current price: " + tickRecord.Ask);
    }

    IMPORTANT! If you want to display a single price for the user you should use only the tick records with the level value equal to 0. That is why the line if (tickRecord.Level == 0) should not be omitted.

  3. Subscribe to the TickRecordReceived event using the callback function.
  4. connector.Streaming.TickRecordReceived += Streaming_TickRecordReceived;

Step 3. Get initial symbol price

  1. Use GetSymbol command to get initial symbol price.
// Get current price
SymbolResponse symbolResponse = APICommandFactory.ExecuteSymbolCommand(connector, "EURUSD");
Console.WriteLine("Initial symbol price: " + symbolResponse.Symbol.Ask);

Step 4. Run the code!

  1. Run the program. After a while you should see similar output:

Summary

In this tutorial you have learned how to obtain current prices for chosen symbol in real time. Check out our other tutorials that will help you build your own application!