Run DCA bot

This is a sample Pine Script and a corresponding alert message to run a DCA(Dollar-Cost Averaging) bot with the following conditions as an example.
- Execute BTC-USD Long order daily with 200 USD when close price < 20,000 until total position size is less than 1 BTC
- Execute close(short) order when daily close price > 24,000
All of the above values are parameterized and you can customize them.

- 1.Select market you want to run DCA bot and set arbitrary time-interval(the above example is 1D)
2. Open "Pine Editor" tab and paste the following script after deleting shown original script. Then click "Add to chart"
Pine Script
1
//@version=5
2
strategy("DCA bot", pyramiding = 999, default_qty_type=strategy.percent_of_equity, default_qty_value=1)
3
4
// Execute long order when close price is under this price
5
thresholdPrice = input(20000, "Entry Threshold Price")
6
ExitPrice = input(24000, "Exit Price")
7
orderSize = input(200, "Order Amount in USD")
8
maxPosition = input.float(1.00, "Max Position Size", 0.01, 100.00, 0.01)
9
startDate = timestamp(2022, 01, 01, 0, 0)
10
11
if startDate <= time
12
if close < thresholdPrice and strategy.position_size < maxPosition
13
strategy.entry("Long", strategy.long, (orderSize / close))
14
15
else if close > ExitPrice and strategy.position_size > 0
16
strategy.close_all("Close Position")
In the above script, We assume
- No remaining positions/orders in the target market running this bot.
- Enough amount of collateral is already in place to avoid liquidation.
3. Click Alert button and paste the following code into message field.
Alert message body
{
"exchange": "perpetual", // or replace with "dydx"
"strategy":"DCABot",
"market":"BTC_USD",
"size":"{{strategy.order.contracts}}",
"reverse":false,
"order":"{{strategy.order.action}}",
"position":"{{strategy.market_position}}",
"price":"{{strategy.order.price}}"
}
Last modified 3mo ago