/// <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)) { PriceCalculator priceCalc = new PriceCalculator(LoggingConfig); QuantityCalculator qtyCalc = new QuantityCalculator(LoggingConfig); double 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 }); double targetQuantity = qtyCalc.Calculate(new QuantityCalculatorInput() { NumberOfPositions = NumberOfPositions, PortfolioAmt = PortfolioAmount, PortfolioAllocationPercentage = PortfolioAllocationPercentage, MaxPortfolioRisk = MaxPortfolioRisk, MaxPositionRisk = MaxPositionRisk, RoundLots = RoundLots, TargetPrice = targetPrice, StopPercentage = StopPercentage, PositionSizePercentage = PositionSizePercentage }); 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, "Auto-Opened", Instrument.Symbol); strategyOrder = CreateOrder(OrderSide, targetQuantity, orderName, targetPrice); LoggingUtility.LogOrder(LoggingConfig, orderName, OrderSide, targetQuantity, targetPrice, retryCount); if (AutoSubmit) strategyOrder.Send(); } }
/// <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(); } } }
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)); } } }