1

I am attempting to add a specific alert system for the CM_Stochastic Highlight Bars indicator by Chris Moody, to get notified when 1) a crossUpAll is higher than the former one 2) a crossDownAll is lower than the former one

I have written this but it does not triggers an alert :

pos_change = (crossUpAll[1] >= crossUpAll) 
neg_change = (crossDownAll[1] <= crossDownAll)

alertcondition (pos_change, "higher high") 
alertcondition (neg_change, "lower low")

Would you suggest an alternative ?

Michel_T.
  • 2,741
  • 5
  • 21
  • 31
Jordan
  • 11
  • 1
  • `crossUpAll` and `crossDownAll` are pseudo-booleans with 0/1 values. No level is associated with them. If you want an alert when they occur, just use them as-is for the `alertcondition()` condition. If you want to track the level a line (`k` or `d` for example) when one of those events occurs, it can be done, but we need to know which one to help. – PineCoders-LucF Jan 09 '20 at 19:24
  • Thanks for your comment, the event is a classic stochastic cross (crossUpAll = (k[1] < d[1] and k > d) ? 1 : 0) BUT I am actually interested in the plot because it is located in relation with the price of the asset (location=location.belowbar). So I want to define the moment when 1) a positive stochastic cross happens and the asset price is higher than when the former positive stochastic cross happened (ex: first positive stoch cross when EURUSD is at 1.10 and the next positive stoch cross when EURUSD is above 1.10) 2) same for the negative stoch cross – Jordan Jan 09 '20 at 20:40

1 Answers1

0

This saves the price whenever a cross occurs and only triggers the alert when the chart symbol's price is higher/lower than at previous cross. Note that the type of cross used is the one where k isn't required to be below/above the low/high lines, so the default script's settings need to be changed to show that type of cross. Script was converted to v4:

//@version=4
//Created by ChrisMoody on October 23, 2014 by user request - belgusinc
//Changes Barcolor when Stochastic is Overbought Oversold.

//Necessary for Highlight Bars
//Only Necessary if you want you want Stochastic Croses
study(title="CM_Stochastic_Highlight Bars", shorttitle="CM_Stoch_HighlightBars v4", overlay=true)
len = input(14, minval=1, title="Length for Stochastic") 
smoothK = input(3, minval=1, title="SmoothK for Stochastic")
smoothD = input(3, minval=1, title="SmoothD for Stochastic")
upLine = input(80, minval=50, maxval=90, title="Upper Line Value?")
lowLine = input(20, minval=10, maxval=50, title="Lower Line Value?")

//Not Necessary, In Inputs Tab, capability to turn On/Off Highlight Bars, and Both Crosses
//These are all checkboxes in inputs tab....Show Barcolor Highlights, Show Background Hghlight, Plot B and S for Buy Sell 
sbc = input(true, title="Show Price Bar highlight When Stoch is Above/Below High/Low Lines?")
sbh = input(false, title="Show Background highlight When Stoch is Above/Below High/Low Lines?")
sch = input(false, title="Show Back Ground Highlights When Stoch Cross - Strict Criteria - K Greater/LesThan High/Low Line - Crosses D ?")
sl = input(true, title="Show 'B' and 'S' Letters When Stoch Crosses High/Low Line & D?")
sac = input(false, title="Show Back Ground Highlights When Stoch Cross - Any Cross?")
sacl = input(false, title="Show 'B' and 'S' Letters When Stoch Crosses - Any Cross?")

//Necessary for Highlight Bars
//Stoch formula
k = sma(stoch(close, high, low, len), smoothK)
d = sma(k, smoothD)

//Necessary for Highlight Bars
//aboveline = OverBought, belowline = Oversold Definitions
aboveLine = k > upLine ? 1 : 0
belowLine = k < lowLine ? 1 : 0

//Only Necessary if you want you want Stochastic Croses
//Definition for Cross when Above High-Low lines
crossUp = (k[1] < d[1] and k[1] < lowLine[1]) and (k > d)  ? 1 : 0
crossDn = (k[1] > d[1] and k[1] > upLine[1]) and (k < d) ? 1 : 0

//Only Necessary if you want you want Stochastic Croses
//Definition for Cross that doesn't have to be above or below High and Low line.
crossUpAll = (k[1] < d[1] and k > d) ? 1 : 0
crossDownAll = (k[1] > d[1] and k < d) ? 1 : 0
var lastCrossUpPrice = 0.
var lastCrossDnPrice = 10e10
crossUpAlert = false
crossDnAlert = false
if crossUpAll and not crossUpAll[1]
    crossUpAlert := close > lastCrossUpPrice
    lastCrossUpPrice := close
if crossDownAll and not crossDownAll[1]
    crossDnAlert := close < lastCrossDnPrice
    lastCrossDnPrice := close

//Only Necessary if you want background Highlighting also - Take out the sbh/sbc and part
//BackGround Highlights based on Above/Below Lines, Strict Cross Up/Down
//BackGroound Color Plots
bgcolor(sbh and aboveLine ? color.red : na, transp=70)
bgcolor(sbh and belowLine ? color.lime : na, transp=70)
bgcolor(sch and crossUp ? color.lime : na, transp=40)
bgcolor(sch and crossDn ? color.red : na, transp=40)

//Only Necessary if you want background Highlighting also
//plots bgcolor Cross with no filter
bgcolor(sac and crossUpAll ? color.lime : na, transp=40)
bgcolor(sac and crossDownAll ? color.red : na, transp=40)

//Necessary for Highlight Bars
//Highlight Bar Definitions
overBought() => sbc and aboveLine 
overSold() => sbc and belowLine 

//Necessary for Highlight Bars
//Highlight Bar Plot Statement based on Above Definitions
barcolor(overBought() ? color.orange : overSold() ? color.fuchsia : na)

//Not Necessary if you just want Highlight Bars  - Extra Feature
//These are just the Letters B and Sell Based on Crosses
plotchar(sl and crossUp ? crossUp : na, title="Buy Signal Strict Criteria", char='B', location=location.belowbar, color=color.lime, transp=0, offset=0)
plotchar(sl and crossDn ? crossDn : na, title="Sell Signal Strict Criteria", char='S', location=location.abovebar, color=color.red, transp=0, offset=0)
plotchar(sacl and crossUpAll ? crossUpAll : na, title="Buy Signal Any Cross Up", char='B', location=location.belowbar, color=color.lime, transp=0, offset=0)
plotchar(sacl and crossDownAll ? crossDownAll : na, title="Sell Signal Any Cross Down", char='S', location=location.abovebar, color=color.red, transp=0, offset=0)

alertcondition (crossUpAlert, "higher high")
alertcondition (crossDnAlert, "lower low")

// Debugging.
plotchar(crossUpAll, "crossUpAll", "•", location.bottom, transp = 0)
plotchar(crossDownAll, "crossDownAll", "•", location.top, transp = 0)
plotchar(crossUpAlert, "crossUpAlert", "▲", location.bottom, transp = 60)
plotchar(crossDnAlert, "crossDnAlert", "▼", location.top, transp = 60)

Debugging plots are included at the end. Triangles show where an alert would trigger, while the dots show all places where crosses occur.

enter image description here

PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21