Beispiel #1
0
        protected override void PlaceLimitStopOrder(Order order)
        {
            ThreadPool.QueueUserWorkItem(_ =>
            {
                var qty = order.Quantity;
                if (qty > 0 && order.OrderSide == Side.Sell)
                {
                    qty = -qty;
                }

                var security = Securities.First(i => i.Symbol.Equals(order.Symbol, IgnoreCase));
                if (security == null || _session == null)
                {
                    return;
                }

                if (order.OrderType == CommonObjects.OrderType.Limit)
                {
                    _session.PlaceLimitOrder(new LimitOrderSpecification(order.UserID, security.SecurityId,
                                                                         order.Price, qty, Converter.ToTIF(order.TimeInForce), order.SLOffset, order.TPOffset),
                                             id => OnOrderPlaced(order), this.GeneralFailureCallback);
                }
                else if (order.OrderType == CommonObjects.OrderType.Stop)
                {
                    _session.PlaceStopOrder(new StopOrderSpecification(order.UserID, security.SecurityId,
                                                                       order.Price, qty, Converter.ToTIF(order.TimeInForce), order.SLOffset, order.TPOffset),
                                            id => OnOrderPlaced(order), this.GeneralFailureCallback);
                }
            });
        }
Beispiel #2
0
        public override void PlaceOrder(Order order)
        {
            var tradingError = CheckIfCanTrade();

            if (tradingError != null)
            {
                OnOrderRejected(order, tradingError);
            }
            else
            {
                base.PlaceOrder(order);
            }
        }
Beispiel #3
0
        private void OnInstructionFailed(object sender, Tuple <OrderSpecification, string> failed)
        {
            var instrument = Securities.FirstOrDefault(i => i.SecurityId == failed.Item1.InstrumentId);

            if (instrument != null)
            {
                var order = new Order(failed.Item1.InstructionId, instrument.Symbol)
                {
                    AccountId  = AccountInfo.ID,
                    BrokerName = Name,
                    Quantity   = failed.Item1.Quantity,
                    SLOffset   = failed.Item1.StopLossPriceOffset,
                    TPOffset   = failed.Item1.StopProfitPriceOffset
                };
                OnOrderRejected(order, failed.Item2);
            }
        }
Beispiel #4
0
        protected override void PlaceMarketOrder(Order order)
        {
            ThreadPool.QueueUserWorkItem(_ =>
            {
                var qty = order.Quantity;
                if (qty > 0 && order.OrderSide == Side.Sell)
                {
                    qty = -qty;
                }

                var security = Securities.First(i => i.Symbol.Equals(order.Symbol, IgnoreCase));
                if (security == null || _session == null)
                {
                    OnOrderRejected(order, "Broker API error");
                    return;
                }

                _session.PlaceMarketOrder(new MarketOrderSpecification(order.UserID, security.SecurityId,
                                                                       qty, Converter.ToTIF(order.TimeInForce), order.SLOffset, order.TPOffset),
                                          id => OnOrderPlaced(order), GeneralFailureCallback);
            });
        }
Beispiel #5
0
        public override void ModifyOrder(Order order, decimal?sl, decimal?tp, bool isServerSide = false)
        {
            var tradingError = CheckIfCanTrade();

            if (tradingError != null)
            {
                OnOrderRejected(order, tradingError);
                return;
            }

            var isExisitingOrder = false;

            lock (Orders)
                isExisitingOrder = Orders.Contains(order);

            var unmodified = order.Clone() as Order;

            base.ModifyOrder(order, sl, tp, isServerSide);

            var security = Securities.FirstOrDefault(i => i.Symbol.Equals(order.Symbol, IgnoreCase));

            if (security == null || security.SecurityId < 1)
            {
                return;
            }

            if (isServerSide && isExisitingOrder && !unmodified.ServerSide && unmodified.OpenQuantity > 0 &&
                (unmodified.SLOffset.HasValue || unmodified.TPOffset.HasValue))
            {
                _session.AmendStops(new AmendStopLossProfitRequest(security.SecurityId,
                                                                   unmodified.UserID + "_m", unmodified.UserID, null, null), id => { }, GeneralFailureCallback);
            }
            else if (!isServerSide && unmodified.OpenQuantity > 0 && (sl.HasValue || tp.HasValue))
            {
                _session.AmendStops(new AmendStopLossProfitRequest(security.SecurityId,
                                                                   unmodified.UserID + "_m", unmodified.UserID, sl, tp), id => { }, GeneralFailureCallback);
            }
        }
Beispiel #6
0
        public override void CancelOrder(Order order)
        {
            var tradingError = CheckIfCanTrade();

            if (tradingError != null)
            {
                OnOrderRejected(order, tradingError);
                return;
            }

            if (order.ServerSide)
            {
                base.CancelOrder(order);
            }
            else
            {
                var security = Securities.FirstOrDefault(i => i.Symbol.Equals(order.Symbol, IgnoreCase));
                if (security != null)
                {
                    _session.CancelOrder(new CancelOrderRequest(order.UserID + "_c",
                                                                security.SecurityId, order.UserID), id => { }, GeneralFailureCallback);
                }
            }
        }
Beispiel #7
0
 private void OnOrderPlaced(Order order) =>
 order.PlacedDate = DateTime.UtcNow;