Snippets

GainFuturesDev GFApi LoadTicks with 3x1000

Created by Vitaliy Pfau
using System;
using System.Linq;
using GF.Api.Contracts;
using GF.Api.Margin.Calculation;
using GF.Api.Subscriptions;

namespace GFApi_LoadTicks
{
    internal class Program
    {
        private const string Symbol = "ESU24";
        private const int PortionAmount = 1000;

        private static int _iterations = 3;
        private static DateTime _earliestDate = new DateTime(2024, 07, 01, 13, 30, 00, DateTimeKind.Utc); //Change this for starting load point

        private const string UserName = "user_name";
        private const string Password = "password";

        public static void Main(string[] args)
        {
            var client = GF.Api.Impl.GFApi.CreateClient();

            var runner = new GF.Api.Threading.GFClientRunner(client);
            runner.Start();

            client.Connection.Aggregate.LoginCompleted += (gfClient, eventArgs) =>
            {
                Console.WriteLine("Login Completed");
                gfClient.Contracts.Lookup.BySymbol(Symbol);
            };
            client.ServerErrors.Error += (gfClient, eventArgs) =>
            {
                Console.WriteLine($"TraderApi error occurred: {eventArgs.Message}");
            };

            client.Contracts.Lookup.SymbolLookupReceived += (gfClient, eventArgs) =>
            {
                Console.WriteLine($"Requesting {PortionAmount} ticks of {eventArgs.Contracts.First().Symbol} till now");
                gfClient.Subscriptions.Ticks.Subscribe(
                    eventArgs.Contracts.First().ID,
                    //If we don't specify end date, we will create a continuous subscription (after initial data load it will send updates with new ticks)
                    gfClient.Subscriptions.Ticks.Duration.Create(_earliestDate, PortionAmount));
            };

            client.Subscriptions.Ticks.TicksReceived += (gfClient, eventArgs) =>
            {
                Console.WriteLine($"Received {eventArgs.Ticks.Count} {eventArgs.Subscription.Contract.Symbol} ticks on {eventArgs.Subscription.ID} subscription");

                if (eventArgs.Ticks.Count > 0 && eventArgs.Ticks[0].Timestamp < _earliestDate) //Track the earliest date for next request
                    _earliestDate = eventArgs.Ticks[0].Timestamp;

                var isEndOfTicketsFlowForOneRequest = eventArgs.Ticks.Count <= 0;
                if (!isEndOfTicketsFlowForOneRequest) return; //We will have more data; 0 indicates the end of flow

                _iterations--;
                if (_iterations <= 0) return; //No need to load more

                Console.WriteLine($"Requesting {PortionAmount} ticks of {eventArgs.Subscription.Contract.Symbol} till {_earliestDate}");

                client.Subscriptions.Ticks.Subscribe(
                    eventArgs.Subscription.Contract.ID,
                    client.Subscriptions.Ticks.Duration.Create(_earliestDate, PortionAmount));
            };

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

            client.Connection.Aggregate.Connect(
                new GF.Api.Connection.ConnectionContextBuilder()
                    .WithUserName(UserName)
                    .WithPassword(Password)
                    .WithPort(9210)
                    .WithHost("api.gainfutures.com")
                    .WithUUID("UUID")
                    .WithForceLogin(true)
                    .Build());

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

            runner.Stop();
        }
    }
}

Comments (0)

HTTPS SSH

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