Pinescript / TradingView:设置最大金字塔(相同方向的多个堆叠条目)但大于1

问题描述 投票:0回答:1

我正在撰写一项研究(不是一项策略),并在较低的5分钟TF上发出警报,该警报允许同一方向的多个输入(和单个关闭)。理想情况下,在关闭之前最多3个多头或3个短裤,否则将继续等待相反的进入或关闭。 (请注意,我并不是在寻找一次限制进入/退出一次的代码)。到目前为止,这与网络研究的建议相去甚远,但是由于某种原因,它似乎不起作用(在我的图表中,图表显示得很少)–我认为我已将其定位到security()函数条件基于更高的TF数据。当单独隔离金字塔时,看起来不错。当隔离安全功能以检查数据是否正在传递时,它可以很好地绘制。请让我知道是否存在替代的最大金字塔逻辑或如何使其与更高的TF数据一起使用。预先感谢,感谢您的帮助。

// == ORDER CONDITIONS ==
longCond = security(tickerid,'60',someHFCond1)
shortCond = security(tickerid,'60',someHFCond2)

// Count your long short conditions for more control with Pyramiding

sectionLongs = 0
sectionLongs := nz(sectionLongs[1])

sectionShorts = 0
sectionShorts := nz(sectionShorts[1])

if longCond
    sectionLongs := sectionLongs + 1
    sectionShorts := 0

if shortCond
    sectionLongs := 0
    sectionShorts := sectionShorts + 1

// Pyramiding
pyrl = input(3,title="Max Pyramiding (stackable entries, default=3)",minval=1)
longCondition = longCond and sectionLongs <= pyrl 
shortCondition = shortCond and sectionShorts <= pyrl 

// Get the price of the last opened long or short
last_open_longCondition = na
last_open_shortCondition = na
last_open_longCondition := longCondition ? close : nz(last_open_longCondition[1])
last_open_shortCondition := shortCondition ? close : nz(last_open_shortCondition[1])

// Check if your last position was a long or a short
last_longCondition = na
last_shortCondition = na
last_longCondition := longCondition ? time : nz(last_longCondition[1])
last_shortCondition := shortCondition ? time : nz(last_shortCondition[1])

in_longCondition = last_longCondition > last_shortCondition
in_shortCondition = last_shortCondition > last_longCondition

// Take profit
Ltakeprofit = last_open_longCondition * (1+profittarget)
Stakeprofit = last_open_shortCondition * (1-profittarget)
long_tp = high[1] > Ltakeprofit and longCondition == 0 and in_longCondition  == 1 and not longCondition[1]
short_tp = low[1] < Stakeprofit and shortCondition == 0 and in_shortCondition == 1 and not shortCondition[1]

// Create a single close for all the different closing conditions.
long_close = long_tp or long_sl ? 1 : 0
short_close = short_tp or short_sl ? 1 : 0

// Get the time of the last close
last_long_close = na
last_long_close := long_close ? time : nz(last_long_close[1])
last_short_close = na
last_short_close := short_close ? time : nz(last_short_close[1])

// Alerts & Signals
bton(b) => b ? 1 : 0
plotshape(longCondition , title="buy alert", color=green, textcolor=green, transp=0, 
          style=shape.triangleup, location=location.belowbar, size=size.small,text="LONG",offset=0)
plotshape(shortCondition, title="sell alert", color=red, textcolor=red, transp=0, 
          style=shape.triangledown, location=location.abovebar, size=size.small,text="SHORT",offset=0)
plotshape(long_tp and last_longCondition > nz(last_long_close[1]), text ="Close", title="Take Profit Long", style=shape.triangledown, 
   location=location.abovebar, color = green, size=size.tiny, editable = false, transp = 0,offset=0) 
plotshape(short_tp and last_shortCondition > nz(last_short_close[1]) , text ="Cover", title="Take Profit Short", style=shape.triangleup, 
   location=location.belowbar, color = red, size=size.tiny, editable = false, transp = 0,offset=0)
bots pine-script algorithmic-trading trading
1个回答
0
投票

这显示您的金字塔控制工作正常:

//@version=4
study("", "", true)
// == ORDER CONDITIONS ==
barUp = close > open
longCond = barUp and barUp[1]
shortCond = not barUp and not barUp[1]

// Count your long short conditions for more control with Pyramiding

sectionLongs = 0
sectionLongs := nz(sectionLongs[1])

sectionShorts = 0
sectionShorts := nz(sectionShorts[1])

if longCond
    sectionLongs := sectionLongs + 1
    sectionShorts := 0
    label.new(bar_index, low - tr, tostring(sectionLongs, "▲\n#"), xloc.bar_index, yloc.price, color.green, label.style_none, color.green, size.large)

if shortCond
    sectionLongs := 0
    sectionShorts := sectionShorts + 1
    label.new(bar_index, na, tostring(sectionShorts, "#\n▼"), xloc.bar_index, yloc.abovebar, color.maroon, label.style_none, color.maroon, size.large)

// Pyramiding
pyrl = input(3,title="Max Pyramiding (stackable entries, default=3)",minval=1)
longCondition = longCond and sectionLongs <= pyrl 
shortCondition = shortCond and sectionShorts <= pyrl 

bgcolor(longCondition ? color.lime : shortCondition ? color.red : na)
plotchar(sectionLongs, "sectionLongs", "", location.top)
plotchar(sectionShorts, "sectionShorts", "", location.top)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.