Ejemplo n.º 1
0
        private bool TrySyncPosition(int pendingAdjustments)
        {
            var           positionDelta = desiredPosition - actualPosition;
            var           delta         = positionDelta - pendingAdjustments;
            PhysicalOrder physical;

            if (delta > 0)
            {
                physical = new PhysicalOrderDefault(OrderState.Active, symbol, OrderSide.Buy, OrderType.BuyMarket, 0, delta, 0, 0, null, null);
                TryCreateBrokerOrder(physical);
                return(true);
            }
            else if (delta < 0)
            {
                OrderSide side;
                if (actualPosition > 0 && desiredPosition < 0)
                {
                    side  = OrderSide.Sell;
                    delta = actualPosition;
                }
                else
                {
                    side = OrderSide.SellShort;
                }
                side     = actualPosition >= Math.Abs(delta) ? OrderSide.Sell : OrderSide.SellShort;
                physical = new PhysicalOrderDefault(OrderState.Active, symbol, side, OrderType.SellMarket, 0, Math.Abs(delta), 0, 0, null, null);
                TryCreateBrokerOrder(physical);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 2
0
 private void ProcessMatchPhysicalChangePriceAndSide(LogicalOrder logical, PhysicalOrder physical, int delta)
 {
     if (logical.Price.ToLong() != physical.Price.ToLong())
     {
         var origBrokerOrder = physical.BrokerOrder;
         physicalOrders.Remove(physical);
         var side = GetOrderSide(logical.Type);
         if (side == physical.Side)
         {
             physical = new PhysicalOrderDefault(OrderState.Active, symbol, logical, side, Math.Abs(delta));
             if (debug)
             {
                 log.Debug("(Price) Changing " + origBrokerOrder + " to " + physical);
             }
             TryChangeBrokerOrder(physical, origBrokerOrder);
         }
         else
         {
             if (debug)
             {
                 log.Debug("(Price) Canceling wrong side" + physical);
             }
             TryCancelBrokerOrder(physical);
         }
     }
     else
     {
         VerifySide(logical, physical);
     }
 }
Ejemplo n.º 3
0
 private void ProcessMissingPhysical(LogicalOrder logical, int size)
 {
     if (logical.StrategyPosition > 0)
     {
         if (logical.Type == OrderType.SellLimit ||
             logical.Type == OrderType.SellStop ||
             logical.Type == OrderType.SellMarket)
         {
             if (debug)
             {
                 log.Debug("ProcessMissingPhysical(" + logical + ")");
             }
             var side     = GetOrderSide(logical.Type);
             var physical = new PhysicalOrderDefault(OrderState.Active, symbol, logical, side, size);
             TryCreateBrokerOrder(physical);
         }
     }
     if (logical.StrategyPosition < 0)
     {
         if (logical.Type == OrderType.BuyLimit ||
             logical.Type == OrderType.BuyStop ||
             logical.Type == OrderType.BuyMarket)
         {
             if (debug)
             {
                 log.Debug("ProcessMissingPhysical(" + logical + ")");
             }
             var side     = GetOrderSide(logical.Type);
             var physical = new PhysicalOrderDefault(OrderState.Active, symbol, logical, side, size);
             TryCreateBrokerOrder(physical);
         }
     }
 }
Ejemplo n.º 4
0
        private void ProcessMissingPhysical(LogicalOrder logical)
        {
            switch (logical.TradeDirection)
            {
            case TradeDirection.Entry:
                if (debug)
                {
                    log.Debug("ProcessMissingPhysicalEntry(" + logical + ")");
                }
                var side     = GetOrderSide(logical.Type);
                var physical = new PhysicalOrderDefault(OrderState.Active, symbol, logical, side, logical.Position);
                TryCreateBrokerOrder(physical);
                break;

            case TradeDirection.Exit:
            case TradeDirection.ExitStrategy:
                var size = Math.Abs(logical.StrategyPosition);
                ProcessMissingPhysical(logical, size);
                break;

            case TradeDirection.Reverse:
                var logicalPosition =
                    logical.Type == OrderType.BuyLimit ||
                    logical.Type == OrderType.BuyMarket ||
                    logical.Type == OrderType.BuyStop ?
                    logical.Position : -logical.Position;
                size = Math.Abs(logicalPosition - logical.StrategyPosition);
                if (size != 0)
                {
                    ProcessMissingPhysical(logical, size);
                }
                break;

            case TradeDirection.Change:
                logicalPosition =
                    logical.Type == OrderType.BuyLimit ||
                    logical.Type == OrderType.BuyMarket ||
                    logical.Type == OrderType.BuyStop ?
                    logical.Position : -logical.Position;
                logicalPosition += logical.StrategyPosition;
                size             = Math.Abs(logicalPosition - logical.StrategyPosition);
                if (size != 0)
                {
                    if (debug)
                    {
                        log.Debug("ProcessMissingPhysical(" + logical + ")");
                    }
                    side     = GetOrderSide(logical.Type);
                    physical = new PhysicalOrderDefault(OrderState.Active, symbol, logical, side, size);
                    TryCreateBrokerOrder(physical);
                }
                break;

            default:
                throw new ApplicationException("Unknown trade direction: " + logical.TradeDirection);
            }
        }
Ejemplo n.º 5
0
        private void ProcessMatchPhysicalReverse(LogicalOrder logical, PhysicalOrder physical)
        {
            var strategyPosition = logical.StrategyPosition;
            var logicalPosition  =
                logical.Type == OrderType.BuyLimit ||
                logical.Type == OrderType.BuyMarket ||
                logical.Type == OrderType.BuyStop ?
                logical.Position : -logical.Position;
            var physicalPosition =
                physical.Side == OrderSide.Buy ?
                physical.Size : -physical.Size;
            var delta      = logicalPosition - strategyPosition;
            var difference = delta - physicalPosition;

            if (delta == 0 || strategyPosition == 0 ||
                strategyPosition > 0 && logicalPosition > 0 ||
                strategyPosition < 0 && logicalPosition < 0)
            {
                TryCancelBrokerOrder(physical);
            }
            else if (difference != 0)
            {
                var origBrokerOrder = physical.BrokerOrder;
                if (delta > 0)
                {
                    physical = new PhysicalOrderDefault(OrderState.Active, symbol, logical, OrderSide.Buy, Math.Abs(delta));
                    TryChangeBrokerOrder(physical, origBrokerOrder);
                }
                else
                {
                    OrderSide side;
                    if (strategyPosition > 0 && logicalPosition < 0)
                    {
                        side  = OrderSide.Sell;
                        delta = strategyPosition;
                        if (delta == physical.Size)
                        {
                            ProcessMatchPhysicalChangePriceAndSide(logical, physical, delta);
                            return;
                        }
                    }
                    else
                    {
                        side = OrderSide.SellShort;
                    }
                    side     = (long)strategyPosition >= (long)Math.Abs(delta) ? OrderSide.Sell : OrderSide.SellShort;
                    physical = new PhysicalOrderDefault(OrderState.Active, symbol, logical, side, Math.Abs(delta));
                    TryChangeBrokerOrder(physical, origBrokerOrder);
                }
            }
            else
            {
                ProcessMatchPhysicalChangePriceAndSide(logical, physical, delta);
            }
        }
Ejemplo n.º 6
0
        private void VerifySide(LogicalOrder logical, PhysicalOrder physical)
        {
            var side = GetOrderSide(logical.Type);

            if (physical.Side != side)
            {
                TryCancelBrokerOrder(physical);
                physical = new PhysicalOrderDefault(OrderState.Active, symbol, logical, side, physical.Size);
                TryCreateBrokerOrder(physical);
            }
        }
Ejemplo n.º 7
0
 private void ProcessMatchPhysicalReversePriceAndSide(LogicalOrder logical, PhysicalOrder physical, int delta)
 {
     if (logical.Price.ToLong() != physical.Price.ToLong())
     {
         var origBrokerOrder = physical.BrokerOrder;
         physicalOrders.Remove(physical);
         var side = GetOrderSide(logical.Type);
         physical = new PhysicalOrderDefault(OrderState.Active, symbol, logical, side, Math.Abs(delta));
         TryChangeBrokerOrder(physical, origBrokerOrder);
     }
     else
     {
         VerifySide(logical, physical);
     }
 }
Ejemplo n.º 8
0
        private void ProcessMatchPhysicalExitStrategy(LogicalOrder logical, PhysicalOrder physical)
        {
            var strategyPosition = logical.StrategyPosition;

            if (strategyPosition == 0)
            {
                TryCancelBrokerOrder(physical);
            }
            else if (Math.Abs(strategyPosition) != physical.Size || logical.Price.ToLong() != physical.Price.ToLong())
            {
                var origBrokerOrder = physical.BrokerOrder;
                physicalOrders.Remove(physical);
                var side = GetOrderSide(logical.Type);
                physical = new PhysicalOrderDefault(OrderState.Active, symbol, logical, side, Math.Abs(strategyPosition));
                TryChangeBrokerOrder(physical, origBrokerOrder);
            }
            else
            {
                VerifySide(logical, physical);
            }
        }
Ejemplo n.º 9
0
        private void ProcessMatchPhysicalChange(LogicalOrder logical, PhysicalOrder physical)
        {
            var strategyPosition = logical.StrategyPosition;
            var logicalPosition  =
                logical.Type == OrderType.BuyLimit ||
                logical.Type == OrderType.BuyMarket ||
                logical.Type == OrderType.BuyStop ?
                logical.Position : -logical.Position;

            logicalPosition += strategyPosition;
            var physicalPosition =
                physical.Side == OrderSide.Buy ?
                physical.Size : -physical.Size;
            var delta      = logicalPosition - strategyPosition;
            var difference = delta - physicalPosition;

            if (debug)
            {
                log.Debug("PhysicalChange(" + logical.SerialNumber + ") delta=" + delta + ", strategyPosition=" + strategyPosition + ", difference=" + difference);
            }
            if (delta == 0 || strategyPosition == 0)
            {
                if (debug)
                {
                    log.Debug("(Flat) Canceling: " + physical);
                }
                TryCancelBrokerOrder(physical);
            }
            else if (difference != 0)
            {
                var origBrokerOrder = physical.BrokerOrder;
                if (delta > 0)
                {
                    physical = new PhysicalOrderDefault(OrderState.Active, symbol, logical, OrderSide.Buy, Math.Abs(delta));
                    if (debug)
                    {
                        log.Debug("(Delta) Changing " + origBrokerOrder + " to " + physical);
                    }
                    TryChangeBrokerOrder(physical, origBrokerOrder);
                }
                else
                {
                    OrderSide side;
                    if (strategyPosition > 0 && logicalPosition < 0)
                    {
                        side  = OrderSide.Sell;
                        delta = strategyPosition;
                        if (delta == physical.Size)
                        {
                            if (debug)
                            {
                                log.Debug("Delta same as size: Check Price and Side.");
                            }
                            ProcessMatchPhysicalChangePriceAndSide(logical, physical, delta);
                            return;
                        }
                    }
                    else
                    {
                        side = OrderSide.SellShort;
                    }
                    side = (long)strategyPosition >= (long)Math.Abs(delta) ? OrderSide.Sell : OrderSide.SellShort;
                    if (side == physical.Side)
                    {
                        physical = new PhysicalOrderDefault(OrderState.Active, symbol, logical, side, Math.Abs(delta));
                        if (debug)
                        {
                            log.Debug("(Size) Changing " + origBrokerOrder + " to " + physical);
                        }
                        TryChangeBrokerOrder(physical, origBrokerOrder);
                    }
                    else
                    {
                        if (debug)
                        {
                            log.Debug("(Side) Canceling " + physical);
                        }
                        TryCancelBrokerOrder(physical);
                    }
                }
            }
            else
            {
                ProcessMatchPhysicalChangePriceAndSide(logical, physical, delta);
            }
        }
Ejemplo n.º 10
0
        private void ProcessMatchPhysicalEntry(LogicalOrder logical, PhysicalOrder physical)
        {
            log.Trace("ProcessMatchPhysicalEntry()");
            var strategyPosition = logical.StrategyPosition;
            var difference       = logical.Position - Math.Abs(strategyPosition);

            log.Trace("position difference = " + difference);
            if (difference == 0)
            {
                TryCancelBrokerOrder(physical);
            }
            else if (difference != physical.Size)
            {
                var origBrokerOrder = physical.BrokerOrder;
                if (strategyPosition == 0)
                {
                    physicalOrders.Remove(physical);
                    var side = GetOrderSide(logical.Type);
                    physical = new PhysicalOrderDefault(OrderState.Active, symbol, logical, side, difference);
                    TryChangeBrokerOrder(physical, origBrokerOrder);
                }
                else
                {
                    if (strategyPosition > 0)
                    {
                        if (logical.Type == OrderType.BuyStop || logical.Type == OrderType.BuyLimit)
                        {
                            physicalOrders.Remove(physical);
                            var side = GetOrderSide(logical.Type);
                            physical = new PhysicalOrderDefault(OrderState.Active, symbol, logical, side, difference);
                            TryChangeBrokerOrder(physical, origBrokerOrder);
                        }
                        else
                        {
                            TryCancelBrokerOrder(physical);
                        }
                    }
                    if (strategyPosition < 0)
                    {
                        if (logical.Type == OrderType.SellStop || logical.Type == OrderType.SellLimit)
                        {
                            physicalOrders.Remove(physical);
                            var side = GetOrderSide(logical.Type);
                            physical = new PhysicalOrderDefault(OrderState.Active, symbol, logical, side, difference);
                            TryChangeBrokerOrder(physical, origBrokerOrder);
                        }
                        else
                        {
                            TryCancelBrokerOrder(physical);
                        }
                    }
                }
            }
            else if (logical.Price.ToLong() != physical.Price.ToLong())
            {
                var origBrokerOrder = physical.BrokerOrder;
                physicalOrders.Remove(physical);
                var side = GetOrderSide(logical.Type);
                physical = new PhysicalOrderDefault(OrderState.Active, symbol, logical, side, difference);
                TryChangeBrokerOrder(physical, origBrokerOrder);
            }
            else
            {
                VerifySide(logical, physical);
            }
        }