HalSkyTrader
HalSkyTrader

Reputation: 45

Alertcondition based on the previous or 2 bars ago before EMA13/50 crossover

Please see the code below. I have two inputs. The first one is for when EMA13 crosses over EMA50, and the second input defines bars with high volume. I would like to inquire how it is possible to do the following:

  1. How can we show on the chart (without using the offset built-in function) any bar or bars with high volume occurring in the previous bar, or two bars ago, before EMA13 crosses over EMA50? The reason why I would like to do this without using the offset and plot functions together to shift the bar to the left, is because I guess the shift would be cosmetic and the actual value will not be moved. So, I cannot use it for the alertcondition function if I use offset.
  2. How can we create an alertcondition (not the alert function) which will be based on the following events?

Thank you very much

//@version=4
study(title="High volume with EMA13/50", shorttitle="High vol EMA13/50", overlay = true )
EMA13 = input(13, minval=1)
EMA50 = input(50, minval=1)

xPrice = close
xEMA13 = ema(xPrice, EMA13)
xEMA50 = ema(xPrice, EMA50)

plot(xEMA13, color=color.red, title="EMA 13")
plot(xEMA50, color=color.purple, title="EMA 50")

// Bar with high volume
smalength=input(21, "length", minval=1)
avrg=sma(volume,smalength)
volu1 = volume > avrg*1.5 and close>open
colu1=#006400
color = volu1 ? colu1 : na
barcolor(color)
plotshape(volu1 ? low : na, title="Bar with high volume", text="High\nVolume\n", style=shape.labelup, location=location.belowbar, color=colu1, textcolor=color.white, size=size.tiny)

EMA13crossesoverEMA50 = crossover(xEMA13, xEMA50)

plotshape(EMA13crossesoverEMA50 ? low : na, title="EMA13crossesoverEMA50", text="EMA(13,50)\nCrossover\n", style=shape.labelup, location=location.belowbar, color=color.green, textcolor=color.white, size=size.tiny)

// Question :1
// How can we show, on the chart, any bar or bars with high volume occurring in the previous bar, or two bars ago, before EMA13 crossses over EMA50?
// input for bar or bars with high volume that occur any time: "volu1"
// input for EMA13 crosses over EMA50: "EMA13crossesoverEMA50 "


// Question: 2
// How can we create an alertcondition (not the alert function), which will be based on the following events?
// First condition: EMA13 crossover over EMA50
// Second condition: There must be a bar or bar with high volume, in the last 2 bars preceding the EMA13/50 crossover

//alertcondition(condition, title="High Volume happened in the last 2 bars before EMA13/50 crossover", message="High volume before EMA13/50 crossover")

Upvotes: 0

Views: 815

Answers (1)

Bjorgum
Bjorgum

Reputation: 2310

What about something like this my friend? we can reference previous bars using [] after your condition. eg: (volu1[1] or volu1[2]) would check if your volume condition was true one bar previous or 2 bars ago. We can add that to your cross and only print a label and an alert if all are satisfied:

//@version=4
study(title="High volume with EMA13/50", shorttitle="High vol EMA13/50", 
overlay = true )
EMA13 = input(13, minval=1)
EMA50 = input(50, minval=1)

xPrice = close
xEMA13 = ema(xPrice, EMA13)
xEMA50 = ema(xPrice, EMA50)

plot(xEMA13, color=color.red, title="EMA 13")
plot(xEMA50, color=color.purple, title="EMA 50")

// Bar with high volume
malength=input(21, "length", minval=1)
avrg=sma(volume,smalength)
volu1 = volume > avrg*1.5 and close>open
colu1=#006400
color = volu1 ? colu1 : na
barcolor(color)

EMA13crossesoverEMA50 = crossover(xEMA13, xEMA50)

highCross = EMA13crossesoverEMA50 and (volu1[1] or volu1[2]) // volume on previous bar or bar before that 

plotshape(volu1 and not highCross ? low : na, title="Bar with high volume", text="High\nVolume\n", style=shape.labelup, location=location.belowbar, color=colu1, textcolor=color.white, size=size.tiny)

plotshape(highCross ? low : na, title="High Volume Cross", text="High\nCross\n", style=shape.labelup, location=location.belowbar, color=color.green, textcolor=color.white, size=size.tiny)

alertcondition(highCross, title="High Volume happened in the last 2 bars before EMA13/50 crossover", message="High volume before EMA13/50 crossover")

note that if we want to check the same bar of the cross then we would just say and volu1 etc.. you said previous bar, so i did 1 ago, but you can change this

Cheers and good luck with your trading and coding

Upvotes: 1

Related Questions