Beispiel #1
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);
     }
 }
Beispiel #2
0
 public void OnInitialize()
 {
     marketOrder = Factory.Engine.LogicalOrder(Strategy.Data.SymbolInfo, Strategy);
     marketOrder.TradeDirection = TradeDirection.ExitStrategy;
     marketOrder.Tag            = "ExitStrategy";
     Strategy.AddOrder(marketOrder);
     breakEvenBuyStopOrder = Factory.Engine.LogicalOrder(Strategy.Data.SymbolInfo, Strategy);
     breakEvenBuyStopOrder.TradeDirection = TradeDirection.ExitStrategy;
     breakEvenBuyStopOrder.Type           = OrderType.BuyStop;
     breakEvenBuyStopOrder.Tag            = "ExitStrategy";
     Strategy.AddOrder(breakEvenBuyStopOrder);
     breakEvenSellStopOrder = Factory.Engine.LogicalOrder(Strategy.Data.SymbolInfo, Strategy);
     breakEvenSellStopOrder.TradeDirection = TradeDirection.ExitStrategy;
     breakEvenSellStopOrder.Type           = OrderType.SellStop;
     breakEvenSellStopOrder.Tag            = "ExitStrategy";
     Strategy.AddOrder(breakEvenSellStopOrder);
     buyStopLossOrder = Factory.Engine.LogicalOrder(Strategy.Data.SymbolInfo, Strategy);
     buyStopLossOrder.TradeDirection = TradeDirection.ExitStrategy;
     buyStopLossOrder.Type           = OrderType.BuyStop;
     buyStopLossOrder.Tag            = "ExitStrategy";
     Strategy.AddOrder(buyStopLossOrder);
     sellStopLossOrder = Factory.Engine.LogicalOrder(Strategy.Data.SymbolInfo, Strategy);
     sellStopLossOrder.TradeDirection = TradeDirection.ExitStrategy;
     sellStopLossOrder.Type           = OrderType.SellStop;
     sellStopLossOrder.Tag            = "ExitStrategy";
     Strategy.AddOrder(sellStopLossOrder);
     if (IsTrace)
     {
         Log.Trace(Strategy.FullName + ".Initialize()");
     }
     Strategy.Drawing.Color = Color.Black;
 }
Beispiel #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);
         }
     }
 }
Beispiel #4
0
        private bool TryMatchTypeOnly(LogicalOrder logical, out PhysicalOrder physicalOrder)
        {
            double difference = logical.Position - Math.Abs(actualPosition);

            foreach (var physical in originalPhysicals)
            {
                if (logical.Type == physical.Type)
                {
                    if (logical.TradeDirection == TradeDirection.Entry)
                    {
                        if (difference != 0)
                        {
                            physicalOrder = physical;
                            return(true);
                        }
                    }
                    if (logical.TradeDirection == TradeDirection.Exit)
                    {
                        if (actualPosition != 0)
                        {
                            physicalOrder = physical;
                            return(true);
                        }
                    }
                }
            }
            physicalOrder = default(PhysicalOrder);
            return(false);
        }
 private void TryDrawTrade(LogicalOrder order, LogicalFill fill)
 {
     if (drawTrade != null && strategy != null && strategy.Performance.GraphTrades)
     {
         drawTrade(order, fill);
     }
 }
 private void TryDrawTrade(LogicalOrder order, double price, double position)
 {
     if (drawTrade != null && graphTrades == true)
     {
         drawTrade(order, price, position);
     }
 }
Beispiel #7
0
        public virtual void TestPositionSyncAndStopExits()
        {
            using (VerifyFeed verify = Factory.Utility.VerifyFeed(symbol))
                using (Agent provider = ProviderFactory()) {
                    verify.PauseSeconds = secondsDelay;
                    provider.SendEvent(new EventItem(verify, null, EventType.Connect, null));
                    provider.SendEvent(new EventItem(verify, symbol, EventType.StartSymbol, new StartSymbolDetail(TimeStamp.MinValue)));
                    VerifyConnected(verify);

                    ClearOrders(0);
                    ClearPosition(provider, verify, secondsDelay);
                    TickIO lastTick = verify.LastTick;
                    double bid      = lastTick.IsTrade ? lastTick.Price : lastTick.Bid;
                    double ask      = lastTick.IsTrade ? lastTick.Price : lastTick.Ask;

                    int strategyPosition = 0;
                    CreateExit(strategy, OrderType.SellStop, bid - 180 * symbol.MinimumTick, strategyPosition);
                    LogicalOrder exitBuyStop = CreateExit(strategy, OrderType.BuyStop, ask + 540 * symbol.MinimumTick, strategyPosition);
                    var          count       = verify.Verify(2, assertTick, secondsDelay, () => {
                        SendOrders(provider, verify, 0, secondsDelay);
                    });
                    Assert.GreaterOrEqual(count, 2, "tick count");

                    ClearOrders(0);
                    ClearPosition(provider, verify, secondsDelay);
                    var expectedTicks = 1;
                    count = verify.Wait(expectedTicks, secondsDelay);
                }
        }
Beispiel #8
0
        /// <summary>
        /// Draws a trade and annotateg with hover message if possible.
        /// </summary>
        /// <param name="order"></param>
        /// <param name="fillPrice"></param>
        /// <param name="resultingPosition"></param>
        /// <returns></returns>
        public int DrawTrade(LogicalOrder order, double fillPrice, double resultingPosition)
        {
            Color          color     = Color.Empty;
            ArrowDirection direction = ArrowDirection.Up;

            switch (order.Type)
            {
            case OrderType.BuyLimit:
            case OrderType.BuyStop:
            case OrderType.BuyMarket:
                color     = Color.Green;
                direction = ArrowDirection.Up;
                break;

            case OrderType.SellLimit:
            case OrderType.SellStop:
            case OrderType.SellMarket:
                color     = Color.Red;
                direction = ArrowDirection.Down;
                break;

            default:
                throw new ApplicationException("Unknown OrderType " + order.Type + " for drawing a trade.");
            }
            if (order.TradeDirection == TradeDirection.Exit ||
                order.TradeDirection == TradeDirection.ExitStrategy)
            {
                color = Color.Black;
            }
            // One ticket open on tickzoom is to draw arrows to scale based
            // on the price range of the chart. This numbers for size and position
            // were hard code, calibrated to Forex prices.
            ChartArrow chartArrow = CreateArrow(direction, color, 12.5f, ChartBars.BarCount, fillPrice);
            var        sb         = new StringBuilder();

            if (order.Tag != null)
            {
                sb.AppendLine(order.Tag.ToString());
            }
            sb.Append(order.TradeDirection);
            sb.Append(" ");
            sb.AppendLine(order.Type.ToString());
            if (order.Price > 0)
            {
                sb.Append("at ");
                sb.AppendLine(order.Price.ToString());
            }
            sb.Append("size ");
            sb.AppendLine(order.Position.ToString());
            sb.Append("filled ");
            sb.AppendLine(fillPrice.ToString());
            sb.Append("new positions ");
            sb.AppendLine(resultingPosition.ToString());
            chartArrow.Tag = sb.ToString();
            objectId++;
            graphObjs.Add(objectId, chartArrow);
            return(objectId);
        }
Beispiel #9
0
 public void AddOrder(LogicalOrder order)
 {
     if (OrderManager != null)
     {
         OrderManager.AddOrder(order);
     }
     allOrders.AddLast(order);
     ordersHash.Add(order.Id, order);
 }
Beispiel #10
0
 public void OrderModified(LogicalOrder order)
 {
     if (order.IsActive)
     {
         // Any change to an active order even if only
         // a price change means the list change.
         IsActiveOrdersChanged = true;
         if (!activeOrders.Contains(order))
         {
             var  newNode = nodePool.Create(order);
             bool found   = false;
             var  next    = activeOrders.First;
             for (var node = next; node != null; node = next)
             {
                 next = node.Next;
                 LogicalOrder other = node.Value;
                 if (order.CompareTo(other) < 0)
                 {
                     activeOrders.AddBefore(node, newNode);
                     found = true;
                     break;
                 }
             }
             if (!found)
             {
                 activeOrders.AddLast(newNode);
             }
         }
     }
     else
     {
         var node = activeOrders.Find(order);
         if (node != null)
         {
             activeOrders.Remove(node);
             nodePool.Free(node);
             // Since this order became inactive, it
             // means the active list changed.
             IsActiveOrdersChanged = true;
         }
     }
     if (order.IsNextBar)
     {
         if (!nextBarOrders.Contains(order))
         {
             nextBarOrders.Add(order);
         }
     }
     else
     {
         if (nextBarOrders.Contains(order))
         {
             nextBarOrders.Remove(order);
         }
     }
     ActiveOrdersChanged(order);
 }
Beispiel #11
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);
            }
        }
        public void CanDecreasePriority()
        {
            var x = new LogicalOrder(100);
            x = x.PushBackBy(50);

            var y = new LogicalOrder(100);

            Assert.IsTrue(x < y);
        }
        public void CanIncreasePriority()
        {
            var x = new LogicalOrder(100);
            x = x.BringForwardBy(50);

            var y = new LogicalOrder(100);

            Assert.IsTrue(x > y);
        }
Beispiel #14
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);
            }
        }
Beispiel #15
0
        private void UpdateOrderCache(LogicalOrder order, LogicalFill fill)
        {
            var strategyPosition = (StrategyPosition)order.Strategy;

            if (debug)
            {
                log.Debug("Adjusting strategy position to " + order.Position + ", strategy position was " + strategyPosition.Position);
            }
            strategyPosition.Position = fill.Position;
//			orderCache.RemoveInactive(order);
        }
Beispiel #16
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);
            }
        }
Beispiel #17
0
        private bool PlaceOrder(LimeFIXProvider target, SymbolInfo symbol, OrderType type,
                                int size, double price, out LogicalOrder logicalOrder, out CreateOrChangeOrderDefault priceOrder)
        {
            logicalOrder = Factory.Engine.LogicalOrder(symbol);
            logicalOrder.TradeDirection = TradeDirection.Entry;
            logicalOrder.Type           = type;
            logicalOrder.Price          = price;
            logicalOrder.Position       = size;

            priceOrder = new CreateOrChangeOrderDefault(OrderAction.Create, symbol, logicalOrder, OrderSide.Buy, logicalOrder.Position, logicalOrder.Price);

            return(target.OnCreateBrokerOrder(priceOrder));
        }
Beispiel #18
0
 private bool TryMatchId(LogicalOrder logical, out PhysicalOrder physicalOrder)
 {
     foreach (var physical in originalPhysicals)
     {
         if (logical.Id == physical.LogicalOrderId)
         {
             physicalOrder = physical;
             return(true);
         }
     }
     physicalOrder = default(PhysicalOrder);
     return(false);
 }
 public PhysicalOrderDefault(OrderState orderState, SymbolInfo symbol, LogicalOrder logical, OrderSide side, int size)
 {
     this.orderState          = orderState;
     this.symbol              = symbol;
     this.side                = side;
     this.type                = logical.Type;
     this.price               = logical.Price;
     this.size                = size;
     this.logicalOrderId      = logical.Id;
     this.logicalSerialNumber = logical.SerialNumber;
     this.tag         = logical.Tag;
     this.reference   = null;
     this.replace     = null;
     this.brokerOrder = CreateBrokerOrderId(logicalOrderId);
 }
Beispiel #20
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);
     }
 }
        public LogicalOrder CreateOrder(StrategyInterface strategy, TradeDirection tradeDirection, OrderType orderType, double price, int position, int strategyPosition)
        {
            LogicalOrder order = Factory.Engine.LogicalOrder(symbol, strategy);

            order.StrategyId       = 1;
            order.StrategyPosition = strategyPosition;
            order.TradeDirection   = tradeDirection;
            order.Type             = orderType;
            order.Price            = price;
            order.Position         = position * lotSize;
            order.Status           = OrderStatus.Active;
            orders.AddLast(order);
            strategy.Position.Change(strategyPosition, 100.00, TimeStamp.UtcNow);
            return(order);
        }
        public void ProcessFill(StrategyInterface strategyInterface, LogicalFill fill)
        {
            if (debug)
            {
                Log.Debug("ProcessFill: " + fill + " for strategy " + strategyInterface);
            }
            var          strategy    = (Strategy)strategyInterface;
            int          orderId     = fill.OrderId;
            LogicalOrder filledOrder = null;

            if (strategyInterface.TryGetOrderById(fill.OrderId, out filledOrder))
            {
                if (debug)
                {
                    Log.Debug("Matched fill with orderId: " + orderId);
                }
                if (!doStrategyOrders && filledOrder.TradeDirection != TradeDirection.ExitStrategy)
                {
                    if (debug)
                    {
                        Log.Debug("Skipping fill, strategy order fills disabled.");
                    }
                    return;
                }
                if (!doExitStrategyOrders && filledOrder.TradeDirection == TradeDirection.ExitStrategy)
                {
                    if (debug)
                    {
                        Log.Debug("Skipping fill, exit strategy orders fills disabled.");
                    }
                    return;
                }
                TryDrawTrade(filledOrder, fill.Price, fill.Position);
                if (debug)
                {
                    Log.Debug("Changed strategy position to " + fill.Position + " because of fill.");
                }
                changePosition(strategy.Data.SymbolInfo, fill);
            }
            else
            {
                throw new ApplicationException("A fill for order id: " + orderId + " was incorrectly routed to: " + strategyInterface.Name);
            }
        }
Beispiel #23
0
        public void TestLogicalStopOrders()
        {
            using (VerifyFeed verify = Factory.Utility.VerifyFeed())
                using (Provider provider = ProviderFactory()) {
                    verify.PauseSeconds = secondsDelay;
                    provider.SendEvent(verify, null, (int)EventType.Connect, null);
                    provider.SendEvent(verify, symbol, (int)EventType.StartSymbol, new StartSymbolDetail(TimeStamp.MinValue));
                    VerifyConnected(verify);

                    ClearOrders(0);
                    ClearPosition(provider, verify, secondsDelay);
//				var expectedTicks = 1;
//	            var count = verify.Wait(symbol,expectedTicks,secondsDelay);
//	            Assert.GreaterOrEqual(count,expectedTicks,"at least one tick");
                    TickIO lastTick = verify.LastTick;
                    double bid      = lastTick.IsTrade ? lastTick.Price : lastTick.Bid;
                    double ask      = lastTick.IsTrade ? lastTick.Price : lastTick.Ask;

                    int          strategyPosition = 0;
                    LogicalOrder enterBuyStop     = CreateEntry(strategy, OrderType.BuyStop, bid + 420 * symbol.MinimumTick, 2, strategyPosition);
                    LogicalOrder enterSellStop    = CreateEntry(strategy, OrderType.SellStop, bid - 400 * symbol.MinimumTick, 2, strategyPosition);
                    CreateExit(strategy, OrderType.SellStop, bid - 180 * symbol.MinimumTick, strategyPosition);
                    LogicalOrder exitBuyStop = CreateExit(strategy, OrderType.BuyStop, ask + 540 * symbol.MinimumTick, strategyPosition);
                    var          count       = verify.Verify(2, assertTick, symbol, secondsDelay, () => {
                        SendOrders(provider, verify, 0, secondsDelay);
                    });
                    Assert.GreaterOrEqual(count, 2, "tick count");

                    ClearOrders(0);
                    enterSellStop.Price = bid - 360 * symbol.MinimumTick;
                    enterBuyStop.Price  = ask + 380 * symbol.MinimumTick;
                    orders.AddLast(enterBuyStop);
                    orders.AddLast(enterSellStop);
                    orders.AddLast(exitBuyStop);
                    count = verify.Verify(2, assertTick, symbol, secondsDelay, () => {
                        SendOrders(provider, verify, 0, secondsDelay);
                    });
                    Assert.GreaterOrEqual(count, 2, "tick count");

//				ClearOrders(0);
//				ClearPosition(provider,verify,secondsDelay);
                }
        }
Beispiel #24
0
        public void TestLogicalLimitOrders()
        {
            using (VerifyFeed verify = Factory.Utility.VerifyFeed(symbol))
                using (Agent provider = ProviderFactory()) {
                    verify.PauseSeconds = secondsDelay;
                    provider.SendEvent(new EventItem(verify, null, EventType.Connect, null));
                    provider.SendEvent(new EventItem(verify, symbol, EventType.StartSymbol, new StartSymbolDetail(TimeStamp.MinValue)));
                    VerifyConnected(verify);

                    ClearOrders(0);
                    ClearPosition(provider, verify, secondsDelay);
                    TickIO       lastTick         = verify.LastTick;
                    double       bid              = lastTick.IsTrade ? lastTick.Price : lastTick.Bid;
                    double       ask              = lastTick.IsTrade ? lastTick.Price : lastTick.Ask;
                    int          strategyPosition = 0;
                    LogicalOrder enterBuyLimit    = CreateEntry(strategy, OrderType.BuyLimit, bid - 280 * symbol.MinimumTick, 2, strategyPosition);
                    LogicalOrder enterSellLimit   = CreateEntry(strategy, OrderType.SellLimit, ask + 340 * symbol.MinimumTick, 2, strategyPosition);
                    LogicalOrder exitSellLimit    = CreateExit(strategy, OrderType.SellLimit, ask + 380 * symbol.MinimumTick, strategyPosition);
                    CreateExit(strategy, OrderType.SellLimit, ask + 400 * symbol.MinimumTick, strategyPosition);
                    CreateExit(strategy, OrderType.BuyLimit, bid - 150 * symbol.MinimumTick, strategyPosition);
                    LogicalOrder exitBuyStop = CreateExit(strategy, OrderType.BuyStop, ask + 540 * symbol.MinimumTick, strategyPosition);
                    var          count       = verify.Verify(2, assertTick, secondsDelay, () => {
                        SendOrders(provider, verify, 0, secondsDelay);
                    });
                    Assert.GreaterOrEqual(count, 2, "tick count");

                    ClearOrders(0);
                    enterBuyLimit.Price  = bid - 260 * symbol.MinimumTick;
                    enterSellLimit.Price = ask + 280 * symbol.MinimumTick;
                    orders.AddLast(enterBuyLimit);
                    orders.AddLast(enterSellLimit);
                    orders.AddLast(exitSellLimit);
                    count = verify.Verify(2, assertTick, secondsDelay, () => {
                        SendOrders(provider, verify, 0, secondsDelay);
                    });
                    Assert.GreaterOrEqual(count, 2, "tick count");

                    ClearOrders(0);
                    //ClearPosition(provider,verify,secondsDelay);
                    count = verify.Wait(1, secondsDelay);
                }
        }
Beispiel #25
0
 private void ActiveOrdersChanged(LogicalOrder order)
 {
     if (trace)
     {
         StringBuilder sb = new StringBuilder();
         sb.AppendLine("Active Orders:");
         foreach (var item in activeOrders)
         {
             sb.Append("        ");
             sb.AppendLine(item.ToString());
         }
         sb.AppendLine("NextBar Orders:");
         foreach (var item in nextBarOrders)
         {
             sb.Append("        ");
             sb.AppendLine(item.ToString());
         }
         log.Trace("Order #" + order.Id + " was modified while position = " + position.Current + "\n" + sb);
         sb.AppendLine();
     }
 }
 public CreateOrChangeOrderDefault(OrderState orderState, SymbolInfo symbol, LogicalOrder logical, OrderSide side, int size, double price)
 {
     binary.action              = OrderAction.Create;
     OrderState                 = orderState;
     binary.lastModifyTime      = Factory.Parallel.UtcNow;
     binary.symbol              = symbol;
     binary.side                = side;
     binary.type                = logical.Type;
     binary.price               = price;
     binary.size                = size;
     binary.logicalOrderId      = logical.Id;
     binary.logicalSerialNumber = logical.SerialNumber;
     binary.tag                 = logical.Tag;
     binary.reference           = null;
     binary.replacedBy          = null;
     binary.originalOrder       = null;
     binary.brokerOrder         = CreateBrokerOrderId();
     binary.utcCreateTime       = logical.UtcChangeTime;
     binary.orderFlags          = logical.OrderFlags;
     instanceId                 = ++nextInstanceId;
 }
Beispiel #27
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);
            }
        }
Beispiel #28
0
        private void ProcessMatch(LogicalOrder logical, PhysicalOrder physical)
        {
            if (trace)
            {
                log.Trace("Process Match()");
            }
            if (physical.OrderState == OrderState.Suspended)
            {
                if (debug)
                {
                    log.Trace("Cannot change a suspended order: " + physical);
                }
                return;
            }
            switch (logical.TradeDirection)
            {
            case TradeDirection.Entry:
                ProcessMatchPhysicalEntry(logical, physical);
                break;

            case TradeDirection.Exit:
                ProcessMatchPhysicalExit(logical, physical);
                break;

            case TradeDirection.ExitStrategy:
                ProcessMatchPhysicalExitStrategy(logical, physical);
                break;

            case TradeDirection.Reverse:
                ProcessMatchPhysicalReverse(logical, physical);
                break;

            case TradeDirection.Change:
                ProcessMatchPhysicalChange(logical, physical);
                break;

            default:
                throw new ApplicationException("Unknown TradeDirection: " + logical.TradeDirection);
            }
        }
Beispiel #29
0
        private void ProcessExtraLogical(LogicalOrder logical)
        {
            // When flat, allow entry orders.
            switch (logical.TradeDirection)
            {
            case TradeDirection.Entry:
                if (logical.StrategyPosition == 0)
                {
                    ProcessMissingPhysical(logical);
                }
                break;

            case TradeDirection.Exit:
            case TradeDirection.ExitStrategy:
                if (logical.StrategyPosition != 0)
                {
                    ProcessMissingPhysical(logical);
                }
                break;

            case TradeDirection.Reverse:
                if (logical.StrategyPosition != 0)
                {
                    ProcessMissingPhysical(logical);
                }
                break;

            case TradeDirection.Change:
                if (logical.StrategyPosition != 0)
                {
                    ProcessMissingPhysical(logical);
                }
                break;

            default:
                throw new ApplicationException("Unknown trade direction: " + logical.TradeDirection);
            }
        }
Beispiel #30
0
        private void flattenSignal(LogicalOrder order, Tick tick)
        {
// Actual fills of exit strategy orders now handles via the OrderManager interceptor.
//            if (Strategy.Performance.GraphTrades)
//            {
//				double fillPrice = 0;
//				if( position.IsLong) {
//					order.Positions = context.Position.Size;
//					fillPrice = tick.Bid;
//				}
//				if( position.IsShort) {
//					order.Positions = context.Position.Size;
//					fillPrice = tick.Ask;
//				}
//                Strategy.Chart.DrawTrade(order, fillPrice, 0);
//            }
//            position.Change(0);
//			CancelOrders();
//			if( controlStrategy) {
//				Strategy.Orders.Exit.ActiveNow.GoFlat();
//				strategySignal = 0;
//			}
        }
Beispiel #31
0
 private void ActiveOrdersChanged(LogicalOrder order)
 {
     if (trace)
     {
         StringBuilder sb = new StringBuilder();
         sb.AppendLine("Active Orders:");
         var next = activeOrders.First;
         for (var current = next; current != null; current = next)
         {
             next = current.Next;
             var item = current.Value;
             sb.Append("        ");
             sb.AppendLine(item.ToString());
         }
         sb.AppendLine("NextBar Orders:");
         foreach (var item in nextBarOrders)
         {
             sb.Append("        ");
             sb.AppendLine(item.ToString());
         }
         instanceLog.Trace("Order #" + order.Id + " was modified while position = " + position.Current + "\n" + sb);
         sb.AppendLine();
     }
 }
Beispiel #32
0
 public void AddOrder(LogicalOrder order)
 {
     throw new NotImplementedException();
 }