神经医生交易机器人

EMA BB MACD RSI DCA
创建日期: 2025-10-20 15:48:05 最后修改: 2025-10-20 15:48:05
复制: 0 点击次数: 215
avatar of ianzeng123 ianzeng123
2
关注
329
关注者

神经医生交易机器人 神经医生交易机器人

这不是普通的DCA策略,而是一个会思考的交易机器人

看了上千个Pine Script代码,这个”Master Trading Bot”确实有两把刷子。作者把DCA(定投)玩出了新高度:不是无脑定投,而是基于技术指标的智能加仓系统。初始仓位5%,每次DCA增加2.5%,最高不超过100%。这种渐进式资金管理比传统等额定投更科学。

关键在于DCA触发条件:价格必须跌破平均成本价,且跌幅要达到2%+步数×4%的动态阈值。第一次DCA需要跌2%,第二次需要跌6%,第三次需要跌10%。这种设计避免了在小幅波动中频繁加仓,只在真正的回调中增持。

多重技术指标组合,但逻辑清晰不冗余

策略使用3/7/18周期EMA构建趋势框架,配合20周期布林带判断价格位置,52/200/3的MACD参数设置偏向中长期信号,14周期RSI用于超买超卖判断。这个组合覆盖了趋势、动量、波动性三个维度,比单一指标策略更可靠。

买入条件严格:快EMA>慢EMA+MACD金叉+价格在布林中轨上方+RSI<65。这四个条件同时满足才开仓,过滤掉了大部分假信号。卖出条件同样严谨:必须有最低2%利润+趋势转弱+MACD死叉。这种”有利润才卖”的设计避免了无意义的止损。

100%止损设置看似激进,实则合理

代码中100%的止损看起来很夸张,但注释说得很清楚:”价格必须跌到0才触发”。这实际上是关闭了传统止损,完全依靠技术指标和利润目标来管理风险。对于DCA策略来说,这种设计是合理的——既然要在下跌中加仓,传统止损就失去了意义。

真正的风险控制在于:2%价格下跌信号+动态DCA阈值+强制盈利退出。策略会追踪500周期内的最高价,一旦当前价格较最高点下跌超过2%就触发卖出信号。这比固定止损更灵活,能够适应不同的市场环境。

资金管理是这个策略的核心竞争力

每次买入金额=当前权益×DCA百分比÷当前价格,这种按权益比例而非固定金额的设计,让策略能够随着账户增长而扩大仓位。初始5%的仓位控制了单次风险,而渐进式加仓确保了在真正的机会面前有足够的火力。

最精妙的是”just_sold”状态管理:刚卖出后不会立即重新买入,除非出现强烈的看涨信号。这避免了在震荡市场中的频繁交易,降低了手续费成本和情绪化操作的风险。

适用场景明确,不是万能策略

这个策略最适合中长期上涨趋势中的回调买入,在熊市或长期横盘中表现会很一般。MACD的52/200参数设置决定了它更适合较大级别的趋势判断,不适合短线交易。

RSI超卖阈值设在25而非30,说明策略偏向在更深度的回调中买入。这种设计在牛市中能获得更好的买点,但在熊市中可能会”接飞刀”。建议在明确的上升趋势中使用,避免在市场顶部或下降趋势中启动。

回测表现需要关注最大回撤和连续亏损

策略的理论逻辑很完善,但实际表现还要看具体的回测数据。需要重点关注:最大回撤是否在可接受范围内,连续亏损次数是否过多,不同市场环境下的表现差异。

DCA策略的天然特点是会在下跌过程中持续加仓,这意味着账户净值会先下降再上升。投资者需要有足够的心理承受能力和资金储备。建议先在小资金上测试,确认策略特性后再逐步加大投资规模。

风险提示:任何量化策略都存在亏损风险,历史回测不代表未来收益,需要严格的风险管理和适当的资金配置。

策略源码
/*backtest
start: 2024-10-20 00:00:00
end: 2025-10-18 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT","balance":500000}]
*/

// This Pine Script™ code is subject to the terms of the MPL 2.0 at https://mozilla.org/MPL/2.0/
// © MTB by Neurodoc
// By Nicolás Astorga

//@version=5
strategy("Master Trading Bot by Neurodoc", 
         shorttitle="MTB Adaptation", 
         overlay=true, 
         initial_capital=10000, 
         pyramiding=100,
         commission_value=0.1, 
         commission_type=strategy.commission.percent,
         default_qty_type = strategy.cash)

// —————— CONFIGURATION (Based on ve.env) ——————
// Purchase and DCA Percentages
var GRP_DCA = "DCA Configuration"
start_percentage = input.float(5.0, "Initial Buy Percentage (%)", group=GRP_DCA)
increment_percentage = input.float(2.5, "Increment per DCA Buy (%)", group=GRP_DCA)
max_percentage = input.float(100.0, "Maximum Buy Percentage (%)", group=GRP_DCA)
min_profit_percent = input.float(2.0, "Minimum Profit for Sell (%)", group=GRP_DCA)

// Stop Loss and Drop Signal
var GRP_RISK = "Risk Management"
stop_loss_percent = input.float(100.0, "Stop Loss (%)", group=GRP_RISK, tooltip="A value of 100 means there’s no real stop loss, as price would have to go to 0.")
drop_percent_signal = input.float(2.0, "Price Drop for Sell Signal (%)", group=GRP_RISK)

// Indicator Parameters
var GRP_INDICATORS = "Indicator Parameters"
ema_fast_period = input.int(3, "Fast EMA", group=GRP_INDICATORS)
ema_mid_period = input.int(7, "Medium EMA", group=GRP_INDICATORS)
ema_slow_period = input.int(18, "Slow EMA", group=GRP_INDICATORS)
bb_length = input.int(20, "Bollinger Bands Length", group=GRP_INDICATORS)
bb_stddev = input.float(2.0, "BB Standard Deviation", group=GRP_INDICATORS)
macd_fast = input.int(52, "MACD Fast", group=GRP_INDICATORS)
macd_slow = input.int(200, "MACD Slow", group=GRP_INDICATORS)
macd_signal = input.int(3, "MACD Signal", group=GRP_INDICATORS)
rsi_length = input.int(14, "RSI Length", group=GRP_INDICATORS)
rsi_oversold_threshold = input.int(25, "RSI Oversold (for divergence)", group=GRP_INDICATORS)

// —————— INDICATOR CALCULATIONS ——————
// EMAs
ema_fast = ta.ema(open, ema_fast_period)
ema_mid = ta.ema(open, ema_mid_period)
ema_slow = ta.ema(open, ema_slow_period)

// Bollinger Bands
[bb_middle, bb_upper, bb_lower] = ta.bb(close, bb_length, bb_stddev)
bb_width = (bb_upper - bb_lower) / bb_middle * 100
is_bb_expanding = bb_width > bb_width[1]

// MACD
[macd_line, signal_line, _] = ta.macd(close, macd_fast, macd_slow, macd_signal)

// RSI
rsi = ta.rsi(close, rsi_length)

// Price drop signal from highest price (similar to `cummax` in Python)
highest_price = ta.highest(high, 500) // Using 500-bar lookback to approximate the high
price_drop_percent = ((highest_price - close) / highest_price) * 100
is_price_drop_signal = price_drop_percent >= drop_percent_signal

// —————— TRADING LOGIC ——————
// Trend Conditions
is_bullish = ema_fast > ema_slow and macd_line > signal_line and close > bb_middle
is_bearish = ema_fast < ema_slow and macd_line < signal_line and close < bb_middle
is_weakening = rsi < rsi[1]

// Variables to manage strategy state
var bool just_sold = false
var int dca_step = 0

// Determine next buy percentage of capital
dca_buy_percentage = start_percentage + (dca_step * increment_percentage)
if dca_buy_percentage > max_percentage
    dca_buy_percentage := max_percentage

avg_buy_price = strategy.position_avg_price

// Initial Long Condition
long_signal_initial = strategy.position_size == 0 and is_bullish and macd_line > signal_line and rsi < 65

// DCA Condition
price_drop_from_avg = ((avg_buy_price - close) / avg_buy_price) * 100
dca_required_drop = 2.0 + (dca_step * 4.0) // DCA price drop start and increment logic
long_signal_dca = strategy.position_size > 0 and is_bearish and close < avg_buy_price and price_drop_from_avg >= dca_required_drop

// Manage `just_sold` state
if strategy.position_size > 0
    just_sold := false
if strategy.position_size == 0 and strategy.position_size[1] > 0
    just_sold := true

// Avoid immediate repurchase after sell unless bullish condition is strong
long_signal = (just_sold and is_bullish) ? long_signal_initial : (not just_sold ? (long_signal_initial or long_signal_dca) : false)

// Sell (Close) Condition
current_profit_percent = ((close - avg_buy_price) / avg_buy_price) * 100
has_min_profit = current_profit_percent >= min_profit_percent
stop_loss_price = avg_buy_price * (1 - stop_loss_percent / 100)
is_stoploss_triggered = close <= stop_loss_price

short_signal = strategy.position_size > 0 and has_min_profit and ((is_bearish and is_weakening) or is_price_drop_signal or is_stoploss_triggered or (macd_line < signal_line))

// —————— ORDER EXECUTION ——————
if (long_signal)
    // Calculate how much MONEY (USDT) to invest in this trade
    cash_to_invest = (strategy.equity * dca_buy_percentage / 100) / close
    strategy.entry("Buy", strategy.long, qty=cash_to_invest)
    dca_step := dca_step + 1
        
if (short_signal)
    strategy.close_all(comment="Sell")
    dca_step := 0 // Reset DCA counter after selling

// —————— VISUALIZATION ——————
// Background color by trend
bgcolor(is_bullish ? color.new(color.green, 90) : is_bearish ? color.new(color.red, 90) : na)

// Plot EMAs and Bollinger Bands
plot(ema_fast, "Fast EMA", color.blue)
plot(ema_slow, "Slow EMA", color.orange)
p1 = plot(bb_upper, "Upper BB", color=color.gray)
p2 = plot(bb_lower, "Lower BB", color=color.gray)
fill(p1, p2, color=color.new(color.gray, 90))

// Plot average buy price when in position
plot(strategy.position_size > 0 ? avg_buy_price : na, "Average Buy Price", color.yellow, style=plot.style_linebr, linewidth=2)

// Plot Take Profit target
plot(strategy.position_size > 0 ? avg_buy_price * (1 + min_profit_percent / 100) : na, "Sell Price (TP)", color.aqua, style=plot.style_linebr, linewidth=2)

// Plot Stop Loss level
plot(strategy.position_size > 0 ? stop_loss_price : na, "Stop Loss", color.fuchsia, style=plot.style_linebr, linewidth=2)
相关推荐