简体   繁体   中英

How to set a variable to change upon a condition being met - Pine Script

I am building a script using Pine-Script language and the EMA 20 and EMA 50 trading strategy, I want to when an open of a candle stick is larger than EMA 20, and the EMA 20 is larger than EMA 50, the slower moving line, the plot a 'Start Long' trade plot below the candle stick, once this happens I want the code to set a 'GoneLong' variable value to TRUE, so if the next candle opens under the same conditions as the previous it wont trigger another 'Start Long' trade on the graph, as this has already been started in the previous candle.

I then wanted the GoneLong variable to be set back to 'FALSE' when a candle stick closes below the EMA 50 line, this then alows a new 'Start Long' plot to be made on the graph if the start long conditions are met.

I have tried to code this below, (Please see code)

// © SDudders

//@version=4
study(title="Double EMA + Alerts", shorttitle="DEMA", overlay=true, resolution="")

length_20 = input(20, minval=1)
src_20 = input(close, title="Source")
e1_20 = ema(src_20, length_20)
e2_20 = ema(e1_20, length_20)
dema_20 = 2 * e1_20 - e2_20
plot(dema_20, "DEMA 20", color=color.green)

length_50 = input(50, minval=1)
src_50 = input(close, title="Source")
e1_50 = ema(src_50, length_50)
e2_50 = ema(e1_50, length_50)
dema_50 = 2 * e1_50 - e2_50
plot(dema_50, "DEMA 50", color=color.red)


GoneLong = "FALSE"

Start_Long = (GoneLong == "FALSE" and open >= dema_20 and dema_20 > dema_50), GoneLong := "TRUE"
plotshape(series=Start_Long,text="START LONG", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)

End_Long = (GoneLong == "TRUE" and close < dema_50), GoneLong := "FALSE"
plotshape(series=End_Long,text="END LONG", style=shape.triangledown, location=location.abovebar, color=color.green, size=size.small)

This code does not work properly... Every time a new candle stick opens above EMA 20 and EMA 50, it plots a new 'Start Long' plot on the chart.

How can I get the code to only plot on the chart when this happens up until the 'End_Long' conditions are met and not flag on every candle?

Any help is much appreciated on this.

See screenshot https://i.stack.imgur.com/Ua2eq.png

Cheers:)

Slightly modified your script.

-Removed the GoneLong string variable

-Re-assigned Start_Long and End_Long variables in the local if scope.

-Added a check to show only non-consecutive calls.

//@version=4
study(title="Double EMA + Alerts", shorttitle="DEMA", overlay=true, resolution="")

length_20 = input(20, minval=1)
src_20 = input(close, title="Source")
e1_20 = ema(src_20, length_20)
e2_20 = ema(e1_20, length_20)
dema_20 = 2 * e1_20 - e2_20
plot(dema_20, "DEMA 20", color=color.green)

length_50 = input(50, minval=1)
src_50 = input(close, title="Source")
e1_50 = ema(src_50, length_50)
e2_50 = ema(e1_50, length_50)
dema_50 = 2 * e1_50 - e2_50
plot(dema_50, "DEMA 50", color=color.red)

var bool Start_Long = na
var bool End_Long = na

if (open >= dema_20 and dema_20 > dema_50)
    Start_Long := true
    End_Long := false

plotshape(series=Start_Long and not nz(Start_Long[1]),text="START LONG", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)

if close < dema_50
    Start_Long := false
    End_Long := true

plotshape(series=End_Long and not nz(End_Long[1]),text="END LONG", style=shape.triangledown, location=location.abovebar, color=color.green, size=size.small)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM