Пример #1
0
        public async Task Subscribe()
        {
            try
            {
                if (_igStreamApiClient != null)
                {
                    if (_tradeSubscriptionStk == null)
                    {
                        _tradeSubscriptionStk = _igStreamApiClient.SubscribeToPositions(this);
                        Log.Instance.WriteEntry("TradeSubscription : Subscribe");
                    }
                    if (Config.ReplayEnabled)
                    {
                        await Task.Delay(0);

                        return;
                    }
                    var response = await _igRestApiClient.getOTCOpenPositionsV2();

                    foreach (var pos in response.Response.positions)
                    {
                        var ptfPos = GetPosition(pos.market.epic);
                        if (ptfPos != null)
                        {
                            var trade = new Trade(DateTime.Parse(pos.market.updateTime), pos.market.epic,
                                                  pos.position.direction == "BUY" ? SIGNAL_CODE.BUY : SIGNAL_CODE.SELL, (int)pos.position.size.Value,
                                                  pos.position.direction == "BUY" ? pos.market.offer.Value : pos.market.bid.Value);
                            trade.Id               = pos.position.dealId;
                            trade.Reference        = "RECOVER_" + pos.position.dealId;
                            trade.ConfirmationTime = trade.TradingTime;
                            ReplayPositionUpdateInfo updateInfo = new ReplayPositionUpdateInfo(DateTime.Parse(pos.market.updateTime), pos.market.epic,
                                                                                               pos.position.dealId, trade.Reference, "OPEN", "ACCEPTED", (int)pos.position.size.Value,
                                                                                               pos.position.direction == "BUY" ? pos.market.offer.Value : pos.market.bid.Value,
                                                                                               pos.position.direction == "BUY" ? SIGNAL_CODE.BUY : SIGNAL_CODE.SELL);
                            _trades.Add(trade);
                            ptfPos.AddIncomingTrade(trade);
                            ptfPos.OnUpdate(updateInfo);
                        }
                        else
                        {
                            Log.Instance.WriteEntry("Trade subscription error: mktdata: " + pos.market.epic + ", direction: " + pos.position.direction + ", size: " + pos.position.size.Value +
                                                    ", dealid:" + pos.position.dealId, System.Diagnostics.EventLogEntryType.Error);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Instance.WriteEntry("Portfolio subscription error: " + ex.Message, System.Diagnostics.EventLogEntryType.Error);
                if (Portfolio.Instance.ShutDownFunc != null)
                {
                    Log.Instance.WriteEntry("Terminating...", System.Diagnostics.EventLogEntryType.Error);
                    Portfolio.Instance.ShutDownFunc();
                }
            }
        }