Symbol Lookup

GF API Documentation

Code Snippet: Symbol Lookup in continuation-passing style

Symbol Lookup

This class can be helpful in applications that use symbol lookup requests intensively.

Symbol Lookup Response Handler
public class SymbolLookupResponseHandler : IDisposable
{
    private readonly IGFClient _client;
    private readonly Dictionary<SymbolLookupRequestID, Action<IContract>> _responseActions =
        new Dictionary<SymbolLookupRequestID, Action<IContract>>();

    public SymbolLookupResponseHandler(IGFClient client)
    {
        _client = client;
        _client.Connection.Aggregate.Disconnected += GFClient_OnDisconnected;
        _client.Contracts.Lookup.SymbolLookupReceived += GFClient_OnSymbolLookupReceived;
    }

    public void ResolveSymbol(string symbol, Action<IContract> responseAction)
    {
        var lookupRequestID = _client.Contracts.Lookup.BySymbol(symbol);
        if (lookupRequestID != null)
            _responseActions[lookupRequestID] = responseAction;
    }

    private void GFClient_OnSymbolLookupReceived(IGFClient client, GF.Api.Contracts.Lookup.SymbolLookupEventArgs e)
    {
        if (_responseActions.TryGetValue(e.RequestID, out var responseAction))
        {
            _responseActions.Remove(e.RequestID);
            responseAction(e.Contracts.Count == 1 ? e.Contracts.First() : null);
        }
    }

    public void Dispose()
    {
        _responseActions.Clear();
        _client.Connection.Aggregate.Disconnected -= GFClient_OnDisconnected;
        _client.Contracts.Lookup.SymbolLookupReceived -= GFClient_OnSymbolLookupReceived;
    }

    private void GFClient_OnDisconnected(IGFClient client, DisconnectedEventArgs disconnectedEventArgs)
    {
        _responseActions.Clear();
    }
}

Sample of using:

Symbol Lookup Example
internal class Program
{
    private static SymbolLookupResponseHandler _symbolLookupResponseHandler;

    private static void Main(string[] args)
    {
        var client = GFApi.CreateClient();
        var runner = new GFClientRunner(client);
        runner.Start();

        _symbolLookupResponseHandler = new SymbolLookupResponseHandler(client);

        client.Connection.Aggregate.LoginCompleted += GFClient_OnLoginCompleted;

        Console.WriteLine("Connecting...");

        client.Connection.Aggregate.Connect(
            new ConnectionContextBuilder()
                .WithUserName("username")
                .WithPassword("password")
                .WithPort(9210)
                .WithHost("api.gainfutures.com")
                .WithUUID("9e61a8bc-0a31-4542-ad85-33ebab0e4e86")
                .WithForceLogin(true)
                .Build());

        Console.WriteLine("Press any key to exit");
        Console.ReadKey();

        runner.Stop();
    }

    private static void GFClient_OnLoginCompleted(IGFClient client, LoginCompleteEventArgs e)
    {
        _symbolLookupResponseHandler.ResolveSymbol("ESH18", contract =>
        {
            if (contract != null)
            {
                DisplayContract(contract);
            }
            else
            {
                Console.WriteLine("Contract is not found");
            }
        });
    }

    private static void DisplayContract(IContract contract)
    {
        Console.WriteLine($"Contract: {contract.Symbol} - {contract.BaseSymbol}");
        Console.WriteLine($"\tExpiration Date: {contract.ExpirationDate}");
        Console.WriteLine($"\tTick Size: {contract.TickSize}");
        Console.WriteLine($"\tContract Size: {contract.ContractSize:c}");
        Console.WriteLine($"\tHas Options: {contract.HasOptions}");
    }
}