/// <summary> /// Break even procedure for positions in profit /// </summary> /// <param name="bot"></param> public static void BreakEven(this NewBot bot) { // attempt break-event only if a position is open if (bot.CanOpenPosition) { return; } // retrieve all open positions for this bot var openPositions = bot.Positions.Where(p => p.Label == bot.BotID); foreach (var position in openPositions) { // check if position is in profit if (position.NetProfit <= 0) { continue; } // handle buy positions if (position.TradeType == TradeType.Buy) { // check if position has already broken even if (position.StopLoss >= position.EntryPrice) { continue; } // break-even if price and entry-price difference is >= bot.TrailBy if (bot.Bid - position.EntryPrice >= bot.ConvertPipsToDecimal(bot.TrailBy)) { position.ModifyStopLossPrice(position.EntryPrice + bot.ConvertPipsToDecimal(2)); } } // handle sell positions if (position.TradeType == TradeType.Sell) { // check if position has already broken even if (position.StopLoss <= position.EntryPrice) { continue; } // break-even if price and entry-price difference is >= bot.TrailBy if (position.EntryPrice - bot.Ask >= bot.ConvertPipsToDecimal(bot.TrailBy)) { position.ModifyStopLossPrice(position.EntryPrice - bot.ConvertPipsToDecimal(2)); } } } }
/// <summary> /// Price Trailing procedure for positions in profit /// </summary> /// <param name="bot"></param> public static void TrailStop(this NewBot bot) { // attempt trail-stop only if a position is open if (bot.CanOpenPosition) { return; } // retrieve all open positions for this bot var openPositions = bot.Positions.Where(p => p.Label == bot.BotID).ToList(); foreach (var position in openPositions) { // handle buy positions trailing if (position.TradeType == TradeType.Buy) { // check if position has broken even if (position.StopLoss < position.EntryPrice) { continue; } // trail price by bot.TrailBy if (bot.Bid - position.StopLoss >= bot.ConvertPipsToDecimal(bot.TrailBy)) { if (bot.Bid - bot.ConvertPipsToDecimal(bot.TrailBy) > position.EntryPrice) { position.ModifyStopLossPrice(bot.Bid - bot.ConvertPipsToDecimal(bot.TrailBy)); } } } else if (position.TradeType == TradeType.Sell) { // check if position has broken even if (position.StopLoss > position.EntryPrice) { continue; } // trail price by bot.TrailBy if (position.StopLoss - bot.Ask >= bot.ConvertPipsToDecimal(bot.TrailBy)) { if (bot.Ask + bot.ConvertPipsToDecimal(bot.TrailBy) < position.EntryPrice) { position.ModifyStopLossPrice(bot.Ask + bot.ConvertPipsToDecimal(bot.TrailBy)); } } } } }