コード例 #1
0
        public double CalculateSlippageAdjustedPrice(PriceCalculatorInput input)
        {
            double effectiveSlippage = input.Strategy.GetEffectiveAllowedSlippage(input.CurrentBar);
            double slippageAmount = input.Atr * effectiveSlippage;
            double returnValue = input.CurrentBar.Open;

            if (input.OrderSide == OrderSide.Buy)
            {
                returnValue = returnValue + slippageAmount;
            }
            else
            {
                returnValue = returnValue - slippageAmount;
            }

            LoggingUtility.WriteInfo(
                logConfig,
                string.Format(
                    "Allowed slippage is {0}x ATR {1:c} ({2:p}) = {3:c} ({4:p}) on price of {5:c}, so slippage adjusted price is {6:c}",
                    effectiveSlippage,
                    input.Atr,
                    input.Atr/input.CurrentBar.Open,
                    slippageAmount,
                    slippageAmount/input.CurrentBar.Open,
                    input.CurrentBar.Open,
                    returnValue));

            return RoundPrice(returnValue, input.Instrument);
        }
コード例 #2
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="bar"></param>
        /// <returns></returns>
        protected double GetTargetPrice(Bar bar)
        {
            GapType gap = GapType.Allowed;
            double targetPrice = bar.Open;

            PriceCalculatorInput priceInput = new PriceCalculatorInput()
                                                  {
                                                      Strategy = this,
                                                      Atr = EffectiveAtrPrice,
                                                      CurrentBar = bar,
                                                      Instrument = Instrument,
                                                      OrderSide = EffectiveOrderSide
                                                  };

            PriceCalculator calc = new PriceCalculator(LoggingConfig);

            if (AccountForGaps)
            {
                double currentPrice = 0;
                double lastPrice = 0;

                gap = GetGapType(bar, ref currentPrice, ref lastPrice);

                double change = Math.Abs(currentPrice - lastPrice);

                if (gap != GapType.Allowed)
                {
                    LoggingUtility.WriteWarn(LoggingConfig, string.Format(
                        "Found a gap of {0:c} ({1:p}) which is {2} from previous price of {3} to new price of {4}",
                        change,
                        change/lastPrice,
                        gap,
                        lastPrice,
                        currentPrice));

                }

                switch (gap)
                {
                    case GapType.Allowed:
                        break;

                    case GapType.Favorable:
                        UpdateAllowedSlippages(FavorableGapAllowedSlippage, GapType.Favorable);
                        break;

                    case GapType.Unfavorable:
                        UpdateAllowedSlippages(UnfavorableGapAllowedSlippage, GapType.Unfavorable);
                        break;

                    default:

                        throw new InvalidOperationException("");
                }

                targetPrice = calc.CalculateSlippageAdjustedPrice(priceInput);

            }
            else
            {
                if (PriceCalculationStrategy == Enums.PriceCalculationStrategy.CurrentMarketPrice)
                    targetPrice = bar.Open;
                else
                    targetPrice = calc.CalculateSlippageAdjustedPrice(priceInput);
            }

            LoggingUtility.WriteDebug(LoggingConfig,
                                      string.Format("Calculated target price based on {0} strategy: {1:c}",
                                                    PriceCalculationStrategy, targetPrice));

            return targetPrice;
        }