Overview
Trailing stop orders are a type of order that adjusts the trigger price as the market price moves. Instead of setting a fixed trigger price, traders set a "trail value" as a percentage (e.g. 3.5%). On Binance.us, trailing stops are executed as limit orders. Not all exchanges support trailing stop orders, so it's important to check if this feature is available on your exchange.
When creating a trailing stop order, the trail value is used to calculate the starting limit order price. For instance, if an asset's price is currently $100 and a trader sets a sell stop "trail value" of 3.5%, the starting limit price will be $96.50 ($100 - ($100 * 3.5%)). As the asset's market price increases, the stop order will trail the market price by the specified trail value. Once the market price decreases by the same percentage, the stop order will be triggered and begin working against the order book at its limit price of $96.50. This means it will start filling the order from the current market price to the limit price of $96.50 with buyers that are within that range.
SELL Trailing Stop Order Example:
Suppose the market price of BTC-USD is currently $60,000.
A customer submits a sell trailing stop order with a trailing percentage of 2%.
The current limit price is set at $58,800, which is 2% below the market price.
If the market price rises to $62,000, the trigger price will rise to $60,760, which is 2% below the new high.
If the market price continues to fall, eventually the order will be triggered and start working against the order book at its limit price of $58,800. This means it will start filling the order from the current market price ($60,760) to the limit price of $58,800 with buyers that are within that range or better.
BUY Trailing Stop Order Example:
Suppose the market price of BTC-USD is currently $50,000.
A customer submits a buy trailing stop order with a trailing percentage of 3%.
The current limit price is set at $51,500, which is 3% above the market price.
If the market price falls to $48,000, the trigger price will fall to $49,440, which is 3% above the new low.
If the market price continues to rise, eventually the order will be triggered and start working against the order book at its limit price of $51,500. This means it will start filling the order from the current market price ($49,440) to the limit price of $51,500 with sellers that are within that range or better.
To clarify, in a sell trailing stop order, the limit price will remain the same and the trigger price will increase as the market price increases, but will not decrease. And in a buy trailing stop order, the limit price will remain the same and the trigger price will decrease as the market price decreases, but will not increase.
It's important to note that in a sell trailing stop order, the limit price will remain the same, and the trigger price will increase as the market price increases but not decrease. On the other hand, in a buy trailing stop order, the limit price will remain the same, and the trigger price will decrease as the market price decreases but not increase.
What is a Trigger Order?
Trigger Orders do not enter the order-book until the market price reaches a trigger price, at which point they are sent as orders on the market. The “order-type” parameter must be set to “trailing-stop” for Trailing Stop orders.
The Code
To begin, please ensure that your TradingView alert has the correct Webhook URL that contains the unique URL to your bot. Additionally, it's essential to read and understand the How to Create a TradingView Alert article before proceeding.
To get started with the integration, you can either use the Code Generator to generate the code or copy the code below and adjust the values manually. Click the "Copy" button below to copy the code and paste it into your TradingView alert message.
Create a Trailing Buy Stop trigger order that will buy BTC using 5% of my available USD balance at the market if the price rises by 3.5%
NOTE: The "current-price" parameter is required to calculate the correct trailing value in the quote currency using the trail-value percentage. In most cases, you will want to use the "{{close}}" TradingView variable to set the current price dynamically. This parameter is required with trailing-stop orders.
Create a Trailing Sell Stop trigger order that will sell 75% of my BTC balance at the market if the price drops by 3.5%
* trailing-stop not available on all exchanges.
One of amount, amount-percent-balance, or amount-percent-position is required. The "amount" is a decimal and represents the exact number of units, coins, or shares that you would like to buy or sell. (ie. buy "2.0" BTC).
(Optional with limit orders) The "price-value-tracker-id" can be specified instead of the "price" parameter if you want to dynamically set the price using the high, low, or average value from a value tracker.
(Required with price-value-tracker-id) "high" or "low" or "avg"; This tells the bot to take the high, low, or average of the specified value tracker's data set.
(Optional with limit orders) "true" or "false"; If you send a post only order, your order will not be allowed to take. So if it would provide it will be sent as a normal Limit Order, but if it would cross the book it will be canceled instead. See this article for more details.
(Optional with limit orders) "true" or "false"; Immediate or cancel (IOC) orders are the opposite of post only: they can only take. If you send an IOC that would not immediately trade, it will be canceled. See this article for more details.
(Optional) "true" or "false"; These orders can only close your positions and will only trade if it would decrease your position size. You can combine with any order type: limit, market, stop, trailing-stop, or take-profit. See this article for more details.
(Required with "stop" and "take-profit" order types, but not required if a Value Tracker is specified.) This is the price at which you want your stop or take profit to trigger. Example: "95.99"
(Required with trailing-stop orders) The "trail-value" should be a percentage (ie. "3.25%") of the current price.
(Required with trailing-stop orders on the FTX exchange) The "current-price" is needed to calculate the correct trailing value in the quote currency using the trail-value percentage. In most cases, you will want to use the "{{close}}" TradingView variable to set the current price dynamically like so: "current-price":"{{close}}"
(Optional with limit and market orders) "true" or "false". Include this if you want to add a stop loss trigger order to your limit or market order, in the exact same alert as your limit or market order.
(Required with "add-stop") Example: "95.99". The price at which you want to set your stop. Alternatively, you may use a value tracker to set your stop price. In this case, use "stop-value-tracker-id" instead.
(Required with "add-stop") The amount can be specified as a decimal (ie. "150.00") or a percentage (ie. "50.0%") of your position size.
(Optional with "add-stop". Also optional with "stop" order types, but only when using a Value Tracker with the stop.) "limit" or "market"; Will default to market if not specified.
(Optional with "add-stop") Example: "your unique id". The value tracker id that contains the high, low, or average value that you want to use to set your stop. Alternatively, you may use "stop-price" to explicitly set your stop price.
(Required with "stop-value-tracker-id") "high" or "low" or "avg"; This tells the bot to take the high, low, or average of the specified value tracker's data set.
(Optional with limit and market orders) "true" or "false". Include this if you want to add a take profit trigger order to your limit or market order, in the exact same alert as your limit or market order.
(Required with "add-take-profit") Example: "95.99". The price at which you want to set your take profit. Alternatively, you may use a value tracker to set your take profit price. In this case, use "tp-value-tracker-id" instead.
(Required with "add-take-profit") The amount can be specified as a decimal (ie. "150.00") or a percentage (ie. "50.0%") of your position size.
(Optional with "add-take-profit") "limit" or "market"; Will default to market if not specified.
(Optional with "add-take-profit") Example: "your unique id". The value tracker id that contains the high, low, or average value that you want to use to set your take profit. Alternatively, you may use "tp-price" to explicitly set your take profit price.
(Required with "tp-value-tracker-id") "high" or "low" or "avg"; This tells the bot to take the high, low, or average of the specified value tracker's data set.
(Optional when "add-take-profit" and "add-stop" are used together) A decimal value (ie. 2.5) that represents the risk-reward ratio that should be used to calculate the take profit price based on the stop price.
(Optional with trigger market orders) "true" or "false"; Retry all trigger market orders (stop, take-profit, and trailing-stop) until they get filled. See this article for more details.
* Not available on all exchanges.
(Optional) A unique identifier for a group of alerts. You can use any alphanumeric name that you like as long as it's 50 characters or less. Example: "strategy-b-group"
(Required with alert-group-id) The integer number which identifies a particular alert within a group of alerts. The value must be between 1 and 9 (a max of 9 alerts per group). Example: "2"
(Required with alert-group-id) The integer number which identifies the total number of alerts in a particular group of alerts. The value must be between 1 and 9 (a max of 9 alerts per group). Example: "3"
(Required with alert-group-id) The time window in which all alerts need to be received relative to each other. It can be specified in minutes, hours, days, or weeks. (ie. "15 minutes", "2 hours", "3 days", "1 week"...etc)
(Optional with alert-group-id) When included with an alert that is part of an alert group, this value is used to compare the current value in the alert to the value from the previous alert that came through with the same alert-number. This value can only contain one of the following: ">" (greater than) or "<" (less than) or ">=" (greater than or equal to) or "<=" (less than or equal to) or "==" (equal to).
(Required with alert-trigger-if) When using the alert-trigger-if feature, the alert-value parameter is required to capture a decimal value. This could be any value on your indicator or chart (ie. the price, a moving average, RSI value...etc) In most cases, you will use a TradingView variable here to dynamically set the value in the alert message (ie. "{{plot_3}}")
(Optional with alert-group-id) A list of alert numbers within an alert group that should have the logical OR operator applied. Each number should be separated by hyphens. Example: "2-4-5". Each alert that is part of the OR group should include the "or-group" parameter with the same value.
(Optional) A unique identifier or name for the Value Tracker Dataset. You can use any alphanumeric name that you like as long as it's 50 characters or less. Example: "BTC 1HR ATR Stop"
(Required with value-tracker-id) The numeric value that you would like to track as a dataset over the last X alerts or candles.
(Required with value-tracker-id) This is an integer number that represents the number of alerts or candles that you want to keep as a dataset to reference later from other alerts. For example, if you want to reference the swing low over the last 50 candles from other alerts, set track-last-x-alerts to 50 and configure your Value Tracker Alert to trigger on every candle close. The max number of alerts that you can track in one dataset is 100.
Checking the Order Status
Once your alert has been triggered, follow these steps:
Check the Alert Logs tab under My Account for any errors or additional information.
Log in to your exchange account and confirm that the order has been placed.