FIRST STEPS WITH XAPI, C# AND VISUAL STUDIO

This tutorial shows how to combine xAPI, C# and Visual Studio to create wonderful financial applications. It describes some basic concepts on how to use the .NET wrapper.

Commands used in this tutorial:

  • login,
  • getServerTime,
  • getAllSymbols.

Before you start...

Step 1. Create new project

  1. Extract the downloaded archive and open <wrappers folder>/SyncAPIConnector.sln file
  2. Click Solution SyncAPIConnector with right mouse button and choose Add > New Project... option
  3. Choose Visual C# > Console Application, enter the name for your project and click OK

Step 2. Add references in your project

  1. In your new project tree click References with right mouse button and choose Add Reference...
  2. In the Reference Manager window click Browse... button
  3. Find <wrappers folder>/SyncAPIConnector/JSON/Newtonsoft.Json.dll file which is the wrappers and proceed with Add
  4. Choose Solution > Projects from the menu on the left, select SyncAPIConnect and click OK

Step 3. Set your project as a startup project

  1. Click on your new project with right mouse button and choose Set as StartUp Project option

Wrappers are ready now so we can create our first application!

Step 4. Write the code

  1. Expand your project and double click the Program.cs class
  2. Add necessary imports at the beginning of the file
  3. using xAPI.Codes;
    using xAPI.Commands;
    using xAPI.Errors;
    using xAPI.Records;
    using xAPI.Responses;
    using xAPI.Sync;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
  4. Write the following code in the main function (between '{' and '}' brackets)
  5. Server serverData = Servers.DEMO;
    long userId = 12345;
    string password = "pass";
    string appName = "firstApp";
    // should be left blank
    // to get more info about appId and appName visit http://developers.xstore.pro/api/tutorials/appid_and_appname
    string appId = "";
    try
    {
        Console.WriteLine("Server address: " + serverData.Address + " port: " + serverData.MainPort + " streaming port: " + serverData.StreamingPort);
        // Connect to server
        SyncAPIConnector connector = new SyncAPIConnector(serverData);
        Console.WriteLine("Connected to the server");
        // Login to server
        Credentials credentials = new Credentials(userId, password, appId, appName);
        LoginResponse loginResponse = APICommandFactory.ExecuteLoginCommand(connector, credentials, true);
        Console.WriteLine("Logged in as: " + userId);
        // Execute GetServerTime command
        ServerTimeResponse serverTimeResponse = APICommandFactory.ExecuteServerTimeCommand(connector, true);
        Console.WriteLine("Server time: " + serverTimeResponse.TimeString);
        // Execute GetAllSymbols command
        AllSymbolsResponse allSymbolsResponse = APICommandFactory.ExecuteAllSymbolsCommand(connector, true);
        Console.WriteLine("All symbols count: " + allSymbolsResponse.SymbolRecords.Count);
        // Print first 5 symbols
        Console.WriteLine("First five symbols:");
        foreach (SymbolRecord symbolRecord in allSymbolsResponse.SymbolRecords.Take(5))
        {
            Console.WriteLine(" > " + symbolRecord.Symbol + " ask: " + symbolRecord.Ask + " bid: " + symbolRecord.Bid);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("An exception occured: " + ex.ToString());
    }
    Console.Read();
  6. Please remember to change your credentials in the first lines of the code above

Step 5. Run the application

  1. Locate and click the green triangle icon on the top toolbar
  2. As a result you should see your application running like on the screen below

Summary

In this tutorial you learned how to prepare Visual Studio to work with xAPI and .NET wrappers. You have also created a very simple application using C# that:

  • connects to the xAPI,
  • logins to the server,
  • gets server time,
  • gets list of all available symbols,
  • lists first five of them.

Now you are ready to create your own app!