Author Message:
Background
Inspired by NILX's "Tool: Chop & Trade Zones". This can used as an element for trading system control.
Function
I use my own customized algorithm to replace that core of NILX one, which is targeting to provide smoother and trend for chop and trend judgement.
Since it is quite different now but an oscillator within range of 0~100. The pro is it can use the constant threshold values for all time frames and all trading pairs now.
CODE:
CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © blackcat1402
#study("[blackcat] L1 Chop Zones","L1 Chop Zones", overlay=false, max_bars_back=5000, max_labels_count =500)
# Converted and modifiedTrueRange by Sam4@Cok@samer800 - 02/2023
declare lower;
input BarColor = yes;
input ShowLabel = yes;
input len = 500; # "Length"
input src = close; # "Source"
input dist_limit_long_h = 60.0; # "% Distance Threshold Center (Long)"
input dist_limit_long_l = 20.0; # "% Distance Threshold Lower (Long)"
input dist_limit_short_l = 40.0; # "% Distance Threshold Center (Short)"
input dist_limit_short_h = 80.0; # "% Distance Threshold Upper (Short)"
#//functions
def na = Double.NaN;
def last = isNaN(close);
script nz {
input data = 1;
input repl = 0;
def ret_val = if IsNaN(data) then repl else data;
plot return = ret_val;
}
#xrf(values, length) =>
script xrf {
input values = close;
input length = 0;
def r_val;
r_val = if length >= 1 then
fold i = 0 to length with p do
if IsNaN(r_val[1]) or !IsNaN(values[i]) then values[i] else r_val[1] else Double.NaN;
plot return = r_val;
}
#xsa(src,len,wei) =>
script xsa {
input src = close;
input len = 0;
input wei = 0;
def sumf;
def ma;
def out;
sumf = if(sumf[1]==0, Double.NaN,sumf[1]) - nz(src[len]) + src;
ma = if IsNaN(src[len]) then Double.NaN else sumf / len;
out = if IsNaN(out[1]) then ma else (src * wei + out[1] * (len - wei)) / len;
plot return = out;
}
#//risk assessment algorithm
def rsva1 = (close-lowest(low,9))/(highest(high,9)-lowest(low,9))*100;
def rsva2 = 100*(highest(high,9)-close)/(highest(high,9)-lowest(low,9));
def var21 = xsa(rsva2,9,1)+100;
def var11 = xsa(rsva1,3,1);
def var51 = xsa(var11,3,1)+100;
def poleline = var51-var21+50;
def var2 = lowest(low,33);
def var3 = highest(high,21);
def var4 = ExpAverage((close-var2)/(var3-var2)*100,10)*-1+100;
def trendstrength = 100 - ExpAverage(0.191*xrf(var4,1) + 0.809*var4,1);
plot trendRange = trendstrength;
trendRange.SetDefaultColor(Color.WHITE);
trendRange.SetLineWeight(2);
def p1 = if last then na else dist_limit_long_h;
def p2 = if last then na else dist_limit_short_l;
def p3 = if last then na else dist_limit_long_l;
def p4 = if last then na else dist_limit_short_h;
AddCloud(p4, p1, Color.DARK_GREEN); # "Long Only"
AddCloud(p1, p2, Color.GRAY, Color.DARK_GRAY, yes); # "Chop Zone"
AddCloud(p2, p3, Color.DARK_RED, Color.DARK_RED); # "Short Only"
#--- LAbel and Br Color
def LongOB = trendstrength> p4;
def ShortOS = trendstrength< p3;
def LongOnly = trendstrength> p1 and trendstrength < p4;
def ShortOnly = trendstrength>p3 and trendstrength< p2;
def NoTrade = trendstrength>p2 and trendstrength < p1;
AddLabel(ShowLabel and (LongOB or ShortOS), "Long & Short", color.white);
Addlabel(ShowLabel and LongOnly, "Long Only" , color.green);
AddLabel(ShowLabel and ShortOnly, "Short Only" , color.red);
AddLabel(ShowLabel and NoTrade, "No Trade" , color.GRAY);
AssignPriceColor(if !BarColor then Color.CURRENT else
if LongOB then Color.LIGHT_GREEN else
if ShortOS then Color.PINK else
if LongOnly and trendstrength>trendstrength[1] then Color.GREEN else
if LongOnly then Color.DARK_GREEN else
if NoTrade then Color.GRAY else
if ShortOnly and trendstrength<trendstrength[1] then Color.RED else Color.DARK_RED);
#--- END CODE