title: C# - Interactive Brokers API - get market data
body: <p>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. </p>
  
  <p>Here is what I have now. Main:</p>
  
  <pre><code>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&lt;TagValue&gt; mktDataOptions = new List&lt;TagValue&gt;();
  
  
              // calling method every X seconds
              var timer = new System.Threading.Timer(
              e =&gt; ibClient.ClientSocket.reqMktData(1, contract, "", true, mktDataOptions),
              null,
              TimeSpan.Zero,
              TimeSpan.FromSeconds(10));
  
           }
      }
  }
  </code></pre>
  
  <p>The API function "reqMktData" calls the following two functions:</p>
  
  <pre><code>        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);
          }
  </code></pre>
  
  <p>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: <a href="https://www.interactivebrokers.com/en/software/api/apiguide/java/tickprice.htm" rel="nofollow">https://www.interactivebrokers.com/en/software/api/apiguide/java/tickprice.htm</a>)</p>
  
  <p><strong>What I would need to know now is</strong> 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). </p>
  
  <p>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. </p>
  
  <p>Thanks for any help or hints</p>