
Author Message: (Not Typical conversion, some scripts can't be converted to TOS)
The Range Sentiment Profile indicator is inspired from the volume profile and aims to indicate the degree of bullish/bearish variations within equidistant price areas inside the most recent price range.
CSS:
#// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) #https://creativecommons.org/licenses/by-nc-sa/4.0/
#// © LuxAlgo
#indicator("Range Sentiment Profile [LuxAlgo]", overlay = true, max_boxes_count = 500)
# Converted by Sam4Cok@Samer800 - 07/2023 - Not exact results!
#//Settings
input length = 80;
input rows = 20;
input useChartTimeframe = {Default "yes", "No"};
input manualTimeframe = AggregationPeriod.FIFTEEN_MIN;
input showRange = yes; # 'Show Range Levels'
input bullMax = no; # 'Maximum'
input bearMax = no; # 'Minimum'
input showFill = yes; # 'Show Fill'
#//Function
def na = Double.NaN;
def c;
def h;
def l;
def o;
Switch (useChartTimeframe) {
Case "Yes" :
c = close;
h = high;
l = low;
o = open;
Case "No" :
c = close(Period=manualTimeframe);
h = high(Period=manualTimeframe);
l = low(Period=manualTimeframe);
o = open(Period=manualTimeframe);
}
#def get_close = close(Period=tf);
#def get_open = open(Period=tf);
def last = isNaN(close);
def int = inertiaAll(c, length);
def islast = !last;#!isNaN(int) and isNaN(int[-1]);#!isNaN(close);
#def isfirst = !isNaN(int) and isNaN(int[1]);
def rng = !isNaN(int);
def n = AbsValue(CompoundValue(1, BarNumber(), 0));
#--- Colors
DefineGlobalColor("up" , CreateColor(33,87,243));
DefineGlobalColor("dn" , CreateColor(255,93,0));
DefineGlobalColor("dup" , CreateColor(6,34,113));
DefineGlobalColor("ddn" , CreateColor(98,36,0));
def upper = highest(h, length);
def lower = lowest(l, length);
def sumad = sum(AbsValue(c - o), length);
def avg;def avg75; def avg25;def top; def btm;
def ltop;
def l75;
def l50;
def l25;
def lbtm;
#/Set profile
if islast {
avg = (upper + lower) / 2;
avg75 = (upper + avg) / 2;
avg25 = (lower + avg) / 2;
top = upper;
btm = lower;
} else {
avg = avg[1];
avg75 = avg75[1];
avg25 = avg25[1];
top = top[1];
btm = btm[1];
}
def toplvl = if !rng then na else highestAll(inertiaAll(top, 2));
def botlvl = if !rng then na else highestAll(inertiaAll(btm, 2));
def lvl75 = if !rng then na else highestAll(inertiaAll(avg75, 2));
def lvl50 = if !rng then na else highestAll(inertiaAll(avg, 2));
def lvl25 = if !rng then na else highestAll(inertiaAll(avg25, 2));
#//Display range levels
if showRange {
ltop = toplvl;
lbtm = botlvl;
l75 = lvl75;
l50 = lvl50;
l25 = lvl25;
} else {
ltop = na;
lbtm = na;
l75 = na;
l50 = na;
l25 = na;
}
plot l75_ = l75;
plot l50_ = l50;
plot l25_ = l25;
plot ltop_ = ltop;
plot lbtm_ = lbtm;
l75_.SetStyle(Curve.SHORT_DASH);
l25_.SetStyle(Curve.SHORT_DASH);
l75_.SetDefaultColor(Color.GRAY);
l50_.SetDefaultColor(Color.GRAY);
l25_.SetDefaultColor(Color.GRAY);
ltop_.SetDefaultColor(Color.GRAY);
lbtm_.SetDefaultColor(Color.GRAY);
#//Loop trough most recent bars
def introw = inertiaAll(c, rows-1);
def rngrow = isNaN(introw);
def dn = if rngrow then upper else
dn[1] - (upper - lower) / rows;
def sum = fold i = 0 to rows -1 with q=upper do
fold j = 0 to length -1 with p do
p + if h[j] > GetValue(dn,rows - i-1) and l[j] < if(i==0,upper, GetValue(dn,rows-i))
then c[j] - o[j] else 0;
def sums = sum>0;
def css = if showFill then sums else na;
AddCloud(if css then toplvl else botlvl, if css then botlvl else toplvl, GlobalColor("dup"), GlobalColor("ddn"));
def sums_abs = AbsValue(sum);
def max = highest(sum, rows);
def min = lowest(sum, rows);
def up = fold k = 0 to rows -1 with r=(dn+dn[1])/2 do
if GetValue(sum,rows-k) == max then (GetValue(dn,rows-k-1) + GetValue(dn,rows-k))/2 else r;
def dn1 = fold k1 = 0 to rows -1 with r1=(dn+dn[1])/2 do
if GetValue(sum,rows-k1) == min then (GetValue(dn,rows-k1-1) + GetValue(dn,rows-k1))/2 else r1;
def lvlMax = up;#if sum == max then up else lvlMax[1];
def lvlMin = dn1;#if sum == min then dn1 else lvlMin[1];
plot BullBin = if !bullMax or !rng then na else highestAll(inertiaAll(lvlMax, 2));
plot BearBin = if !bearMax or !rng then na else highestAll(inertiaAll(lvlMin, 2));
BullBin.SetDefaultColor(GlobalColor("up"));
BearBin.SetDefaultColor(GlobalColor("dn"));
#--- END of CODE