#/ Indicator for TOS
#// © tradeforopp
#indicator("TTrades Daily Bias [TFO]", "TTrades Daily Bias [TFO]", true, max_lines_count = 500, max_labels_count = 500)
# Converted by Sam4Cok@Samer800 - 04/2024
input DailyBias = yes; # "Daily Bias", tooltip = "Apply TTrades bias concepts to the 1 Day timeframe", group = g_VIS)
input WeeklyBias = no; # "Weekly Bias", tooltip = "Apply TTrades bias concepts to the 1 Week timeframe", group = g_VIS)
input ShowBiasReasoning = yes; # "Show Bias Reasoning", tooltip = "Show the reason why a given bias is being established",
input PlotDailyBias = yes; # "Plot Daily Bias", inline = "DBIAS", group = g_PLT)
input PlotWeeklyBias = yes; # "Plot Weekly Bias", inline = "WBIAS", group = g_PLT)
input DaySeparator = no; # "Day Separator", inline = "DSEP", group = g_STY)
input WeekSeparator = no; # "Week Separator", inline = "WSEP", group = g_STY)
def na = Double.NaN;
def last = isNaN(close);
def tf = GetAggregationPeriod();
def day = GetDay();
def week = GetWeek();
def new_day = day != day[1];
def new_week = week != week[1];
def can_plot_d = DailyBias and tf < AggregationPeriod.DAY;
def can_plot_w = WeeklyBias and tf < AggregationPeriod.WEEK;
#--Color
DefineGlobalColor("bull", CreateColor(0,137,123)); #teal
DefineGlobalColor("bear", CreateColor(255,82,82));
DefineGlobalColor("up", CreateColor(33,150,243));
DefineGlobalColor("dn", CreateColor(255,82,82));
Script handle_bias {
input nph = high;
input npl = low;
input ncl = low;
input nch = high;
input np_up = yes;
input can_plot = yes;
input bias_reason = yes;
input tf = 1;
def nbias;def txt;#def nclose_ph;def nclose_pl;
if close[1] > nph {
# nclose_ph = if nbias[1] == 1 then nclose_ph[1] + 1 else nclose_ph[1];
nbias = 1;
txt = if bias_reason and can_plot then 1 else 0;#"Close Above P"+tf+"H\nBias P"+tf+"H"
} else if close[1] < npl {
# nclose_pl = if nbias[1] == -1 then nclose_pl[1] + 1 else nclose_pl[1];
nbias = -1;
txt = if bias_reason and can_plot then 2 else 0; #"Close Below P"+tf+"L\nBias P"+tf+"L"
} else if close[1] < nph and close[1] > npl and nch > nph and ncl > npl {
nbias = -1;
txt = if bias_reason and can_plot then 3 else 0; #"Failed to Close Above P"+tf+"H\nBias P"+tf+"L"
} else if close[1] > npl and close[1] < nph and nch < nph and ncl < npl {
nbias = 1;
txt = if bias_reason and can_plot then 4 else 0; #"Failed to Close Below P"+tf+"L\nBias P"+tf+"H"
} else if nch <= nph and ncl >= npl {
nbias = if np_up then 1 else -1;
txt = if bias_reason and can_plot then (if np_up then 5 else 5.5) else 0; #"Close Inside\nBias P"+tf + (n.p_up ? "H" : "L")
} else {
nbias = 0;
txt = if bias_reason and can_plot then 6 else 0; #"Outside Bar but Closed Inside\nNo Bias"
}
plot bias = nbias;
plot reason = if !isNaN(close) then txt else 0;
}
Script update_info {
input tf = yes;
input can_plot = yes;
input bias_reason = yes;
input dw = 1;
def np_up;
def nph; # := n.ch
def npl; # := n.cl
def nch; # := high
def ncl; # := low
def nco; # := open
def nph1 = if nph[1]==0 then high[1] else nph[1];
def npl1 = if npl[1]==0 then low[1] else npl[1];
def nch1 = if nch[1]==0 then high else nch[1];
def ncl1 = if ncl[1]==0 then low else ncl[1];
def nco1 = if nco[1]==0 then open else nco[1];
def nbias; def nLabel;
if tf {
nbias = handle_bias(nph1, npl1, ncl1, nch1, np_up[1], can_plot, bias_reason, dw).bias;
nLabel = handle_bias(nph1, npl1, ncl1, nch1, np_up[1], can_plot, bias_reason, dw).reason;
np_up = if close[1] >= nco1 then yes else no;
nph = nch[1];
npl = ncl[1];
nch = high;
ncl = low;
nco = open;
} else {
nbias = nbias[1];
nLabel = 0;
np_up = np_up[1];
nph = nph[1];
npl = npl[1];
nco = nco[1];
nch = max(high, nch1);
ncl = min(low, ncl1);
}
plot up = np_up;
plot ph = nph;
plot pl = npl;
plot bias = nbias;
plot Label = nLabel;
}
#-- Daily
def dup = update_info(new_day).up;
def dph = update_info(new_day).ph;
def dpl = update_info(new_day).pl;
def dbias = update_info(new_day, can_plot_d, ShowBiasReasoning, 1).bias;
def dLabel = update_info(new_day, can_plot_d, ShowBiasReasoning, 1).Label;
def colDH = if high >= dph then 1 else -1;
def colDL = if low <= dpl then 1 else -1;
#--- Weekly
def wup = update_info(new_week).up;
def wph = update_info(new_week).ph;
def wpl = update_info(new_week).pl;
def wbias = update_info(new_week, can_plot_w, ShowBiasReasoning, 0).bias;
def wLabel = update_info(new_week, can_plot_w, ShowBiasReasoning, 0).Label;
def colWH = if high >= wph then 1 else -1;
def colWL = if low <= wpl then 1 else -1;
plot PDHRaid = if can_plot_d and new_day and high >= dph then low else na; # "PDH Raid"
plot PDLRaid = if can_plot_d and new_day and low <= dpl then high else na; # "PDL Raid"
PDHRaid.SetPaintingStrategy(paintingStrategy.ARROW_UP);
PDLRaid.SetPaintingStrategy(paintingStrategy.ARROW_DOWN);
PDHRaid.SetDefaultColor(GlobalColor("up"));
PDLRaid.SetDefaultColor(GlobalColor("dn"));
plot PWHRaid = if can_plot_w and new_week and high >= wph then wph else na; # "PWH Raid"
plot PWLRaid = if can_plot_w and new_week and low <= wpl then wpl else na; # "PWL Raid"
PWHRaid.SetPaintingStrategy(paintingStrategy.ARROW_UP);
PWLRaid.SetPaintingStrategy(paintingStrategy.ARROW_DOWN);
PWHRaid.SetDefaultColor(Color.CYAN);
PWLRaid.SetDefaultColor(Color.MAGENTA);
AddVerticalLine(DaySeparator and new_day and can_plot_d, "NewDay", Color.GRAY);
AddVerticalLine(WeekSeparator and new_week and can_plot_w, "NewWeek", Color.ORANGE);
plot dHH = if DailyBias and dph and !new_day and !last then dph else na;
plot dLL = if DailyBias and dpl and !new_day and !last then dpl else na;
dHH.AssignValueColor(if colDH > 0 then GlobalColor("dn") else GlobalColor("up"));
dLL.AssignValueColor(if colDL > 0 then GlobalColor("dn") else GlobalColor("up"));
plot wHH = if WeeklyBias and wph and !new_week and !last then wph else na;
plot wLL = if WeeklyBias and wpl and !new_week and !last then wpl else na;
wHH.SetStyle(Curve.MEDIUM_DASH);
wLL.SetStyle(Curve.MEDIUM_DASH);
wHH.AssignValueColor(if colWH > 0 then GlobalColor("dn") else GlobalColor("up"));
wLL.AssignValueColor(if colWL > 0 then GlobalColor("dn") else GlobalColor("up"));
#-- Reasons
def LabW = if colWH==1 then 1 else
if colWL==1 then -1 else 0;
def LabD = if colDH==1 then 1 else
if colDL==1 then -1 else 0;
AddchartBubble(dLabel==1, dpl, "Close Above PDH\nBias PDH", if LabD>0 then GlobalColor("up") else
if LabD<0 then GlobalColor("dn") else Color.GRAY, if open >dpl then no else yes);
AddchartBubble(dLabel==2, dpl, "Close Below PDL\nBias PDL", if LabD>0 then GlobalColor("up") else
if LabD<0 then GlobalColor("dn") else Color.GRAY, if open >dpl then no else yes);
AddchartBubble(dLabel==3, dpl, "Failed to Close Above PDH\nBias PDL", if LabD>0 then GlobalColor("up") else
if LabD<0 then GlobalColor("dn") else Color.GRAY, if open >dpl then no else yes);
AddchartBubble(dLabel==4, dpl, "Failed to Close Below PDL\nBias PDH", if LabD>0 then GlobalColor("up") else
if LabD<0 then GlobalColor("dn") else Color.GRAY, if open >dpl then no else yes);
AddchartBubble(dLabel==5, dpl, "Close Inside\nBias PDH", if LabD>0 then GlobalColor("up") else
if LabD<0 then GlobalColor("dn") else Color.GRAY, if open >dpl then no else yes);
AddchartBubble(dLabel==5.5, dpl, "Close Inside\nBias PDL", if LabD>0 then GlobalColor("up") else
if LabD<0 then GlobalColor("dn") else Color.GRAY, if open >dpl then no else yes);
AddchartBubble(dLabel==6, dpl, "Outside Bar but Closed Inside\nNo Bias", if LabD>0 then GlobalColor("up") else
if LabD<0 then GlobalColor("dn") else Color.GRAY, if open >dpl then no else yes);
#--Week
AddchartBubble(wLabel==1, wph, "Close Above PWH\nBias PWH", if LabW>0 then GlobalColor("up") else
if LabW<0 then GlobalColor("dn") else Color.GRAY, if open >wpl then yes else no);
AddchartBubble(wLabel==2, wph, "Close Below PWL\nBias PWL", if LabW>0 then GlobalColor("up") else
if LabW<0 then GlobalColor("dn") else Color.GRAY, if open >wpl then yes else no);
AddchartBubble(wLabel==3, wph, "Failed to Close Above PWH\nBias PWL", if LabW>0 then GlobalColor("up") else
if LabW<0 then GlobalColor("dn") else Color.GRAY, if open >wpl then yes else no);
AddchartBubble(wLabel==4, wph, "Failed to Close Below PWL\nBias PWH", if LabW>0 then GlobalColor("up") else
if LabW<0 then GlobalColor("dn") else Color.GRAY, if open >wpl then yes else no);
AddchartBubble(wLabel==5, wph, "Close Inside\nBias PWH", if LabW>0 then GlobalColor("up") else
if LabW<0 then GlobalColor("dn") else Color.GRAY, if open >wpl then yes else no);
AddchartBubble(wLabel==5.5, wph, "Close Inside\nBias PWL", if LabW>0 then GlobalColor("up") else
if LabW<0 then GlobalColor("dn") else Color.GRAY, if open >wpl then yes else no);
AddchartBubble(wLabel==6, wph, "Outside Bar but Closed Inside\nNo Bias", if LabW>0 then GlobalColor("up") else
if LabW<0 then GlobalColor("dn") else Color.GRAY, if open >wpl then yes else no);
def hh = InertiaAll(highest(high + atr(200), 500), 2);
def ll = InertiaAll(lowest(low - atr(200), 500), 2);
plot biasDaily = if !last and PlotDailyBias then inertiaAll(highestAll(hh), 500) else na;
plot biasWeekly = if !last and PlotWeeklyBias then inertiaAll(highestAll(ll), 500) else na;
biasDaily.SetPaintingStrategy(PaintingStrategy.SQUARES);
biasDaily.AssignValueColor(if dbias == 1 then GlobalColor("bull") else if dbias == -1 then Color.RED else Color.GRAY);
biasWeekly.SetPaintingStrategy(PaintingStrategy.SQUARES);
biasWeekly.AssignValueColor(if wbias == 1 then GlobalColor("bull") else if wbias == -1 then Color.RED else Color.GRAY);
#-- end OF code