Exemplo n.º 1
0
        public Tuple <MyTrade, bool> ProcessMyTradeMessage(Order order, Security security, ExecutionMessage message, long transactionId)
        {
            if (security == null)
            {
                throw new ArgumentNullException(nameof(security));
            }

            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            var securityData = GetData(security);

            if (transactionId == 0 && message.OrderId == null && message.OrderStringId.IsEmpty())
            {
                throw new ArgumentOutOfRangeException(nameof(message), transactionId, LocalizedStrings.Str715);
            }

            var myTrade = securityData.MyTrades.TryGetValue(Tuple.Create(transactionId, message.TradeId ?? 0));

            if (myTrade != null)
            {
                return(Tuple.Create(myTrade, false));
            }

            if (order == null)
            {
                order = GetOrder(security, transactionId, message.OrderId, message.OrderStringId);

                if (order == null)
                {
                    return(null);
                }
            }

            var trade = message.ToTrade(EntityFactory.CreateTrade(security, message.TradeId, message.TradeStringId));

            var isNew = false;

            myTrade = securityData.MyTrades.SafeAdd(Tuple.Create(order.TransactionId, trade.Id), key =>
            {
                isNew = true;

                var t = EntityFactory.CreateMyTrade(order, trade);

                if (t.ExtensionInfo == null)
                {
                    t.ExtensionInfo = new Dictionary <object, object>();
                }

                if (message.Commission != null)
                {
                    t.Commission = message.Commission;
                }

                if (message.Slippage != null)
                {
                    t.Slippage = message.Slippage;
                }

                if (message.PnL != null)
                {
                    t.PnL = message.PnL;
                }

                if (message.Position != null)
                {
                    t.Position = message.Position;
                }

                message.CopyExtensionInfo(t);

                _myTrades.Add(t);

                return(t);
            });

            return(Tuple.Create(myTrade, isNew));
        }
Exemplo n.º 2
0
        public Tuple <MyTrade, bool> ProcessMyTradeMessage(Security security, ExecutionMessage message)
        {
            if (security == null)
            {
                throw new ArgumentNullException("security");
            }

            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            var originalTransactionId = _orderStatusTransactions.Contains(message.OriginalTransactionId)
                                ? 0 : message.OriginalTransactionId;

            if (originalTransactionId == 0 && message.OrderId == null && message.OrderStringId.IsEmpty())
            {
                throw new ArgumentOutOfRangeException("message", originalTransactionId, LocalizedStrings.Str715);
            }

            var securityData = _cache.GetData(security);

            var myTrade = securityData.MyTrades.TryGetValue(Tuple.Create(originalTransactionId, message.TradeId ?? 0));

            if (myTrade != null)
            {
                return(Tuple.Create(myTrade, false));
            }

            var order = GetOrder(security, originalTransactionId, message.OrderId, message.OrderStringId);

            if (order == null)
            {
                return(null);
            }

            var trade = message.ToTrade(EntityFactory.CreateTrade(security, message.TradeId, message.TradeStringId));

            var isNew = false;

            myTrade = securityData.MyTrades.SafeAdd(Tuple.Create(order.TransactionId, trade.Id), key =>
            {
                isNew = true;

                var t = EntityFactory.CreateMyTrade(order, trade);

                if (t.ExtensionInfo == null)
                {
                    t.ExtensionInfo = new Dictionary <object, object>();
                }

                if (message.Commission != null)
                {
                    t.Commission = message.Commission;
                }

                if (message.Slippage != null)
                {
                    t.Slippage = message.Slippage;
                }

                message.CopyExtensionInfo(t);

                //trades.Add(t);
                _cache.MyTrades.Add(t);

                return(t);
            });

            return(Tuple.Create(myTrade, isNew));

            // mika
            // http://stocksharp.com/forum/yaf_postst1072_Probliemy-so-sdielkami--pozitsiiami.aspx
            // из-за того, что сделки по заявке иногда приходит быстрее события NewOrders, неправильно расчитывается поза по стратегиям

            //var raiseOrderChanged = false;

            //trades.SyncDo(d =>
            //{
            //    var newBalance = order.Volume - d.Sum(t => t.Trade.Volume);

            //    if (order.Balance > newBalance)
            //    {
            //        raiseOrderChanged = true;

            //        order.Balance = newBalance;

            //        if (order.Balance == 0)
            //            order.State = OrderStates.Done;
            //    }
            //});

            //if (raiseOrderChanged)
            //    RaiseOrderChanged(order);
        }
Exemplo n.º 3
0
        private Tuple <MyTrade, bool> AddMyTrade(Order order, Trade trade, ExecutionMessage message)
        {
            if (order == null)
            {
                throw new ArgumentNullException("order");
            }

            if (trade == null)
            {
                throw new ArgumentNullException("trade");
            }

            var isNew = false;

            var myTrade = _cache.GetData(order.Security).MyTrades.SafeAdd(Tuple.Create(order.TransactionId, trade.Id), key =>
            {
                isNew = true;

                var t = EntityFactory.CreateMyTrade(order, trade);

                if (t.ExtensionInfo == null)
                {
                    t.ExtensionInfo = new Dictionary <object, object>();
                }

                if (message.Commission != null)
                {
                    t.Commission = message.Commission;
                }

                if (message.Slippage != null)
                {
                    t.Slippage = message.Slippage;
                }

                message.CopyExtensionInfo(t);

                //trades.Add(t);
                _cache.MyTrades.Add(t);

                return(t);
            });

            return(Tuple.Create(myTrade, isNew));

            // mika
            // http://stocksharp.com/forum/yaf_postst1072_Probliemy-so-sdielkami--pozitsiiami.aspx
            // из-за того, что сделки по заявке иногда приходит быстрее события NewOrders, неправильно расчитывается поза по стратегиям

            //var raiseOrderChanged = false;

            //trades.SyncDo(d =>
            //{
            //    var newBalance = order.Volume - d.Sum(t => t.Trade.Volume);

            //    if (order.Balance > newBalance)
            //    {
            //        raiseOrderChanged = true;

            //        order.Balance = newBalance;

            //        if (order.Balance == 0)
            //            order.State = OrderStates.Done;
            //    }
            //});

            //if (raiseOrderChanged)
            //    RaiseOrderChanged(order);
        }