Exemplo n.º 1
0
        private void ProcessPortfolioLookup(PortfolioLookupMessage msg)
        {
            if (msg == null)
            {
                throw new ArgumentNullException(nameof(msg));
            }

            if (!msg.IsSubscribe || (msg.Adapter != null && msg.Adapter != this))
            {
                base.OnSendInMessage(msg);
                return;
            }

            foreach (var portfolio in _positionStorage.Portfolios.Filter(msg))
            {
                RaiseNewOutMessage(portfolio.ToMessage(msg.TransactionId));
                RaiseNewOutMessage(portfolio.ToChangeMessage());
            }

            foreach (var position in _positionStorage.Positions.Filter(msg))
            {
                RaiseNewOutMessage(position.ToChangeMessage(msg.TransactionId));
            }

            base.OnSendInMessage(msg);
        }
Exemplo n.º 2
0
        private bool ProcessPortfolioLookup(PortfolioLookupMessage msg)
        {
            if (msg == null)
            {
                throw new ArgumentNullException(nameof(msg));
            }

            if (!msg.IsSubscribe || (msg.Adapter != null && msg.Adapter != this))
            {
                return(base.OnSendInMessage(msg));
            }

            var now     = CurrentTime;
            var transId = msg.TransactionId;

            foreach (var portfolio in _positionStorage.Portfolios.Filter(msg))
            {
                RaiseNewOutMessage(portfolio.ToMessage(transId).SetSubscriptionIds(subscriptionId: transId));

                var changeMsg = portfolio.ToChangeMessage().SetSubscriptionIds(subscriptionId: transId);
                changeMsg.ServerTime = now;
                RaiseNewOutMessage(changeMsg);
            }

            foreach (var position in _positionStorage.Positions.Filter(msg))
            {
                var changeMsg = position.ToChangeMessage(transId).SetSubscriptionIds(subscriptionId: transId);
                changeMsg.ServerTime = now;
                RaiseNewOutMessage(changeMsg);
            }

            return(base.OnSendInMessage(msg));
        }
        private void ProcessPortfolioLookup(PortfolioLookupMessage message)
        {
            if (message != null)
            {
                if (!message.IsSubscribe)
                {
                    return;
                }
            }

            var transactionId = message?.TransactionId ?? 0;

            var pfName = PortfolioName;

            SendOutMessage(new PortfolioMessage
            {
                PortfolioName         = pfName,
                BoardCode             = Extensions.BitStampBoard,
                OriginalTransactionId = transactionId,
            });

            if (message != null)
            {
                SendSubscriptionResult(message);
            }

            var tuple = _httpClient.GetBalances();

            foreach (var pair in tuple.Item1)
            {
                var currValue  = pair.Value.First;
                var currPrice  = pair.Value.Second;
                var blockValue = pair.Value.Third;

                if (currValue == null && currPrice == null && blockValue == null)
                {
                    continue;
                }

                var msg = this.CreatePositionChangeMessage(pfName, pair.Key.ToUpperInvariant().ToStockSharp(false));

                msg.TryAdd(PositionChangeTypes.CurrentValue, currValue, true);
                msg.TryAdd(PositionChangeTypes.CurrentPrice, currPrice, true);
                msg.TryAdd(PositionChangeTypes.BlockedValue, blockValue, true);

                SendOutMessage(msg);
            }

            foreach (var pair in tuple.Item2)
            {
                SendOutMessage(new Level1ChangeMessage
                {
                    SecurityId = pair.Key.ToStockSharp(),
                    ServerTime = CurrentTime.ConvertToUtc()
                }.TryAdd(Level1Fields.CommissionTaker, pair.Value));
            }

            _lastTimeBalanceCheck = CurrentTime;
        }
Exemplo n.º 4
0
        /// <inheritdoc />
        public void UnSubscribePositions()
        {
            var msg = new PortfolioLookupMessage
            {
                IsSubscribe   = false,
                TransactionId = TransactionIdGenerator.GetNextId(),
            };

            this.AddInfoLog(nameof(UnSubscribePositions));
            SendInMessage(msg);
        }
Exemplo n.º 5
0
 private void ProcessPortolioLookupMessage(PortfolioLookupMessage pfMsg)
 {
     if (_lookupPortfoliosId == 0)
     {
         _lookupPortfoliosId = pfMsg.TransactionId;
         _wrapper.LookupPortfolios();
     }
     else
     {
         SendOutError(LocalizedStrings.Str1868);
     }
 }
        private bool ProcessPortfolioLookup(PortfolioLookupMessage msg)
        {
            if (msg == null)
            {
                throw new ArgumentNullException(nameof(msg));
            }

            if (!msg.IsSubscribe || (msg.Adapter != null && msg.Adapter != this))
            {
                return(base.OnSendInMessage(msg));
            }

            var now     = CurrentTime;
            var transId = msg.TransactionId;

            void SendOut <TMessage>(TMessage outMsg)
                where TMessage : Message, ISubscriptionIdMessage
            {
                outMsg.SetSubscriptionIds(subscriptionId: transId);

                if (outMsg is IServerTimeMessage timeMsg)
                {
                    timeMsg.ServerTime = now;
                }

                outMsg.OfflineMode = MessageOfflineModes.Ignore;
                RaiseNewOutMessage(outMsg);
            }

            if (msg.StrategyId.IsEmpty())
            {
                foreach (var portfolio in _positionStorage.Portfolios.Filter(msg))
                {
                    SendOut(portfolio.ToMessage(transId));
                    SendOut(portfolio.ToChangeMessage());
                }
            }

            foreach (var position in _positionStorage.Positions)
            {
                var posMsg = position.ToChangeMessage(transId);

                if (!posMsg.IsMatch(msg, false))
                {
                    continue;
                }

                SendOut(posMsg);
            }

            return(base.OnSendInMessage(msg));
        }
Exemplo n.º 7
0
        /// <inheritdoc />
        public void SubscribePositions(Security security = null, DateTimeOffset?from = null, DateTimeOffset?to = null, long?count = null, IMessageAdapter adapter = null)
        {
            var msg = new PortfolioLookupMessage
            {
                IsSubscribe   = true,
                TransactionId = TransactionIdGenerator.GetNextId(),
                Adapter       = adapter,
            };

            _portfolioLookups.Add(msg.TransactionId, new LookupInfo <PortfolioLookupMessage, Portfolio>(msg));

            this.AddInfoLog(nameof(SubscribePositions));
            SendInMessage(msg);
        }
        private void ProcessPortfolioLookupMessage(PortfolioLookupMessage message)
        {
            var portfolios = _client.GetPortfolios();

            foreach (var portfolio in portfolios)
            {
                SendOutMessage(new PortfolioMessage
                {
                    PortfolioName         = portfolio.bstrAcct,
                    State                 = PortfolioStates.Active,     // ???
                    OriginalTransactionId = message.TransactionId,
                });
            }

            var pos = _client.GetPositions();

            foreach (var position in pos)
            {
                var m = new PositionMessage
                {
                    PortfolioName = position.bstrAcct,
                    SecurityId    = new SecurityId {
                        SecurityCode = position.bstrSym, BoardCode = "All", SecurityType = position.bstrInstrument.ToSecurityType()
                    },
                    OriginalTransactionId = message.TransactionId,
                };

                SendOutMessage(m);

                var changeMsg = new PositionChangeMessage
                {
                    PortfolioName = position.bstrAcct,
                    SecurityId    = new SecurityId {
                        SecurityCode = position.bstrSym, BoardCode = "All", SecurityType = position.bstrInstrument.ToSecurityType()
                    },
                    ServerTime = CurrentTime
                };

                changeMsg.TryAdd(PositionChangeTypes.RealizedPnL, (decimal)position.fReal);
                changeMsg.TryAdd(PositionChangeTypes.BeginValue, (decimal)position.nOpeningPosition);
                changeMsg.TryAdd(PositionChangeTypes.CurrentValue, (decimal)(position.nOpeningPosition + (position.nSharesBot - position.nSharesSld)));
                changeMsg.TryAdd(PositionChangeTypes.Commission, (decimal)position.fPositionCost);

                SendOutMessage(message);
            }

            SendOutMessage(new PortfolioLookupResultMessage {
                OriginalTransactionId = message.TransactionId
            });
        }
Exemplo n.º 9
0
        private void ProcessPortfolioLookup(PortfolioLookupMessage message)
        {
            //SendOutMessage(new PortfolioMessage
            //{
            //	PortfolioName = GetPortfolioName()
            //});

            ProcessBalance(_httpClient.RequestBalance());

            if (message != null)
            {
                SendOutMessage(new PortfolioLookupResultMessage {
                    OriginalTransactionId = message.TransactionId
                });
            }
        }
Exemplo n.º 10
0
            public void UnSubscribe(Subscription subscription)
            {
                if (subscription == null)
                {
                    throw new ArgumentNullException(nameof(subscription));
                }

                ISubscriptionMessage unsubscribe;

                if (subscription.DataType.IsMarketData)
                {
                    unsubscribe = new MarketDataMessage();
                }
                else if (subscription.DataType == DataType.Transactions)
                {
                    unsubscribe = new OrderStatusMessage();
                }
                else if (subscription.DataType == DataType.PositionChanges)
                {
                    unsubscribe = new PortfolioLookupMessage();
                }
                else if (subscription.DataType.IsPortfolio)
                {
                    unsubscribe = new PortfolioMessage();
                }
                else
                {
                    throw new ArgumentOutOfRangeException(nameof(subscription), subscription.DataType, LocalizedStrings.Str1219);
                }

                //// "immediate" unsubscribe
                //if (unsubscribe == null)
                //{
                //	lock (_syncObject)
                //	{
                //		_subscriptions.Remove(subscription.TransactionId);
                //	}

                //	return;
                //}

                unsubscribe.TransactionId         = _connector.TransactionIdGenerator.GetNextId();
                unsubscribe.OriginalTransactionId = subscription.TransactionId;
                unsubscribe.IsSubscribe           = false;

                SendRequest(unsubscribe, subscription);
            }
Exemplo n.º 11
0
        //private static string GetOECAccountName(string localPortfolioName)
        //{
        //	return localPortfolioName.Split(new[] { '-' })[0];
        //}

        private void ProcessPortfolioLookupMessage(PortfolioLookupMessage message)
        {
            foreach (var account in _client.Accounts)
            {
                ProcessAccount(account, null);

                foreach (var list in account.DetailedPositions)
                {
                    foreach (var position in list)
                    {
                        ProcessPosition(position);
                    }
                }
            }

            SendOutMessage(new PortfolioLookupResultMessage {
                OriginalTransactionId = message.TransactionId
            });
        }
        private void ProcessPortfolioLookup(PortfolioLookupMessage message)
        {
            var reply = _client.GetInfo();

            ProcessFunds(reply.State.Funds);

            SendOutMessage(new PortfolioMessage
            {
                PortfolioName         = GetPortfolioName(),
                State                 = reply.State.Rights.CanTrade ? PortfolioStates.Active : PortfolioStates.Blocked,
                OriginalTransactionId = message == null ? 0 : message.TransactionId
            });

            if (message != null)
            {
                SendOutMessage(new PortfolioLookupResultMessage {
                    OriginalTransactionId = message.TransactionId
                });
            }
        }
Exemplo n.º 13
0
        private void ProcessPortfolioLookupMessage(PortfolioLookupMessage message)
        {
            foreach (var account in _restClient.GetAccounts())
            {
                _accountIds[account.Name] = account.Id;

                SendOutMessage(new PortfolioMessage
                {
                    PortfolioName = account.Name,
                    Currency      = account.Currency.To <CurrencyTypes>(),
                });

                var details = _restClient.GetAccountDetails(account.Id);

                SendOutMessage(new PortfolioChangeMessage {
                    PortfolioName = account.Name
                }
                               .TryAdd(PositionChangeTypes.RealizedPnL, (decimal)details.RealizedPnL)
                               .TryAdd(PositionChangeTypes.UnrealizedPnL, (decimal)details.UnrealizedPnL)
                               .TryAdd(PositionChangeTypes.CurrentPrice, (decimal)details.Balance)
                               .TryAdd(PositionChangeTypes.BlockedValue, (decimal)details.MarginUsed)
                               .TryAdd(PositionChangeTypes.CurrentValue, (decimal)details.MarginAvailable));

                foreach (var position in _restClient.GetPositions(account.Id))
                {
                    SendOutMessage(new PositionChangeMessage
                    {
                        PortfolioName = account.Name,
                        SecurityId    = position.Instrument.ToSecurityId(),
                    }
                                   .TryAdd(PositionChangeTypes.CurrentValue, (decimal)(position.Side.To <Sides>() == Sides.Buy ? position.Units : -position.Units))
                                   .TryAdd(PositionChangeTypes.AveragePrice, (decimal)position.AveragePrice));
                }
            }

            SendOutMessage(new PortfolioLookupResultMessage {
                OriginalTransactionId = message.TransactionId
            });
        }
Exemplo n.º 14
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Subscription"/>.
        /// </summary>
        /// <param name="dataType">Data type info.</param>
        /// <param name="security">Security.</param>
        public Subscription(DataType dataType, Security security)
        {
            DataType = dataType ?? throw new ArgumentNullException(nameof(dataType));
            Security = security;

            if (dataType.IsMarketData)
            {
                MarketDataMessage = new MarketDataMessage
                {
                    DataType    = dataType.ToMarketDataType().Value,
                    Arg         = dataType.Arg,
                    IsSubscribe = true
                };

                if (Security != null)
                {
                    MarketDataMessage.FillSecurityInfo(Security);
                }
            }
            else if (dataType == DataType.Transactions)
            {
                OrderStatusMessage = new OrderStatusMessage {
                    IsSubscribe = true
                }
            }
            ;
            else if (dataType == DataType.PositionChanges)
            {
                PortfolioLookupMessage = new PortfolioLookupMessage {
                    IsSubscribe = true
                }
            }
            ;
            else
            {
                throw new ArgumentOutOfRangeException(nameof(dataType), dataType, LocalizedStrings.Str1219);
            }
        }
Exemplo n.º 15
0
        /// <inheritdoc />
        public void RequestState(PortfolioLookupMessage lookupMsg, Action <Message> result)
        {
            var time = lookupMsg.LocalTime;

            RequestPortfolioState(time, result);

            foreach (var pair in _positions)
            {
                var info = pair.Value;

                result(
                    new PositionChangeMessage
                {
                    LocalTime             = time,
                    ServerTime            = time,
                    PortfolioName         = _portfolioName,
                    SecurityId            = pair.Key,
                    OriginalTransactionId = lookupMsg.TransactionId,
                }
                    .Add(PositionChangeTypes.CurrentValue, info.PositionCurrentValue)
                    .TryAdd(PositionChangeTypes.AveragePrice, info.PositionAveragePrice)
                    );
            }
        }
 private void ProcessPortfolioLookupMessage(PortfolioLookupMessage message)
 {
     ProcessInSubscriptionMessage(message, DataType.PositionChanges);
 }
Exemplo n.º 17
0
 public void LookupPortfolios(PortfolioLookupMessage criteria)
 {
     SubscribePositions(criteria);
 }
Exemplo n.º 18
0
 /// <inheritdoc />
 public void SubscribePositions(PortfolioLookupMessage criteria)
 {
     Subscribe(new Subscription(criteria, (SecurityMessage)null));
 }
Exemplo n.º 19
0
 /// <summary>
 /// To call the event <see cref="LookupPortfoliosResult"/>.
 /// </summary>
 /// <param name="message">Message.</param>
 /// <param name="error">An error of lookup operation. The value will be <see langword="null"/> if operation complete successfully.</param>
 /// <param name="portfolios">Found portfolios.</param>
 private void RaiseLookupPortfoliosResult(PortfolioLookupMessage message, Exception error, IEnumerable <Portfolio> portfolios)
 {
     LookupPortfoliosResult?.Invoke(message, portfolios, error);
 }
Exemplo n.º 20
0
 /// <summary>
 /// To call the event <see cref="LookupPortfoliosResult"/>.
 /// </summary>
 /// <param name="message">Message.</param>
 /// <param name="error">An error of lookup operation. The value will be <see langword="null"/> if operation complete successfully.</param>
 /// <param name="portfolios">Found portfolios.</param>
 /// <param name="newPortfolios">Newly created.</param>
 private void RaiseLookupPortfoliosResult(PortfolioLookupMessage message, Exception error, Portfolio[] portfolios, Portfolio[] newPortfolios)
 {
     LookupPortfoliosResult?.Invoke(message, portfolios, error);
     LookupPortfoliosResult2?.Invoke(message, portfolios, newPortfolios, error);
 }
Exemplo n.º 21
0
 private void ProcessInPortfolioLookupMessage(PortfolioLookupMessage message)
 {
     ProcessInSubscriptionMessage(message, message.TransactionId, _pfLookupSubscribers, null, null, null);
 }