You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

长短仓Pine Script脚本合并后盈利因子异常下降问题求助

策略合并后盈利因子骤降问题

问题描述

单独运行空仓脚本时盈利因子约为2,多仓脚本盈利因子约1.5,但将两者合并为一个脚本后,盈利因子降至0.9。

测试环境

  • 交易平台:Kucoin,交易对BTCUSDT合约
  • 参数设置:金字塔加仓数设为10,其余参数保持默认

多仓脚本

//@version=5
strategy("MACD-EMA Strategy", overlay=true)

// Create variable to store TP level
var float longTp = na
var float stopLossLong = na

// MACD settings
fastLength = input(12, title="Fast Length")
slowLength = input(26, title="Slow Length")
signalLength = input(9, title="Signal Smoothing")

[macdLine, signalLine, _] = ta.macd(close, fastLength, slowLength, signalLength)

// 200 Period EMA
ema200 = ta.ema(close, 200)

// Entry conditions
enterLong = ta.crossover(macdLine, signalLine) and macdLine < 0 and close > ema200

// Strategy logic
if (enterLong)
    strategy.entry("Long", strategy.long)
    alert("Long Order: " + str.tostring(bar_index))
    longTp := close + ((close - ema200) * 1.5) // Calculate TP for the long trade

// Take Profit level based on open trades
takeProfit = strategy.position_size > 0 ? longTp : na

// Check if take profit or stop loss level is hit
if strategy.position_size > 0
    strategy.exit("Exit TP", "Long", limit=longTp, comment="Take Profit Long")

// Plot TP and Stop Loss lines
plot(takeProfit, color=color.green, title="Take Profit", linewidth=1, style=plot.style_line)

空仓脚本

//@version=5
strategy("Inverted MACD-EMA Strategy", overlay=true)

// Create variables to store TP and Stop Loss levels
var float shortTp = na
var float stopLossShort = na

// MACD settings
fastLength = input(12, title="Fast Length")
slowLength = input(26, title="Slow Length")
signalLength = input(9, title="Signal Smoothing")

[macdLine, signalLine, _] = ta.macd(close, fastLength, slowLength, signalLength)

// 200 Period EMA
ema200 = ta.ema(close, 200)

// Entry conditions (inverted for short strategy)
enterShort = ta.crossunder(macdLine, signalLine) and macdLine > 0 and close < ema200

// Strategy logic
if (enterShort)
    strategy.entry("Short", strategy.short)
    alert("Short Order: " + str.tostring(bar_index))
    shortTp := close - ((ema200 - close) * 1.5) // Calculate TP for the short trade

// Take Profit level based on open trades
takeProfit = strategy.position_size < 0 ? shortTp : na

// Check if take profit or stop loss level is hit
if strategy.position_size < 0
    strategy.exit("Exit TP", "Short", limit=shortTp, comment="Take Profit Short")

// Plot TP and Stop Loss lines
plot(takeProfit, color=color.red, title="Take Profit", linewidth=1, style=plot.style_line)

合并后的脚本

//@version=5
strategy("Combined MACD-EMA Strategy", overlay=true)

// Create variables to store TP and Stop Loss levels for long and short trades
var float longTp = na
var float stopLossLong = na
var float shortTp = na
var float stopLossShort = na

// MACD settings
fastLength = input(12, title="Fast Length")
slowLength = input(26, title="Slow Length")
signalLength = input(9, title="Signal Smoothing")

[macdLine, signalLine, _] = ta.macd(close, fastLength, slowLength, signalLength)

// 200 Period EMA for long trades
ema200Long = ta.ema(close, 200)

// Entry conditions for long trades
enterLong = ta.crossover(macdLine, signalLine) and macdLine < 0 and close > ema200Long

// Strategy logic for long trades
if (enterLong)
    strategy.entry("Long", strategy.long)
    alert("Long Order: " + str.tostring(bar_index))
    longTp := close + ((close - ema200Long) * 1.5) // Calculate TP for the long trade

// Take Profit level for long trades based on open positions
takeProfitLong = strategy.position_size > 0 ? longTp : na

// Check if take profit or stop loss level is hit for long trades
if strategy.position_size > 0
    strategy.exit("Exit TP Long", "Long", limit=longTp, comment="Take Profit Long")

// Plot TP and Stop Loss lines for long trades
plot(takeProfitLong, color=color.green, title="Take Profit Long", linewidth=1, style=plot.style_line)

// 200 Period EMA for short trades
ema200Short = ta.ema(close, 200)

// Entry conditions for short trades
enterShort = ta.crossunder(macdLine, signalLine) and macdLine > 0 and close < ema200Short

// Strategy logic for short trades
if (enterShort)
    strategy.entry("Short", strategy.short)
    alert("Short Order: " + str.tostring(bar_index))
    shortTp := close - ((ema200Short - close) * 1.5) // Calculate TP for the short trade

// Take Profit level for short trades based on open positions
takeProfitShort = strategy.position_size < 0 ? shortTp : na

// Check if take profit or stop loss level is hit for short trades
if strategy.position_size < 0
    strategy.exit("Exit TP Short", "Short", limit=shortTp, comment="Take Profit Short")

// Plot TP and Stop Loss lines for short trades
plot(takeProfitShort, color=color.red, title="Take Profit Short", linewidth=1, style=plot.style_line)

已尝试的解决动作

重命名了两个脚本中的函数,但问题仍未解决。

内容的提问来源于stack exchange,提问作者jw25115

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 01:27:02