问题:将短脚本和长脚本合并为一个脚本时遇到问题。

huangapple go评论58阅读模式
英文:

Issue combining short and long pinescript scripts into one

问题

我有一个问题与我的脚本。在测试中,如果我运行短订单脚本,它会给我一个大约2的利润因子,然后运行脚本的长订单版本会给我一个大约1.5的利润因子。但是出现了一个问题,当我将这两个脚本合并为一个时,它给我一个大约0.9的利润因子。

注意:这是在BTCUSDT(Kucoin)上进行的测试,杠杆比例为10,其他所有值均为默认值。

长订单脚本:

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

// 创建变量以存储TP水平
var float longTp = na
var float stopLossLong = na

// MACD设置
fastLength = input(12, title="快速长度")
slowLength = input(26, title="慢速长度")
signalLength = input(9, title="信号平滑")

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

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

// 入场条件
enterLong = ta.crossover(macdLine, signalLine) and macdLine < 0 and close > ema200

// 策略逻辑
if (enterLong)
    strategy.entry("Long", strategy.long)
    alert("Long订单:" + str.tostring(bar_index))
    longTp := close + ((close - ema200) * 1.5) // 计算长交易的TP

// 基于开放交易的止盈水平
takeProfit = strategy.position_size > 0 ? longTp : na

// 检查是否触及止盈或止损水平
if strategy.position_size > 0
    strategy.exit("Exit TP", "Long", limit=longTp, comment="Take Profit Long")

// 绘制TP和止损线
plot(takeProfit, color=color.green, title="止盈", linewidth=1, style=plot.style_line)

短订单脚本:

//@version=5
strategy("反向MACD-EMA策略", overlay=true)

// 创建变量以存储TP和止损水平
var float shortTp = na
var float stopLossShort = na

// MACD设置
fastLength = input(12, title="快速长度")
slowLength = input(26, title="慢速长度")
signalLength = input(9, title="信号平滑")

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

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

// 入场条件(用于短策略的反向条件)
enterShort = ta.crossunder(macdLine, signalLine) and macdLine > 0 and close < ema200

// 策略逻辑
if (enterShort)
    strategy.entry("Short", strategy.short)
    alert("Short订单:" + str.tostring(bar_index))
    shortTp := close - ((ema200 - close) * 1.5) // 计算短交易的TP

// 基于开放交易的止盈水平
takeProfit = strategy.position_size < 0 ? shortTp : na

// 检查是否触及止盈或止损水平
if strategy.position_size < 0
    strategy.exit("Exit TP", "Short", limit=shortTp, comment="Take Profit Short")

// 绘制TP和止损线
plot(takeProfit, color=color.red, title="止盈", linewidth=1, style=plot.style_line)

合并脚本:

//@version=5
strategy("合并MACD-EMA策略", overlay=true)
// 创建变量以存储长和短交易的TP和止损水平
var float longTp = na
var float stopLossLong = na
var float shortTp = na
var float stopLossShort = na
// MACD设置
fastLength = input(12, title="快速长度")
slowLength = input(26, title="慢速长度")
signalLength = input(9, title="信号平滑")
[macdLine, signalLine, _] = ta.macd(close, fastLength, slowLength, signalLength)
// 长交易的200周期EMA
ema200Long = ta.ema(close, 200)
// 长交易入场条件
enterLong = ta.crossover(macdLine, signalLine) and macdLine < 0 and close > ema200Long
// 长交易策略逻辑
if (enterLong)
strategy.entry("Long", strategy.long)
alert("Long订单:" + str.tostring(bar_index))
longTp := close + ((close - ema200Long) * 1.5) // 计算长交易的TP
// 长交易基于开放仓位的止盈水平
takeProfitLong = strategy.position_size > 0 ? longTp : na
// 检查是否触及长交易的止盈或止损水平
if strategy.position_size > 0
strategy.exit("Exit TP Long", "Long", limit=longTp, comment="Take Profit Long")
// 绘制长交易的TP和止损线
plot(takeProfitLong, color=color.green, title="止盈 Long", linewidth=1, style=plot.style_line)
// 短交易的200周期EMA
ema200Short = ta.ema(close, 200)
// 短交易入场条件
enterShort = ta.crossunder(macdLine, signalLine) and macdLine > 0 and close < ema200Short
// 短交易策略逻辑
if (enterShort)
strategy.entry("Short", strategy.short)
alert("Short订单:" + str.tostring(bar_index))
shortTp := close - ((ema200Short - close) * 1.5) // 计算短交易的TP
// 短交易基于开放仓位的止盈水平
takeProfitShort = strategy.position_size < 0 ? shortTp : na
// 检查是否触及短交易的止盈或止损水平
if strategy.position_size < 0
strategy.exit("Exit TP Short",
<details>
<summary>英文:</summary>
I&#39;m having an issue with my script. Upon testing it, if I run the short order script, it gets me a profit factor of 2 or so, then the long order version of the script gives me a profit factor of 1.5 or something. For some reason when I combine the scripts into one, it gives me a profit factor of 0.9.
Note: This is tested on BTCUSDT (Kucoin), with Pyramiding of 10, and everything else is default values
Long script:
//@version=5
strategy(&quot;MACD-EMA Strategy&quot;, overlay=true)
// Create variable to store TP level
var float longTp = na
var float stopLossLong = na
// MACD settings
fastLength = input(12, title=&quot;Fast Length&quot;)
slowLength = input(26, title=&quot;Slow Length&quot;)
signalLength = input(9, title=&quot;Signal Smoothing&quot;)
[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 &lt; 0 and close &gt; ema200
// Strategy logic
if (enterLong)
strategy.entry(&quot;Long&quot;, strategy.long)
alert(&quot;Long Order: &quot; + 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 &gt; 0 ? longTp : na
// Check if take profit or stop loss level is hit
if strategy.position_size &gt; 0
strategy.exit(&quot;Exit TP&quot;, &quot;Long&quot;, limit=longTp, comment=&quot;Take Profit Long&quot;)
// Plot TP and Stop Loss lines
plot(takeProfit, color=color.green, title=&quot;Take Profit&quot;, linewidth=1, style=plot.style_line)
Short script:
//@version=5
strategy(&quot;Inverted MACD-EMA Strategy&quot;, 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=&quot;Fast Length&quot;)
slowLength = input(26, title=&quot;Slow Length&quot;)
signalLength = input(9, title=&quot;Signal Smoothing&quot;)
[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 &gt; 0 and close &lt; ema200
// Strategy logic
if (enterShort)
strategy.entry(&quot;Short&quot;, strategy.short)
alert(&quot;Short Order: &quot; + 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 &lt; 0 ? shortTp : na
// Check if take profit or stop loss level is hit
if strategy.position_size &lt; 0
strategy.exit(&quot;Exit TP&quot;, &quot;Short&quot;, limit=shortTp, comment=&quot;Take Profit Short&quot;)
// Plot TP and Stop Loss lines
plot(takeProfit, color=color.red, title=&quot;Take Profit&quot;, linewidth=1, style=plot.style_line)
Combined script:
//@version=5
strategy(&quot;Combined MACD-EMA Strategy&quot;, 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=&quot;Fast Length&quot;)
slowLength = input(26, title=&quot;Slow Length&quot;)
signalLength = input(9, title=&quot;Signal Smoothing&quot;)
[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 &lt; 0 and close &gt; ema200Long
// Strategy logic for long trades
if (enterLong)
strategy.entry(&quot;Long&quot;, strategy.long)
alert(&quot;Long Order: &quot; + 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 &gt; 0 ? longTp : na
// Check if take profit or stop loss level is hit for long trades
if strategy.position_size &gt; 0
strategy.exit(&quot;Exit TP Long&quot;, &quot;Long&quot;, limit=longTp, comment=&quot;Take Profit Long&quot;)
// Plot TP and Stop Loss lines for long trades
plot(takeProfitLong, color=color.green, title=&quot;Take Profit Long&quot;, 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 &gt; 0 and close &lt; ema200Short
// Strategy logic for short trades
if (enterShort)
strategy.entry(&quot;Short&quot;, strategy.short)
alert(&quot;Short Order: &quot; + 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 &lt; 0 ? shortTp : na
// Check if take profit or stop loss level is hit for short trades
if strategy.position_size &lt; 0
strategy.exit(&quot;Exit TP Short&quot;, &quot;Short&quot;, limit=shortTp, comment=&quot;Take Profit Short&quot;)
// Plot TP and Stop Loss lines for short trades
plot(takeProfitShort, color=color.red, title=&quot;Take Profit Short&quot;, linewidth=1, style=plot.style_line)
Tried renaming the functions all be separate in the two scripts. No luck.
</details>
# 答案1
**得分**: 0
这里发生的情况是,当你运行两个方向时,有时会在之前的交易达到止盈之前中断之前的交易。在Tradingview上不允许对冲,因此任何相反方向的入场信号都会关闭之前的交易。
以下是我在默认设置下在`BTCBUSD:BINANCE`的12小时时间框架上测试的示例。
如果我只运行空头交易,这是第一笔交易。它在2019年12月31日进入交易,于2020年3月12日盈利退出。
现在,当我同时运行两个方向时,会在2020年3月3日触发一笔多头交易。这会导致之前的空头交易出现亏损。这就是为什么在同时运行两个方向时统计数据不同的原因。
[![enter image description here][1]][1]
[![enter image description here][2]][2]
[1]: https://i.stack.imgur.com/z2g2C.png
[2]: https://i.stack.imgur.com/LSxpV.png
<details>
<summary>英文:</summary>
What&#39;s happening here is, when you run both directions, sometimes it interrupts the previous trade before it hits its TP. Hedging is not allowed on Tradingview so any opposite direction entry signal will close the previous trade.
Attached is an example that I tested on `BTCBUSD:BINANCE` on 12h timeframe with the default settings.
If I run short only, this is the first trade that takes place. It enters the trade on Dec 31, 2019 and exits the trade with profit on Mar 12, 2020.
[![enter image description here][1]][1]
Now, when I run it in both directions, a long trade is triggered on Mar 03, 2020. This closes the open short trade with a loss. That&#39;s why the statistics are different when you run both directions.
[![enter image description here][2]][2]
[1]: https://i.stack.imgur.com/z2g2C.png
[2]: https://i.stack.imgur.com/LSxpV.png
</details>

huangapple
  • 本文由 发表于 2023年7月3日 22:00:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605492.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定