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.
Set them in pips or as prices
Section titled “Set them in pips or as prices”// In pips from the opening priceopenPosition("BUY", { quantity: 10000, stopLossPips: 20, takeProfitPips: 40,});
// As pricesopenPosition("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.
Add a trailing stop
Section titled “Add a trailing stop”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.
What gets refused
Section titled “What gets refused”- 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.
Common questions
Section titled “Common questions”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.