Exemple #1
0
        private void SendCancelOrder(CreateOrChangeOrder order, bool resend)
        {
            var fixMsg = (FIXMessage4_2)(resend ? FixFactory.Create(order.Sequence) : FixFactory.Create());

            order.Sequence = fixMsg.Sequence;
            OrderStore.SetOrder(order);
            var newClientOrderId = order.BrokerOrder;

            fixMsg.SetOriginalClientOrderId(order.OriginalOrder.BrokerOrder.ToString());
            fixMsg.SetClientOrderId(newClientOrderId.ToString());
#if NOT_LIME
            fixMsg.SetAccount(AccountNumber);
            fixMsg.SetSide(GetOrderSide(order.OriginalOrder.Side));
            fixMsg.SetSymbol(order.Symbol.Symbol);
            fixMsg.SetTransactTime(TimeStamp.UtcNow);
#endif
            fixMsg.AddHeader("F");
            if (resend)
            {
                fixMsg.SetDuplicate(true);
            }
            SendMessage(fixMsg);
        }
Exemple #2
0
        private void OnCreateOrChangeBrokerOrder(CreateOrChangeOrder order, bool resend)
        {
            var fixMsg = (FIXMessage4_2)(resend ? FixFactory.Create(order.Sequence) : FixFactory.Create());

            order.Sequence = fixMsg.Sequence;
            OrderStore.SetOrder(order);
            OrderStore.SetSequences(RemoteSequence, FixFactory.LastSequence);

            if (order.Size > order.Symbol.MaxOrderSize)
            {
                throw new ApplicationException("Order was greater than MaxOrderSize of " + order.Symbol.MaxPositionSize + " for:\n" + order);
            }

            var orderHandler = GetAlgorithm(order.Symbol.BinaryIdentifier);
            var orderSize    = order.Type == OrderType.SellLimit || order.Type == OrderType.SellMarket || order.Type == OrderType.SellStop ? -order.Size : order.Size;

            if (Math.Abs(orderHandler.OrderAlgorithm.ActualPosition + orderSize) > order.Symbol.MaxPositionSize)
            {
                throw new ApplicationException("Order was greater than MaxPositionSize of " + order.Symbol.MaxPositionSize + " for:\n" + order);
            }

            if (debug)
            {
                log.Debug("Adding Order to open order list: " + order);
            }
            if (order.Action == OrderAction.Change)
            {
                var origBrokerOrder = order.OriginalOrder.BrokerOrder;
                fixMsg.SetClientOrderId(order.BrokerOrder.ToString());
                fixMsg.SetOriginalClientOrderId(origBrokerOrder.ToString());
                CreateOrChangeOrder origOrder;
                if (OrderStore.TryGetOrderById(origBrokerOrder, out origOrder))
                {
                    origOrder.ReplacedBy = order;
                    if (debug)
                    {
                        log.Debug("Setting replace property of " + origBrokerOrder + " to " + order.BrokerOrder);
                    }
                }
            }
            else
            {
                fixMsg.SetClientOrderId(order.BrokerOrder.ToString());
            }

            if (order.Action == OrderAction.Change)
            {
                fixMsg.AddHeader("G");
            }
            else
            {
                fixMsg.AddHeader("D");
                if (order.Symbol.Destination.ToLower() == "default")
                {
                    fixMsg.SetDestination(Destination);
                }
                else
                {
                    fixMsg.SetDestination(order.Symbol.Destination);
                }
            }
            fixMsg.SetSymbol(order.Symbol.Symbol);
            fixMsg.SetSide(order.Side == OrderSide.Buy ? 1 : 5);
            switch (order.Type)
            {
            case OrderType.BuyLimit:
                fixMsg.SetOrderType(2);
                fixMsg.SetPrice(order.Price);
                switch (order.Symbol.TimeInForce)
                {
                case TimeInForce.Day:
                    fixMsg.SetTimeInForce(0);
                    break;

                case TimeInForce.GTC:
                    throw new LimeException("Lime does not accept GTC Buy Lime Orders");

                default:
                    throw new ArgumentOutOfRangeException();
                }
                break;

            case OrderType.BuyMarket:
                fixMsg.SetOrderType(1);
                //fixMsg.SetTimeInForce(0);
                break;

            case OrderType.BuyStop:
                // throw new LimeException("Lime does not accept Buy Stop Orders");
                log.Error("Lime: Buy Stops not supproted");
                break;

            case OrderType.SellLimit:
                fixMsg.SetOrderType(2);
                fixMsg.SetPrice(order.Price);
                switch (order.Symbol.TimeInForce)
                {
                case TimeInForce.Day:
                    fixMsg.SetTimeInForce(0);
                    break;

                case TimeInForce.GTC:
                    throw new LimeException("Lime does not accept GTC Buy Lime Orders");

                default:
                    throw new ArgumentOutOfRangeException();
                }
                break;

            case OrderType.SellMarket:
                fixMsg.SetOrderType(1);
                //fixMsg.SetTimeInForce(0);
                break;

            case OrderType.SellStop:
                //throw new LimeException("Lime does not accept Sell Stop Orders");
                log.Error("Lime: Sell Stops not supproted");
                break;

            default:
                throw new LimeException("Unknown OrderType");
            }
            fixMsg.SetOrderQuantity((int)order.Size);
            if (order.Action == OrderAction.Change)
            {
                if (verbose)
                {
                    log.Verbose("Change order: \n" + fixMsg);
                }
            }
            else
            {
                if (verbose)
                {
                    log.Verbose("Create new order: \n" + fixMsg);
                }
            }
            if (resend)
            {
                fixMsg.SetDuplicate(true);
            }
#if NOT_LIME
            fixMsg.SetAccount(AccountNumber);
            fixMsg.SetHandlingInstructions(1);
            fixMsg.SetLocateRequired("N");
            fixMsg.SetTransactTime(order.UtcCreateTime);
            fixMsg.SetOrderCapacity("A");
            fixMsg.SetUserName();
#endif
            fixMsg.SetSendTime(order.UtcCreateTime);
            SendMessage(fixMsg);
        }