Author Message:
Harmonic, wave and Fibonacci indicator is designed to identify various harmonic patterns, wave formations, and Fibonacci retracements directly on your charts. The script offers a comprehensive toolset for traders who use technical analysis to spot potential market reversals and continuation patterns.
This study was converted from Tradingview:
https://www.tradingview.com/script/nor75vWR-Harmonic-wave-and-Fibonacci-Hunter-Algo/
CODE:
CSS:
#// Indicator For TOS
#// © Hunter_Algo
#indicator(title='Harmonic, wave and Fibonacci [Hunter Algo]', overlay=true)
# Converted by Sam4Cok@Samer800 - 08/2024
input extendLines = no;
input UseHeikinAshiCandles = no; #(false, title='Use Heikken Ashi Candles')
input useHigherTimeframe = yes; #(true, title='Use Alt Timeframe')
input showZigzagLine = yes;
input higherTimeframe = AggregationPeriod.THIRTY_MIN; #('60', title='Alt Timeframe')
input showPatterns = yes; #(true, title='Show Patterns')
input showFib0000 = yes; #(title='Display Fibonacci 0.000:', defval=true)
input showFib0236 = yes; #(title='Display Fibonacci 0.236:', defval=true)
input showFib0382 = yes; #(title='Display Fibonacci 0.382:', defval=true)
input showFib0500 = yes; #(title='Display Fibonacci 0.500:', defval=true)
input showFib0618 = yes; #(title='Display Fibonacci 0.618:', defval=true)
input showFib0764 = yes; #(title='Display Fibonacci 0.764:', defval=true)
input showFib1000 = yes; #(title='Display Fibonacci 1.000:', defval=true)
def na = Double.NaN;
def last = IsNaN(close);
def extend = if extendLines then no else last;
def current = GetAggregationPeriod();
def tf = Max(current, higherTimeframe);
def cTF = if !last then close(Period = tf) else cTF[1];
# Colors
DefineGlobalColor("labUp", CreateColor(76, 175, 80));
DefineGlobalColor("labDn", CreateColor(136, 14, 79));
DefineGlobalColor("fib1", GetColor(9));
DefineGlobalColor("fib2", CreateColor(255, 82, 82));
DefineGlobalColor("fib3", CreateColor(128,128, 0));
DefineGlobalColor("fib4", CreateColor(0 ,230,118));
DefineGlobalColor("fib5", CreateColor(0 ,137,123));
DefineGlobalColor("fib6", CreateColor(33 ,150,243));
DefineGlobalColor("fib7", GetColor(9));
# range
Script f_last_fib {
input _rate = 1;
input fib_range = 1;
input d = open;
input c = close;
def f_last_fib = if d > c then d - fib_range * _rate else d + fib_range * _rate;
plot out = f_last_fib;
}
#-- zigzag
script zigzag {
input useAltTF = yes;
input alTimeframe = AggregationPeriod.HOUR;
input useHA = no;
def current = GetAggregationPeriod();
def tf = if useAltTF then Max(current, alTimeframe) else current;
def haClose = ohlc4(Period = tf);
def haOpen = CompoundValue(1, (haOpen[1] + haClose[1]) / 2, (open(Period = tf)[1] + close(Period = tf)[1]) / 2);
def haHigh = Max(Max(high(Period = tf), haOpen), haClose);
def haLow = Min(Min(low(Period = tf), haOpen), haClose);
def _isUp = If(useHA, haClose, close(Period = tf)) >= If(useHA, haOpen, open(Period = tf));
def _isDown = If(useHA, haClose, close(Period = tf)) <= If(useHA, haOpen, open(Period = tf));
def _direction = if _isUp[1] and _isDown then -1 else
if _isDown[1] and _isUp then 1 else If(IsNaN(_direction[1]), 0 , _direction[1]);
def highest_1 = Highest(If(useHA, haHigh, high(Period = tf)), 2);
def lowest_1 = Lowest(If(useHA, haLow, low(Period = tf)), 2);
def _zigzag = if _isUp[1] and _isDown and _direction[1] != -1 then highest_1 else
if _isDown[1] and _isUp and _direction[1] != 1 then lowest_1 else Double.NaN;
plot out = _zigzag;
}
#-- Patternes
Script isBat {
input _mode = 0;
input xab = 0;
input xad = 0;
input abc = 0;
input bcd = 0;
input d = close;
input c = open;
def _xab = xab >= 0.382 and xab <= 0.5;
def _abc = abc >= 0.382 and abc <= 0.886;
def _bcd = bcd >= 1.618 and bcd <= 2.618;
def _xad = xad <= 0.618 and xad <= 1.000;
def isBat = _xab and _abc and _bcd and _xad and (if _mode == 1 then d < c else d > c);
plot out = if isNaN(isBat) then 0 else isBat;
}
Script isAntiBat {
input _mode = 0;
input xab = 0;
input xad = 0;
input abc = 0;
input bcd = 0;
input d = close;
input c = open;
def _xab = xab >= 0.500 and xab <= 0.886;
def _abc = abc >= 1.000 and abc <= 2.618;
def _bcd = bcd >= 1.618 and bcd <= 2.618;
def _xad = xad >= 0.886 and xad <= 1.000;
def isAntiBat = _xab and _abc and _bcd and _xad and (if _mode == 1 then d < c else d > c);
plot out = if isNaN(isAntiBat) then 0 else isAntiBat;
}
Script isAltBat {
input _mode = 0;
input xab = 0;
input xad = 0;
input abc = 0;
input bcd = 0;
input d = close;
input c = open;
def _xab = xab <= 0.382;
def _abc = abc >= 0.382 and abc <= 0.886;
def _bcd = bcd >= 2.0 and bcd <= 3.618;
def _xad = xad <= 1.13;
def isAltBat = _xab and _abc and _bcd and _xad and (if _mode == 1 then d < c else d > c);
plot out = if isNaN(isAltBat) then 0 else isAltBat;
}
Script isButterfly {
input _mode = 0;
input xab = 0;
input xad = 0;
input abc = 0;
input bcd = 0;
input d = close;
input c = open;
def _xab = xab <= 0.786;
def _abc = abc >= 0.382 and abc <= 0.886;
def _bcd = bcd >= 1.618 and bcd <= 2.618;
def _xad = xad >= 1.27 and xad <= 1.618;
def isButterfly = _xab and _abc and _bcd and _xad and (if _mode == 1 then d < c else d > c);
plot out = if isNaN(isButterfly) then 0 else isButterfly;
}
Script isAntiButterfly {
input _mode = 0;
input xab = 0;
input xad = 0;
input abc = 0;
input bcd = 0;
input d = close;
input c = open;
def _xab = xab >= 0.236 and xab <= 0.886;
def _abc = abc >= 1.130 and abc <= 2.618;
def _bcd = bcd >= 1.000 and bcd <= 1.382;
def _xad = xad >= 0.500 and xad <= 0.886;
def isAntiButterfly = _xab and _abc and _bcd and _xad and (if _mode == 1 then d < c else d > c);
plot out = if isNaN(isAntiButterfly) then 0 else isAntiButterfly;
}
Script isABCD {
input _mode = 0;
input xab = 0;
input xad = 0;
input abc = 0;
input bcd = 0;
input d = close;
input c = open;
def _abc = abc >= 0.382 and abc <= 0.886;
def _bcd = bcd >= 1.13 and bcd <= 2.618;
def isABCD = _abc and _bcd and (if _mode == 1 then d < c else d > c);
plot out = if isNaN(isABCD) then 0 else isABCD;
}
Script isGartley {
input _mode = 0;
input xab = 0;
input xad = 0;
input abc = 0;
input bcd = 0;
input d = close;
input c = open;
def _xab = xab >= 0.5 and xab <= 0.618;
def _abc = abc >= 0.382 and abc <= 0.886;
def _bcd = bcd >= 1.13 and bcd <= 2.618;
def _xad = xad >= 0.75 and xad <= 0.875;
def isGartley = _xab and _abc and _bcd and _xad and (if _mode == 1 then d < c else d > c);
plot out = if isNaN(isGartley) then 0 else isGartley;
}
Script isAntiGartley {
input _mode = 0;
input xab = 0;
input xad = 0;
input abc = 0;
input bcd = 0;
input d = close;
input c = open;
def _xab = xab >= 0.500 and xab <= 0.886;
def _abc = abc >= 1.000 and abc <= 2.618;
def _bcd = bcd >= 1.500 and bcd <= 5.000;
def _xad = xad >= 1.000 and xad <= 5.000;
def isAntiGartley = _xab and _abc and _bcd and _xad and (if _mode == 1 then d < c else d > c);
plot out = if isNaN(isAntiGartley) then 0 else isAntiGartley;
}
Script isCrab {
input _mode = 0;
input xab = 0;
input xad = 0;
input abc = 0;
input bcd = 0;
input d = close;
input c = open;
def _xab = xab >= 0.500 and xab <= 0.875;
def _abc = abc >= 0.382 and abc <= 0.886;
def _bcd = bcd >= 2.000 and bcd <= 5.000;
def _xad = xad >= 1.382 and xad <= 5.000;
def isCrab = _xab and _abc and _bcd and _xad and (if _mode == 1 then d < c else d > c);
plot out = if isNaN(isCrab) then 0 else isCrab;
}
Script isAntiCrab {
input _mode = 0;
input xab = 0;
input xad = 0;
input abc = 0;
input bcd = 0;
input d = close;
input c = open;
def _xab = xab >= 0.250 and xab <= 0.500;
def _abc = abc >= 1.130 and abc <= 2.618;
def _bcd = bcd >= 1.618 and bcd <= 2.618;
def _xad = xad >= 0.500 and xad <= 0.750;
def isAntiCrab = _xab and _abc and _bcd and _xad and (if _mode == 1 then d < c else d > c);
plot out = if isNaN(isAntiCrab) then 0 else isAntiCrab;
}
Script isShark {
input _mode = 0;
input xab = 0;
input xad = 0;
input abc = 0;
input bcd = 0;
input d = close;
input c = open;
def _xab = xab >= 0.500 and xab <= 0.875;
def _abc = abc >= 1.130 and abc <= 1.618;
def _bcd = bcd >= 1.270 and bcd <= 2.240;
def _xad = xad >= 0.886 and xad <= 1.130;
def isShark = _xab and _abc and _bcd and _xad and (if _mode == 1 then d < c else d > c);
plot out = if isNaN(isShark) then 0 else isShark;
}
Script isAntiShark {
input _mode = 0;
input xab = 0;
input xad = 0;
input abc = 0;
input bcd = 0;
input d = close;
input c = open;
def _xab = xab >= 0.382 and xab <= 0.875;
def _abc = abc >= 0.500 and abc <= 1.000;
def _bcd = bcd >= 1.250 and bcd <= 2.618;
def _xad = xad >= 0.500 and xad <= 1.250;
def isAntiShark = _xab and _abc and _bcd and _xad and (if _mode == 1 then d < c else d > c);
plot out = if isNaN(isAntiShark) then 0 else isAntiShark;
}
Script is5o {
input _mode = 0;
input xab = 0;
input xad = 0;
input abc = 0;
input bcd = 0;
input d = close;
input c = open;
def _xab = xab >= 1.13 and xab <= 1.618;
def _abc = abc >= 1.618 and abc <= 2.24;
def _bcd = bcd >= 0.5 and bcd <= 0.625;
def _xad = xad >= 0.0 and xad <= 0.236;
def is5o = _xab and _abc and _bcd and _xad and (if _mode == 1 then d < c else d > c);
plot out = if isNaN(is5o) then 0 else is5o;
}
Script isWolf {
input _mode = 0;
input xab = 0;
input xad = 0;
input abc = 0;
input bcd = 0;
input d = close;
input c = open;
def _xab = xab >= 1.27 and xab <= 1.618;
def _abc = abc >= 0 and abc <= 5;
def _bcd = bcd >= 1.27 and bcd <= 1.618;
def _xad = xad >= 0.0 and xad <= 5;
def isWolf = _xab and _abc and _bcd and _xad and (if _mode == 1 then d < c else d > c);
plot out = if isNaN(isWolf) then 0 else isWolf;
}
Script isHnS {
input _mode = 0;
input xab = 0;
input xad = 0;
input abc = 0;
input bcd = 0;
input d = close;
input c = open;
def _xab = xab >= 2.0 and xab <= 10;
def _abc = abc >= 0.90 and abc <= 1.1;
def _bcd = bcd >= 0.236 and bcd <= 0.88;
def _xad = xad >= 0.90 and xad <= 1.1;
def isHnS = _xab and _abc and _bcd and _xad and (if _mode == 1 then d < c else d > c);
plot out = if isNaN(isHnS) then 0 else isHnS;
}
Script isConTria {
input _mode = 0;
input xab = 0;
input xad = 0;
input abc = 0;
input bcd = 0;
input d = close;
input c = open;
def _xab = xab >= 0.382 and xab <= 0.618;
def _abc = abc >= 0.382 and abc <= 0.618;
def _bcd = bcd >= 0.382 and bcd <= 0.618;
def _xad = xad >= 0.236 and xad <= 0.764;
def isConTria = _xab and _abc and _bcd and _xad and (if _mode == 1 then d < c else d > c);
plot out = if isNaN(isConTria) then 0 else isConTria;
}
Script isExpTria {
input _mode = 0;
input xab = 0;
input xad = 0;
input abc = 0;
input bcd = 0;
input d = close;
input c = open;
def _xab = xab >= 1.236 and xab <= 1.618;
def _abc = abc >= 1.000 and abc <= 1.618;
def _bcd = bcd >= 1.236 and bcd <= 2.000;
def _xad = xad >= 2.000 and xad <= 2.236;
def isExpTria = _xab and _abc and _bcd and _xad and (if _mode == 1 then d < c else d > c);
plot out = if isNaN(isExpTria) then 0 else isExpTria;
}
def changeHTF = cTF - cTF[1];
def zigzagHTF = zigzag(useHigherTimeframe, higherTimeframe, UseHeikinAshiCandles);
def zigzagTF = zigzag(no, higherTimeframe, UseHeikinAshiCandles);
def sz = if useHigherTimeframe then if changeHTF != 0 then zigzagHTF else na else zigzagTF;
plot zigzag = if showZigzagLine then sz else na;
zigzag.EnableApproximation();
zigzag.SetDefaultColor(GetColor(3));
#// ||--- Pattern Recognition:
def zigCond = !IsNaN(sz);
def x1;
def a1;
def b1;
def c1;
def d1;
if zigCond {
x1 = a1[1];
a1 = b1[1];
b1 = c1[1];
c1 = d1[1];
d1 = sz;
} else {
x1 = x1[1];
a1 = a1[1];
b1 = b1[1];
c1 = c1[1];
d1 = d1[1];
}
def d = if d1 then d1 else na;
def c = if c1 then c1 else na;
def b = if b1 then b1 else na;
def a = if a1 then a1 else na;
def x = if x1 then x1 else na;
def xab = AbsValue(b - a) / AbsValue(x - a);
def xad = AbsValue(a - d) / AbsValue(x - a);
def abc = AbsValue(b - c) / AbsValue(a - b);
def bcd = AbsValue(c - d) / AbsValue(b - c);
#/---Fib Level
def fib_range = AbsValue(d - c);
def fib_0000 = if d > c then d - fib_range * 0.000 else d + fib_range * 0.000;
def fib_0236 = if d > c then d - fib_range * 0.236 else d + fib_range * 0.236;
def fib_0382 = if d > c then d - fib_range * 0.382 else d + fib_range * 0.382;
def fib_0500 = if d > c then d - fib_range * 0.500 else d + fib_range * 0.500;
def fib_0618 = if d > c then d - fib_range * 0.618 else d + fib_range * 0.618;
def fib_0764 = if d > c then d - fib_range * 0.764 else d + fib_range * 0.764;
def fib_1000 = if d > c then d - fib_range * 1.000 else d + fib_range * 1.000;
plot fib00 = if extend or !showFib0000 or fib_0000 != fib_0000[-1] then na else fib_0000;
plot fib23 = if extend or !showFib0236 or fib_0236 != fib_0236[-1] then na else fib_0236;
plot fib38 = if extend or !showFib0382 or fib_0382 != fib_0382[-1] then na else fib_0382;
plot fib50 = if extend or !showFib0500 or fib_0500 != fib_0500[-1] then na else fib_0500;
plot fib61 = if extend or !showFib0618 or fib_0618 != fib_0618[-1] then na else fib_0618;
plot fib76 = if extend or !showFib0764 or fib_0764 != fib_0764[-1] then na else fib_0764;
plot fib10 = if extend or !showFib1000 or fib_1000 != fib_1000[-1] then na else fib_1000;
fib00.SetDefaultColor(GlobalColor("fib1"));
fib23.SetDefaultColor(GlobalColor("fib2"));
fib38.SetDefaultColor(GlobalColor("fib3"));
fib50.SetDefaultColor(GlobalColor("fib4"));
fib61.SetDefaultColor(GlobalColor("fib5"));
fib76.SetDefaultColor(GlobalColor("fib6"));
fib10.SetDefaultColor(GlobalColor("fib7"));
#-- Patterns
#-- Bearish
def isABCDN = isABCD(-1, xab, xad, abc, bcd, d, c);
def isBatN = isBat(-1, xab, xad, abc, bcd, d, c);
def isAntiBatN = isAntiBat(-1, xab, xad, abc, bcd, d, c);
def isAltBatN = isAltBat(-1, xab, xad, abc, bcd, d, c);
def isButterflyN = isButterfly(-1, xab, xad, abc, bcd, d, c);
def isAntiButterflyN = isAntiButterfly(-1, xab, xad, abc, bcd, d, c);
def isGartleyN = isGartley(-1, xab, xad, abc, bcd, d, c);
def isAntiGartleyN = isAntiGartley(-1, xab, xad, abc, bcd, d, c);
def isCrabN = isCrab(-1, xab, xad, abc, bcd, d, c);
def isAntiCrabN = isAntiCrab(-1, xab, xad, abc, bcd, d, c);
def isSharkN = isShark(-1, xab, xad, abc, bcd, d, c);
def isAntiSharkN = isAntiShark(-1, xab, xad, abc, bcd, d, c);
def is5oN = is5o(-1, xab, xad, abc, bcd, d, c);
def isWolfN = isWolf(-1, xab, xad, abc, bcd, d, c);
def isHnSN = isHnS(-1, xab, xad, abc, bcd, d, c);
def isConTriaN = isConTria(-1, xab, xad, abc, bcd, d, c);
def isExpTriaN = isExpTria(-1, xab, xad, abc, bcd, d, c);
def labDnLoc = Max(fib_0000, fib_1000);
AddChartBubble(showPatterns and isABCDN and !isABCDN[1], labDnLoc, "ABCD", GlobalColor("labDn"));
AddChartBubble(showPatterns and isBatN and !isBatN[1], labDnLoc, "Bat", GlobalColor("labDn"));
AddChartBubble(showPatterns and isAntiBatN and !isAntiBatN[1], labDnLoc, "Anti Bat", GlobalColor("labDn"));
AddChartBubble(showPatterns and isAltBatN and !isAltBatN[1], labDnLoc, "Alt Bat", GlobalColor("labDn"));
AddChartBubble(showPatterns and isButterflyN and !isButterflyN[1], labDnLoc, "Butterfly", GlobalColor("labDn"));
AddChartBubble(showPatterns and isAntiButterflyN and !isAntiButterflyN[1], labDnLoc, "Anti Butterfly", GlobalColor("labDn"));
AddChartBubble(showPatterns and isGartleyN and !isGartleyN[1], labDnLoc, "Gartley", GlobalColor("labDn"));
AddChartBubble(showPatterns and isAntiGartleyN and !isAntiGartleyN[1], labDnLoc, "Anti Gartley", GlobalColor("labDn"));
AddChartBubble(showPatterns and isCrabN and !isCrabN[1], labDnLoc, "Crab", GlobalColor("labDn"));
AddChartBubble(showPatterns and isAntiCrabN and !isAntiCrabN[1], labDnLoc, "Anti Crab", GlobalColor("labDn"));
AddChartBubble(showPatterns and isSharkN and !isSharkN[1], labDnLoc, "Shark", GlobalColor("labDn"));
AddChartBubble(showPatterns and isAntiSharkN and !isAntiSharkN[1], labDnLoc, "Anti Shark", GlobalColor("labDn"));
AddChartBubble(showPatterns and is5oN and !is5oN[1], labDnLoc, "5-O", GlobalColor("labDn"));
AddChartBubble(showPatterns and isWolfN and !isWolfN[1], labDnLoc, "Wolf Wave", GlobalColor("labDn"));
AddChartBubble(showPatterns and isHnSN and !isHnSN[1], labDnLoc, "Head and Shoulders", GlobalColor("labDn"));
AddChartBubble(showPatterns and isConTriaN and !isConTriaN[1], labDnLoc, "Contracting triangle", GlobalColor("labDn"));
AddChartBubble(showPatterns and isExpTriaN and !isExpTriaN[1], labDnLoc, "Expanding Triangle", GlobalColor("labDn"));
#-- Bullish
def isABCDP = isABCD(1, xab, xad, abc, bcd, d, c);
def isBatP = isBat(1, xab, xad, abc, bcd, d, c);
def isAntiBatP = isAntiBat(1, xab, xad, abc, bcd, d, c);
def isAltBatP = isAltBat(1, xab, xad, abc, bcd, d, c);
def isButterflyP = isButterfly(1, xab, xad, abc, bcd, d, c);
def isAntiButterflyP = isAntiButterfly(1, xab, xad, abc, bcd, d, c);
def isGartleyP = isGartley(1, xab, xad, abc, bcd, d, c);
def isAntiGartleyP = isAntiGartley(1, xab, xad, abc, bcd, d, c);
def isCrabP = isCrab(1, xab, xad, abc, bcd, d, c);
def isAntiCrabP = isAntiCrab(1, xab, xad, abc, bcd, d, c);
def isSharkP = isShark(1, xab, xad, abc, bcd, d, c);
def isAntiSharkP = isAntiShark(1, xab, xad, abc, bcd, d, c);
def is5oP = is5o(1, xab, xad, abc, bcd, d, c);
def isWolfP = isWolf(1, xab, xad, abc, bcd, d, c);
def isHnSP = isHnS(1, xab, xad, abc, bcd, d, c);
def isConTriaP = isConTria(1, xab, xad, abc, bcd, d, c);
def isExpTriaP = isExpTria(1, xab, xad, abc, bcd, d, c);
def labUpLoc = Min(fib_0000, fib_1000);
AddChartBubble(showPatterns and isABCDP and !isABCDP[1], labUpLoc, "ABCD", GlobalColor("labUp"), no);
AddChartBubble(showPatterns and isBatP and !isBatP[1], labUpLoc, "Bat", GlobalColor("labUp"), no);
AddChartBubble(showPatterns and isAntiBatP and !isAntiBatP[1], labUpLoc, "Anti Bat", GlobalColor("labUp"), no);
AddChartBubble(showPatterns and isAltBatP and !isAltBatP[1], labUpLoc, "Alt Bat", GlobalColor("labUp"), no);
AddChartBubble(showPatterns and isButterflyP and !isButterflyP[1], labUpLoc, "Butterfly", GlobalColor("labUp"), no);
AddChartBubble(showPatterns and isAntiButterflyP and !isAntiButterflyP[1], labUpLoc, "Anti Butterfly", GlobalColor("labUp"), no);
AddChartBubble(showPatterns and isGartleyP and !isGartleyP[1], labUpLoc, "Gartley", GlobalColor("labUp"), no);
AddChartBubble(showPatterns and isAntiGartleyP and !isAntiGartleyP[1], labUpLoc, "Anti Gartley", GlobalColor("labUp"), no);
AddChartBubble(showPatterns and isCrabP and !isCrabP[1], labUpLoc, "Crab", GlobalColor("labUp"), no);
AddChartBubble(showPatterns and isAntiCrabP and !isAntiCrabP[1], labUpLoc, "Anti Crab", GlobalColor("labUp"), no);
AddChartBubble(showPatterns and isSharkP and !isSharkP[1], labUpLoc, "Shark", GlobalColor("labUp"), no);
AddChartBubble(showPatterns and isAntiSharkP and !isAntiSharkP[1], labUpLoc, "Anti Shark", GlobalColor("labUp"), no);
AddChartBubble(showPatterns and is5oP and !is5oP[1], labUpLoc, "5-O", GlobalColor("labUp"), no);
AddChartBubble(showPatterns and isWolfP and !isWolfP[1], labUpLoc, "Wolf Wave", GlobalColor("labUp"), no);
AddChartBubble(showPatterns and isHnSP and !isHnSP[1], labUpLoc, "Head and Shoulders", GlobalColor("labUp"), no);
AddChartBubble(showPatterns and isConTriaP and !isConTriaP[1], labUpLoc, "Contracting triangle", GlobalColor("labUp"), no);
AddChartBubble(showPatterns and isExpTriaP and !isExpTriaP[1], labUpLoc, "Expanding Triangle", GlobalColor("labUp"), no);
#// Plot Entry
input signalTriggerLevel = {"0.236", "0.382", "0.500",Default "0.618", "0.764", "1.000"};
def fibMulti;
Switch (signalTriggerLevel) {
Case "0.236" : fibMulti = 0.236;
Case "0.382" : fibMulti = 0.382;
Case "0.500" : fibMulti = 0.500;
Case "0.764" : fibMulti = 0.764;
Case "1.000" : fibMulti = 1.000;
Default : fibMulti = 0.618;
}
def buy_patterns = isABCDP + isBatP + isAltBatP + isButterflyP + isGartleyP + isCrabP + isSharkP + is5oP + isWolfP +
isHnSP + isConTriaP + isExpTriaP + isAntiBatP + isAntiButterflyP + isAntiGartleyP + isAntiCrabP + isAntiSharkP;
def sell_patterns = isABCDN + isBatN + isAltBatN + isButterflyN + isGartleyN + isCrabN + isSharkN + is5oN + isWolfN +
isHnSN + isConTriaN + isExpTriaN + isAntiBatN + isAntiButterflyN + isAntiGartleyN + isAntiCrabN + isAntiSharkN;
def entry = f_last_fib(fibMulti, fib_range, d, c);
def target01_buy_entry = buy_patterns and close <= entry;
def target01_sell_entry = sell_patterns and close >= entry;
def longpositionvalide; # = false
def shortconditionvalide; # = false
if target01_buy_entry {
longpositionvalide = yes;
shortconditionvalide = no;
} else
if target01_sell_entry {
longpositionvalide = no;
shortconditionvalide = yes;
} else {
longpositionvalide = longpositionvalide[1];
shortconditionvalide = shortconditionvalide[1];
}
plot sigUp = if longpositionvalide and shortconditionvalide[1] then low else na;
plot sigDn = if longpositionvalide[1] and shortconditionvalide then high else na;
sigUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
sigDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
sigUp.SetDefaultColor(Color.CYAN);
sigDn.SetDefaultColor(Color.MAGENTA);
#-- BackTest
input labelOptions = {Default "Summary", "Detailed", "Don't Show"};
input tradeDirection = {default "Both", "Long", "Short", "Don't Show Trades"}; # "Trading Direction"
input TakeProfiPercent = 2.25; # "Take Profit %"
input StopLossPercent = 1.75; # "Stop Loss %"
input lotSize = 15;
def extTrade = if extendLines then yes else !last;
def long = tradeDirection == tradeDirection."Long" or tradeDirection == tradeDirection."Both";
def short = tradeDirection == tradeDirection."Short" or tradeDirection == tradeDirection."Both";
def full = labelOptions == labelOptions."Detailed";
def summ = (long or short) and (labelOptions == labelOptions."Summary" or full);
#/ Apply Take Profit and Stop Loss conditions
#/ Entry/Exit conditions
def LongEntry = longpositionvalide and shortconditionvalide[1];
def ShortEntry = longpositionvalide[1] and shortconditionvalide;
def exitLong = sell_patterns;
def exitShort = buy_patterns;
def state = {Default "ini", "long", "short"};
def takeProfit;
def stoploss;
def entryPrice;
def closePrice;
def win;
def tradingCond = adx(Length = 14) >= 10;
def longCond = barNumber()> 0 and long and LongEntry and tradingCond;
def shortCond = barNumber()> 0 and short and ShortEntry and tradingCond;
Switch (state[1]) {
Case "long" :
if (exitLong or (high >= takeProfit[1]) or (close < stoploss[1])) {
state = state."ini";
closePrice = close - entryPrice[1];
entryPrice = na;
takeProfit = na;
stoploss = na;
win = if closePrice > 0 then 1 else 0;
} else {
state = state[1];
closePrice = 0;
entryPrice = entryPrice[1];
takeProfit = takeProfit[1];
stoploss = stoploss[1];
win = 0;
}
Case "short" :
if (exitShort or (low <= takeProfit[1]) or (close > stoploss[1])) {
state = state."ini";
closePrice = entryPrice[1] - close;
entryPrice = na;
takeProfit = na;
stoploss = na;
win = if closePrice > 0 then -1 else 0;
} else {
state = state[1];
closePrice = 0;
entryPrice = entryPrice[1];
takeProfit = takeProfit[1];
stoploss = stoploss[1];
win = 0;
}
Default :
state = if longCond then state."long" else if shortCond then state."short" else state[1];
entryPrice = open[-1];
takeProfit = entryPrice * (1 + (if longCond then 1 else -1) * TakeProfiPercent / 1000);
stoploss = entryPrice * (1 + (if longCond then -1 else 1) * StopLossPercent / 1000);
closePrice = 0;
win = 0;
}
def longSig = state==state."long" and state!=state[1];
def shortSig = state==state."short" and state!=state[1];
def exitL = state[1]==state."long" and state!=state."long";
def exitS = state[1]==state."short" and state!=state."short";
plot entryLong = if extTrade and state==state."long" then entryPrice else na;
plot tpLong = if extTrade and state==state."long" then takeProfit else na;
plot slLong = if extTrade and state==state."long" then stoploss else na;
plot entryShort = if extTrade and state==state."short" then entryPrice else na;
plot tpShort = if extTrade and state==state."short" then takeProfit else na;
plot slShort = if extTrade and state==state."short" then stoploss else na;
entryLong.SetPaintingStrategy(PaintingStrategy.DASHES);
entryShort.SetPaintingStrategy(PaintingStrategy.DASHES);
entryLong.SetDefaultColor(Color.GRAY);
entryShort.SetDefaultColor(Color.GRAY);
tpLong.SetDefaultColor(Color.GREEN);
slLong.SetDefaultColor(Color.RED);
tpShort.SetDefaultColor(Color.GREEN);
slShort.SetDefaultColor(Color.RED);
AddCloud(tpLong, entryLong, Color.DARK_GREEN);
AddCloud(entryLong, slLong, Color.DARK_RED);
AddCloud(entryShort, tpShort, Color.DARK_GREEN);
AddCloud(slShort, entryShort, Color.DARK_RED);
def totLong = TotalSum(if exitL then 1 else 0);
def totShort = TotalSum(if exitS then 1 else 0);
def tottrd = totLong + totShort;
def WinL = TotalSum(if win>0 then 1 else 0);
def WinS = TotalSum(if win<0 then 1 else 0);
def totWin = (WinL + WinS);
def winRt = if !tottrd then 0 else Round(totWin / tottrd * 100, 1);
def winRtL = if !totLong then 0 else Round(WinL / totLong * 100, 1);
def winRtS = if !totShort then 0 else Round(WinS / totShort * 100, 1);
def amtL = TotalSum(if exitL then closePrice else 0) * lotSize;
def amtS = TotalSum(if exitS then closePrice else 0) * lotSize;
def pl = RoundDown(amtL + amtS, 2);
def plL = if isNaN(amtL) then 0 else RoundDown(amtL, 2);
def plS = if isNaN(amtS) then 0 else RoundDown(amtS, 2);
AddLabel(summ, "WinRate(" + winRt + "%, " + tottrd + ")", if winRt >= 50 then Color.GREEN else Color.RED);
AddLabel(long and full, "Long(" + winRtL + "%, " + totLong + ")", if winRtL >= 50 then Color.GREEN else Color.RED);
AddLabel(short and full, "Short(" + winRtS + "%, " + totShort + ")", if winRtS >= 50 then Color.GREEN else Color.RED);
AddLabel(long and full, "AmtLong($" + plL + ")", if amtL > 0 then Color.GREEN else Color.RED);
AddLabel(short and full, "AmtShort($" + plS + ")", if amtS > 0 then Color.GREEN else Color.RED);
AddLabel(summ, "P/L($" + pl + ")", if pl > 0 then Color.GREEN else if pl < 0 then Color.RED else Color.GRAY);
#-- END of CODe
Last edited by a moderator: