Max Lauter Max Lauter - 1 month ago 12
C# Question

C# - Interactive Brokers API - get market data

I am trying to use the basis Interactive Broker API in C# to Forex market data. My goal is to get the bid and ask price of multiple currency pairs.

Here is what I have now. Main:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBApi;

namespace TWS_Data
{
class Program
{
static void Main(string[] args)
{
//IB's main object
EWrapperImpl ibClient = new EWrapperImpl();

//Connect
ibClient.ClientSocket.eConnect("127.0.0.1", 7497, 0);

//Creat and define a contract to fetch data for
Contract contract = new Contract();
contract.Symbol = "EUR";
contract.SecType = "CASH";
contract.Currency = "USD";
contract.Exchange = "IDEALPRO";

// Create a new TagValue List object (for API version 9.71)
List<TagValue> mktDataOptions = new List<TagValue>();


// calling method every X seconds
var timer = new System.Threading.Timer(
e => ibClient.ClientSocket.reqMktData(1, contract, "", true, mktDataOptions),
null,
TimeSpan.Zero,
TimeSpan.FromSeconds(10));

}
}
}


The API function "reqMktData" calls the following two functions:

public virtual void tickSize(int tickerId, int field, int size)
{
Console.WriteLine("Tick Size. Ticker Id:" + tickerId + ", Field: " + field + ", Size: " + size + "\n");
}

public virtual void tickPrice(int tickerId, int field, double price, int canAutoExecute)
{
Console.WriteLine("Tick Price. Ticker Id:" + tickerId + ", Field:" + field + ", Price:" + price + ", CanAutoExecute: " + canAutoExecute + "\n");
string str = Convert.ToString(price);
System.IO.File.WriteAllText(@"C:\Users\XYZ\Documents\price.txt", str);
}


In this code I am trying to save the market data on my hard drive as a dummy test. However, here my problem comes up. During one run the function tickPrice(..) gets called 6 times and deliveres 6 different prices (see the API guide here: https://www.interactivebrokers.com/en/software/api/apiguide/java/tickprice.htm)

What I would need to know now is how it is possible in C# to save these individual results somehow ? (they all get posted into the console, but obviously only the last price is the one that stayes saved in the file).

I am quiet familiar with Matlab coding, but new to the C# syntax. So I am somehow thinking in forms of vectors or for loops to solve this, but it doesn't work out that way.

Thanks for any help or hints

Answer

You can use List to store all values, like:

    List<string> priceList = new List<string>();
    public virtual void tickPrice(int tickerId, int field, double price, int canAutoExecute)
    {
        Console.WriteLine("Tick Price. Ticker Id:" + tickerId + ", Field:" + field + ", Price:" + price + ", CanAutoExecute: " + canAutoExecute + "\n");

        // here you add your prices to list
        priceList.Add(price);

        string str = Convert.ToString(price);
        System.IO.File.WriteAllText(@"C:\Users\XYZ\Documents\price.txt", str);
    }

For getting the last entry from list:

    string lastPrice = priceList[priceList.Count - 1];
Comments