private bool IsInNegativeArea(int index, Position position, LossStrategy strategy) { if (strategy == LossStrategy.FULL_CANDLE) { if (position.TradeType == TradeType.Buy) { return(Robot.MarketSeries.High[index] < position.EntryPrice); } return(Robot.MarketSeries.Low[index] > position.EntryPrice); } if (strategy == LossStrategy.CANDLE_BODY) { if (position.TradeType == TradeType.Buy) { return(Robot.MarketSeries.Open[index] < position.EntryPrice && Robot.MarketSeries.Close[index] < position.EntryPrice); } return(Robot.MarketSeries.Open[index] > position.EntryPrice && Robot.MarketSeries.Close[index] > position.EntryPrice); } return(false); }
private void ApplyNegativeProfitStrategy(Position position, LossStrategy strategy) { if (Params.CandlesInNegativeArea > 0) { int negativeBarsCount = 0; for (int i = positionStartIndex(position) + 1; i < lastBarIndex() - 1; i++) // check only for finished bars { if (IsInNegativeArea(i, position, strategy)) { negativeBarsCount++; } } Func <Position, double> negativeBreakOffset = p => (p.EntryPrice - p.StopLoss.Value) * Params.NegativeBreakEvenOffset; if (position.TradeType == TradeType.Sell) { negativeBreakOffset = p => (p.StopLoss.Value - p.EntryPrice) * -Params.NegativeBreakEvenOffset; } if (negativeBarsCount == Params.CandlesInNegativeArea) { logger.Info(String.Format("Moving Profit to Breakeven as {0}% of original Stop Loss. Reason: {1} candle(s) in negative area", Params.NegativeBreakEvenOffset, negativeBarsCount)); Robot.Print("Moving Profit to Breakeven as {0}% of original Stop Loss. Reason: {1} candle(s) in negative area", Params.NegativeBreakEvenOffset, negativeBarsCount); double newPrice = position.EntryPrice - negativeBreakOffset(position); TradeResult result = Robot.ModifyPosition(position, position.StopLoss, newPrice); if (result.IsSuccessful) { Robot.Print(result.Error); negativeBePositions.Add(position); } else { Robot.Print(result.Error.ToString()); } } } }