Join & EARN

FOREX ALGOS { }

Stop-Loss Logic

The mechanism for placing and adjusting stop-loss orders to limit losses. Stop-loss logic can be static (set once at entry) or dynamic (e.g. trailing stops, breakeven stops). In code, a stop loss is usually attached when placing an order or modified during the trade. Many platforms provide built-in methods: for instance, NinjaTrader’s SetStopLoss() call automatically submits a protective stop when the position opens.

  • Definition: Rules for where to set the stop-loss price. Examples: a fixed 50-pip stop, ATR-based stop, or moving the stop to break-even after X profit.

  • Context: In MQL, the stop-loss price is passed to OrderSend() (or modified with OrderModify()). In cTrader, it’s a parameter to ExecuteMarketOrder(). In NinjaScript, calling SetStopLoss() in Initialize() ensures the platform places the stop. The NinjaTrader guide explains that SetStopLoss() will “submit a stop … with a price calculated… offset from the position’s average entry price”.

  • Example (MQL4):

    double sl = Bid - 50*Point;
    OrderSend(Symbol(), OP_SELL, 0.1, Bid, 5, sl, 0, "Sell Order", 0, 0, clrRed);
  • Example (cAlgo):

    ExecuteMarketOrder(TradeType.Buy, SymbolName, 10000, "Bot", stopLossPips: 30);
  • Example (NinjaScript):

    // In Initialize():
    SetStopLoss(CalculationMode.Pips, 30);