0

我在 tradingview 脚本页面上找到了一个开源代码,我在这段代码中添加了 TP2 和 TP3 价格点,但是代码中有一个代码会在价格达到 tp 和 sl 点时发出警告,我知道如何乘以它,所以我需要帮助。

这是代码

// © theCrypster 2020

//@version=4
study("Fixed Percent Stop Loss & Take Profit % - For Study Scripts", overlay=true)

// Moving Averages to get some example trades generated
eg1 = ema(close, 5)
eg2 = ema(close, 32)

long = crossover(eg1, eg2)
short = crossunder(eg1, eg2)

//plot signals on MA cross
plotshape(long, style=shape.labelup, location=location.belowbar, color=color.green, size=size.tiny, title="Long Entry", text="LONG", textcolor=color.white)
plotshape(short, style=shape.labeldown, location=location.abovebar, color=color.red, size=size.tiny, title="Short Entry", text="SHORT", textcolor=color.white)

///////////////
/////////
// The Fixed Percent Stop Loss Code
// User Options to Change Inputs (%)
stopPer = input(3, title='Stop Loss %', type=input.float) / 100
takePer = input(9, title='Take Profit %', type=input.float) / 100

//detect what was last signal (long or short)
long_short = 0
long_last = long and (nz(long_short[1]) == 0 or nz(long_short[1]) == -1)
short_last = short and (nz(long_short[1]) == 0 or nz(long_short[1]) == 1)
long_short := long_last ? 1 : short_last ? -1 : long_short[1]

//entry price
longPrice = valuewhen(long_last, close, 0)
shortPrice = valuewhen(short_last, close, 0)
//fixed sltp prices
longStop = longPrice * (1 - stopPer)
shortStop = shortPrice * (1 + stopPer)
longTake = longPrice * (1 + takePer)
shortTake = shortPrice * (1 - takePer)
//plot sltp lines
plot(long_short==1  ? longStop : na, style=plot.style_linebr, color=color.red, linewidth=1, title="Long Fixed SL")
plot(long_short==-1 ? shortStop : na, style=plot.style_linebr, color=color.red, linewidth=1, title="Short Fixed SL")
plot(long_short==1  ? longTake : na, style=plot.style_linebr, color=color.green, linewidth=1, title="Long Fixed TP")
plot(long_short==-1 ? shortTake : na, style=plot.style_linebr, color=color.green, linewidth=1, title="Short Fixed TP")

//remove first bar for SL/TP (you can't enter a trade at bar close THEN hit your SL on that same bar)
longBar1 = barssince(long_last)
longBar2 = longBar1 >= 1 ? true : false
shortBar1 = barssince(short_last)
shortBar2 = shortBar1 >= 1 ? true : false

//check for SL hit during a bar
longSLhit = long_short==1 and longBar2 and low < longStop
plotshape(longSLhit, style=shape.labelup, location=location.belowbar, color=color.gray, size=size.tiny, title="Long SL", text=" Long SL", textcolor=color.white)
shortSLhit = long_short==-1 and shortBar2 and high > shortStop
plotshape(shortSLhit, style=shape.labeldown, location=location.abovebar, color=color.gray, size=size.tiny, title="Short SL", text=" Short SL", textcolor=color.white)

//check for TP hit during bar
longTPhit = long_short==1 and longBar2 and high > longTake
plotshape(longTPhit, style=shape.labeldown, location=location.abovebar, color=color.purple, size=size.tiny, title="Long TP", text="Long TP", textcolor=color.white)
shortTPhit = long_short==-1 and shortBar2 and low < shortTake
plotshape(shortTPhit, style=shape.labelup, location=location.belowbar, color=color.purple, size=size.tiny, title="Short TP", text="Short TP", textcolor=color.white)

//reset long_short if SL/TP hit during bar
long_short := (long_short==1 or long_short==0) and longBar2 and (longSLhit or longTPhit) ? 0 : (long_short==-1 or long_short==0) and shortBar2 and (shortSLhit or shortTPhit) ? 0 : long_short
//

//set Alerts for Open Long/Short, SL Hit, TP Hit
alertcondition(condition=long_last, title="Long Alarm", message='Open a Long Trade @ ${{close}}')
alertcondition(condition=short_last, title="Short Alarm", message='Open a Short Trade @ ${{close}}')

alertcondition(condition=longSLhit, title="Long Stop", message='Long SL Hit @ ${{plot("Long SL")}}')
alertcondition(condition=shortSLhit, title="Short Stop", message='Short SL Hit @ ${{plot("Short SL")}}')

alertcondition(condition=longTPhit, title="Long Take Profit", message='Long TP Hit @ ${{plot("Long TP")}}')
alertcondition(condition=shortTPhit, title="Short Take Profit", message='Short TP Hit @ ${{plot("Short TP")}}')
//

正如您在代码中看到的,当价格达到 tp 和 sl 时,有一个代码会发出警报。

这是我需要帮助的地方

//remove first bar for SL/TP (you can't enter a trade at bar close THEN hit your SL on that same bar)
longBar1 = barssince(long_last)
longBar2 = longBar1 >= 1 ? true : false
shortBar1 = barssince(short_last)
shortBar2 = shortBar1 >= 1 ? true : false

//check for SL hit during a bar
longSLhit = long_short==1 and longBar2 and low < longStop
plotshape(longSLhit, style=shape.labelup, location=location.belowbar, color=color.gray, size=size.tiny, title="Long SL", text=" Long SL", textcolor=color.white)
shortSLhit = long_short==-1 and shortBar2 and high > shortStop
plotshape(shortSLhit, style=shape.labeldown, location=location.abovebar, color=color.gray, size=size.tiny, title="Short SL", text=" Short SL", textcolor=color.white)

//check for TP hit during bar
longTPhit = long_short==1 and longBar2 and high > longTake
plotshape(longTPhit, style=shape.labeldown, location=location.abovebar, color=color.purple, size=size.tiny, title="Long TP", text="Long TP", textcolor=color.white)
shortTPhit = long_short==-1 and shortBar2 and low < shortTake
plotshape(shortTPhit, style=shape.labelup, location=location.belowbar, color=color.purple, size=size.tiny, title="Short TP", text="Short TP", textcolor=color.white)

//reset long_short if SL/TP hit during bar
long_short := (long_short==1 or long_short==0) and longBar2 and (longSLhit or longTPhit) ? 0 : (long_short==-1 or long_short==0) and shortBar2 and (shortSLhit or shortTPhit) ? 0 : long_short
//

我在这部分找不到如何多路复用 longTPHit 或 shortTPHit 我需要你的帮助

4

0 回答 0