我是pine脚本的新手,并且希望在趋势切换(绿色到红色以及红色到绿色)时发出一次警报。
谁可以帮助我对此脚本发出警报?
//@version=4
study("Hilo script with alert",overlay=true)
period=input(10,"Period")
shift=input(1,"Shift")
exp=input(false,"EMA")
max=exp?ema(high[shift],period):sma(high[shift],period)
min=exp?ema(low[shift],period):sma(low[shift],period)
pos=close>max?-1:close<min?1:0
pos:=pos==0?nz(pos[1]):pos
hilo=pos==1?max:min
plotbar(hilo,hilo,hilo,hilo,color=pos==1?color.red:color.green)
非常感谢您提前提供的所有帮助
我的想法是这样的:
sell = pos:=1
buy = pos==1
alertcondition(sell,title='sell', message='sell')
alertcondition(buy,title='buy', message='buy')
但我会向每支蜡烛发出警报。这不是我想要的。
在互联网上进行研究之后,我发现了这一点:
// Deternine if we are currently LONG
isLong = false
isLong := nz(isLong[1], false)
// Determine if we are currently SHORT
isShort = false
isShort := nz(isShort[1], false)
// Buy if the buy signal is triggered and we are not already long
buySignal = not isLong
// Sell if the sell signal is triggered and we are not already short
sellSignal= not isShort
if (buySignal)
isLong := true
isShort := false
if (sellSignal)
isLong := false
isShort := true
alertcondition(buySignal,title='buy',message='buy')
alertcondition(sellSignal,title='sell', message='sell')
但是我在Tradingview中遇到一些错误:
//@version=4
study("Hilo script with alertr",overlay=true)
period=input(8,"Period")
shift=input(1,"Shift")
exp=input(false,"Exponential moving average")
max=exp?ema(high[shift],period):sma(high[shift],period)
min=exp?ema(low[shift],period):sma(low[shift],period)
pos=close>max?-1:close<min?1:0
isLong:=pos==0?nz(pos[1]):pos
isShort=pos==1?max:min
plotbar(isShort,isShort,isShort,isShort,color=pos==1?color.red:color.green)
// Deternine if we are currently LONG
isLong = false
isLong := nz(isLong[1], false)
// Determine if we are currently SHORT
isShort = false
isShort := nz(isShort[1], false)
// Buy only if the buy signal is triggered and we are not already long
buySignal = not isLong
// Sell only if the sell signal is triggered and we are not already short
sellSignal= not isShort
if (buySignal)
isLong := true
isShort := false
if (sellSignal)
isLong := false
isShort := true
alertcondition(sellSignal,title='sell', message='sell')
alertcondition(buySignal,title='buy', message='buy')
在您的宝贵反馈后,我改写了以下几行:
isLong=pos==0?nz(pos[1]):pos
isShort=pos==1?max:min
plotbar(isShort,isShort,isShort,isShort,color=pos==1?color.red:color.green)
// Deternine if we are currently LONG
isLong = false
isLong := nz(isLong[1], false)
// Determine if we are currently SHORT
isShort = false
isShort := nz(isShort[1], false)
现在Iam在第14行中出现一个标记为>>>>的错误:
// Deternine if we are currently LONG
>>>>isLong = false
isLong := nz(isLong[1], false)
答案 0 :(得分:0)
仅当您声明变量时,才应使用=
运算符。之后,当您想给变量赋值时,应使用:=
运算符。
如果使用=
运算符将值分配给先前定义的变量,则会出现以下错误:
第x行:已经声明了变量
xxx
。
如果在不声明变量的情况下使用:=
运算符,则会出现以下错误:
第x行:未声明的标识符
xxx
;
此脚本将仅触发一次警报。为此,我添加了两个plotshape()
。
//@version=4
study("Hilo script with alert",overlay=true)
period=input(10,"Period")
shift=input(1,"Shift")
exp=input(false,"EMA")
var isLong = false
var isShort = false
max=exp?ema(high[shift],period):sma(high[shift],period)
min=exp?ema(low[shift],period):sma(low[shift],period)
buySignal = not isLong and (close >= max) // Buy when close >= max and we are not already long
sellSignal = not isShort and (close < min) // Sell when close < min and we are not already short
if (buySignal) // Set the flags for buy condition
isLong := true
isShort := false
if (sellSignal) // Set the flags for sell condition
isLong := false
isShort := true
plotshape(series=buySignal, text="BUY", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape(series=sellSignal, text="SELL", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small)
alertcondition(buySignal, title="BUY", message="BUY")
alertcondition(sellSignal, title="SELL", message="SELL")
如您所见,脚本仅在指示器更改颜色时才会绘制“买入”或“卖出”。