警报条件发生后,当条件从true/false更改时.当前警告任一条件为真.当它改变时不 [英] After alert condition when condition changes from true/false. Currently alerts if either condition is true. Not when it changes

查看:204
本文介绍了警报条件发生后,当条件从true/false更改时.当前警告任一条件为真.当它改变时不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是松木交易视图脚本的一部分.在'//Condition'之后的脚本上,我希望仅在条件从长到短或从短到长变化时才生成警报.不像现在那样,每根蜡烛的尽头都是,因为一个条件总是正确的. 这已更改为研究.

This is part of a pine script for tradingview. On the script after '//Condition', I want an alert to generate only when the condition changes from long to short or short to long. Not the end of each candle as it does now, as one condition is always true. This has been changed to a study.

threshold = input(title="Threshold", type=float, defval=0.0014, step=0.0001)

buying  = l3_0 > threshold ? true : l3_0 < -threshold ? false : buying[1]
///// T edit
selling = l3_0 > -threshold ? true : l3_0 < threshold ? false : 
selling[1] //// T edit END

hline(0, title="base line")
bgcolor(l3_0 > 0.0014 ? green : l3_0 < -0.0014 ? red : gray, transp=20)
bgcolor(buying ? green : red, transp=20)
plot(l3_0, color=silver, style=area, transp=75)
plot(l3_0, color=aqua, title="prediction")

/////     Stragegy     
/////////////////////////////////////////////////////
//longCondition = buying
//if (longCondition)
    //strategy.entry("Long", strategy.long)

//shortCondition = buying != true
//if (shortCondition)
    //strategy.entry("Short", strategy.short)

//////警报///////////////////////////////////////////////////////alertcondition(条件,标题,消息)

///// Alerts ///////////////////////////////////////////////////////alertcondition(condition, title, message)

//Condition
long  = l3_0 > 0.0014
short = l3_0 < -0.0014


alertcondition(long, title = "ANN Long", message= "ANN Long")
alertcondition(short, title = "ANN Short", message= "ANN Short")

推荐答案

让我们看一个使用MACD的较小示例.每当delta>= 0时,我们都想做多,当delta<0时,我们要做空.另外,除非触发相反的信号(输入一次并等待相反的信号),否则我们希望保持原位.

Let's look at a smaller example using MACD. We want to go long whenever delta is >= 0 and go short whenever delta is <0. Also, we would like to stay in our position unless the opposite signal is triggered (enter once and wait for the opposite signal).

您的代码如下:

//@version=3
study("My Script", overlay=true)

// Get the inputs
MACDLengthMACD = input(title="MACD Length", defval=9, minval=1, maxval=100)
fastLengthMACD = input(title="MACD Fast Length", defval=12, minval=1, maxval=100)
slowlengthMACD = input(title="MACD Slow Length", defval=26, minval=1, maxval=100)

// Standard MACD calculations
MACD = ema(close, fastLengthMACD) - ema(close, slowlengthMACD)
aMACD = ema(MACD, MACDLengthMACD)
deltaMACD = MACD - aMACD

buySignal = (deltaMACD >= 0)
sellSignal= (deltaMACD < 0)

plotshape(series=buySignal, text="BUY", style=shape.triangleup, location=location.belowbar, color=green, size=size.small)
plotshape(series=sellSignal, text="SELL", style=shape.triangledown, location=location.abovebar, color=red, size=size.small)

在这种情况下,您会收到多个买入"或卖出"信号,因为只要条件为truebuySignalsellSignal就会为true.

In this case, you will get multiple BUY or SELL signals because buySignal and sellSignal will be true as long as their conditions are true.

但是,为了仅触发一个买入或卖出信号,这些信号应仅持续1个交易日为true.为此,可以使用另一个变量(下面的代码中的isLongisShort)并使用历史记录引用运算符[]来确定您以前是LONG还是SHORT.

However, those signals should be true for one bar only in order to trigger only one BUY or SELL signal. To accomplish that, you can use another variable (isLong, isShort in below code) and use history reference operator [] to determine if you were previously LONG or SHORT.

然后,仅当您还没有买入时才触发买入信号,仅当您还没卖出时才触发卖出信号.这样,您将只会收到一个买入或卖出信号.

Then, only trigger your BUY signal if you are not already LONG and only trigger your SELL signal if you are not already SHORT. This way you will get only one BUY or SELL signal.

//@version=3
study("My Script", overlay=true)

// Get the inputs
MACDLengthMACD = input(title="MACD Length", defval=9, minval=1, maxval=100)
fastLengthMACD = input(title="MACD Fast Length", defval=12, minval=1, maxval=100)
slowlengthMACD = input(title="MACD Slow Length", defval=26, minval=1, maxval=100)

// Standard MACD calculations
MACD = ema(close, fastLengthMACD) - ema(close, slowlengthMACD)
aMACD = ema(MACD, MACDLengthMACD)
deltaMACD = MACD - aMACD

// 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 and (deltaMACD >= 0)

// Sell only if the sell signal is triggered and we are not already short
sellSignal= not isShort and (deltaMACD < 0)

if (buySignal)
    isLong := true
    isShort := false

if (sellSignal)
    isLong := false
    isShort := true

plotshape(series=buySignal, text="BUY", style=shape.triangleup, location=location.belowbar, color=green, size=size.small)
plotshape(series=sellSignal, text="SELL", style=shape.triangledown, location=location.abovebar, color=red, size=size.small)

这将导致:

这篇关于警报条件发生后,当条件从true/false更改时.当前警告任一条件为真.当它改变时不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆