PHP Code:
//@ Julien_Eche
//@version=6
indicator("Directional Strength Index", overlay=true, max_labels_count=500)
length = input.int(20, title="SMEMA Length")
table_size = input.string("normal", title="Table Size", options=["small", "normal", "large"])
table_text_size = table_size
show_deviation_lines = input.bool(true, title="Show Deviation Levels")
show_tf_cells = input.bool(true, title="Show All Timeframes")
approach = input.string("Long Term", title="Overall Trend Weighting", options=["Long Term", "Swing Trading", "Short Term"])
color_bull = input.color(#00897B, title="Bull Color", inline="col")
color_bear = input.color(#C0392B, title="Bear Color", inline="col")
color_neutral = input.color(#C49A6C, title="Neutral Color", inline="col")
get_trend_arrow(score) => score >= 0.5 ? "➚" : score <= -0.5 ? "➘" : "➡"
get_trend_strength(score) => str.tostring(math.round(math.abs(score))) + "/10"
trend_col(score) => score > 0.5 ? color_bull : score < -0.5 ? color_bear : color_neutral
get_dynamic_color(score, alpha) => color.new(trend_col(score), alpha)
get_band_color(level, src, score, is_upper, col_bull, col_bear) =>
r = math.abs(src - level) / ta.percentile_linear_interpolation(math.abs(src - level), 400, 100)
c = score > 0 and is_upper ? col_bull : score < 0 and not is_upper ? col_bear : na
result = na(c) ? na : r <= 0.03 ? color.new(c, 96) : r <= 0.06 ? color.new(c, 90) : r <= 0.10 ? color.new(c, 80) : r <= 0.15 ? color.new(c, 68) : r <= 0.20 ? color.new(c, 55) : r <= 0.27 ? color.new(c, 42) : r <= 0.35 ? color.new(c, 30) : color.new(c, 18)
result
get_score_internal() =>
sm = ta.sma(ta.ema(close, length), length)
sm_prev = ta.sma(ta.ema(close[1], length), length)
step = ta.atr(100)
slope = sm - sm_prev
slope_ratio = slope / sm
slope_ratio := slope_ratio > 0.1 ? 0.1 : slope_ratio < -0.1 ? -0.1 : slope_ratio
slope_weight = 1.0 + slope_ratio * 10.0
bull = (close > sm + step ? 1 : 0) + (close > sm + step * 2 ? 1 : 0) + (close > sm + step * 3 ? 1 : 0)
bear = (close < sm - step ? 1 : 0) + (close < sm - step * 2 ? 1 : 0) + (close < sm - step * 3 ? 1 : 0)
raw_score = (bull - bear) * 10.0 / 3.0
adjusted_score = raw_score * slope_weight
adjusted_score := adjusted_score > 10.0 ? 10.0 : adjusted_score < -10.0 ? -10.0 : adjusted_score
adjusted_score
get_score(tf) => request.security(syminfo.tickerid, tf, get_score_internal(), lookahead=barmerge.lookahead_on)
score_1H = get_score("60")
score_4H = get_score("240")
score_1D = get_score("D")
score_3D = get_score("3D")
score_1W = get_score("W")
score_1M = get_score("M")
w_1H = approach == "Short Term" ? 0.35 : approach == "Swing Trading" ? 0.10 : 0.03
w_4H = approach == "Short Term" ? 0.30 : approach == "Swing Trading" ? 0.15 : 0.07
w_1D = approach == "Short Term" ? 0.20 : approach == "Swing Trading" ? 0.25 : 0.20
w_3D = approach == "Short Term" ? 0.10 : approach == "Swing Trading" ? 0.25 : 0.25
w_1W = approach == "Short Term" ? 0.05 : approach == "Swing Trading" ? 0.15 : 0.35
w_1M = approach == "Short Term" ? 0.00 : approach == "Swing Trading" ? 0.10 : 0.10
global_score = score_1H * w_1H + score_4H * w_4H + score_1D * w_1D + score_3D * w_3D + score_1W * w_1W + score_1M * w_1M
label = "Global " + (approach == "Long Term" ? "LT" : approach == "Swing Trading" ? "SW" : "ST")
var table stats_table = table.new(position=position.top_right, columns=3, rows=8, frame_color=color.new(color.gray, 70), frame_width=1, border_width=0)
table.set_bgcolor(stats_table, color.new(color.rgb(26, 26, 26), 0))
if barstate.islast
table.cell(stats_table, 0, 0, "Timeframe", text_color=color.gray, text_size=table_text_size)
table.cell(stats_table, 1, 0, "Trend", text_color=color.gray, text_size=table_text_size)
table.cell(stats_table, 2, 0, "Strength", text_color=color.gray, text_size=table_text_size)
row_idx = 1
if show_tf_cells
for i = 0 to 5
tf = i == 0 ? "1H" : i == 1 ? "4H" : i == 2 ? "1D" : i == 3 ? "3D" : i == 4 ? "1W" : "1M"
sc = i == 0 ? score_1H : i == 1 ? score_4H : i == 2 ? score_1D : i == 3 ? score_3D : i == 4 ? score_1W : score_1M
table.cell(stats_table, 0, row_idx, tf, text_color=color.gray, text_size=table_text_size)
table.cell(stats_table, 1, row_idx, get_trend_arrow(sc), text_color=trend_col(sc), text_size=table_text_size)
table.cell(stats_table, 2, row_idx, get_trend_strength(sc), text_color=trend_col(sc), text_size=table_text_size)
row_idx := row_idx + 1
table.cell(stats_table, 0, row_idx, label, text_color=color.silver, text_size=table_text_size)
table.cell(stats_table, 1, row_idx, get_trend_arrow(global_score), text_color=trend_col(global_score), text_size=table_text_size)
table.cell(stats_table, 2, row_idx, get_trend_strength(global_score), text_color=trend_col(global_score), text_size=table_text_size)
sm = ta.sma(ta.ema(close, length), length)
sm_prev = ta.sma(ta.ema(close[1], length), length)
step = ta.atr(100)
sm_bull = (close > sm + step ? 1 : 0) + (close > sm + step * 2 ? 1 : 0) + (close > sm + step * 3 ? 1 : 0)
sm_bear = (close < sm - step ? 1 : 0) + (close < sm - step * 2 ? 1 : 0) + (close < sm - step * 3 ? 1 : 0)
sm_score = (sm_bull - sm_bear) * 10.0 / 3.0
slope = sm - sm_prev
slope_ratio = slope / step
slope_ratio := slope_ratio > 2.0 ? 2.0 : slope_ratio < -2.0 ? -2.0 : slope_ratio
slope_weight = 1.0 + slope_ratio * 0.25
adjusted_sm_score = sm_score * slope_weight
adjusted_sm_score := adjusted_sm_score > 10.0 ? 10.0 : adjusted_sm_score < -10.0 ? -10.0 : adjusted_sm_score
plot(show_deviation_lines ? sm + step * 3 : na, color=get_band_color(sm + step * 3, close, adjusted_sm_score, true, color_bull, color_bear))
plot(show_deviation_lines ? sm + step * 2 : na, color=get_band_color(sm + step * 2, close, adjusted_sm_score, true, color_bull, color_bear))
plot(show_deviation_lines ? sm + step : na, color=get_band_color(sm + step , close, adjusted_sm_score, true, color_bull, color_bear))
plot(sm, color=get_dynamic_color(adjusted_sm_score, 0), title="SMEMA", linewidth=2)
plot(show_deviation_lines ? sm - step : na, color=get_band_color(sm - step , close, adjusted_sm_score, false, color_bull, color_bear))
plot(show_deviation_lines ? sm - step * 2 : na, color=get_band_color(sm - step * 2, close, adjusted_sm_score, false, color_bull, color_bear))
plot(show_deviation_lines ? sm - step * 3 : na, color=get_band_color(sm - step * 3, close, adjusted_sm_score, false, color_bull, color_bear))
Yer İmleri