How to add chart based study alert (Fib retracement)


0
0

Hi Mr. Hahn. First of all thank you for all your help. I use a script that plots Fibonacci retracement levels and I wanted to know if it was possible to add an alert when the price reaches or goes below the 78.6 level and when the price goes above the high. Below is the script. Thanks

def length = 1;
def na = Double.NaN;

#def DayHigh = HighestAll(high);
#def DayLow = LowestAll(low);
def DayHigh = HighestAll(high);
def DayLow = LowestAll(low);
plot dh = DayHigh;
plot dl = DayLow;

def hi = GetMaxValueOffset(high);
def lo = GetMinValueOffset(low);

# HI AND LOW VALUES
def hitime = GetMaxValueOffset(DayHigh , length);
def lotime = GetMinValueOffset(DayLow , length);
def updown = if hitime >= lotime then 1 else 0;

# FIB DEFINITIONS
def ud23 = If ( updown, 0.214, (1 – 0.214));
def ud38 = If ( updown, 0.382, (1 – 0.382));
def ud61 = If ( updown, 0.618, (1 – 0.618));
def ud78 = If ( updown, 0.764, (1 – 0.764));
def ud1130 = If ( updown, 1.13, (1 – 1.13));
def ud1236 = If ( updown, 1.236, (1 – 1.236));
def ud1382 = If ( updown, 1.382, (1 – 1.382));
def ud1618 = If ( updown, 1.618, (1 – 1.618));

# FIB PLOTS
plot uFib50 = (DayHigh – DayLow) / 2 + DayLow;
plot uFib786 = (DayHigh – DayLow) * ud23 + DayLow;
plot uFib618 = (DayHigh – DayLow) * ud38 + DayLow;
plot uFib382 = (DayHigh – DayLow) * ud61 + DayLow;
plot uFib23 = (DayHigh – DayLow) * ud78 + DayLow;
#plot uFib1130 = if showExtended is false then na else (DayHigh – DayLow) * ud1130 + DayLow;
#plot uFib1236 = if showExtended is false then na else (DayHigh – DayLow) * ud1236 + DayLow;
#plot uFib1382 = if showExtended is false then na else (DayHigh – DayLow) * ud1382 + DayLow;
#plot uFib1618 = if showExtended is false then na else (DayHigh – DayLow) * ud1618 + DayLow;

AddLabel(yes, Concat(“23.6% : “, uFib23), Color.GRAY);
AddLabel(yes, Concat(“38.2% : “, uFib382), Color.PLUM);
AddLabel(yes, Concat(“50.0% : “, uFib50), Color.DARK_ORANGE);
AddLabel(yes, Concat(“61.8% : “, uFib618), Color.BLUE);
AddLabel(yes, Concat(“78.6% : “, uFib786), Color.GRAY);

# HIDE TITLES
uFib50.HideTitle();
uFib23.HideTitle();
uFib382.HideTitle();
uFib618.HideTitle();
uFib786.HideTitle();
#uFib1130.HideTitle();
#uFib1236.HideTitle();
#uFib1382.HideTitle();
#uFib1618.HideTitle();

# LINE COLOR
dl.SetDefaultColor(Color.BLACK);
uFib23.SetDefaultColor(Color.GRAY);
uFib382.SetDefaultColor(Color.PLUM);
uFib50.SetDefaultColor(Color.DARK_ORANGE);
uFib618.SetDefaultColor(Color.BLUE);
uFib786.SetDefaultColor(Color.GRAY);
dh.SetDefaultColor(Color.BLACK);

# CONDITIONS
def condition0 = close <= dl;
def condition1 = close <= uFib23;
def condition2 = close <= uFib382;
def condition3 = close <= uFib50;
def condition4 = close <= uFib618;
def condition5 = close <= uFib786;
def condition6 = close <= dh;
def switchCase = condition0 + condition1 + condition2 + condition3 + condition4 + condition5 + condition6;

# UPPER BOUND DEFINITION
def upperBound = if switchCase == 6 then 0.236 else if switchCase == 5 then 0.382 else if switchCase == 4 then 0.50 else if switchCase == 3 then 0.618 else if switchCase == 2 then 0.786 else if switchCase == 1 then 1.0 else na;

# UPPER R DEFINITION
def rUpper = if switchCase == 6 then 6 else if switchCase == 5 then 2 else if switchCase == 4 then 3 else if switchCase == 3 then 4 else if switchCase == 2 then 4 else if switchCase == 1 then 6 else na;
plot rUpperP = rUpper;
rUpperP.Hide();

# LOWER BOUND DEFINITION
def lowerBound = if switchCase == 6 then 0.0 else if switchCase == 5 then 0.236 else if switchCase == 4 then 0.382 else if switchCase == 3 then 0.500 else if switchCase == 2 then 0.618 else if switchCase == 1 then 0.786 else na;

# LOWER R DEFINITION
def rLower = if switchCase == 6 then 5 else if switchCase == 5 then 1 else if switchCase == 4 then 2 else if switchCase == 3 then 3 else if switchCase == 2 then 4 else if switchCase == 1 then 5 else na;
plot rLowerP = rLower;
rLowerP.Hide();

Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on January 6, 2019 10:45 pm
466 views
0
Private answer

Well here is the line of code that should give you the touch/cross of the 78,.6 level.

Alert(close[1] > uFib786[1] and close <= uFib786, "Touch 78.6", Alert.BAR, Sound.RING);

I’m not sure yet how to interpret your other request to alert when price goes above the high. The code is set to draw fibs based on the day’s high and day’s low. When price makes a new high on the day the code resets all the levels. Is this when you want to be alerted?

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on January 10, 2019 4:55 pm
0

Hi, thank you for your response. Yes, I would like to be alerted when the price makes a new high on the day. Thanks

( at January 11, 2019 8:10 am)
0

For new intraday high/low, we covered that in a video. You can get details and the code here: https://www.hahn-tech.com/thinkorswim-alert-high-low-version-two/

( at January 13, 2019 9:00 am)