Snippets

GainFuturesDev Paginated Options Lookup

Created by Vitaliy Pfau last modified
using System;
using System.Collections.Generic;
using System.Text;
using OEC.API;

namespace GF.OptionsLookup
{
    class Program
    {
        private static int _request = -1;
        private static Contract _parentContract;

        private const string UUID = "9e61a8bc-0a31-4542-ad85-33ebab0e4e86";
        private const string Symbol = "ESU8";

        static void Main(string[] args)
        {
            Console.WriteLine("Press Ctrl-C to exit");

            var api = new OECClient { UUID = UUID };
            api.Connect("api.gainfutures.com", 9200, "vic", "vic", true);

            Console.Write("Connecting...");

            api.OnLoginComplete += () =>
            {
                Console.WriteLine("Connected");
                Console.WriteLine($"Request symbol {Symbol}");
                Console.WriteLine();

                _request = api.SymbolLookup(Symbol).ID; //Simple contract request
            };

            api.OnLoginFailed += reason => Console.WriteLine($"Login failed: {reason}");

            api.OnSymbolLookupReceived += (criteria, contracts) =>
            {
                if (criteria.ID == _request)
                {
                    _request = -1;

                    Console.WriteLine($"First lookup done. Count: {contracts.Count}; ");
                    if (contracts.Count < 1)
                    {
                        Console.WriteLine($"Contract {Symbol} is not available");
                        return;
                    }

                    _parentContract = contracts.First;

                    //parent contract contains options but they are not available on a client yet
                    Console.WriteLine($"{_parentContract}: HasOptions({_parentContract.HasOptions}), Options Count({_parentContract.Options.Count})");

                    //Second lookup for options
                    api.SymbolLookup(new SymbolLookupCriteria
                    {
                        SearchText = "*",
                        ParentContract = _parentContract,
                        MinStrikePrice = 0,
                        MaxStrikePrice = 500
                    });
                    Console.WriteLine();
                }
                else
                {
                    //contracts contains the lookup result; 
                    //at this moment you can get the options from _parentContract.Options

                    Console.WriteLine($"Second lookup done. Count: {contracts.Count}; ");
                    Console.WriteLine($"{_parentContract}: HasOptions({_parentContract.HasOptions}), Options Count({_parentContract.Options.Count})");
                    Console.WriteLine();
                    Console.WriteLine("Options: ");

                    foreach (var contract in contracts /*ParentContract.Options*/)
                        Console.WriteLine(contract);

                    if (criteria.MaxStrikePrice < 10000)
                    {
                        criteria.MinStrikePrice += 500;
                        criteria.MaxStrikePrice += 500;
                        api.SymbolLookup(criteria);
                    }
                }
            };

            System.Windows.Forms.Application.Run();
        }
    }
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.