当条件从真/假变化时发出警报条件后.如果任一条件为真,当前会发出警报.不是当它改变 [英] After alert condition when condition changes from true/false. Currently alerts if either condition is true. Not when it changes

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

问题描述

这是用于交易视图的 pine 脚本的一部分.在'//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 时执行 long 并在 delta 时执行 short代码> 是 <代码><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)

在这种情况下,您将获得多个买入或卖出信号,因为只要 buySignalsellSignal 的条件为 true,它们就会为 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.

然而,这些信号应该为 true 仅用于一根柱线,以便仅触发一个买入或卖出信号.为此,您可以使用另一个变量(以下代码中的 isLongisShort)并使用历史引用运算符 [] 来确定您是否以前长或短.

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)

这将导致:

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

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