繁体   English   中英

如何根据 TP / SL 条件发出买入信号? 请帮我

[英]How to make buy signals dependent on TP / SL conditions? please help me

'对 Pine Script 很陌生,我很感谢你的帮助。 下面是一个示例代码,它生成“买入”信号。 但是,我想让这些“买入”信号仅在满足“获利”或“止损”条件时才会发生。 任何想法?

在此处输入图像描述

在从先前的信号增益中获利后,我想 plot 买入/卖出信号,以免在较小的图表上重新绘制

但我制作的这段代码只显示 19 根蜡烛后的信号

我正在尝试找到一种解决方案,以便在我已经从前一个信号中获得或停止之后出现买入/卖出信号

我的代码没有错误,只是没有做我想要的 function

欢迎任何帮助

这是我的代码

//@version=5
indicator("My script", overlay=true)

var cnt = 0
var startCounting = false

var cnt1 = 0
var startCounting1 = false

src = hlc3


atrPeriod = input(10, "ATR Length")
factor = input.float(2.99, "Factor", step = 0.01)

[supertrend, direction] = ta.supertrend(factor, atrPeriod)

bodyMiddle = plot((open + close) / 2, display=display.none)
upTrend = plot(direction < 0 ? supertrend : na, "Up Trend", color = color.green, style=plot.style_linebr)
downTrend = plot(direction < 0? na : supertrend, "Down Trend", color = color.red, style=plot.style_linebr)

fill(bodyMiddle, upTrend, color.new(color.green, 90), fillgaps=false)
fill(bodyMiddle, downTrend, color.new(color.red, 90), fillgaps=false)

//Stoch Inputs
smoothK = input.int(3, 'K', minval=1)
smoothD = input.int(3, 'D', minval=1)
lengthRSI = input.int(14, 'RSI Length', minval=1)
lengthStoch = input.int(14, 'Stochastic Length', minval=1)

upperlimit = input.float(83, 'Upper Limit', minval=0.01)
lowerlimit = input.float(17, 'Upper Limit', minval=0.01)

//Stochastic Code
rsi1 = ta.rsi(src, lengthRSI)
k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = ta.sma(k, smoothD)

sh = ta.crossover(k, d)
lg = ta.crossunder(d, k)

long = direction > 0 ? na : supertrend and sh and lowerlimit and d[0] < lowerlimit
short = direction < 0 ? na : supertrend and lg and upperlimit and d[0] > upperlimit

//condiçoes long
startCounting := long ? true : startCounting  // 
cnt := startCounting ? cnt + 1 : cnt    // 

canlong = long and cnt > 19
cnt := canlong ? 0 : cnt     //

//condiçoes short
startCounting1 := short ? true : startCounting1  //
cnt1 := startCounting1 ? cnt1 + 1 : cnt1

canshort = short and cnt1 > 19
cnt1 := canshort ? 0 : cnt1    


plotshape(canlong, style=shape.triangleup, location=location.belowbar, color=color.green, text="Buy", size=size.small)
plotshape(canshort, style=shape.triangledown, location=location.abovebar, color=color.red, text="Short", size=size.small)

// User Options to Change Inputs (%)
stopPer = input.float(4, title='Stop Loss %') / 100
takePer = input.float(0.7, title='Take Profit 1 %') / 100

//detect what was last signal (long or short)
long_short = 0
long_last = canlong and (nz(long_short[1]) == 0 or nz(long_short[1]) == -1)
short_last = canshort 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 = ta.valuewhen(long_last, close, 0)
shortPrice = ta.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.new(color.red, 0), linewidth=1, title='Long Fixed SL')
plot(long_short == -1 ? shortStop : na, style=plot.style_linebr, color=color.new(color.red, 0), linewidth=1, title='Short Fixed SL')
plot(long_short == 1 ? longTake : na, style=plot.style_linebr, color=color.new(color.green, 0), linewidth=1, title='Long Fixed TP')
plot(long_short == -1 ? shortTake : na, style=plot.style_linebr, color=color.new(color.green, 0), 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 = ta.barssince(long_last)
longBar2 = longBar1 >= 1 ? true : false
shortBar1 = ta.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.new(color.gray, 0), size=size.tiny, title='Stop', text=' Stop', textcolor=color.new(color.white, 0))
shortSLhit = long_short == -1 and shortBar2 and high > shortStop
plotshape(shortSLhit, style=shape.labeldown, location=location.abovebar, color=color.new(color.gray, 0), size=size.tiny, title='Stop', text=' Stop', textcolor=color.new(color.white, 0))

//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.new(color.purple, 0), size=size.tiny, title='take 1', text='take 1', textcolor=color.new(color.white, 0))

shortTPhit = long_short == -1 and shortBar2 and low < shortTake
plotshape(shortTPhit, style=shape.labelup, location=location.belowbar, color=color.new(color.purple, 0), size=size.tiny, title='take 1', text='take 1', textcolor=color.new(color.white, 0))


//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
  1. 声明var allowNewPosition = true
  2. 更改条件: canlong = long and cnt > 19 and allowNewPosition and canshort = short and cnt1 > 19 and allowNewPosition
  3. 之后在某处插入以下内容:
     if canlong or canshort
    allowNewPosition:= false
    在您收到买入/卖出信号后激活您的封锁条件
  4. 最后,在您知道您是否有 TP/SL 之后,解除对新仓位的封锁:
     if longSLhit or shortSLhit or longTPhit or shortTPhit
    allowNewPosition:= true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM