/// <summary> /// /// </summary> /// <param name="bar"></param> protected override void HandleBarOpen(Bar bar) { if (IsItTimeToTrigger(bar, false)) { if (stopPrice.HasValue) { double lastClosePrice = bar.Close; if (StopPriceManager.IsEntryStopPriceMet(lastClosePrice, stopPrice.Value, OrderSide)) { LoggingUtility.LogCurrentBarArrival(LoggingConfig, bar); PriceCalculator priceCalc = new PriceCalculator(LoggingConfig); double targetPrice = priceCalc.CalculateSlippageAdjustedPrice(new PriceCalculatorInput() { AllowedSlippage = AllowedSlippage, Atr = GetAtrValue(Instrument, AtrPeriod, triggerTime.Value), CurrentBar = bar, PreviousBar = GetPreviousBar(Instrument, bar, PeriodConstants.PERIOD_MINUTE), OrderSide = OrderSide }); double targetQuantity = new QuantityCalculator(LoggingConfig).Calculate(new QuantityCalculatorInput() { MaxPortfolioRisk = MaxPortfolioRisk, MaxPositionRisk = MaxPositionRisk, NumberOfPositions = NumberOfPositions, PortfolioAmt = PortfolioAmount, PortfolioAllocationPercentage = PortfolioAllocationPercentage, PositionSizePercentage = PositionSizePercentage, RoundLots = RoundLots, StopPercentage = StopPercentage, TargetPrice = targetPrice }); 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, string.Format("Auto-Open Stop Order @ {0:c}", stopPrice.Value), Instrument.Symbol); strategyOrder = CreateOrder(OrderSide, targetQuantity, orderName, targetPrice); LoggingUtility.LogOrder(LoggingConfig, orderName, OrderSide, 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(); } } } }