Pine script 策略。第二次交叉后很久,松树脚本

Pine script 策略。第二次交叉后很久,松树脚本,pine-script,Pine Script,我试图在交易视图中进行反向测试,当以下条件满足时 1) 长=RSI>50和MACD>信号(反之亦然) 2) 当两者都满足时,在打开(0)>高位置进入(满足上述条件时的信号条) 并在达到利润目标时退出 3) 而且长时间只会再次发生重新交叉…不是立即发生的,这是我无法在pine脚本中完成的,需要帮助 以下是相同的代码: //@version=4 strategy("Test1 script",overlay=false) var a = 0 var hiBar = 0 var loBar = 0 /

我试图在交易视图中进行反向测试,当以下条件满足时

1) 长=RSI>50和MACD>信号(反之亦然)

2) 当两者都满足时,在打开(0)>高位置进入(满足上述条件时的信号条)

并在达到利润目标时退出

3) 而且长时间只会再次发生重新交叉…不是立即发生的,这是我无法在pine脚本中完成的,需要帮助

以下是相同的代码:

//@version=4
strategy("Test1 script",overlay=false)
var a = 0
var hiBar = 0
var loBar = 0
//MACD
fast = 12, slow = 26

fastMA = ema(close, fast)
slowMA = ema(close, slow)
macd = fastMA - slowMA
signal = sma(macd, 9)

//RSI
rsiSource = close //input(title="RSI Source", type=input.source, defval=close)
rsiLength = 7 //input(title="RSI Length", type=input.integer, defval=14)
rsiOverbought = 70 //nput(title="RSI Overbought Level", type=input.integer, defval=80)
rsiOversold = 30 //input(title="RSI Oversold Level", type=input.integer, defval=20)
rsiValue = rsi(rsiSource, rsiLength)
isRsiOB = rsiValue >= rsiOverbought
isRsiOS = rsiValue <= rsiOversold

uptrend1 = rsiValue > 50 and macd > signal 
downtrend1 = rsiValue < 50  and macd < signal 


if uptrend1 or downtrend1
    a := 10
else 
    a := 20

if uptrend1
    hiBar := bar_index

if downtrend1
    loBar := bar_index


barcolor(uptrend1 ? color.purple:downtrend1 ? color.yellow: a == 20 ? color.gray: na) 

if uptrend1
    strategy.entry("Long", strategy.long,when = close[0] > high[hiBar-1] or open[0] > high[hiBar-1])


strategy.exit("exit long","Long", profit = 2400, loss = 1200)

if downtrend1
    strategy.entry("Short", strategy.short)

strategy.exit("exit short", "Short", profit = 2800, loss = 1200)
/@version=4
策略(“Test1脚本”,overlay=false)
变量a=0
var hiBar=0
var-loBar=0
//MACD
快=12,慢=26
fastMA=ema(关闭,快速)
slowMA=ema(关闭,慢速)
macd=fastMA-slowMA
信号=sma(macd,9)
//RSI
rsiSource=close//input(title=“RSI Source”,type=input.Source,deffal=close)
rsiLength=7//input(title=“RSI-Length”,type=input.integer,deffal=14)
RSI超买=70//nput(title=“RSI超买水平”,type=input.integer,deffal=80)
RSI超卖=30//输入(title=“RSI超卖级别”,type=input.integer,deffal=20)
rsiValue=rsi(rsiSource,rsiLength)
isRsiOB=rsiValue>=RSIoverbund
isRsiOS=RSI50值和macd>信号
下降趋势1=RSI值<50且macd<信号
如果是上升趋势1或下降趋势1
a:=10
其他的
a:=20
如果上升趋势1
hiBar:=条形图索引
如果是下降趋势1
loBar:=条形图索引
barcolor(上升趋势1?颜色。紫色:下降趋势1?颜色。黄色:a==20?颜色。灰色:na)
如果上升趋势1
strategy.entry(“Long”,strategy.Long,when=收盘[0]>高位[hiBar-1]或开盘[0]>高位[hiBar-1])
策略.退出(“退出多头”,“多头”,利润=2400,亏损=1200)
如果是下降趋势1
strategy.entry(“Short”,strategy.Short)
策略.退出(“短线退出”,“短线退出”,利润=2800,亏损=1200)
还附上了背面测试的图片


这应该实现该逻辑。您的目标和利润数字没有用刻度表示,现在您可以通过输入来更改它们。值大于您的值以防止过早退出:

//@version=4
strategy("Test1 script",overlay=true)
targetProfit = input(1000., "Profit target (in currency)") / syminfo.mintick
targetLoss = input(500., "Profit loss (in currency)") / syminfo.mintick

var a = 0
var float hiBar = na
var float loBar = na
var float referenceHi = na
// This is true when a long was exited and we're waiting for downtrend condition to occur.
var waitingForLongReset = false

//MACD
fast = 12, slow = 26
fastMA = ema(close, fast)
slowMA = ema(close, slow)
macd = fastMA - slowMA
signal = sma(macd, 9)

//RSI
rsiSource = close //input(title="RSI Source", type=input.source, defval=close)
rsiLength = 7 //input(title="RSI Length", type=input.integer, defval=14)
rsiOverbought = 70 //nput(title="RSI Overbought Level", type=input.integer, defval=80)
rsiOversold = 30 //input(title="RSI Oversold Level", type=input.integer, defval=20)
rsiValue = rsi(rsiSource, rsiLength)
isRsiOB = rsiValue >= rsiOverbought
isRsiOS = rsiValue <= rsiOversold

uptrend1 = rsiValue > 50 and macd > signal 
downtrend1 = rsiValue < 50  and macd < signal 
enterUpTrend = not uptrend1[1] and uptrend1
enterDnTrend = not downtrend1[1] and downtrend1

// Only save bar on transition into trend.
if enterUpTrend and not waitingForLongReset
    hiBar := bar_index
referenceHi := high[max(0, bar_index - hiBar)]
if uptrend1 and max(close, open) > referenceHi and not waitingForLongReset
    waitingForLongReset := true
    strategy.entry("Long", strategy.long)
    strategy.exit("exit long","Long", profit = targetProfit, loss = targetLoss)
if strategy.position_size[1] > 0 and strategy.position_size == 0
    // Long was exited, wait for reset before next long.
    waitingForLongReset := true

if enterDnTrend
    loBar := bar_index
    // Allow another long condition to become true.
    waitingForLongReset := false


barcolor(uptrend1 ? color.purple:downtrend1 ? color.yellow: a == 20 ? color.gray: color.blue) 

// Debugging plots.
plotchar(uptrend1, "uptrend1", "▲", location.top)
plotchar(downtrend1, "downtrend1", "▼", location.bottom)
plotchar(waitingForLongReset, "waitingForLongReset", "—", location.bottom)
plotchar(max(close, open) > referenceHi, "max(close, open) > referenceHi", "—", location.top)
plot(referenceHi, "referenceHi", color.green, 2, plot.style_circles)
/@version=4
策略(“Test1脚本”,overlay=true)
targetProfit=输入(1000.,“利润目标(货币)”)/syminfo.mintick
targetLoss=输入(500.,“利润损失(货币)”)/syminfo.mintick
变量a=0
var浮动hiBar=na
var float loBar=na
var float referenceHi=na
//这是真的,当一个长期退出,我们正在等待下降趋势的情况发生。
var waitingforlongset=false
//MACD
快=12,慢=26
fastMA=ema(关闭,快速)
slowMA=ema(关闭,慢速)
macd=fastMA-slowMA
信号=sma(macd,9)
//RSI
rsiSource=close//input(title=“RSI Source”,type=input.Source,deffal=close)
rsiLength=7//input(title=“RSI-Length”,type=input.integer,deffal=14)
RSI超买=70//nput(title=“RSI超买水平”,type=input.integer,deffal=80)
RSI超卖=30//输入(title=“RSI超卖级别”,type=input.integer,deffal=20)
rsiValue=rsi(rsiSource,rsiLength)
isRsiOB=rsiValue>=RSIoverbund
isRsiOS=RSI50值和macd>信号
下降趋势1=RSI值<50且macd<信号
enterUpTrend=非uptrend1[1]和uptrend1
enterDnTrend=非下降趋势1[1]和下降趋势1
//仅在过渡到趋势时保存条。
如果输入UpTrend而不等待LongReset
hiBar:=条形图索引
referenceHi:=高[max(0,bar_索引-hiBar)]
如果uptrend1和max(关闭,打开)>参考Hi,不等待长时间重置
waitingForLongReset:=真
strategy.entry(“Long”,strategy.Long)
策略.退出(“退出多头”,“多头”,利润=目标利润,损失=目标损失)
如果strategy.position\u size[1]>0且strategy.position\u size==0
//Long已退出,请在下一个Long之前等待重置。
waitingForLongReset:=真
如果输入趋势
loBar:=条形图索引
//让另一个长期条件变为现实。
waitingForLongReset:=false
barcolor(上升趋势1?颜色。紫色:下降趋势1?颜色。黄色:a==20?颜色。灰色:颜色。蓝色)
//调试绘图。
plotchar(uptrend1,“uptrend1”▲", 位置(上)
plotchar(向下趋势1,“向下趋势1”▼“,位置。底部)
plotchar(waitingForLongReset、“waitingForLongReset”、“-”,location.bottom)
plotchar(最大值(关闭,打开)>referenceHi,“最大值(关闭,打开)>referenceHi”、“-”,location.top)
绘图(referenceHi,“referenceHi”,color.green,2,plot.style\u圆圈)

这还不清楚:3)并且只会再次进行长距离交叉…不是立即发生的。你能再解释一遍吗?我已经重新附上了一张带有一些解释的图片,请看一看,让我知道这是否足够,或者我可以查看以提供更多信息。@PineCoders LucF