Advanced DCA Bot

fullmax · strategy · 72 行 · 点赞 65 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © fullmax
 
 
//@version=6
strategy('Advanced DCA Bot', overlay = true,pyramiding = 20)
 
// --- INPUTS ---
// Initial Entry Settings
rsi_length = input.int(14, 'RSI Length')
rsi_oversold = input.int(30, 'RSI Oversold Level')
 
// DCA Settings
base_qty = input.int(100, 'Start order qty in usdc-usdt', minval = 1)
dca_drop_pct = input.float(5.0, 'Price Drop to Trigger DCA (%)', minval = 0.1) / 100
dca_multiplier = input.float(1.5, 'DCA Multiplier (Size of next buy)', minval = 1.0)
max_dcas = input.int(5, 'Maximum DCA Steps', minval = 1)
 
// Take Profit Settings
tp_pct = input.float(3.0, 'Take Profit % from Average Price', minval = 0.1) / 100
 
// --- STATE VARIABLES ---
var float last_entry_price = na
var int dca_count = 0
var float current_base_qty = base_qty// Starting % of equity
 
// --- CALCULATIONS ---
rsi = ta.rsi(close, rsi_length)
avg_price = strategy.position_avg_price
 
// --- STRATEGY LOGIC ---
 
// 1. INITIAL ENTRY
// Enter the first trade when RSI is oversold and we have no open position
if ta.crossunder(rsi, rsi_oversold) and strategy.position_size == 0
    strategy.entry('Initial_Entry', strategy.long, qty = current_base_qty)
    last_entry_price := close
    dca_count := 0
    dca_count
 
// 2. DCA LOGIC
// If we are in a trade and the price drops by X% from the last entry
if strategy.position_size > 0 and dca_count < max_dcas
    float drop_threshold = last_entry_price * (1 - dca_drop_pct)
 
    if close <= drop_threshold
        dca_count := dca_count + 1
        // Calculate new quantity based on multiplier
        float next_qty = current_base_qty * math.pow(dca_multiplier, dca_count)
 
        strategy.entry('DCA_' + str.tostring(dca_count), strategy.long, qty = next_qty)
        last_entry_price := close
 
        // Log to chart for debugging
        label.new(bar_index, high, 'DCA #' + str.tostring(dca_count), color = color.orange, textcolor = color.white)
 
// 3. TAKE PROFIT LOGIC
// Exit everything when the current price is X% above the average entry price
if strategy.position_size > 0
    float tp_price = avg_price * (1 + tp_pct)
 
    if close >= tp_price
        strategy.close_all(comment = 'Take Profit Hit')
        // Reset variables for the next cycle
        dca_count := 0
        last_entry_price := na
        last_entry_price
 
// --- VISUALIZATION ---
plot(strategy.position_size > 0 ? avg_price : na, 'Average Price', color = color.white, style = plot.style_linebr, linewidth = 2)
plot(strategy.position_size > 0 ? avg_price * (1 + tp_pct) : na, 'Take Profit Level', color = color.green, style = plot.style_linebr, linewidth = 2)
plot(strategy.position_size > 0 ? last_entry_price * (1 - dca_drop_pct) : na, 'Next DCA Level', color = color.red, style = plot.style_linebr, linewidth = 1)

← 返回列表