Systematic Trading research and development, with a flavour of Trend Following
Au.Tra.Sy blog – Automated trading System header image 2

Unfair Advantage API Code (C#): Extract Futures Continuous data

October 4th, 2009 · 2 Comments · Backtest, Code, Data, Futures, Software

As mentioned in the previous post on retrieving Back-Adjusted Contracts using the RetrieveBackAdjustedContract2 function of the Unfair Advantage API, I have coded up this very simple project to read a list of Futures underlying instruments, retrieve a proportionally back-adjusted contract for each of the instruments and oputput it to a file.

Getting started with the API

Fire up your favourite IDE/language that supports COM Interoperability (I am using Visual C# 2008 Express Edition), start Console Application project and add a reference to the Unfair Advantage API. For this you just need to “Add reference” and browse to the Unfair Advantage installation folder and select the main EXE (uad.exe). This gives access to the CSI UA API.

Add UA into your project and "Bob's your Uncle!"

Add UA into your project and "Bob's your Uncle!"

Simple extraction code

The rest is fairly simple as long as you understand the RetrieveBackAdjustedContract2 parameters.
The code below takes 2 parameters: an input file name containing the list of markets to extract and a folder name to extract each continuous contract to. It sets specific UA session parameters and calls the RetrieveBackAdjustedContract2 with specific variables to obtain the concatenation desired. Each continuous contract is output to a folder in a .txt file.
The file containing the list of markets is of the following format (CSI number, Symbol, Market name):


The code

Please find the code there (very nicely formatted thanks to this great tool by manoli). You can also download the Program.cs file:

Note: You can receive a 10% discount on CSI data subscriptions (use coupon code LIBERTY)

Related Posts with Thumbnails

Tags: ······

2 Comments so far ↓

  • Matthew Brand

    Hi Jez,

    I am trying to access the CSI UA data using C# and the guidance from the CSI website and your web pages but cannot get any data out. Do you have any tips as to how to make the code below work?

    Is there a forum for CSIData programmers? I could not find one yet.

    Also, how do you get the program to compile using the API2Class? When I try to do it I get the error “Interop type ‘UA.API2Class’ cannot be embedded. use the applicable interface instead.”

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

    namespace CSIData
    class Program
    static void Main(string[] args)
    // The examples say to use API2Class but it generates the compile error:
    // “Interop type ‘UA.API2Class’ cannot be embedded. use the applicable interface instead.”
    //API2Class x = new API2Class();

    // So let’s try API2 object
    // When this line executes the CSIUNfairAdvantage GUI program launches
    //(just as though I had clicked “Launch UA” on the start menu)
    API2 x = new API2();

    // So we set the symbol and IsStock as per the instructions
    x.MarketSymbol = “C2”;
    x.IsStock = 0;

    // and look up the market number for the input symbol … and that works:
    int i = x.FindMarketNumber();
    Console.WriteLine(“x.FindMarketNumber() returns ” + i);

    // Now set the marketnumber to the looked up value and get the market profile:
    x.MarketNumber = i;
    var errNo = x.GetMarketProfile();
    Console.WriteLine(“x.GetMarketProfile() returns errNo ” + errNo);

    // There was no error (errNo is 0)
    // So let’s try to get the price data for 2007 December Corn
    int monthYear = 200712;
    int nPoints = x.RetrieveContract(i, monthYear, -1, -1);
    Console.WriteLine(“x.RetrieveContract(i, 200712, -1, -1) returns ” + nPoints + ” data points”);

    object IntResultArray;
    object FltResultArray;
    int count = x.CopyRetrievedDataToArray2(0, out IntResultArray, out FltResultArray);

    // and that is where it goes wrong:
    string MarketName = x.MarketName;
    if (MarketName == null)
    Console.WriteLine(“marketName is null, program has failed!”);

    x.HoldUAOpenOnClose = 0;
    x = null;

  • Jez Liberty

    Your best bet is probably to get in touch with CSI support directly.
    I think CSI might have changed the API access compared to when I signed up so you might have to get them to activate something on their end (whereas for me it worked fine from the start/sign-up)

    In any case I tried my old code (I have not used the CSI API in a while) and it seems to work fine as it used to. Maybe my UA install is different from yours too. Did you add the reference to uad.exe to your project?

Leave a Comment