declare upper;
#===================================================
# INPUTS & GLOBAL VARIABLES
#===================================================
input show_bubbles = {default Right, Left, Labels, None};
input show_retracements = {Default All, Closest, None};
input Line_color = {Default Magenta, Cyan, Pink, White, Gold, Red, Green, Dark_Gray, Yellow};
input _H = high;
input _L = low;
input Expand_Right = Yes;
input Extend_Left = no;
input Coeffient_0 = 0.000;
input Coeffient_1 = .236;
input Coeffient_2 = .382;
input Coeffient_3 = .500;
input Coeffient_4 = .618;
input Coeffient_5 = .786;
input Coeffient_6 = 1.000;
def price = close;
def _nan = Double.NaN;
def a = HighestAll(high);
def b = LowestAll(low);
def _BN = BarNumber();
def c = if high == a then _BN else Double.NaN;
def d = if low == b then _BN else Double.NaN;
rec _HN = CompoundValue(1, if IsNaN(c) then _HN[1] else c, c);
def _HNA = HighestAll(_HN);
rec _LN = CompoundValue(1, if IsNaN(d) then _LN[1] else d, d);
def _LNA = LowestAll(_LN);
def x = AbsValue(_LNA - _HNA );
def slope = (a - b) / x;
def slopelow = (b - a) / x;
def line = b + (slope * (_BN - _LN));
def linelow = a + (slopelow * (_BN - _HN));
def currentlinelow = if _BN <= _LNA then linelow else Double.NaN;
def currentline = if _BN <= _HNA then line else Double.NaN;
#shortened variables for retracements script call
def _CFF0 = Coeffient_0;
def _CFF1 = Coeffient_1;
def _CFF2 = Coeffient_2;
def _CFF3 = Coeffient_3;
def _CFF4 = Coeffient_4;
def _CFF5 = Coeffient_5;
def _CFF6 = Coeffient_6;
def _ER = Expand_Right;
def _EL = Extend_Left;
def _HH = a;
def _LL = b;
def _DELTA =
if _HNA > _LNA then -1 else
if _HNA < _LNA then 1 else _nan;
#===================================================
# INPUT PROCESSING
#===================================================
#chart bubble and retracement line parameters
def position;
def placement;
def shade;
def HideRet;
def closest;
Switch (Line_Color){
Case Magenta:
shade = 0;
Case Cyan:
shade = 1;
Case Pink:
shade = 2;
Case White:
shade = 3;
Case Gold:
shade = 4;
Case Red:
shade = 5;
Case Green:
shade = 6;
Case Dark_Gray:
shade = 7;
Case Yellow:
shade = 8;
};
switch (show_bubbles){
case None:
position = _nan;
# shade = 9;
placement = _nan;
case Labels:
position = _nan;
# shade = 9;
placement = -1;
case Right:
position =
if _DELTA != 0 and _BN == HighestAll(_BN)
then Highestall(_BN) else _nan;
# shade = 4;
placement = 0;
case Left:
position =
if _DELTA > 0 and _BN == HighestAll(_LNA)
then highestall(_LNA) else
if _DELTA < 0 and _BN == HighestAll(_HNA)
then highestall(_HNA) else _nan ;
# shade = 5;
placement = 1;
};
#===================================================
# RETRACEMENTS SCRIPT
#===================================================
script findRetracement {
input _DELTA = 0;
input _CFF = 0.000;
input _ER = 0;
input _EL = 0;
input _LL = 0;
input _HH = 0;
input _HNA = 0;
input _LNA = 0;
input _BN = 0;
def _nan = Double.NaN;
def day = GetDay() == GetLastDay();
def month = GetMonth() == GetLastMonth();
def year = GetYear() == GetLastYear();
def isToday = If(day and month and year, 1, 0);
def istodaybN = HighestAll(if isToday then _BN else Double.NaN);
def _range = _HH - _LL;
plot result =
if _DELTA < 0 and !_ER and !_EL and _BN >= _HNA and _BN <= istodaybN
then HighestAll(_LL + (_range * _CFF))
else if _DELTA > 0 and !_EL and !_ER and _BN >= _LNA and _BN <= istodaybN
then HighestAll(_HH - (_range * _CFF)) #
else if _DELTA < 0 and _ER and !_EL and _BN >= _HNA
then HighestAll(_LL + (_range * _CFF))
else if _DELTA > 0 and _ER and !_EL and _BN >= _LNA
then HighestAll(_HH - (_range * _CFF))
else if _DELTA < 0 and !_ER and _EL and _BN <= istodaybN
then HighestAll(_LL + (_range * _CFF))
else if _DELTA > 0 and _EL and !_ER and _BN <= istodaybN
then HighestAll(_HH - (_range * _CFF))
else if _DELTA < 0 and _ER and _EL
then HighestAll(_LL + (_range * _CFF))
else if _DELTA > 0 and _ER and _EL
then HighestAll(_HH - (_range * _CFF))
else _nan;
}
;
#===================================================
# PLOTS
#===================================================
plot FibFan =
if _DELTA < 0 then currentlinelow
else if _DELTA > 0 then currentline
else Double.NaN;
plot Retracement0 =
findRetracement(_DELTA, _CFF0, _ER, _EL, _LL, _HH, _HNA, _LNA, _BN)."result";
plot Retracement1 =
findRetracement(_DELTA, _CFF1, _ER, _EL, _LL, _HH, _HNA, _LNA, _BN)."result";
plot Retracement2 =
findRetracement(_DELTA, _CFF2, _ER, _EL, _LL, _HH, _HNA, _LNA, _BN)."result";
plot Retracement3 =
findRetracement(_DELTA, _CFF3, _ER, _EL, _LL, _HH, _HNA, _LNA, _BN)."result";
plot Retracement4 =
findRetracement(_DELTA, _CFF4, _ER, _EL, _LL, _HH, _HNA, _LNA, _BN)."result";
plot Retracement5 =
findRetracement(_DELTA, _CFF5, _ER, _EL, _LL, _HH, _HNA, _LNA, _BN)."result";
plot Retracement6 =
findRetracement(_DELTA, _CFF6, _ER, _EL, _LL, _HH, _HNA, _LNA, _BN)."result";
switch (show_retracements){
case All:
HideRet = 0;
closest = 0;
Case None:
HideRet = 1;
closest = 0;
Case Closest:
HideRet = 0;
closest = 1;
};
#apply hiding/showing parameters to retracement plots
FibFan.SetStyle(Curve.SHORT_DASH);
FibFan.AssignValueColor(Color.RED);
FibFan.HideBubble();
Retracement0.assignvaluecolor(GetColor(shade));
Retracement0.sethiding(HideRet);
Retracement1.assignvaluecolor(GetColor(shade));
Retracement1.sethiding(HideRet);
Retracement2.assignvaluecolor(GetColor(shade));
Retracement2.sethiding(HideRet);
Retracement3.assignvaluecolor(GetColor(shade));
Retracement3.sethiding(HideRet);
Retracement4.assignvaluecolor(GetColor(shade));
Retracement4.sethiding(HideRet);
Retracement5.assignvaluecolor(GetColor(shade));
Retracement5.sethiding(HideRet);
Retracement6.assignvaluecolor(GetColor(shade));
Retracement6.sethiding(HideRet);
#===================================================
# CHART BUBBLES & LABELS
#===================================================
AddChartBubble(
if position > 0 then position else _nan ,
Retracement0,
(_CFF0 * 100)+ "%" + " " + retracement0,
GetColor(shade),
placement);
AddChartBubble(
if position > 0 then position else _nan ,
Retracement1,
(_CFF1 * 100)+ "%" + " " + retracement1,
GetColor(shade),
placement);
AddChartBubble(
if position > 0 then position else _nan ,
Retracement2,
(_CFF2 * 100)+ "%" + " " + retracement2,
GetColor(shade),
placement);
AddChartBubble(
if position > 0 then position else _nan ,
Retracement3,
(_CFF3 * 100)+ "%" + " " + retracement3,
GetColor(shade),
placement);
AddChartBubble(
if position > 0 then position else _nan ,
Retracement4,
(_CFF4 * 100)+ "%" + " " + retracement4,
GetColor(shade),
placement);
AddChartBubble(
if position > 0 then position else _nan ,
Retracement5,
(_CFF5 * 100)+ "%" + " " + retracement5,
GetColor(shade),
placement);
AddChartBubble(
if position > 0 then position else _nan ,
Retracement6,
(_CFF6 * 100)+ "%" + " " + retracement6,
GetColor(shade),
placement);
#labels
addlabel(placement < 0, "Retracements: " +
(_CFF6 * 100) + "%" + " = " + retracement6 + "\n " +
(_CFF5 * 100) + "%" + " = " + retracement5 + "\n " +
(_CFF4 * 100) + "%" + " = " + retracement4 + "\n " +
(_CFF3 * 100) + "%" + " = " + retracement3 + "\n " +
(_CFF2 * 100) + "%" + " = " + retracement2 + "\n " +
(_CFF1 * 100) + "%" + " = " + retracement1 + "\n " +
(_CFF0 * 100) + "%" + " = " + retracement0
, color.yellow);
#individual labels instead of 1 long one
#addlabel(placement < 0, (_CFF6 * 100) + "%" + " = " + retracement6 , color.yellow);
#addlabel(placement < 0, (_CFF5 * 100) + "%" + " = " + retracement5 , color.yellow);
#addlabel(placement < 0, (_CFF4 * 100) + "%" + " = " + retracement4 , color.yellow);
#addlabel(placement < 0, (_CFF3 * 100) + "%" + " = " + retracement3 , color.yellow);
#addlabel(placement < 0, (_CFF2 * 100) + "%" + " = " + retracement2 , color.yellow);
#addlabel(placement < 0, (_CFF1 * 100) + "%" + " = " + retracement1 , color.yellow);
#addlabel(placement < 0, (_CFF0 * 100) + "%" + " = " + retracement0 , color.yellow);
#===================================================
# ALERTS
#===================================================
#===================================================
# HINTS
#===================================================
#hint show_retracements: presently only all or none is functioning. If 'closest' is selected it will show all.
#hint show_bubbles: determines if and where the labels for retracements are placed. Left = left side of retracement, Right = right side of retracement plot, Label = labels at the top of the chart, None = no labels of any kind. Currently, Labels has not been enabled.