对plotshape使用条件if语句

时间:2019-12-10 22:31:24

标签: pine-script

我对以下代码有疑问,希望获得帮助:

//@version=4
study(title="Example", shorttitle="Ex", overlay=true)

//Average volume, 45 periods
sum = volume[1]
for i = 1 to 45
    sum := sum + volume[i]
volumeMA = sum / 45
VolumeRatio = volume/volumeMA

//Calculate Scores - Volume
VolumeScore = 0
if VolumeRatio > 1 and VolumeRatio < 1.5 
    VolumeScore := 1
if VolumeRatio >= 1.5 and VolumeRatio < 2 
    VolumeScore := 2
if VolumeRatio >= 2 
    VolumeScore := 3

//Calculate Scores - HH
HH_Bull = 0
if high >= highest(high, 5)
    HH_Bull := 1    
if high >= highest(high, 8)
    HH_Bull := 2    
if high >= highest(high, 14)
    HH_Bull := 3

HH_Bear = 0
if low >= lowest(low, 3)
    HH_Bear := 1    
if low >= lowest(low, 6)
    HH_Bear := 2    
if low >= lowest(low, 10)
    HH_Bear := 3

//Caclulate Total Score
Candle_Bull_Score = VolumeScore + HH_Bull
Candle_Bear_Score = VolumeScore + HH_Bear

BullCandle = close > close[1] and open-low >= 10
BearCandle = close < close[1] and high-open >= 10

plotshape(BullCandle, title= "Bull", location=location.belowbar, color=color.green, transp=0, style=shape.triangleup, text="BUY-" & Candle_Bull_Score)
plotshape(BearCandle, title= "Bear", location=location.belowbar, color=color.red, transp=0, style=shape.triangledown, text="SELL-" & Candle_Bear_Score)

请注意,我故意将text="Buy" & Candle_Bull_Score组合在一起,请注意,这并不能解释我要做什么。 我想绘制“ BullCandle”变量,但通过使用Candle_Bull_Score和Candle_Bear_Score给出信号强度的指示。

2 个答案:

答案 0 :(得分:1)

查看代码中的注释:

//@version=4
study("Volume Ratio", "", true)
//Calculate Scores - Volume
VolumeRatio = roc(volume, 1)
VolumeScore = 0

// Note the ":=" assignment operator so you don't redeclare local vars in "if" blocks.
// Also note "case" structure so you are testing exclusively. Not essential but more elegant and negligibly faster.
if VolumeRatio > 1 and VolumeRatio < 1.5 
    VolumeScore:=1
else
    if VolumeRatio >= 1.5 and VolumeRatio < 2 
        VolumeScore:=2
    else
        if VolumeRatio >= 2 
            VolumeScore:=3

// Note "==" operator for logical test. The 3 "pltoshape()" calls are necessary, as there's no way to use 
//  a "series string" (i.e., varying dynamically during script's execution) argument for "text=".
plotshape(VolumeScore==1, title= "Case1", location=location.belowbar, color=color.green, transp=0, style=shape.triangleup, text="BUY-1")
plotshape(VolumeScore==2, title= "Case2", location=location.belowbar, color=color.green, transp=0, style=shape.triangleup, text="BUY-2")
plotshape(VolumeScore==3, title= "Case3", location=location.belowbar, color=color.green, transp=0, style=shape.triangleup, text="BUY-3")

// With labels we can use a series string, but only the last ~50 will appear on chart.
if VolumeScore > 0
    label.new(bar_index, na, tostring(VolumeScore, "BUY- #"), xloc.bar_index, yloc.abovebar, color.lime, label.style_triangleup, color.lime, size.small)

enter image description here

答案 1 :(得分:0)

由于先前的回答,我设法解决了我的问题。谢谢LucF!

我将IF语句与“ label.new”和“ tostring”组合在一起,而没有plot命令,以获得我想要的东西。似乎工作正常。

这解决了它:

if close > close[1] and open-low >= 10    
    label.new(bar_index, na, tostring(Candle_Bull_Score, "BUY- #"), xloc.bar_index, yloc.belowbar, color.lime, label.style_triangleup, color.lime, size.small)