public void do_nothing_if_order_is_filled()
        {
            Signal s1 = new Signal(this.strategyHeader, BrokerDateTime.Make(DateTime.Now), TradeAction.Sell, OrderType.Limit, 150000, 0, 150000);

            this.tradingData.Get <ICollection <Signal> >().Add(s1);

            Order o1 = new Order(s1);

            this.tradingData.Get <ICollection <Order> >().Add(o1);

            Trade t1 = new Trade(o1, this.strategyHeader.Portfolio, this.strategyHeader.Symbol, 150000, -10, BrokerDateTime.Make(DateTime.Now));

            this.tradingData.Get <ICollection <Trade> >().Add(t1);
            o1.FilledAmount = t1.Amount * -1;
            Assert.IsTrue(o1.IsFilled);
            Assert.IsFalse(o1.IsFilledPartially);

            double   currentPrice = 150040;
            DateTime date         = BrokerDateTime.Make(DateTime.Now);
            string   description  = String.Format("Текущая цена {0} на расстоянии одного шага от stop loss цены {1} стратегии.", currentPrice, s1.Limit + this.slSettings.Points);

            IGenericFactory <OrderCancellationRequest> factory = new UnfilledOrderCancellationRequestFactory(currentPrice, o1, tradingData);

            Assert.IsNull(factory.Make());
        }
        public void cancel_partially_filled_for_limit_to_buy_order_when_current_price_near_stop_loss()
        {
            Signal s1 = new Signal(this.strategyHeader, BrokerDateTime.Make(DateTime.Now), TradeAction.Buy, OrderType.Limit, 150000, 0, 150000);

            this.tradingData.Get <ICollection <Signal> >().Add(s1);

            Order o1 = new Order(s1);

            this.tradingData.Get <ICollection <Order> >().Add(o1);

            Trade t1 = new Trade(o1, this.strategyHeader.Portfolio, this.strategyHeader.Symbol, 150000, 3, BrokerDateTime.Make(DateTime.Now));

            this.tradingData.Get <ICollection <Trade> >().Add(t1);
            o1.FilledAmount = t1.Amount;
            Assert.IsFalse(o1.IsFilled);
            Assert.IsTrue(o1.IsFilledPartially);

            double   currentPrice = 149960;
            DateTime date         = BrokerDateTime.Make(DateTime.Now);
            string   description  = String.Format("Текущая цена {0} на расстоянии одного шага от stop loss цены {1} стратегии.", currentPrice, s1.Limit - this.slSettings.Points);

            IGenericFactory <OrderCancellationRequest> factory = new UnfilledOrderCancellationRequestFactory(currentPrice, o1, tradingData);

            OrderCancellationRequest request = factory.Make();

            Assert.IsTrue(request.Id > 0);
            Assert.AreEqual(o1, request.Order);
            Assert.AreEqual(o1.Id, request.OrderId);
            Assert.IsTrue(request.DateTime >= date);
            Assert.AreEqual(description, request.Description);
        }
        public void make_request_for_unfilled_limit_to_sell_order_when_current_price_near_take_profit()
        {
            Signal s1 = new Signal(this.strategyHeader, BrokerDateTime.Make(DateTime.Now), TradeAction.Sell, OrderType.Limit, 150000, 0, 150000);

            this.tradingData.Get <ICollection <Signal> >().Add(s1);

            Order o1 = new Order(s1);

            this.tradingData.Get <ICollection <Order> >().Add(o1);

            Assert.IsFalse(o1.IsFilled);
            Assert.IsFalse(o1.IsFilledPartially);

            double   currentPrice = 149930;
            DateTime date         = BrokerDateTime.Make(DateTime.Now);
            string   description  = String.Format("Текущая цена {0} на расстоянии одного шага от take profit цены {1} стратегии.", currentPrice, s1.Limit - this.tpSettings.Points);

            IGenericFactory <OrderCancellationRequest> factory = new UnfilledOrderCancellationRequestFactory(currentPrice, o1, tradingData);

            OrderCancellationRequest request = factory.Make();

            Assert.IsTrue(request.Id > 0);
            Assert.AreEqual(o1, request.Order);
            Assert.AreEqual(o1.Id, request.OrderId);
            Assert.IsTrue(request.DateTime >= date);
            Assert.AreEqual(description, request.Description);
        }
        public override void OnItemAdded(Tick item)
        {
            //TODO здесь что-то очень медленно:
            IEnumerable <Order> unfilled = this.tradingData.Get <ICollection <Order> >().GetUnfilled(item.Symbol);

            if (unfilled == null)
            {
                return;
            }

            if (unfilled.Count() == 0)
            {
                return;
            }
            try
            {
                foreach (Order o in unfilled)
                {
                    if (!this.tradingData.Get <ICollection <Position> >().Exists(o.Portfolio, o.Symbol))
                    {
                        IGenericFactory <OrderCancellationRequest> cancellationFactory = new UnfilledOrderCancellationRequestFactory(item.Price, o, (TradingDataContext)this.tradingData);
                        //cancellationFactory.currentPrice = item.Price;
                        //cancellationFactory.order = o;
                        //cancellationFactory.tradingData = (TradingDataContext)this.tradingData;

                        OrderCancellationRequest request = cancellationFactory.Make();

                        if (request != null)
                        {
                            this.logger.Log(String.Format("{0:dd/MM/yyyy H:mm:ss.fff}, {1}, сформирован новый запрос {2} на отмену заявки {3}.", DateTime.Now, this.GetType().Name, o.ToString(), request.Description));
                            this.tradingData.Get <ObservableHashSet <OrderCancellationRequest> >().Add(request);
                        }
                    }
                }
            }
            catch
            {
                this.logger.Log(String.Format("{0:dd/MM/yyyy H:mm:ss.fff}, {1}, Ошибка в {2}.", DateTime.Now, this.GetType().Name, "OnItemAdded"));
            }
        }