示例#1
0
        public void Handlers_cancel_stop_order_when_short_position_closed_by_limit()
        {
            Signal sl = new Signal(this.str2, BrokerDateTime.Make(DateTime.Now), TradeAction.Buy, OrderType.Stop, 33000, 33100, 0);

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

            Order slo = new Order(sl);

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

            Signal tp = new Signal(this.str2, BrokerDateTime.Make(DateTime.Now), TradeAction.Buy, OrderType.Limit, 33000, 0, 32900);

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

            Order tpo = new Order(tp);

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

            Assert.AreEqual(-10, this.tradingData.GetAmount(this.str2));
            Assert.AreEqual(2, this.tradingData.Get <ICollection <Order> >().GetUnfilled(this.str2).Count());
            Assert.AreEqual(20, this.tradingData.Get <ICollection <Order> >().GetUnfilledSignedAmount(this.str2));
            Assert.AreEqual(0, this.tradingData.Get <ObservableHashSet <OrderCancellationRequest> >().Count);

            Trade t = new Trade(slo, this.str2.Portfolio, this.str2.Symbol, 33100, this.str1.Amount, BrokerDateTime.Make(DateTime.Now));

            this.tradingData.Get <ObservableHashSet <Trade> >().Add(t);
            Assert.AreEqual(1, this.tradingData.Get <ObservableHashSet <OrderCancellationRequest> >().Count);

            OrderCancellationRequest request = this.tradingData.Get <ObservableHashSet <OrderCancellationRequest> >().Last();

            Assert.AreEqual(tpo, request.Order);
            Assert.AreEqual(tpo.Id, request.OrderId);
            Assert.AreEqual(String.Format("Отменить заявку {0}, потому что позиция была закрыта заявкой {1}", tpo.ToString(), slo.ToString()), request.Description);
        }
示例#2
0
        public void Handlers_place_stop_order_when_no_any_unfilled_orders_exists()
        {
            Assert.AreEqual(0, this.tradingData.Get <IEnumerable <Order> >().Count());
            Assert.AreEqual(0, this.tradingData.Get <IEnumerable <Signal> >().Count());
            Assert.AreEqual(0, this.tradingData.Get <ObservableHashSet <OrderCancellationConfirmation> >().Count);

            StrategyHeader strategyHeader = this.tradingData.Get <ICollection <StrategyHeader> >().Single(s => s.Id == 2);
            Signal         signal         = new Signal(strategyHeader, BrokerDateTime.Make(DateTime.Now), TradeAction.Buy, OrderType.Stop, 150000, 150100, 0);

            this.signalQueue.Enqueue(signal);

            Assert.AreEqual(1, this.orderQueue.Count);
            Assert.AreEqual(0, this.tradingData.Get <IEnumerable <Order> >().Count());
            Assert.AreEqual(1, this.tradingData.Get <IEnumerable <Signal> >().Count());
            Assert.AreEqual(0, this.signalQueue.Count);
            Assert.AreEqual(1, this.orderQueue.Count);

            Order order = this.orderQueue.Dequeue();

            Assert.IsTrue(order.Id > 0);
            Assert.AreEqual(strategyHeader.Portfolio, order.Portfolio);
            Assert.AreEqual(strategyHeader.Symbol, order.Symbol);
            Assert.AreEqual(strategyHeader.Amount, order.Amount);
            Assert.AreEqual(signal.TradeAction, order.TradeAction);
            Assert.AreEqual(signal.OrderType, order.OrderType);
            Assert.AreEqual(signal.Limit, order.Price);
            Assert.AreEqual(signal.Stop, order.Stop);
        }
示例#3
0
        private void OnChange(string symbol)
        {
            if (this.symbol != symbol)
            {
                return;
            }

            if (this.currentIndex == this.maxIndex - 1)
            {
                this.currentIndex = 0;
            }

            CultureInfo ci = CultureInfo.InvariantCulture;

            string message =
                String.Format("{0:dd/MM/yyyy H:mm:ss.fff},{1},{2},{3},{4},{5}",
                              BrokerDateTime.Make(DateTime.Now),
                              this.currentIndex,
                              this.orderBook.GetBidPrice(this.symbol, this.currentIndex).ToString("0.0000", ci),
                              this.orderBook.GetBidVolume(this.symbol, this.currentIndex).ToString("0.0000", ci),
                              this.orderBook.GetOfferPrice(this.symbol, this.currentIndex).ToString("0.0000", ci),
                              this.orderBook.GetOfferVolume(this.symbol, this.currentIndex).ToString("0.0000", ci));

            this.currentIndex++;

            this.logger.Log(message);
        }
示例#4
0
        public void ignore_OrderMoveSucceeded_if_request_already_confirmed_test()
        {
            Signal signal = new Signal(this.strategyHeader, BrokerDateTime.Make(DateTime.Now), TradeAction.Buy, OrderType.Limit, 150000, 0, 150000);

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

            Order order = new Order(signal);

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

            OrderMoveRequest request = new OrderMoveRequest(order, 151000, 0, "Move order");

            this.tradingData.Get <ICollection <OrderMoveRequest> >().Add(request);

            Assert.IsFalse(request.IsDelivered);

            OrderMoveSucceeded confirmation = new OrderMoveSucceeded(request.OrderId, "588");

            this.rawData.GetData <OrderMoveSucceeded>().Add(confirmation);

            Assert.IsTrue(request.IsDelivered);

            OrderMoveSucceeded duplicate = new OrderMoveSucceeded(request.OrderId, "588");

            duplicate.DateTime = duplicate.DateTime.AddMilliseconds(1);
            this.rawData.GetData <OrderMoveSucceeded>().Add(duplicate);

            Assert.AreNotEqual(request.DeliveryDate, duplicate.DateTime);
            Assert.AreEqual(request.DeliveryDate, confirmation.DateTime);
        }
示例#5
0
        public void Update(UpdateBidAsk item)
        {
            if (item.Row != 0)
            {
                this.rawData.Remove(item);
                return;
            }

            BidAsk bidAsk = GetBidAsk(item.Symbol);

            BidAsk ba = MakeBidAsk(item);

            this.rawData.Remove(item);


            if (bidAsk != null)
            {
                this.tradingData.Get <ICollection <BidAsk> >().Remove(bidAsk);
            }

            this.logger.Log(String.Format("{0:dd/MM/yyyy H:mm:ss.fff}, {1}, UpdateBidAsk преобразован в BidAsk, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}",
                                          BrokerDateTime.Make(DateTime.Now),
                                          this.GetType().Name,
                                          ba.Symbol,
                                          ba.Row,
                                          ba.NRows,
                                          ba.Bid,
                                          ba.BidSize,
                                          ba.Ask,
                                          ba.AskSize,
                                          ba.DateTime));

            this.tradingData.Get <ObservableCollection <BidAsk> >().Add(ba);
        }
        public void cant_add_two_confirmations_for_one_order_to_hashset()
        {
            ObservableHashSet <OrderDeliveryConfirmation> confirmations =
                new ObservableHashSet <OrderDeliveryConfirmation>(new OrderDeliveryConfirmationComparer());

            Order o1 = new Order(1,
                                 BrokerDateTime.Make(DateTime.Now),
                                 "BP12345-RF-01",
                                 "RTS-9.13_FT",
                                 TradeAction.Buy,
                                 OrderType.Limit,
                                 10,
                                 136000,
                                 0);

            OrderDeliveryConfirmation c1 =
                new OrderDeliveryConfirmation(o1, BrokerDateTime.Make(DateTime.Now));

            confirmations.Add(c1);
            Assert.AreEqual(1, confirmations.Count);

            OrderDeliveryConfirmation c2 =
                new OrderDeliveryConfirmation(o1, BrokerDateTime.Make(DateTime.Now));

            confirmations.Add(c2);
            Assert.AreEqual(1, confirmations.Count);
        }
示例#7
0
        public void Setup()
        {
            Symbol symbol = new Symbol("RTS-9.13_FT", 1, 8, 10, BrokerDateTime.Make(DateTime.Now));

            this.tradingData.Get <HashSetOfNamedMutable <Symbol> >().Add(symbol);

            this.strategyHeader = new StrategyHeader(10, "strategyHeader", "BP12345-RF-01", "RTS-9.13_FT", 10);
            this.tradingData.Get <ICollection <StrategyHeader> >().Add(this.strategyHeader);

            StopPointsSettings slSettings = new StopPointsSettings(this.strategyHeader, 300, false);

            this.tradingData.Get <ICollection <StopPointsSettings> >().Add(slSettings);

            ProfitPointsSettings tpSettings = new ProfitPointsSettings(this.strategyHeader, 500, false);

            this.tradingData.Get <ICollection <ProfitPointsSettings> >().Add(tpSettings);

            StopLossOrderSettings slOrderSettings = new StopLossOrderSettings(this.strategyHeader, 3600);

            this.tradingData.Get <ICollection <StopLossOrderSettings> >().Add(slOrderSettings);

            TakeProfitOrderSettings tpOrderSettings = new TakeProfitOrderSettings(this.strategyHeader, 3600);

            this.tradingData.Get <ICollection <TakeProfitOrderSettings> >().Add(tpOrderSettings);

            StrategyStopLossByPointsOnTick stopLossHandler =
                new StrategyStopLossByPointsOnTick(strategyHeader, this.tradingData, this.signalQueue, new NullLogger());
            StrategyTakeProfitByPointsOnTick takeProfitHandler =
                new StrategyTakeProfitByPointsOnTick(strategyHeader, this.tradingData, this.signalQueue, new NullLogger());

            PlaceStrategyStopLossByPointsOnTrade placeStopOnTradeHandler =
                new PlaceStrategyStopLossByPointsOnTrade(strategyHeader, this.tradingData, this.signalQueue, new NullLogger());
            PlaceStrategyTakeProfitByPointsOnTrade placeTakeProfitOnTradeHandler =
                new PlaceStrategyTakeProfitByPointsOnTrade(strategyHeader, this.tradingData, this.signalQueue, new NullLogger());
        }
示例#8
0
        public void UpdateOrder_Constructor()
        {
            DateTime date = BrokerDateTime.Make(DateTime.Now);

            UpdateOrder update = new UpdateOrder("ST30151-RF-01", "RTS-12.12_FT", StOrder_State.StOrder_State_Open, StOrder_Action.StOrder_Action_Buy, StOrder_Type.StOrder_Type_Market, StOrder_Validity.StOrder_Validity_Day, 150000, 1, 0, 0, date, "id", "no", 0, 1);

            Assert.AreEqual("ST30151-RF-01", update.Portfolio);
            Assert.AreEqual("RTS-12.12_FT", update.Symbol);
            Assert.AreEqual(StOrder_State.StOrder_State_Open, update.State);
            Assert.AreEqual(StOrder_Action.StOrder_Action_Buy, update.Action);
            Assert.AreEqual(StOrder_Type.StOrder_Type_Market, update.Type);
            Assert.AreEqual(StOrder_Validity.StOrder_Validity_Day, update.Validity);
            Assert.AreEqual(150000, update.Price);
            Assert.IsInstanceOfType(update.Price, typeof(double));
            Assert.AreEqual(1, update.OrderAmount);
            Assert.IsInstanceOfType(update.OrderAmount, typeof(double));
            Assert.AreEqual(0, update.Stop);
            Assert.IsInstanceOfType(update.Stop, typeof(double));
            Assert.AreEqual(0, update.OrderUnfilled);
            Assert.IsInstanceOfType(update.OrderUnfilled, typeof(double));
            Assert.AreEqual(date, update.Datetime);
            Assert.AreEqual("id", update.OrderId);
            Assert.AreEqual("no", update.OrderNo);
            Assert.AreEqual(0, update.StatusMask);
            Assert.IsInstanceOfType(update.StatusMask, typeof(int));
            Assert.AreEqual(1, update.Cookie);
            Assert.IsInstanceOfType(update.Cookie, typeof(int));
        }
        public void CalculateSpreadOnQuotesUpdate(string symbol)
        {
            if (!arbitrageSettings.HasSymbol(symbol))
            {
                return;
            }

            double buySpreadPrice  = new BuySpreadFactory(this.arbitrageSettings.LeftLeg, this.arbitrageSettings.RightLeg, this.orderBook).Make();
            double sellSpreadPrice = new SellSpreadFactory(this.arbitrageSettings.LeftLeg, this.arbitrageSettings.RightLeg, this.orderBook).Make();

            if (buySpreadPrice == 0 || sellSpreadPrice == 0)
            {
                return;
            }

            SpreadValue lastSpread = GetLastSpreadValue();

            if (lastSpread != null &&
                lastSpread.BuyBeforePrice == buySpreadPrice &&
                lastSpread.SellAfterPrice == sellSpreadPrice)
            {
                return;
            }

            SpreadValue spreadValue = new SpreadValue(arbitrageSettings.Id, BrokerDateTime.Make(DateTime.Now), sellSpreadPrice, buySpreadPrice);

            this.logger.Log(String.Format("{0:dd/MM/yyyy H:mm:ss.fff}, {1}, выполнено вычисление нового значения {2}.", DateTime.Now, this.GetType().Name, spreadValue.ToString()));

            this.tradingData.Get <ObservableCollection <SpreadValue> >().Add(spreadValue);
        }
示例#10
0
        public override void Update(UpdateOrder item)
        {
            if (item.State != StOrder_State.StOrder_State_Expired)
            {
                return;
            }

            if (item.Cookie == 0)
            {
                return;
            }

            SetOrder(item.Cookie);

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

            this.order.ExpirationDate = item.Datetime;

            this.logger.Log(String.Format("{0:dd/MM/yyyy H:mm:ss.fff}, {1}, срок действия заявки истек {2}.",
                                          BrokerDateTime.Make(DateTime.Now),
                                          this.GetType().Name,
                                          order.ToString()));

            this.order = null;
        }
        public void TradingDataContext_GetFilledPartially_stop_orders()
        {
            StrategyHeader st1 = new StrategyHeader(1, "Strategy 1", "BP12345-RF-01", "RTS-9.13_FT", 8);

            this.tradingData.Get <ICollection <StrategyHeader> >().Add(st1);

            Signal s1 = new Signal(st1, BrokerDateTime.Make(DateTime.Now), TradeAction.Buy, OrderType.Stop, 150000, 150000, 0);

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

            Order o1 = new Order(s1);

            o1.FilledAmount = 2;
            Assert.IsFalse(o1.IsFilled);
            Assert.IsTrue(o1.IsFilledPartially);
            Assert.IsFalse(o1.IsCanceled);
            Assert.IsFalse(o1.IsExpired);
            Assert.IsFalse(o1.IsRejected);

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

            IEnumerable <Order> unfilled = this.tradingData.GetFilledPartially(st1, OrderType.Stop);

            Assert.AreEqual(1, unfilled.Count());

            Assert.AreSame(unfilled.Last(), o1);
        }
示例#12
0
        public override void OnItemAdded(OrderMoveFailed item)
        {
            Order order = FindOrder(item.Cookie);

            if (order == null)
            {
                return;
            }

            OrderMoveRequest request = FindRequest(order);

            if (request == null)
            {
                return;
            }

            if (request.IsFailed)
            {
                return;
            }

            request.Failed(BrokerDateTime.Make(DateTime.Now), item.Reason);

            this.logger.Log(String.Format("{0:dd/MM/yyyy H:mm:ss.fff}, {1}, получено уведомление об отклонении запроса {2} на сдвиг заявки.",
                                          BrokerDateTime.Make(DateTime.Now),
                                          this.GetType().Name,
                                          request.ToString()));
        }
示例#13
0
        private void stServer_UpdateQuote(string symbol, DateTime datetime, double open, double high, double low, double close, double last, double volume, double size, double bid, double ask, double bidsize, double asksize, double open_int, double go_buy, double go_sell, double go_base, double go_base_backed, double high_limit, double low_limit, int trading_status, double volat, double theor_price)
        {
            SymbolSummary item =
                new SymbolSummary(symbol,
                                  datetime,
                                  open,
                                  high,
                                  low,
                                  close,
                                  last,
                                  volume,
                                  size,
                                  ask,
                                  bid,
                                  asksize,
                                  bidsize,
                                  open_int,
                                  go_buy,
                                  go_sell,
                                  high_limit,
                                  low_limit,
                                  (trading_status == 0) ? true : false);

            this.logger.Log(String.Format("{0:dd/MM/yyyy H:mm:ss.fff}, {1}, получен {2}",
                                          BrokerDateTime.Make(DateTime.Now),
                                          this.GetType().Name,
                                          item.ToString()));

            this.symbolsData.Get <HashSetOfNamedMutable <SymbolSummary> >().Add(item);
        }
示例#14
0
        public void Handlers_do_nothing_when_long_position_closed_partially()
        {
            Signal sl = new Signal(this.str1, BrokerDateTime.Make(DateTime.Now), TradeAction.Sell, OrderType.Stop, 150000, 149100, 0);

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

            Order slo = new Order(sl);

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

            Signal tp = new Signal(this.str1, BrokerDateTime.Make(DateTime.Now), TradeAction.Sell, OrderType.Limit, 150000, 0, 150100);

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

            Order tpo = new Order(tp);

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

            Assert.AreEqual(10, this.tradingData.GetAmount(this.str1));
            Assert.AreEqual(2, this.tradingData.Get <ICollection <Order> >().GetUnfilled(this.str1).Count());
            Assert.AreEqual(-20, this.tradingData.Get <ICollection <Order> >().GetUnfilledSignedAmount(this.str1));
            Assert.AreEqual(0, this.tradingData.Get <ObservableHashSet <OrderCancellationRequest> >().Count);

            Trade t = new Trade(tpo, this.str1.Portfolio, this.str1.Symbol, 149100, -3, BrokerDateTime.Make(DateTime.Now));

            this.tradingData.Get <ObservableHashSet <Trade> >().Add(t);
            Assert.AreEqual(0, this.tradingData.Get <ObservableHashSet <OrderCancellationRequest> >().Count);
        }
示例#15
0
        public void Handlers_UpdatePositionOnTrade_ignore_single_duplicate_trade()
        {
            StrategyHeader strategyHeader = new StrategyHeader(1, "Strategy", "BP12345-RF-01", "RTS-9.13_FT", 8);

            this.tradingData.Get <ICollection <StrategyHeader> >().Add(strategyHeader);

            Signal signal = new Signal(strategyHeader, BrokerDateTime.Make(DateTime.Now), TradeAction.Buy, OrderType.Market, 131000, 0, 0);

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

            Order order = new Order(signal);

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

            Assert.AreEqual(0, this.tradingData.GetAmount(strategyHeader));

            Trade trade = new Trade(order, order.Portfolio, order.Symbol, 131010, order.Amount, BrokerDateTime.Make(DateTime.Now));

            this.tradingData.Get <ObservableHashSet <Trade> >().Add(trade);

            Assert.AreEqual(8, this.tradingData.GetAmount(strategyHeader));

            Trade duplicate = new Trade(order, order.Portfolio, order.Symbol, 131010, order.Amount, BrokerDateTime.Make(DateTime.Now));

            this.tradingData.Get <ObservableHashSet <Trade> >().Add(duplicate);

            Assert.AreEqual(8, this.tradingData.GetAmount(strategyHeader));
        }
        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 void two_confirmations_for_different_orders_are_not_equals()
        {
            Order o1 = new Order(1,
                                 BrokerDateTime.Make(DateTime.Now),
                                 "BP12345-RF-01",
                                 "RTS-9.13_FT",
                                 TradeAction.Buy,
                                 OrderType.Limit,
                                 10,
                                 136000,
                                 0);

            OrderDeliveryConfirmation c1 =
                new OrderDeliveryConfirmation(o1, BrokerDateTime.Make(DateTime.Now));

            Order o2 = new Order(2,
                                 BrokerDateTime.Make(DateTime.Now),
                                 "BP12345-RF-01",
                                 "RTS-9.13_FT",
                                 TradeAction.Sell,
                                 OrderType.Limit,
                                 10,
                                 136000,
                                 0);

            OrderDeliveryConfirmation c2 =
                new OrderDeliveryConfirmation(o2, BrokerDateTime.Make(DateTime.Now));

            EqualityComparer <OrderDeliveryConfirmation> ec =
                new OrderDeliveryConfirmationComparer();

            Assert.IsFalse(ec.Equals(c1, c2));
        }
        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());
        }
示例#19
0
        public void BuySpreadOnQuote_ignore_updates_when_any_position_from_basket_of_strategies_exists()
        {
            StrategyHeader strtgy = this.tradingData.Get <IEnumerable <StrategyHeader> >().Single(s => s.Id == 1);

            Signal sgnl = new Signal(strtgy, BrokerDateTime.Make(DateTime.Now), TradeAction.Buy, OrderType.Market, 143000, 0, 0);

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

            Order ordr = new Order(sgnl);

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

            OrderDeliveryConfirmation cnfrmtn = new OrderDeliveryConfirmation(ordr, BrokerDateTime.Make(DateTime.Now));

            this.tradingData.Get <ObservableHashSet <OrderDeliveryConfirmation> >().Add(cnfrmtn);
            Assert.IsTrue(ordr.IsDelivered);

            Trade trd = new Trade(ordr, ordr.Portfolio, ordr.Symbol, 1430000, ordr.Amount, BrokerDateTime.Make(DateTime.Now));

            this.tradingData.Get <ObservableHashSet <Trade> >().Add(trd);
            Assert.AreEqual(1, this.tradingData.Get <IEnumerable <Position> >().Count());

            this.qProvider.Update(0, "RTS-12.13_FT", 128990, 20, 129000, 100);
            this.qProvider.Update(0, "Si-12.13_FT", 33000, 50, 33001, 40);
            this.qProvider.Update(0, "Eu-12.13_FT", 44000, 30, 44001, 0);

            Assert.AreEqual(1, this.tradingData.Get <IEnumerable <Signal> >().Count());
        }
        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);
        }
示例#21
0
        public void Transaction_ExportOrdersTransaction_do_nothing_when_no_orders_exists()
        {
            StrategyHeader st1 = new StrategyHeader(1, "First strategyHeader", "BP12345-RF-01", "RTS-9.13_FT", 10);

            this.tradingData.Get <ICollection <StrategyHeader> >().Add(st1);

            StrategyHeader st2 = new StrategyHeader(2, "Second strategyHeader", "BP12345-RF-01", "RTS-9.13_FT", 8);

            this.tradingData.Get <ICollection <StrategyHeader> >().Add(st2);

            StrategyHeader st3 = new StrategyHeader(3, "Third strategyHeader", "BP12345-RF-01", "RTS-9.13_FT", 5);

            this.tradingData.Get <ICollection <StrategyHeader> >().Add(st3);

            Signal s1 = new Signal(st1, BrokerDateTime.Make(DateTime.Now), TradeAction.Buy, OrderType.Market, 150000, 0, 0);

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

            Signal s2 = new Signal(st2, BrokerDateTime.Make(DateTime.Now), TradeAction.Sell, OrderType.Limit, 150000, 0, 150000);

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

            Signal s3 = new Signal(st3, BrokerDateTime.Make(DateTime.Now), TradeAction.Buy, OrderType.Stop, 150000, 150000, 0);

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

            ITransaction export = new ExportOrdersTransaction((IObservableHashSetFactory)this.tradingData, this.path);

            export.Execute();
            Assert.IsFalse(File.Exists(this.path));
        }
示例#22
0
        private void stServer_AddSymbol(int row, int nrows, string symbol, string short_name, string long_name, string type, int decimals, int lot_size, double punkt, double step, string sec_ext_id, string sec_exch_name, DateTime expiryDate, double days_before_expiry, double strike)
        {
            CultureInfo ci = CultureInfo.InvariantCulture;

            this.logger.Log(String.Format("{0:dd/MM/yyyy H:mm:ss.fff}, {1}, получен Symbol {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15}, {16}",
                                          BrokerDateTime.Make(DateTime.Now),
                                          this.GetType().Name,
                                          row,
                                          nrows,
                                          symbol,
                                          short_name,
                                          long_name,
                                          type,
                                          decimals,
                                          lot_size,
                                          punkt.ToString("0.0000", ci),
                                          step.ToString("0.0000", ci),
                                          sec_ext_id,
                                          sec_exch_name,
                                          expiryDate.ToString(ci),
                                          days_before_expiry.ToString("0.0000", ci),
                                          strike.ToString("0.0000", ci)));

            this.orderData.GetData <RawSymbol>().Add(new RawSymbol(symbol, short_name, long_name, type, decimals, lot_size, punkt, step, sec_ext_id, sec_exch_name, expiryDate, days_before_expiry, strike));
        }
        public void make_signal_to_open_short_position_if_previous_long_closed_by_stop_test()
        {
            Assert.AreEqual(0, this.tradingData.Make <Signal>().Count);
            Assert.AreEqual(0, this.tradingData.Make <Order>().Count);

            Signal signalToOpen = new Signal(this.strategyHeader, BrokerDateTime.Make(DateTime.Now), TradeAction.Buy, OrderType.Market, 150000, 0, 0);

            EmulateTradeFor(signalToOpen);

            Signal signalToClose = new Signal(this.strategyHeader, BrokerDateTime.Make(DateTime.Now), TradeAction.Sell, OrderType.Stop, 149900, 149900, 0);

            EmulateTradeFor(signalToClose);

            this.orderBook.Update(0, this.strategyHeader.Symbol, 149990, 100, 150000, 50);

            Assert.AreEqual(3, this.tradingData.Make <Signal>().Count);
            Assert.AreEqual(3, this.tradingData.Make <Order>().Count);

            Signal signal = this.tradingData.Make <Signal>().Last();

            Assert.AreEqual(this.strategyHeader.Id, signal.StrategyId);
            Assert.AreEqual(TradeAction.Sell, signal.TradeAction);
            Assert.AreEqual(OrderType.Limit, signal.OrderType);
            Assert.AreEqual(150000, signal.Limit);
        }
示例#24
0
 public Order(Signal signal, int timeToLiveSeconds)
     : this(SerialIntegerFactory.Make(), BrokerDateTime.Make(DateTime.Now), signal.Strategy.Portfolio, signal.Strategy.Symbol, signal.TradeAction, signal.OrderType, signal.Amount, signal.Limit, signal.Stop)
 {
     this.Signal         = signal;
     this.SignalId       = signal.Id;
     this.ExpirationDate = BrokerDateTime.Make(DateTime.Now).AddSeconds(timeToLiveSeconds);
 }
示例#25
0
        public void Handlers_place_stop_order_when_unfilled_strategy_market_order_exists()
        {
            Signal s1 = new Signal(this.str2, BrokerDateTime.Make(DateTime.Now), TradeAction.Buy, OrderType.Market, 150000, 0, 0);

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

            Order o1 = new Order(s1);

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

            Assert.AreEqual(0, this.signalQueue.Count);
            Assert.AreEqual(0, this.orderQueue.Count);

            Signal s2 = new Signal(this.str2, BrokerDateTime.Make(DateTime.Now), TradeAction.Buy, OrderType.Stop, 149000, 0, 0);

            this.signalQueue.Enqueue(s2);

            Assert.AreEqual(0, this.signalQueue.Count);
            Assert.AreEqual(1, this.orderQueue.Count);

            Order order = this.orderQueue.Dequeue();

            Assert.IsTrue(order.Id > 0);
            Assert.AreEqual(this.str2.Portfolio, order.Portfolio);
            Assert.AreEqual(this.str2.Symbol, order.Symbol);
            Assert.AreEqual(this.str2.Amount, order.Amount);
            Assert.AreEqual(s2.TradeAction, order.TradeAction);
            Assert.AreEqual(s2.OrderType, order.OrderType);
            Assert.AreEqual(s2.Limit, order.Price);
            Assert.AreEqual(s2.Stop, order.Stop);
        }
示例#26
0
        public override void MakeSignal(Trade item, double positionAmount)
        {
            if (positionAmount.HasOppositeSignWith(item.Amount))
            {
                return;
            }

            if (item.Amount > 0)
            {
                this.signal =
                    new Signal(this.strategyHeader,
                               BrokerDateTime.Make(DateTime.Now),
                               TradeAction.Sell,
                               OrderType.Stop,
                               item.Price,
                               this.measureFromSignalPrice ? (item.Order.Signal.Price - this.spSettings.Points) : (item.Price - this.spSettings.Points),
                               0);
            }
            else
            {
                this.signal =
                    new Signal(this.strategyHeader,
                               BrokerDateTime.Make(DateTime.Now),
                               TradeAction.Buy,
                               OrderType.Stop,
                               item.Price,
                               this.measureFromSignalPrice ? (item.Order.Signal.Price + this.spSettings.Points) : (item.Price + this.spSettings.Points),
                               0);
            }

            if (this.signal != null)
            {
                this.signal.Amount = Math.Abs(positionAmount);
            }
        }
示例#27
0
        public void Bar_IsBlack()
        {
            Bar bar = new Bar("RTS-9.13_FT", 60, BrokerDateTime.Make(DateTime.Now), 150, 151, 147, 148, 10);

            Assert.IsTrue(bar.IsBlack);
            Assert.IsFalse(bar.IsWhite);
        }
示例#28
0
        public void ignore_duplicate_OrderFailed()
        {
            Order order = new Order(this.sg1);

            this.tradingData.Get <ICollection <Order> >().Add(order);
            Assert.IsFalse(order.IsRejected);

            OrderFailed failed = new OrderFailed {
                OrderId = "111", Reason = "Failed", Cookie = order.Id
            };

            this.rawTradingData.GetData <OrderFailed>().Add(failed);

            Assert.IsTrue(order.IsRejected);
            Assert.AreEqual("Failed", order.RejectReason);
            Assert.IsTrue(order.RejectedDate <= BrokerDateTime.Make(DateTime.Now));

            OrderFailed failedTwice = new OrderFailed {
                OrderId = "111", Reason = "Failed twice", Cookie = order.Id
            };

            this.rawTradingData.GetData <OrderFailed>().Add(failedTwice);

            Assert.IsTrue(order.IsRejected);
            Assert.AreEqual("Failed", order.RejectReason);
            Assert.IsTrue(order.RejectedDate <= BrokerDateTime.Make(DateTime.Now));
        }
示例#29
0
        private void OnChange(string symbol)
        {
            if (this.symbol != symbol)
            {
                return;
            }

            UpdateCurrentRowPrices();

            if (CurrentRowPricesAreSameAsPrevious())
            {
                return;
            }

            CultureInfo ci = CultureInfo.InvariantCulture;

            string message =
                String.Format("{0:dd/MM/yyyy H:mm:ss.fff},{1},{2}",
                              BrokerDateTime.Make(DateTime.Now),
                              this.currentBidPrice.ToString("0.0000", ci),
                              this.currentOfferPrice.ToString("0.0000", ci));

            this.logger.Log(message);
            this.loggedRowsCounter++;

            UpdatePreviousRowPricesWithCurrent();
        }
示例#30
0
        public void OrderMoveRequest_Failed_test()
        {
            DateTime date = BrokerDateTime.Make(DateTime.Now).Date;

            double stopPrice = 149900;

            double limitPrice = 0;

            string description = "Move order";

            Order order = new Order(1, DateTime.Now, "BP12345-RF-01", "RTS-12.13_FT", TradeAction.Sell, OrderType.Stop, 1, 0, 150000);

            OrderMoveRequest request = new OrderMoveRequest(order, limitPrice, stopPrice, description);

            Assert.IsFalse(request.IsFailed);

            DateTime faultDate   = BrokerDateTime.Make(DateTime.Now);
            string   faultReason = "Fault";

            request.Failed(faultDate, faultReason);

            Assert.AreEqual(faultDate, request.FaultDate);
            Assert.AreEqual(faultReason, request.FaultDescription);
            Assert.IsTrue(request.IsFailed);
        }