المشاركة الأصلية كتبت بواسطة بندر بن عبدالله
اتمنى انه هذا طلبك اخوي عزيز
وجدتة مع مجموعة مؤشرات ولم اتعامل معه ...
// **************************
// being exploration code
// **************************
// -- what will be our lookback range for the hh and ll?
Nbars = param("number of bars", 12, 5, 40);
btrace = param("include trace output", 1, 0, 1);
nnopivsinsetup = param("no. Pivs in setup", 4, 3, 4, 1);
bshowtcz = param("show tcz", 1, 0, 1);
nminbarsbtwpivs = param("min. Number of bars btw. Pivots", 1, 1, 10, 1);
nminpctbtwpivs = param("min. Percent diff. Btw. Pivots", .05, .04, .2, .01);
blastbarcanbepiv = param("last bar can be a pivot", 1, 0, 1);
retrctolerance = .01;
tcztolerance = .005;
nnumbarstoscan = 120;
// -- added from exploration version 20040204
nexplorebaridx = 0;
nexploredate = 0;
ncurdatenum = 0;
dn = datenum();
dt = datetime();
// -- key exploration variables
btczlong = false;
btczshort = false;
nanchorpividx = 0;
adx8 = adx(8);
// 1 - indicator, 2 - commentary, 3 - scan,
// 4 - exploration, 5 - backtest / optimize
if(status("action")==1) {
bdraw = true;
buselastvis = param("use last visible bar", 1, 0, 1);
} else {
bdraw = false;
buselastvis = false;
btrace = false;
nexploredate = status("rangetodate");
for (i=lastvalue(barindex());i>=0;i--) {
ncurdatenum = dn[i];
if (ncurdatenum == nexploredate) {
nexplorebaridx = i;
}
}
// -- if(status("action")==1...
}
graphxspace=7;
// -- basic candle chart
// -- if this appears inside if , strange
// drawing results!
Plotohlc(open, high, low, close,
"bidx = " + barindex() +
"\n" + "o = " + o + "\n"+"h = "+ h + "\n"+"l = " + l
+ "\n"+"c ",
colorblack, stylecandle);
if (bdraw) {
plot(ma(c, 21), "21 bar ma", coloraqua,
styleline+stylenorescale+stylenolabel);
plot(ma(c, 55), "55 bar ma", colorgreen,
styleline+stylenorescale+stylenolabel);
//plot(ma(c, 233), "233 bar ma", colordarkred,
// styleline+stylenorescale+stylenolabel);
}
// -- create 0-initialized arrays the size of barcount
ahpivs = h - h;
alpivs = l - l;
ahpivhighs = h - h;
alpivlows = l - l;
ahpividxs = h - h;
alpividxs = l - l;
aaddedhpivs = h - h;
aaddedlpivs = l - l;
alegvol = h - h;
aretrcvol = h - h;
nhpivs = 0;
nlpivs = 0;
lasthpidx = 0;
lastlpidx = 0;
lasthph = 0;
lastlpl = 0;
curpivbaridx = 0;
// -- looking back from the current bar, how many bars
// back were the hhv and llv values of the previous
// n bars, etc.?
Ahhvbars = hhvbars(h, nbars);
allvbars = llvbars(l, nbars);
ahhv = hhv(h, nbars);
allv = llv(l, nbars);
// -- initialize value of curtrend
nlastvisbar = lastvalue(
highest(iif(status("barvisible"), barindex(), 0)));
curbar = iif(nlastvisbar > 0 and buselastvis, nlastvisbar,
iif(status("action")==4 and nexplorebaridx > 0, nexplorebaridx,
lastvalue(barindex())));
curtrend = "";
if (allvbars[curbar] < ahhvbars[curbar])
curtrend = "d";
else
curtrend = "u";
// -- loop through bars. Search for
// entirely array-based approach
// in version
/* *******************
find main pivots
******************* */
// -- make sure there are enough bars!
If (curbar >= nnumbarstoscan) {
for (i=0; i<nnumbarstoscan; i++) {
// -- value of curbar dependent on two parameters
curbar = iif(nlastvisbar > 0 and buselastvis,
nlastvisbar-i,
iif(status("action")==4 and nexplorebaridx > 0,
nexplorebaridx-i,
lastvalue(barindex())-i));
// -- have we identified a pivot? If trend is down...
If (allvbars[curbar] < ahhvbars[curbar]) {
// ... And had been up, this is a trend change
if (curtrend == "u") {
curtrend = "d";
// -- capture pivot information
curpivbaridx = curbar - allvbars[curbar];
alpivs[curpivbaridx] = 1;
alpivlows[nlpivs] = l[curpivbaridx];
alpividxs[nlpivs] = curpivbaridx;
nlpivs++;
}
// -- or current trend is up
} else {
if (curtrend == "d") {
curtrend = "u";
curpivbaridx = curbar - ahhvbars[curbar];
ahpivs[curpivbaridx] = 1;
ahpivhighs[nhpivs] = h[curpivbaridx];
ahpividxs[nhpivs] = curpivbaridx;
nhpivs++;
}
// -- if curtrend is up...else...
}
// -- loop through bars
}
}
/* *******************
found main pivots
******************* */
/* *************************
finding missed pivot(s)
************************* */
// -- start at last bar. Reestablish curbar
curbar =
iif(nlastvisbar > 0 and buselastvis,
nlastvisbar,
iif(status("action")==4 and nexplorebaridx > 0,
nexplorebaridx,
lastvalue(barindex()))
);
// -- make sure i found at least two of each above.
If (nhpivs >= 2 and nlpivs >= 2) {
lastlpidx = alpividxs[0];
lastlpl = alpivlows[0];
lasthpidx = ahpividxs[0];
lasthph = ahpivhighs[0];
nlasthorlpividx = max(lastlpidx, lasthpidx);
naddpivsrng = curbar - nlasthorlpividx;
allvafterlastpiv = llv(l, naddpivsrng);
nllvafterlastpiv = allvafterlastpiv[curbar];
allvidxafterlastpiv = llvbars(l, naddpivsrng);
nllvidxafterlastpiv = curbar - allvidxafterlastpiv[curbar];
ahhvafterlastpiv = hhv(h, naddpivsrng);
nhhvafterlastpiv = ahhvafterlastpiv[curbar];
ahhvidxafterlastpiv = hhvbars(h, naddpivsrng);
nhhvidxafterlastpiv = curbar - ahhvidxafterlastpiv[curbar];
// -- later want to add last high pivot only if
// not in buy mode from last and still in trade
/*
note - i'm only interested in adding pivots if i'm in
a higher-highs or lower-lows scenario
*/
// -- ok, let's start where the last high pivot occurs after the
// last low pivot
if (lasthpidx > lastlpidx) {
/* there are at least two possibilities here. One is that
the previous high was higher, indicating that this is a
possible short retracement or one in the making.
The other is that the previous high was lower, indicating
that this is a possible long retracement in the working.
However, both depend on opposing pivots. E.g., if i find
higher highs, what if i have lower lows?
If the highs are descending, then i can consider:
- a lower low, and leave it at that
- a higher high and higher low
- a lower low and another lower high
*/
if (ahpivhighs[0] < ahpivhighs[1]) {
if (nllvafterlastpiv < alpivlows[0] and
(nllvidxafterlastpiv - lasthpidx - 1) >= nminbarsbtwpivs
and nllvidxafterlastpiv != curbar ) {
// -- ok, we'll add this as a pivot.
// mark it for plotting...
Alpivs[nllvidxafterlastpiv] = 1;
aaddedlpivs[nllvidxafterlastpiv] = 1;
// ...and then rearrange s in the
// pivot information arrays
for (j=0; j<nlpivs; j++) {
alpivlows[nlpivs-j] = alpivlows[nlpivs-(j+1)];
alpividxs[nlpivs-j] = alpividxs[nlpivs-(j+1)];
}
alpivlows[0] = nllvafterlastpiv;
alpividxs[0] = nllvidxafterlastpiv;
nlpivs++;
// -- test whether to add piv given last piv is high
// and we have lower highs
}
// -- here, the last piv is a high piv, and we have
// higher-highs. The most likely addition is a
// low piv that is a retracement.
} else {
if (nllvafterlastpiv > alpivlows[0] and
(nllvidxafterlastpiv - lasthpidx - 1) >= nminbarsbtwpivs
and nllvidxafterlastpiv != curbar ) {
// -- ok, we'll add this as a pivot.
// mark it for plotting...
Alpivs[nllvidxafterlastpiv] = 1;
aaddedlpivs[nllvidxafterlastpiv] = 1;
// ...and then rearrange s in the
// pivot information arrays
for (j=0; j<nlpivs; j++) {
alpivlows[nlpivs-j] = alpivlows[nlpivs-(j+1)];
alpividxs[nlpivs-j] = alpividxs[nlpivs-(j+1)];
}
alpivlows[0] = nllvafterlastpiv;
alpividxs[0] = nllvidxafterlastpiv;
nlpivs++;
// -- test whether to add piv given last piv is high
// and we have lower highs
}
// -- the last piv is a high and we have higher highs
// or lower highs
}
/* ****************************************************************
still finding missed pivot(s). Here, the last piv is a low piv.
**************************************************************** */
} else {
// -- first case, lower highs
if (ahpivhighs[0] < ahpivhighs[1]) {
if (nhhvafterlastpiv < ahpivhighs[0] and
(nhhvidxafterlastpiv - lastlpidx - 1) >= nminbarsbtwpivs
and nhhvidxafterlastpiv != curbar ) {
// -- ok, we'll add this as a pivot.
// mark that for plotting
ahpivs[nhhvidxafterlastpiv] = 1;
aaddedhpivs[nhhvidxafterlastpiv] = 1;
// ...and then rearrange s in the
// pivot information arrays
for (j=0; j<nhpivs; j++) {
ahpivhighs[nhpivs-j] = ahpivhighs[nhpivs-(j+1)];
ahpividxs[nhpivs-j] = ahpividxs[nhpivs-(j+1)];
}
ahpivhighs[0] = nhhvafterlastpiv;
ahpividxs[0] = nhhvidxafterlastpiv;
nhpivs++;
// -- test whether to add piv given last piv is high
// and we have lower highs
}
// -- second case when last piv is a low piv, higher highs
// most likely addition is high piv that is a retracement.
// considering adding a high piv as long as it is higher
} else {
// -- where i have higher highs,
if (nhhvafterlastpiv > ahpivhighs[0] and
(nhhvidxafterlastpiv - lastlpidx - 1) >= nminbarsbtwpivs
and nhhvidxafterlastpiv != curbar ) {
// -- ok, we'll add this as a pivot.
// mark it for plotting...
Ahpivs[nhhvidxafterlastpiv] = 1;
aaddedhpivs[nhhvidxafterlastpiv] = 1;
// ...and then rearrange s in the
// pivot information arrays
for (j=0; j<nhpivs; j++) {
ahpivhighs[nhpivs-j] = ahpivhighs[nhpivs-(j+1)];
ahpividxs[nhpivs-j] = ahpividxs[nhpivs-(j+1)];
}
ahpivhighs[0] = nhhvafterlastpiv;
ahpividxs[0] = nhhvidxafterlastpiv;
nhpivs++;
// -- test whether to add piv given last piv is high
// and we have lower highs
}
}
}
// -- if there are at least two of each
}
/* ****************************************
// -- done with finding pivots
***************************************** */
if (bdraw) {
// -- ok, let's plot the pivots using arrows
plotshapes(
iif(ahpivs==1, shapedownarrow, shapenone),
colorred, 0, high, offset=-15);
plotshapes(
iif(aaddedhpivs==1, shapedownarrow, shapenone),
colordarkred, 0, high, offset=-15);
plotshapes(
iif(alpivs==1, shapeuparrow , shapenone),
colorgreen, 0, low, offset=-15);
plotshapes(
iif(aaddedlpivs==1, shapeuparrow , shapenone),
colordarkgreen, 0, low, offset=-15);
}
/* ****************************************
// -- done with discovering and plotting pivots
***************************************** */
// -- i'm going to want to look for possible retracement
risk = 0;
profinc = 0;
nleg0pts = 0;
nleg0bars = 0;
nleg0vol = 0;
nleg1pts = 0;
nleg1bars = 0;
nleg1vol = 0;
nlegbarsdiff = 0;
nrtrc0pts = 0;
nrtrc0bars = 0;
nrtrc0vol = 0;
nrtrc1pts = 0;
nrtrc1bars = 0;
nrtrc1vol = 0;
minrtrc = 0;
maxrtrc = 0;
minline = 0;
maxline = 0;
triggerline = 0;
firstprofitline = 0;
triggerinc = 0;
triggerprc = 0;
firstprofitprc = 0;
retrcprc = 0;
retrcbar = 0;
retrcbaridx = 0;
retrcrng = 0;
aretrcprc = h-h;
aretrcprcbars = h-h;
aretrcclose = c;
retrcclose = 0;
// -- do tcz calcs. Arrangement of pivs very specific
// for this setup.
If (nhpivs >= 2 and
nlpivs >=2 and
ahpivhighs[0] > ahpivhighs[1] and
alpivlows[0] > alpivlows[1]) {
tcz500 =
(ahpivhighs[0] -
(.5 * (ahpivhighs[0] - alpivlows[1])));
tcz618 =
(ahpivhighs[0] -
(.618 * (ahpivhighs[0] - alpivlows[1])));
tcz786 =
(ahpivhighs[0] -
(.786 * (ahpivhighs[0] - alpivlows[0])));
retrcrng = curbar - ahpividxs[0];
aretrcprc = llv(l, retrcrng);
retrcprc = aretrcprc[curbar];
aretrcprcbars = llvbars(l, retrcrng);
retrcbaridx = curbar - aretrcprcbars[curbar];
retrcclose = aretrcclose[retrcbaridx];
// -- btczlong setup?
Btczlong = (
// -- are retracement levels arranged in
// tcz order?
Tcz500 >= (tcz786 * (1 - tcztolerance))
and
// .681 is below .786 for long setups
tcz618 <= (tcz786 * (1 + tcztolerance))
and
// -- is the low in the tcz range
// -- is the close >= low of tcz range
// and low <= high of tcz range
retrcclose >= ((1 - retrctolerance) * tcz618)
and
retrcprc <= ((1 + retrctolerance) * tcz500)
);
// -- risk would be high of signal bar minus low of zone
//risk = 0;
// -- lower highs and lower lows
} else if (nhpivs >= 2 and nlpivs >=2
and ahpivhighs[0] < ahpivhighs[1]
and alpivlows[0] < alpivlows[1]) {
tcz500 =
(ahpivhighs[1] -
(.5 * (ahpivhighs[1] - alpivlows[0])));
tcz618 =
(ahpivhighs[0] -
(.618 * (ahpivhighs[1] - alpivlows[0])));
tcz786 =
(ahpivhighs[0] -
(.786 * (ahpivhighs[0] - alpivlows[0])));
retrcrng = curbar - alpividxs[0];
aretrcprc = hhv(h, retrcrng);
retrcprc = aretrcprc[curbar];
aretrcprcbars = hhvbars(h, retrcrng);
retrcbaridx = curbar - aretrcprcbars[curbar];
retrcclose = aretrcclose[retrcbaridx];
btczshort = (
// -- are retracement levels arranged in
// tcz order?
// .500 is below .786 for short setups
tcz500 <= (tcz786 * (1 + tcztolerance))
and
// .681 is above .786 for short setups
tcz618 >= (tcz786 * (1 - tcztolerance))
and
// -- is the close <= high of tcz range
// and high >= low of tcz range
retrcclose <= ((1 + retrctolerance) * tcz618)
and
retrcprc >= ((1 - retrctolerance) * tcz500)
);
// -- risk would be top of zone - low of signal bar
//risk = 0;
}
filter = (btczshort or btczlong);
addcolumn(c, "close");
addcolumn(iif(btczlong, 76, 83), "l/s", formatchar);
// **************************
// end exploration code
// **************************
// **************************
// begin indicator code
// **************************
// -- what will be our lookback range for the hh and ll?
Nbars = param("number of bars", 12, 5, 40);
btrace = param("include trace output", 1, 0, 1);
nnopivsinsetup = param("no. Pivs in setup", 4, 3, 4, 1);
bshowtcz = param("show tcz", 1, 0, 1);
nminbarsbtwpivs = param("min. Number of bars btw. Pivots", 1, 1, 10, 1);
nminpctbtwpivs = param("min. Percent diff. Btw. Pivots", .05, .04, .2, .01);
blastbarcanbepiv = param("last bar can be a pivot", 1, 0, 1);
retrctolerance = .01;
tcztolerance = .005;
nnumbarstoscan = 120;
// -- added from exploration version 20040204
nexplorebaridx = 0;
nexploredate = 0;
ncurdatenum = 0;
dn = datenum();
dt = datetime();
// -- key exploration variables
btczlong = false;
btczshort = false;
nanchorpividx = 0;
adx8 = adx(8);
// 1 - indicator, 2 - commentary, 3 - scan,
// 4 - exploration, 5 - backtest / optimize
if(status("action")==1) {
bdraw = true;
buselastvis = param("use last visible bar", 1, 0, 1);
} else {
bdraw = false;
buselastvis = false;
btrace = false;
nexploredate = status("rangetodate");
for (i=lastvalue(barindex());i>=0;i--) {
ncurdatenum = dn[i];
if (ncurdatenum == nexploredate) {
nexplorebaridx = i;
}
}
// -- if(status("action")==1...
}
graphxspace=7;
// -- basic candle chart
// -- if this appears inside if , strange
// drawing results!
Plotohlc(open, high, low, close,
"bidx = " + barindex() +
"\n" + "o = " + o + "\n"+"h = "+ h + "\n"+"l = " + l
+ "\n"+"c ",
colorblack, stylecandle);
if (bdraw) {
plot(ma(c, 21), "21 bar ma", coloraqua,
styleline+stylenorescale+stylenolabel);
plot(ma(c, 55), "55 bar ma", colorgreen,
styleline+stylenorescale+stylenolabel);
//plot(ma(c, 233), "233 bar ma", colordarkred,
// styleline+stylenorescale+stylenolabel);
}
// -- create 0-initialized arrays the size of barcount
ahpivs = h - h;
alpivs = l - l;
ahpivhighs = h - h;
alpivlows = l - l;
ahpividxs = h - h;
alpividxs = l - l;
aaddedhpivs = h - h;
aaddedlpivs = l - l;
alegvol = h - h;
aretrcvol = h - h;
nhpivs = 0;
nlpivs = 0;
lasthpidx = 0;
lastlpidx = 0;
lasthph = 0;
lastlpl = 0;
curpivbaridx = 0;
// -- looking back from the current bar, how many bars
// back were the hhv and llv values of the previous
// n bars, etc.?
Ahhvbars = hhvbars(h, nbars);
allvbars = llvbars(l, nbars);
ahhv = hhv(h, nbars);
allv = llv(l, nbars);
// -- initialize value of curtrend
nlastvisbar = lastvalue(
highest(iif(status("barvisible"), barindex(), 0)));
curbar = iif(nlastvisbar > 0 and buselastvis, nlastvisbar,
iif(status("action")==4 and nexplorebaridx > 0, nexplorebaridx,
lastvalue(barindex())));
curtrend = "";
if (allvbars[curbar] < ahhvbars[curbar])
curtrend = "d";
else
curtrend = "u";
// -- loop through bars. Search for
// entirely array-based approach
// in version
/* *******************
find main pivots
******************* */
// -- make sure there are enough bars!
If (curbar >= nnumbarstoscan) {
for (i=0; i<nnumbarstoscan; i++) {
// -- value of curbar dependent on two parameters
curbar = iif(nlastvisbar > 0 and buselastvis,
nlastvisbar-i,
iif(status("action")==4 and nexplorebaridx > 0,
nexplorebaridx-i,
lastvalue(barindex())-i));
// -- have we identified a pivot? If trend is down...
If (allvbars[curbar] < ahhvbars[curbar]) {
// ... And had been up, this is a trend change
if (curtrend == "u") {
curtrend = "d";
// -- capture pivot information
curpivbaridx = curbar - allvbars[curbar];
alpivs[curpivbaridx] = 1;
alpivlows[nlpivs] = l[curpivbaridx];
alpividxs[nlpivs] = curpivbaridx;
nlpivs++;
}
// -- or current trend is up
} else {
if (curtrend == "d") {
curtrend = "u";
curpivbaridx = curbar - ahhvbars[curbar];
ahpivs[curpivbaridx] = 1;
ahpivhighs[nhpivs] = h[curpivbaridx];
ahpividxs[nhpivs] = curpivbaridx;
nhpivs++;
}
// -- if curtrend is up...else...
}
// -- loop through bars
}
}
/* *******************
found main pivots
******************* */
/* *************************
finding missed pivot(s)
************************* */
// -- start at last bar. Reestablish curbar
curbar =
iif(nlastvisbar > 0 and buselastvis,
nlastvisbar,
iif(status("action")==4 and nexplorebaridx > 0,
nexplorebaridx,
lastvalue(barindex()))
);
// -- make sure i found at least two of each above.
If (nhpivs >= 2 and nlpivs >= 2) {
lastlpidx = alpividxs[0];
lastlpl = alpivlows[0];
lasthpidx = ahpividxs[0];
lasthph = ahpivhighs[0];
nlasthorlpividx = max(lastlpidx, lasthpidx);
naddpivsrng = curbar - nlasthorlpividx;
allvafterlastpiv = llv(l, naddpivsrng);
nllvafterlastpiv = allvafterlastpiv[curbar];
allvidxafterlastpiv = llvbars(l, naddpivsrng);
nllvidxafterlastpiv = curbar - allvidxafterlastpiv[curbar];
ahhvafterlastpiv = hhv(h, naddpivsrng);
nhhvafterlastpiv = ahhvafterlastpiv[curbar];
ahhvidxafterlastpiv = hhvbars(h, naddpivsrng);
nhhvidxafterlastpiv = curbar - ahhvidxafterlastpiv[curbar];
// -- later want to add last high pivot only if
// not in buy mode from last and still in trade
/*
note - i'm only interested in adding pivots if i'm in
a higher-highs or lower-lows scenario
*/
// -- ok, let's start where the last high pivot occurs after the
// last low pivot
if (lasthpidx > lastlpidx) {
/* there are at least two possibilities here. One is that
the previous high was higher, indicating that this is a
possible short retracement or one in the making.
The other is that the previous high was lower, indicating
that this is a possible long retracement in the working.
However, both depend on opposing pivots. E.g., if i find
higher highs, what if i have lower lows?
If the highs are descending, then i can consider:
- a lower low, and leave it at that
- a higher high and higher low
- a lower low and another lower high
*/
if (ahpivhighs[0] < ahpivhighs[1]) {
if (nllvafterlastpiv < alpivlows[0] and
(nllvidxafterlastpiv - lasthpidx - 1) >= nminbarsbtwpivs
and nllvidxafterlastpiv != curbar ) {
// -- ok, we'll add this as a pivot.
// mark it for plotting...
Alpivs[nllvidxafterlastpiv] = 1;
aaddedlpivs[nllvidxafterlastpiv] = 1;
// ...and then rearrange s in the
// pivot information arrays
for (j=0; j<nlpivs; j++) {
alpivlows[nlpivs-j] = alpivlows[nlpivs-(j+1)];
alpividxs[nlpivs-j] = alpividxs[nlpivs-(j+1)];
}
alpivlows[0] = nllvafterlastpiv;
alpividxs[0] = nllvidxafterlastpiv;
nlpivs++;
// -- test whether to add piv given last piv is high
// and we have lower highs
}
// -- here, the last piv is a high piv, and we have
// higher-highs. The most likely addition is a
// low piv that is a retracement.
} else {
if (nllvafterlastpiv > alpivlows[0] and
(nllvidxafterlastpiv - lasthpidx - 1) >= nminbarsbtwpivs
and nllvidxafterlastpiv != curbar ) {
// -- ok, we'll add this as a pivot.
// mark it for plotting...
Alpivs[nllvidxafterlastpiv] = 1;
aaddedlpivs[nllvidxafterlastpiv] = 1;
// ...and then rearrange s in the
// pivot information arrays
for (j=0; j<nlpivs; j++) {
alpivlows[nlpivs-j] = alpivlows[nlpivs-(j+1)];
alpividxs[nlpivs-j] = alpividxs[nlpivs-(j+1)];
}
alpivlows[0] = nllvafterlastpiv;
alpividxs[0] = nllvidxafterlastpiv;
nlpivs++;
// -- test whether to add piv given last piv is high
// and we have lower highs
}
// -- the last piv is a high and we have higher highs
// or lower highs
}
/* ****************************************************************
still finding missed pivot(s). Here, the last piv is a low piv.
**************************************************************** */
} else {
// -- first case, lower highs
if (ahpivhighs[0] < ahpivhighs[1]) {
if (nhhvafterlastpiv < ahpivhighs[0] and
(nhhvidxafterlastpiv - lastlpidx - 1) >= nminbarsbtwpivs
and nhhvidxafterlastpiv != curbar ) {
// -- ok, we'll add this as a pivot.
// mark that for plotting
ahpivs[nhhvidxafterlastpiv] = 1;
aaddedhpivs[nhhvidxafterlastpiv] = 1;
// ...and then rearrange s in the
// pivot information arrays
for (j=0; j<nhpivs; j++) {
ahpivhighs[nhpivs-j] = ahpivhighs[nhpivs-(j+1)];
ahpividxs[nhpivs-j] = ahpividxs[nhpivs-(j+1)];
}
ahpivhighs[0] = nhhvafterlastpiv;
ahpividxs[0] = nhhvidxafterlastpiv;
nhpivs++;
// -- test whether to add piv given last piv is high
// and we have lower highs
}
// -- second case when last piv is a low piv, higher highs
// most likely addition is high piv that is a retracement.
// considering adding a high piv as long as it is higher
} else {
// -- where i have higher highs,
if (nhhvafterlastpiv > ahpivhighs[0] and
(nhhvidxafterlastpiv - lastlpidx - 1) >= nminbarsbtwpivs
and nhhvidxafterlastpiv != curbar ) {
// -- ok, we'll add this as a pivot.
// mark it for plotting...
Ahpivs[nhhvidxafterlastpiv] = 1;
aaddedhpivs[nhhvidxafterlastpiv] = 1;
// ...and then rearrange s in the
// pivot information arrays
for (j=0; j<nhpivs; j++) {
ahpivhighs[nhpivs-j] = ahpivhighs[nhpivs-(j+1)];
ahpividxs[nhpivs-j] = ahpividxs[nhpivs-(j+1)];
}
ahpivhighs[0] = nhhvafterlastpiv;
ahpividxs[0] = nhhvidxafterlastpiv;
nhpivs++;
// -- test whether to add piv given last piv is high
// and we have lower highs
}
}
}
// -- if there are at least two of each
}
/* ****************************************
// -- done with finding pivots
***************************************** */
if (bdraw) {
// -- ok, let's plot the pivots using arrows
plotshapes(
iif(ahpivs==1, shapedownarrow, shapenone),
colorred, 0, high, offset=-15);
plotshapes(
iif(aaddedhpivs==1, shapedownarrow, shapenone),
colordarkred, 0, high, offset=-15);
plotshapes(
iif(alpivs==1, shapeuparrow , shapenone),
colorgreen, 0, low, offset=-15);
plotshapes(
iif(aaddedlpivs==1, shapeuparrow , shapenone),
colordarkgreen, 0, low, offset=-15);
}
/* ****************************************
// -- done with discovering and plotting pivots
***************************************** */
// -- i'm going to want to look for possible retracement
risk = 0;
profinc = 0;
nleg0pts = 0;
nleg0bars = 0;
nleg0vol = 0;
nleg1pts = 0;
nleg1bars = 0;
nleg1vol = 0;
nlegbarsdiff = 0;
nrtrc0pts = 0;
nrtrc0bars = 0;
nrtrc0vol = 0;
nrtrc1pts = 0;
nrtrc1bars = 0;
nrtrc1vol = 0;
minrtrc = 0;
maxrtrc = 0;
minline = 0;
maxline = 0;
triggerline = 0;
firstprofitline = 0;
triggerinc = 0;
triggerprc = 0;
firstprofitprc = 0;
retrcprc = 0;
retrcbar = 0;
retrcbaridx = 0;
retrcrng = 0;
aretrcprc = h-h;
aretrcprcbars = h-h;
aretrcclose = c;
retrcclose = 0;
// -- do tcz calcs. Arrangement of pivs very specific
// for this setup.
If (nhpivs >= 2 and
nlpivs >=2 and
ahpivhighs[0] > ahpivhighs[1] and
alpivlows[0] > alpivlows[1]) {
tcz500 =
(ahpivhighs[0] -
(.5 * (ahpivhighs[0] - alpivlows[1])));
tcz618 =
(ahpivhighs[0] -
(.618 * (ahpivhighs[0] - alpivlows[1])));
tcz786 =
(ahpivhighs[0] -
(.786 * (ahpivhighs[0] - alpivlows[0])));
retrcrng = curbar - ahpividxs[0];
aretrcprc = llv(l, retrcrng);
aretrcprcbars = llvbars(l, retrcrng);
retrcprc = aretrcprc[curbar];
retrcbaridx = curbar - aretrcprcbars[curbar];
retrcclose = aretrcclose[retrcbaridx];
// -- btczlong setup?
Btczlong = (
// -- are retracement levels arranged in
// tcz order?
// .500 is above .786 for long setups
tcz500 >= (tcz786 * (1 - tcztolerance))
and
// .681 is below .786 for long setups
tcz618 <= (tcz786 * (1 + tcztolerance))
and
// -- is the low in the tcz range
// -- is the close >= low of tcz range
// and low <= high of tcz range
retrcclose >= ((1 - retrctolerance) * tcz618)
and
retrcprc <= ((1 + retrctolerance) * tcz500)
);
// -- risk would be high of signal bar minus low of zone
//risk = 0;
// -- lower highs and lower lows
} else if (nhpivs >= 2 and nlpivs >=2
and ahpivhighs[0] < ahpivhighs[1]
and alpivlows[0] < alpivlows[1]) {
tcz500 =
(ahpivhighs[1] -
(.5 * (ahpivhighs[1] - alpivlows[0])));
tcz618 =
(ahpivhighs[0] -
(.618 * (ahpivhighs[1] - alpivlows[0])));
tcz786 =
(ahpivhighs[0] -
(.786 * (ahpivhighs[0] - alpivlows[0])));
retrcrng = curbar - alpividxs[0];
aretrcprc = hhv(h, retrcrng);
retrcprc = aretrcprc[curbar];
aretrcprcbars = hhvbars(h, retrcrng);
retrcbaridx = curbar - aretrcprcbars[curbar];
retrcclose = aretrcclose[retrcbaridx];
btczshort = (
// -- are retracement levels arranged in
// tcz order?
// .500 is below .786 for short setups
tcz500 <= (tcz786 * (1 + tcztolerance))
and
// .681 is above .786 for short setups
tcz618 >= (tcz786 * (1 - tcztolerance))
and
// -- is the close <= high of tcz range
// and high >= low of tcz range
retrcclose <= ((1 + retrctolerance) * tcz618)
and
retrcprc >= ((1 - retrctolerance) * tcz500)
);
// -- risk would be top of zone - low of signal bar
//risk = 0;
}
// -- show zone if present
if (btczshort or btczlong) {
// -- be prepared to see symmetry
if (btczshort) {
if (alpividxs[0] > ahpividxs[0]) {
// -- valuable, useful symmetry information
nrtrc0pts = ahpivhighs[0] - alpivlows[1];
nrtrc0bars = ahpividxs[0] - alpividxs[1] + 1;
nrtrc1pts = retrcprc - alpivlows[0];
nrtrc1bars = retrcbaridx - alpividxs[0] + 1;
} else {
nrtrc0pts = ahpivhighs[1] - alpivlows[1];
nrtrc0bars = ahpividxs[1] - alpividxs[1] + 1;
nrtrc1pts = ahpivhighs[0] - alpivlows[0];
nrtrc1bars = ahpividxs[0] - alpividxs[0] + 1;
}
} else { // blongsetup
if (alpividxs[0] > ahpividxs[0]) {
nrtrc0pts = ahpivhighs[0] - alpivlows[1];
nrtrc0bars = ahpividxs[0] - alpividxs[1] + 1;
nrtrc1pts = retrcprc - alpivlows[0];
nrtrc1bars = retrcbaridx - alpividxs[0] + 1;
} else {
nrtrc0pts = ahpivhighs[1] - alpivlows[0];
nrtrc0bars = alpividxs[0] - ahpividxs[1] + 1;
nrtrc1pts = ahpivhighs[0] - alpivlows[0];
nrtrc1bars = alpividxs[0] - ahpividxs[0] + 1;
}
}
if (bshowtcz) {
plot(
linearray( iif(btczlong, ahpividxs[0], alpividxs[0]),
tcz500, curbar, tcz500 , 0),
"tcz500", colorpaleblue, styleline);
plot(
linearray( iif(btczlong, ahpividxs[0], alpividxs[0]),
tcz618, curbar, tcz618, 0),
"tcz618", colorpaleblue, styleline);
plot(
linearray( iif(btczlong, ahpividxs[0], alpividxs[0]),
tcz786, curbar, tcz786, 0),
"tcz786", colorturquoise, styleline);
}
// -- if (bshowtcz)
}
if (bdraw) {
title = name() + " (" + strleft(fullname(), 10) +
") atr: " + numtostr(atr(1), 4.2) + " ( " +
numtostr((c - ref(c, -1)), 4.2) + " / " +
numtostr((((c - ref(c, -1)) / ref(c, -1)) * 100), 2.1) + "% ) " +
writeval( selectedvalue( datetime() ), formatdatetime) +
" \no: " + open +
", \nh: " + high +
", \nl: " + low +
", \nc: " + close + ", \n" +
// "risk: " + writeval(risk, 2.1) + "% \n" +
"rtrc 0/1 pts: " + writeval(nrtrc0pts, 2.1) + "/" +
writeval(nrtrc1pts, 2.1) + " \n" +
"rtrc 0/1 bars: " + writeval(nrtrc0bars, 2.0) + "/" +
writeval(nrtrc1bars, 2.0);
}
// **************************
// end indicator code
// **************************