public async Task SubscribeChildOrdersAsync(int instrumentId, Func <int, CollectionReceivedEventArgs <BitFlyerChildOrder>, bool> callback, DateTime?deadline = null, CancellationToken cancellationToken = default)
        {
            using (await _Client.OpenAsync().ConfigureAwait(false))
                using (var res = new OrdersService.OrdersServiceClient(_Client.Channel).SubscribeChildOrders(
                           new Models.Remoting.InstrumentIdRequest()
                {
                    InstrumentId = instrumentId
                },
                           deadline: deadline,
                           cancellationToken: cancellationToken))
                {
                    while (await res.ResponseStream.MoveNext(cancellationToken).ConfigureAwait(false) &&
                           !cancellationToken.IsCancellationRequested)
                    {
                        var c = res.ResponseStream.Current;
                        var m = new CollectionReceivedEventArgs <BitFlyerChildOrder>(
                            c.Action,
                            c.Orders.Select(e => new BitFlyerChildOrder(instrumentId, e)).Where(e => e.IsValid));

                        if (callback?.Invoke(instrumentId, m) != true)
                        {
                            return;
                        }
                    }
                }
        }
 internal void RaiseExecutionsReceived(CollectionReceivedEventArgs <BitFlyerExecution> e)
 {
     if (e.Action == NotifyCollectionChangedAction.Reset || e?.Data?.Count > 0)
     {
         ExecutionsReceived?.Invoke(this, e);
     }
 }
 internal void RaiseChildOrdersReceived(CollectionReceivedEventArgs <BitFlyerChildOrder> e)
 {
     if (e.Action == NotifyCollectionChangedAction.Reset || e?.Data?.Count > 0)
     {
         ChildOrdersReceived?.Invoke(this, e);
     }
 }
 internal void RaiseTradesReceived(CollectionReceivedEventArgs <BitMexTrade> e)
 {
     if (e.Action == NotifyCollectionChangedAction.Reset || e?.Data?.Count > 0)
     {
         TradesReceived?.Invoke(this, e);
     }
 }
        private void Client_PositionsReceived(object sender, CollectionReceivedEventArgs <Position> e)
        {
            var i = Instruments.FirstOrDefault(m => m.Id == e.Data[0].InstrumentId);

            if (i != null)
            {
                i.LastError = null;

                lock (Positions)
                {
                    foreach (var m in e.Data)
                    {
                        Positions.Add(new PositionEntry(e.Action, i, m));
                    }

                    const int MAX = 100;
                    while (Positions.Count > 100)
                    {
                        Positions.RemoveAt(Positions.Count - 1 - MAX);
                    }
                }

                lock (ActivePositions)
                {
                    // 対象のInstrumentのポジションのみを対象に副作用を与える
                    var activePositions = ActivePositions.Where(p => p.Instrument.Id.Equals(i.Id)).ToList();
                    if (e.Action == NotifyCollectionChangedAction.Add ||
                        e.Action == NotifyCollectionChangedAction.Replace)
                    {
                        // Add,Replace が来たらスナップショット受信とみなして入れ替える
                        foreach (var ap in activePositions)
                        {
                            ActivePositions.Remove(ap);
                        }
                        foreach (var m in e.Data)
                        {
                            ActivePositions.Add(new PositionEntry(e.Action, i, m));
                        }
                    }
                    else if (e.Action == NotifyCollectionChangedAction.Remove ||
                             e.Action == NotifyCollectionChangedAction.Reset)
                    {
                        // Remove,Reset が来たらポジションが全て決済されたとみなして削除する
                        foreach (var ap in activePositions)
                        {
                            ActivePositions.Remove(ap);
                        }
                    }
                    else if (e.Action == NotifyCollectionChangedAction.Move)
                    {
                    }
                }
            }
        }
Beispiel #6
0
        private void Client_ExecutionsReceived(object sender, CollectionReceivedEventArgs <Execution> e)
        {
            var i = Instruments.FirstOrDefault(m => m.Id == e.Data[0].InstrumentId);

            if (i != null)
            {
                i.LastError = null;

                lock (Executions)
                {
                    foreach (var m in e.Data)
                    {
                        Executions.Add(new ExecutionEntry(e.Action, i, m));
                    }

                    const int MAX = 100;
                    while (Executions.Count > 100)
                    {
                        Executions.RemoveAt(Executions.Count - 1 - MAX);
                    }
                }
            }
        }
        private void BitFlyer_ChildOrdersReceived(object sender, CollectionReceivedEventArgs <BitFlyerChildOrder> e)
        {
            var i = Instruments.FirstOrDefault(m => m.Id == e.Data[0].InstrumentId);

            if (i != null)
            {
                i.LastError = null;

                lock (ChildOrders)
                {
                    foreach (var m in e.Data)
                    {
                        ChildOrders.Add(new ChildOrderEntry(e.Action, i, m));
                    }

                    const int MAX = 100;
                    while (ChildOrders.Count > 100)
                    {
                        ChildOrders.RemoveAt(ChildOrders.Count - 1 - MAX);
                    }
                }
            }
        }
 protected override void Notify(int key, CollectionReceivedEventArgs <BitFlyerExecution> response)
 => Client.BitFlyer.RaiseExecutionsReceived(response);
 protected override void Notify(int key, CollectionReceivedEventArgs <BitFlyerChildOrder> response)
 => Client.BitFlyer.RaiseChildOrdersReceived(response);
Beispiel #10
0
 protected override void Notify(int key, CollectionReceivedEventArgs <BitMexTrade> response)
 => Client.BitMex.RaiseTradesReceived(response);