Skip to content

Guides

Add stops, targets and a trailing stop

Every position should know where it will close before it opens.

Trading on margin can lose you more than you put in. Past results, including backtests, do not predict future results. Cloud9Trader provides tools and education, not financial advice. Read the full risk warning.

Stops and targets are sent with the order, so the broker closes the position even if your algorithm is busy or stopped.

// In pips from the opening price
openPosition("BUY", {
quantity: 10000,
stopLossPips: 20,
takeProfitPips: 40,
});
// As prices
openPosition("SELL", {
quantity: 10000,
stopLoss: ASK + 20 * PIP_SIZE,
takeProfit: ASK - 40 * PIP_SIZE,
});

Use one form or the other for each: stopLoss or stopLossPips, not both.

openPosition("BUY", {
quantity: 10000,
trailingStopPips: 25,
});

A trailing stop starts 25 pips below the price and moves up as the price rises. It never moves back down, so it locks in part of a gain.

  • A stop on the wrong side of the price, such as a buy’s stop above the bid. The order is refused and a warning is logged.
  • A stop closer than the spread. It would close the position at once.
  • A trailing stop no wider than the spread.

Refused orders are logged and the algorithm carries on.

Where do stops fill? A long position closes when the bid reaches the stop; a short one when the ask does. In backtests and on the demo account it fills exactly at the stop. A real broker can fill worse in a fast market.

Can I move a stop later? Not from the algorithm today. Close the position with closePosition instead.