Join & EARN

FOREX ALGOS { }

Exit Logic

The conditions under which an open position is closed. Exit logic can include profit targets, stop-losses, time-based exits, or opposite signals. In code, exit logic checks whether any closing condition is satisfied and then sends a close order. For example, a strategy might exit when profit exceeds a threshold or when an RSI falls below a level. As stated in Forex Strategy Builder documentation: “exit the market signals… are sent by the exit logic of the strategy”.

  • Definition: Rules specifying when to close an existing trade (taking profit, stopping loss, reversing signal, or time expiry).

  • Context: Exit logic produces exit orders (market/limit) when conditions occur. In many trading platforms, this is implemented by calling a close function (e.g. OrderClose() in MQL, or implicitly when using SetStopLoss()/SetProfitTarget() in NinjaScript).

  • Example (MQL4/5):

    if(PositionSelect(Symbol()) && PositionGetDouble(POSITION_PROFIT) >= 100.0)
    {
    // Close 100% of current position
    OrderClose(PositionGetInteger(POSITION_TICKET), PositionGetDouble(POSITION_VOLUME), Bid, 5);
    }
  • Example (cTrader cAlgo):

    // In OnTick(): if current position profit > $50, close it
    var pos = Positions.Find("MyRobot", SymbolName);
    if (pos != null && pos.GrossProfit >= 50 * Symbol.PipValue)
    ClosePosition(pos);
  • Example (NinjaScript):

    // Using strategy parameters StopLoss and ProfitTarget (ticks)
    SetStopLoss(CalculationMode.Ticks, StopLossTicks);
    SetProfitTarget(CalculationMode.Ticks, ProfitTargetTicks);
    // Once StopLoss or ProfitTarget is reached, NinjaTrader automatically exits the trade.

    (Or manually: ExitLong(); to close a long position.)