private void ExecuteClosingOrder(long size)
        {
            if (OpenQuantity > 0 && closingOrderQueued)
            {
                OrderSide orderSide = OrderSide.Sell;
                double targetQuantity = 0;
                double targetPrice = 0;

                PriceCalculator priceCalc = new PriceCalculator(LoggingConfig);
                QuantityCalculator qtyCalc = new QuantityCalculator(LoggingConfig);

                targetPrice = priceCalc.Calculate(
                    new PriceCalculatorInput()
                        {
                            CurrentBar = closingInstrument.Bar,
                            PreviousBar = GetPreviousBar(closingInstrument, closingInstrument.Bar, PeriodConstants.PERIOD_MINUTE),
                            Atr = GetAtrValue(Instrument, AtrPeriod, triggerTime.Value),
                            AllowedSlippage = AllowedSlippage,
                            FavorableGap = FavorableGap,
                            FavorableGapAllowedSlippage = FavorableGapAllowedSlippage,
                            UnfavorableGap = UnfavorableGap,
                            UnfavorableGapAllowedSlippage = UnfavorableGapAllowedSlippage,
                            OrderSide = orderSide
                        });

                targetQuantity = qtyCalc.CalculatePositionSizedQuantity(OpenQuantity,
                                                                        new QuantityCalculatorInput()
                                                                            {
                                                                                PositionSizePercentage = PositionSizePercentage,
                                                                                RoundLots = RoundLots
                                                                            });

                targetPrice = priceCalc.RoundPrice(targetPrice, closingInstrument);

                if (targetPrice <= 0 || targetQuantity <= 0)
                    throw new ApplicationException(
                        string.Format("Invalid price of quantity calculated. Price {0:c}, Qty {1}", targetPrice,
                                      targetQuantity));

                string orderName = GetAutoPlacedOrderName(orderSide, "FlipFlop-Closed", closingInstrument.Symbol);

                closingOrder = CreateOrder(orderSide, targetQuantity, orderName, targetPrice);

                LoggingUtility.LogOrder(LoggingConfig, orderName, orderSide, targetQuantity, targetPrice, retryCount);

                if (AutoSubmit)
                    closingOrder.Send();

                if (!AmountIncludesOpenPosition)
                {
                    double proceeds = (targetPrice * targetQuantity);
                    PortfolioAmount = PortfolioAmount + proceeds;
                    LoggingUtility.WriteInfo(LoggingConfig,
                                             string.Format(
                                                 "The sale of {0} for {1:c} have been added to portfolio. New total = {2:c} ",
                                                 closingInstrument.Symbol,
                                                 proceeds, PortfolioAmount));
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="bar"></param>
        protected virtual void HandleBarOpen(Bar bar)
        {
            if (RetryOrder(bar, HandleStrategyStart))
            {
                LoggingUtility.LogRetryOrder(LoggingConfig, bar, retryCount);
            }

            if (IsItTimeToTrigger(bar,  true))
            {

                OrderSide? orderSide = null;
                double targetQuantity = 0;
                double targetPrice = 0;

                PriceCalculator priceCalc = new PriceCalculator(LoggingConfig);
                QuantityCalculator qtyCalc = new QuantityCalculator(LoggingConfig);

                if (longPositionQuantity.HasValue && longPositionQuantity.Value > 0)
                {
                    orderSide = OrderSide.Sell;
                    targetPrice = priceCalc.Calculate(
                        new PriceCalculatorInput()
                            {
                                CurrentBar = Bar,
                                PreviousBar = GetPreviousBar(Instrument, bar, PeriodConstants.PERIOD_MINUTE),
                                Atr = GetAtrValue(Instrument, AtrPeriod, triggerTime.Value),
                                AllowedSlippage = AllowedSlippage,
                                FavorableGap = FavorableGap,
                                FavorableGapAllowedSlippage = FavorableGapAllowedSlippage,
                                UnfavorableGap = UnfavorableGap,
                                UnfavorableGapAllowedSlippage = UnfavorableGapAllowedSlippage,
                                OrderSide = orderSide.Value
                            });

                    targetQuantity = qtyCalc.CalculatePositionSizedQuantity(longPositionQuantity.Value,
                                                                                       new QuantityCalculatorInput()
                                                                                           {
                                                                                               PositionSizePercentage = PositionSizePercentage,
                                                                                               RoundLots = RoundLots
                                                                                           });
                }

                if (shortPositionQuantity.HasValue && shortPositionQuantity.Value > 0)
                {
                    orderSide = OrderSide.Buy;
                    targetPrice = priceCalc.Calculate(
                        new PriceCalculatorInput()
                            {
                                CurrentBar = Bar,
                                PreviousBar = GetPreviousBar(Instrument, bar, PeriodConstants.PERIOD_MINUTE),
                                Atr = GetAtrValue(Instrument, AtrPeriod, triggerTime.Value),
                                AllowedSlippage = AllowedSlippage,
                                FavorableGap = FavorableGap,
                                FavorableGapAllowedSlippage = FavorableGapAllowedSlippage,
                                UnfavorableGap = UnfavorableGap,
                                UnfavorableGapAllowedSlippage = UnfavorableGapAllowedSlippage,
                                OrderSide = orderSide.Value
                            });

                    targetQuantity = qtyCalc.CalculatePositionSizedQuantity(shortPositionQuantity.Value,
                                                                                       new QuantityCalculatorInput()
                                                                                           {
                                                                                               PositionSizePercentage = PositionSizePercentage,
                                                                                               RoundLots = RoundLots
                                                                                           });
                }

                targetPrice = priceCalc.RoundPrice(targetPrice, Instrument);

                if (targetPrice <= 0 || targetQuantity <= 0)
                    throw new ApplicationException(string.Format("Invalid price of quantity calculated. Price {0:c}, Qty {1}", targetPrice, targetQuantity));

                if (orderSide.HasValue)
                {
                    string orderName = GetAutoPlacedOrderName(orderSide.Value, "Auto-Closed", Instrument.Symbol);

                    strategyOrder = CreateOrder(orderSide.Value, targetQuantity, orderName, targetPrice);

                    LoggingUtility.LogOrder(LoggingConfig, orderName, orderSide.Value, targetQuantity, targetPrice, retryCount);

                    if (AutoSubmit)
                        strategyOrder.Send();
                }
            }
        }
        protected override void HandleBarOpen(Bar bar)
        {
            if (IsItTimeToTrigger(bar, false))
            {
                if (stopPrice.HasValue)
                {
                    double lastClosePrice = bar.Close;
                    OrderSide? orderSide = null;
                    PositionSide? positionSide = null;

                    double targetPrice = lastClosePrice;
                    double targetQuantity = 0;

                    if (longPositionQuantity.HasValue && longPositionQuantity.Value > 0)
                    {
                        orderSide = OrderSide.Sell;
                        positionSide = PositionSide.Long;
                    }

                    if (shortPositionQuantity.HasValue && shortPositionQuantity.Value > 0)
                    {
                        orderSide = OrderSide.Buy;
                        positionSide = PositionSide.Short;
                    }

                    if (StopPriceManager.IsExitStopPriceMet(lastClosePrice, stopPrice.Value, positionSide.Value))
                    {
                        LoggingUtility.LogCurrentBarArrival(LoggingConfig, bar);

                        PriceCalculator priceCalc = new PriceCalculator(LoggingConfig);
                        QuantityCalculator qtyCalc = new QuantityCalculator(LoggingConfig);

                        if (positionSide.Value == PositionSide.Long)
                        {
                            targetPrice = priceCalc.CalculateSlippageAdjustedPrice(
                                new PriceCalculatorInput()
                                    {
                                        AllowedSlippage = AllowedSlippage,
                                        Atr = GetAtrValue(Instrument, AtrPeriod, triggerTime.Value),
                                        CurrentBar = bar,
                                        OrderSide = orderSide.Value,
                                        PreviousBar = GetPreviousBar(Instrument, bar, PeriodConstants.PERIOD_MINUTE)
                                    });

                            targetQuantity = qtyCalc.CalculatePositionSizedQuantity(
                                longPositionQuantity.Value,
                                new QuantityCalculatorInput()
                                    {
                                        PositionSizePercentage = PositionSizePercentage,
                                        RoundLots = RoundLots
                                    });
                        }

                        if (positionSide.Value == PositionSide.Short)
                        {
                            targetPrice = priceCalc.CalculateSlippageAdjustedPrice(
                                new PriceCalculatorInput()
                                    {
                                        AllowedSlippage = AllowedSlippage,
                                        Atr = GetAtrValue(Instrument, AtrPeriod, triggerTime.Value),
                                        CurrentBar = bar,
                                        OrderSide = orderSide.Value,
                                        PreviousBar = GetPreviousBar(Instrument, bar, PeriodConstants.PERIOD_MINUTE)
                                    });

                            targetQuantity = qtyCalc.CalculatePositionSizedQuantity(
                                shortPositionQuantity.Value,
                                new QuantityCalculatorInput()
                                    {
                                        PositionSizePercentage = PositionSizePercentage,
                                        RoundLots = RoundLots
                                    });
                        }

                        targetPrice = priceCalc.RoundPrice(targetPrice, Instrument);

                        if (targetPrice <= 0 || targetQuantity <= 0)
                            throw new ApplicationException(string.Format("Invalid price of quantity calculated. Price {0:c}, Qty {1}", targetPrice, targetQuantity));

                        string orderName = GetAutoPlacedOrderName(orderSide.Value, string.Format("Auto-Close Stop Order @ {0:c}", stopPrice.Value), Instrument.Symbol);

                        strategyOrder = CreateOrder(orderSide.Value, targetQuantity, orderName, targetPrice);

                        LoggingUtility.LogOrder(LoggingConfig, orderName, orderSide.Value, targetQuantity, targetPrice, retryCount);

                        if (AutoSubmit)
                            strategyOrder.Send();
                    }

                }
            }
        }