コード例 #1
0
ファイル: SimplestExample.cs プロジェクト: dshe/InterReact
    public async Task Test()
    {
        // Create the InterReact client by first connecting to TWS/Gateway on the local host.
        IInterReactClient interReact = await new InterReactClientConnector().ConnectAsync();

        // Create a contract object.
        Contract contract = new()
        {
            SecurityType = SecurityType.Stock,
            Symbol       = "SPY",
            Currency     = "USD",
            Exchange     = "SMART"
        };

        // Create and then subscribe to the observable which can observe ticks for the contract.
        IDisposable subscription = interReact
                                   .Services
                                   .CreateTickObservable(contract)
                                   .OfTickClass(selector => selector.PriceTick)
                                   .Subscribe(onNext: priceTick => Console.WriteLine($"Price = {priceTick.Price}"));

        Console.WriteLine(Environment.NewLine + "press a key to exit...");
        Console.ReadKey();
        Console.Clear();

        // Dispose the subscription to stop receiving ticks.
        subscription.Dispose();

        // Disconnect from TWS/Gateway.
        await interReact.DisposeAsync();
    }
}
コード例 #2
0
ファイル: Client.cs プロジェクト: dshe/InterReact
    internal static async Task Run(int port, ILogger logger, ILogger libLogger)
    {
        IInterReactClient client = await new InterReactClientConnector()
                                   .WithLogger(libLogger)
                                   .WithPorts(port)
                                   .ConnectAsync()
                                   .ConfigureAwait(false);

        logger.LogCritical("Connected to server.");

        logger.LogCritical("Sending some messages...");
        Enumerable.Range(0, 50).ToList().ForEach(client.Request.CancelMarketData);

        // indicate test end
        client.Request.RequestMarketData(42, new Contract());

        // wait to get the first tickSize message, indicating test start
        await client.Response.OfType <SizeTick>().FirstAsync();

        logger.LogCritical("Receiving...");

        // receive some messages to measure throughput
        Stopwatch watch = new();

        watch.Start();
        int count = await client.Response.TakeWhile(m => m is SizeTick).Count();

        watch.Stop();

        long frequency = Stopwatch.Frequency * count / watch.ElapsedTicks;

        logger.LogCritical("Received!! {Frequency:N0} messages/second.", frequency);

        logger.LogCritical("Disconnecting.");
        await client.DisposeAsync();

        logger.LogCritical("Disconnected.");
    }