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...
- Download xAPI .NET Wrappers from:
http://developers.xstore.pro/api/wrappers.html, - In this tutorial we will be using Microsoft Visual Studio. If you don't have one installed, you can download a free Express version from:
http://www.microsoft.com/visualstudio/plk/downloads, - To use the wrappers you must have .NET Framework v.4.0 (or higher) installed. You can download it from here:
http://msdn.microsoft.com/netframework/.
Step 1. Create new project
Step 2. Add references in your project
- In your new project tree click References with right mouse button and choose Add Reference...
- In the Reference Manager window click Browse... button
- Find <wrappers folder>/SyncAPIConnector/JSON/Newtonsoft.Json.dll file which is the wrappers and proceed with Add
- Choose Solution > Projects from the menu on the left, select SyncAPIConnect and click OK
Step 3. Set your project as a startup project
Wrappers are ready now so we can create our first application!
Step 4. Write the code
- Expand your project and double click the Program.cs class
- Add necessary imports at the beginning of the file
- Write the following code in the main function (between '{' and '}' brackets)
- Please remember to change your credentials in the first lines of the code above
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;
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();
Step 5. Run the application
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!