Our plans and pricing are changing on 1st November. Learn more...

Automated Trading Trailing Stop Order


side, amount, offset, limitOffset, reduceOnly, trigger, trailingMethod, stepSize, maxSteps, position, tag, background

Place a trailing stop market order.

When the command first executes, it will place a stop market order at a distance of offset from the current price ('offset' below the current price for sell order, and 'offset' above the current price for a buy order).

In the background, the command will then monitor the price. For a sell stop market order, the order will move to a higher price each time the price reaches a new high, always keeping the order 'offset' away from the local high.

For a buy order, the opposite happens, tracking new lows. When the price turns and drops to meet your stop market order, it will be triggered and filled and this command will stop.

side required text, one of buy, sell. defaults to buy

Is this a buy or sell order.
Required if you don't use position.

amount required number, defaults to 0

The size of the order. The units depend on the symbol and exchange. Can be given as an absolute amount, percentage of your balance or percentage of your available balance.

Example Meaning (if on BitMEX XBTUSD)
amount=1000 Uses an amount of 1000 contracts
amount=50% Will calculate the amount as a percentage of your balance/equity. For example, if your balance is 1 BTC and the current price is $10,000, 50% of 1 btc is 0.5 btc. 0.5 btc is worth $5,000. At $1 per contract, the order will be for 5,000 contracts.
amount=50%% Similar to above, only will use 50% of your available balance/equity. Equity already locked up in positions and pending orders will not be considered.
amount=50%p Will work out 50% of your position size and use that as the amount. The p stands for Position Size. This calculation rounds up to the nearest order size step.
amount=50%b Same as 50% - the b stands for Balance
amount=50%a Same as 50%% - the a stands for Available Balance

Percentage amounts are supported on all exchanges. On BitMEX they are supported on most (but not all) symbols. XBTUSD is supported and all symbols against XBT (eg ETHXBT), but not on symbols backed against other currencies (eg ETHUSD, XBTJPY etc can not use % amounts and must use fixed amounts - this is because we need live price information for additional symbols to complete the calculation, which are not tracked to by default).

Required if you don't use position.

offset required number, defaults to 0

How far behind the price should the stop trail the live price. For an asset quoted in USD (eg BTCUSD), offset can be given in the following ways:-

Example Meaning (assuming asset priced in USD)
offset=50 trail by $50 from the current price.
offset=1% offset is calculated as a percentage of the current price. If the price is $10,000, and offset is 1%, then 1% of 10,000 is 100. A trail of $100 will be used.
offset=@950 The @ at the start of the value indicates this should be treated as an absolute price of $950, regardless of what the current price is. The difference between the current price and this absolute price is then calculated and the trailing order will maintain this distance from the live price.
offset=e50 With an open position, offset will be relative to average entry. With no open position, offset will be relative to current price
offset=e1% With an open position, offset will be relative to average entry. With no open position, offset will be relative to current price
limitOffset optional number,

Place a stop limit order if this value is included, else place a stop market order.

When the stop order triggers it will place a limit order. This is the offset to the limit order price.

This value is calculated at the same time as offset is. Offsets relative to the current price and average entry will be evaluated at the time the stop order is created, and not when it is triggered. So setting offset and limitOffset to the same value will always create a limit order at the trigger price.

The available options for expressing this value (price, percentage offset, average entry offset and absolute price) are the same as for offset.

reduceOnly optional boolean, one of true, false. defaults to true

Attempt to submit the order as a reduce-only order. This is ignored in any spot markets / exchanges, as there is no concept of an open position there.

trigger optional string, one of index, mark, last. defaults to mark

Determine which price to trigger the stop order on, if supported. index represents the underlying index price, mark is the mark price and last is the last traded price. See exchange documentation for more details on how these values are derived.

trailingMethod optional string, one of none, continuous, stepped. defaults to continuous

Determines what method will be used to trail the price. It can be one of the following:-

Example Meaning (assuming asset priced in USD)
trailingMethod=none The stop order will not trail (may as well using a simple stopOrder() instead).
trailingMethod=continuous The order will move every time a new high or low price is reach.
trailingMethod=stepped The order will move only when the price has moved at least stepSize in the right direction. The order will only move in steps of stepSize. See also maxSteps.
stepSize optional offset, defaults to 0

When the order is trailing and trailingMethod is set to stepped, determines how far the order will be moved each time it is updated. The order will wait for the price to move at least stepSize before moving the order. For example, this makes it possible to set the stop for a long 1% below the price, then when the prices reaches 1% in profit, move the stop to break even. See also trailingMethod and maxSteps. The value is only used for the stepped trailing method and ignored for other methods. Can accept all offset style values (10, 10%, etc) - see offset for examples.

maxSteps optional integer, defaults to 0

When the order is trailing and trailingMethod is set to stepped, determines how many times the order can be moved. Once it has been moved maxSteps times, it will not update again and will act as a fixed stop order. See also trailingMethod and stepSize. The value is only used for the stepped trailing method and ignored for other methods.

position optional number,

An alternative way to calculate the side and amount for the order.

The amount traded will be the difference between your current open position and the target position size. For example, if your current position is 100 contracts long, and you request a target position of 300, then an order will be placed to buy 200 contracts (the difference between your current position and target position). In fact, regardless of your current position (long, short, nothing), the order will be whatever size it needs to be to ensure you finish with an open position of the target size. Some more examples...

Example What happens, if you current position is 500 contracts
position=1000 Buy 500 contracts, resulting in a position size of 1000
position=200 Sell 300 contracts, leaving you with 200.
position=0 Sell 500 contracts. This will effectively close your position, regardless of it's current size.
position=-500 Sell 1000 contracts, taking you from 500 contracts long to 500 contracts short.
position=50% Target position size will be 50% of your current balance size
position=50%% Target position size will be 50% of your available balance size
position=50%p Target position size will be 50% of your current position size
position=50%b Same as 50% - the b stands for Balance
position=50%a Same as 50%% - the a stands for Available Balance

On spot exchanges, like Bitfinex, the target amount represents the amount of the asset you'd like to end up with. For example, on the BTCUSD pair, position would represent the amount of BTC you'd like to own after the order completes.

Required if you don't use side and amount.

tag optional string, defaults to varies by order type

Applies a tag to an order that can later be used to cancel this order, even from a different session. Many orders can be given the same tag, making it possible to cancel them all as a group at a later date. These tags can be referenced by the cancel() command to cancel specific named orders. See cancel() with which=taggedfor more details.

background optional boolean, one of true, false. defaults to true

True to run this command in the background.

If true, this command won't wait and the bot will move on to the next command in the list immediately. When all the other commands are complete, this command will continue to work in the background until it is complete too. This is the recommended way of using this command.

If false, then the command will wait for the stop order to be filled before the next command is executed.

Example: Start a trailing stop

myApiKeys(XBTUSD) {
    trailingStop(side=sell, amount=1000, offset=100, trigger=last)

Opens a 1000 contracts long position of XBTUSD. Next, it places a stop market order $100 below our entry and starts to track the price. As the price moves up, the stop market order will also move up, trailing the high price by $100. Eventually, the price will turn and start to fall again, and when it falls $100 from it's high, the stop market order will be triggered and close the position.

Example: Start a trailing stop limit order

myApiKeys(XBTUSD) {
    trailingStop(side=sell, amount=1000, offset=e100, limitOffset=e95, trigger=last)

Places a trailing stop limit order. The stop order will be triggered if the price falls $100 below the current average entry price. When it is triggered, it will place a limit sell order that is $95 below the current average entry price. Note that the offset and limitOffset are converted to an absolute price at the time the order is created and are not re-evaluated when the stop is triggered.

As the order is being positioned relative to the average entry, the bot will also work out the distance from the current price to the triggering price. If the price goes up, then the triggering price and limit order price will also be adjusted upwards by the same amount, maintaining the same distance from high price to the order.

Some images from Unsplash

About Alertatron.com

Your TradingView alerts, with charts, PM'd to you.

Get all your alerts from TradingView, sent to Telegram, Discord, Slack, Email or webhooks, with a chart snapshot attached.

Capture Charts

See a high quality chart with every alert, captured at the moment the alert was triggered. See exactly what the market was doing and make informed decisions.

Automate Trading

Trigger orders on popular cryptocurrency exchanges and trade 24/7. Access powerful algorithmic orders to maximise your profits.


Connect your alerts to Telegram (channels, groups and private chats), Discord, Slack, email or send push notifications to other services with webhooks.

Easy To Setup

Nothing to install and no browser extensions. Captures alerts 24/7, even with your computer switched off. Be up and running in seconds.