The Real-Gains Algo
The Real-Gains Algo
// Color variables
upColor = color.white
midColor = #90bff9
downColor = color.blue
// Source
src = input(defval=close, title="Source")
// Sampling Period
// Settings for 5min chart, BTCUSDC. For Other coin, change the paremeters
per = input.int(defval=100, minval=1, title="Sampling Period")
// Range Multiplier
mult = input.float(defval=3.0, minval=0.1, title="Range Multiplier")
// Range Filter
rngfilt(x, r) =>
rngfilt = x
rngfilt := x > nz(rngfilt[1]) ? x - r < nz(rngfilt[1]) ? nz(rngfilt[1]) : x - r
:
x + r > nz(rngfilt[1]) ? nz(rngfilt[1]) : x + r
rngfilt
filt = rngfilt(src, smrng)
// Filter Direction
upward = 0.0
upward := filt > filt[1] ? nz(upward[1]) + 1 : filt < filt[1] ? 0 : nz(upward[1])
downward = 0.0
downward := filt < filt[1] ? nz(downward[1]) + 1 : filt > filt[1] ? 0 :
nz(downward[1])
// Target Bands
hband = filt + smrng
lband = filt - smrng
// Colors
filtcolor = upward > 0 ? upColor : downward > 0 ? downColor : midColor
barcolor = src > filt and src > src[1] and upward > 0 ? upColor :
src > filt and src < src[1] and upward > 0 ? upColor :
src < filt and src < src[1] and downward > 0 ? downColor :
src < filt and src > src[1] and downward > 0 ? downColor : midColor
// Target
hbandplot = plot(hband, color=color.new(upColor, 70), title="High Target")
lbandplot = plot(lband, color=color.new(downColor, 70), title="Low Target")
// Fills
fill(hbandplot, filtplot, color=color.new(upColor, 90), title="High Target Range")
fill(lbandplot, filtplot, color=color.new(downColor, 90), title="Low Target Range")
// Bar Color
barcolor(barcolor)
// Break Outs
longCond = bool(na)
shortCond = bool(na)
longCond := src > filt and src > src[1] and upward > 0 or
src > filt and src < src[1] and upward > 0
shortCond := src < filt and src < src[1] and downward > 0 or
src < filt and src > src[1] and downward > 0
CondIni = 0
CondIni := longCond ? 1 : shortCond ? -1 : CondIni[1]
longCondition = longCond and CondIni[1] == -1
shortCondition = shortCond and CondIni[1] == 1
//Alerts
plotshape(longCondition, title="Buy Signal", text="Buy", textcolor=color.white,
style=shape.labelup, size=size.small, location=location.belowbar,
color=color.new(#aaaaaa, 20))
plotshape(shortCondition, title="Sell Signal", text="Sell", textcolor=color.white,
style=shape.labeldown, size=size.small, location=location.abovebar,
color=color.new(downColor, 20))
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////
check_impulse() =>
impulse_length = 34
impulse_strength = 9
hi = calc_smma(high, impulse_length)
lo = calc_smma(low, impulse_length)
mi = calc_zlema(hlc3, impulse_length)
md = (mi > hi) ? (mi - hi) : (mi < lo) ? (mi - lo) : 0
md_prev = (mi[1] > hi[1]) ? (mi[1] - hi[1]) : (mi[1] < lo[1]) ? (mi[1] - lo[1])
: 0
sb = ta.sma(md, impulse_strength)
sb_prev = ta.sma(md_prev, impulse_strength)
sh = md - sb
sh_prev = md_prev - sb_prev
get_trend_status() =>
impulse = catch_flat ? check_impulse() : true
ema1_current_candle = ta.ema(close, ema1_length)
ema1_previous_candle = ema1_current_candle[1]
round = ema1_current_candle > 1000 ? 0 : ema1_current_candle > 10 ? 1 :
ema1_current_candle > 0 ? 2 : ema1_current_candle > 0.1 ? 3 : 10
ema1_str = str.tostring(math.round(ema1_current_candle, round))
if (trend_identification_approach == "Direction of a single EMA")
ema1_previous_previous_candle = ema1_current_candle[2]
trend_current_candle = not impulse ? neutraltrend_indicator :
ema1_current_candle > ema1_previous_candle ? uptrend_indicator :
ema1_current_candle < ema1_previous_candle ? downtrend_indicator :
neutraltrend_indicator
trend_previous_candle = not impulse ? neutraltrend_indicator :
ema1_previous_candle > ema1_previous_previous_candle ? uptrend_indicator :
ema1_previous_candle < ema1_previous_previous_candle ? downtrend_indicator :
neutraltrend_indicator
[ema1_str, trend_current_candle, trend_previous_candle]
else
ema2_current_candle = ta.ema(close, ema2_length)
ema2_previous_candle = ema2_current_candle[1]
trend_current_candle = not impulse ? neutraltrend_indicator :
ema1_current_candle > ema2_current_candle ? uptrend_indicator : ema1_current_candle
< ema2_current_candle ? downtrend_indicator : neutraltrend_indicator
trend_previous_candle = not impulse ? neutraltrend_indicator :
ema1_previous_candle > ema2_previous_candle ? uptrend_indicator :
ema1_previous_candle < ema2_previous_candle ? downtrend_indicator :
neutraltrend_indicator
[ema1_str, trend_current_candle, trend_previous_candle]
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////
if (barstate.islast)
if (show_header)
table.cell(t, 0, 0, "Timeframe", text_color = text_color, text_size =
table_text_size)
table.cell(t, 1, 0, "Trend", text_color = text_color, text_size =
table_text_size)
if (show_ema_value)
table.cell(t, 2, 0, str.tostring(ema1_length) + " EMA", text_color =
text_color, text_size = table_text_size)
if (show_1m)
table.cell(t, 0, 1, "1m", text_color = text_color, text_size =
table_text_size)
table.cell(t, 1, 1, trend_indicator_1m, text_color = text_color, text_size
= table_text_size)
if (show_ema_value)
table.cell(t, 2, 1, ema_1m, text_color = text_color, text_size =
table_text_size)
if (show_2m)
table.cell(t, 0, 2, "2m", text_color = text_color, text_size =
table_text_size)
table.cell(t, 1, 2, trend_indicator_2m, text_color = text_color, text_size
= table_text_size)
if (show_ema_value)
table.cell(t, 2, 2, ema_2m, text_color = text_color, text_size =
table_text_size)
if (show_3m)
table.cell(t, 0, 3, "3m", text_color = text_color, text_size =
table_text_size)
table.cell(t, 1, 3, trend_indicator_3m, text_color = text_color, text_size
= table_text_size)
if (show_ema_value)
table.cell(t, 2, 3, ema_3m, text_color = text_color, text_size =
table_text_size)
if (show_5m)
table.cell(t, 0, 4, "5m", text_color = text_color, text_size =
table_text_size)
table.cell(t, 1, 4, trend_indicator_5m, text_color = text_color, text_size
= table_text_size)
if (show_ema_value)
table.cell(t, 2, 4, ema_5m, text_color = text_color, text_size =
table_text_size)
if (show_10m)
table.cell(t, 0, 5, "10m", text_color = text_color, text_size =
table_text_size)
table.cell(t, 1, 5, trend_indicator_10m, text_color = text_color, text_size
= table_text_size)
if (show_ema_value)
table.cell(t, 2, 5, ema_10m, text_color = text_color, text_size =
table_text_size)
if (show_15m)
table.cell(t, 0, 6, "15m", text_color = text_color, text_size =
table_text_size)
table.cell(t, 1, 6, trend_indicator_15m, text_color = text_color, text_size
= table_text_size)
if (show_ema_value)
table.cell(t, 2, 6, ema_15m, text_color = text_color, text_size =
table_text_size)
if (show_30m)
table.cell(t, 0, 7, "30m", text_color = text_color, text_size =
table_text_size)
table.cell(t, 1, 7, trend_indicator_30m, text_color = text_color, text_size
= table_text_size)
if (show_ema_value)
table.cell(t, 2, 7, ema_30m, text_color = text_color, text_size =
table_text_size)
if (show_45m)
table.cell(t, 0, 8, "45m", text_color = text_color, text_size =
table_text_size)
table.cell(t, 1, 8, trend_indicator_45m, text_color = text_color, text_size
= table_text_size)
if (show_ema_value)
table.cell(t, 2, 8, ema_45m, text_color = text_color, text_size =
table_text_size)
if (show_1h)
table.cell(t, 0, 9, "1h", text_color = text_color, text_size =
table_text_size)
table.cell(t, 1, 9, trend_indicator_1h, text_color = text_color, text_size
= table_text_size)
if (show_ema_value)
table.cell(t, 2, 9, ema_1h, text_color = text_color, text_size =
table_text_size)
if (show_2h)
table.cell(t, 0, 10, "2h", text_color = text_color, text_size =
table_text_size)
table.cell(t, 1, 10, trend_indicator_2h, text_color = text_color, text_size
= table_text_size)
if (show_ema_value)
table.cell(t, 2, 10, ema_2h, text_color = text_color, text_size =
table_text_size)
if (show_3h)
table.cell(t, 0, 11, "3h", text_color = text_color, text_size =
table_text_size)
table.cell(t, 1, 11, trend_indicator_3h, text_color = text_color, text_size
= table_text_size)
if (show_ema_value)
table.cell(t, 2, 11, ema_3h, text_color = text_color, text_size =
table_text_size)
if (show_4h)
table.cell(t, 0, 12, "4h", text_color = text_color, text_size =
table_text_size)
table.cell(t, 1, 12, trend_indicator_4h, text_color = text_color, text_size
= table_text_size)
if (show_ema_value)
table.cell(t, 2, 12, ema_4h, text_color = text_color, text_size =
table_text_size)
if (show_12h)
table.cell(t, 0, 13, "12h", text_color = text_color, text_size =
table_text_size)
table.cell(t, 1, 13, trend_indicator_12h, text_color = text_color,
text_size = table_text_size)
if (show_ema_value)
table.cell(t, 2, 13, ema_12h, text_color = text_color, text_size =
table_text_size)
if (show_D)
table.cell(t, 0, 14, "D", text_color = text_color, text_size =
table_text_size)
table.cell(t, 1, 14, trend_indicator_D, text_color = text_color, text_size
= table_text_size)
if (show_ema_value)
table.cell(t, 2, 14, ema_D, text_color = text_color, text_size =
table_text_size)
if (show_3D)
table.cell(t, 0, 15, "3D", text_color = text_color, text_size =
table_text_size)
table.cell(t, 1, 15, trend_indicator_3D, text_color = text_color, text_size
= table_text_size)
if (show_ema_value)
table.cell(t, 2, 15, ema_3D, text_color = text_color, text_size =
table_text_size)
if (show_W)
table.cell(t, 0, 16, "W", text_color = text_color, text_size =
table_text_size)
table.cell(t, 1, 16, trend_indicator_W, text_color = text_color, text_size
= table_text_size)
if (show_ema_value)
table.cell(t, 2, 16, ema_W, text_color = text_color, text_size =
table_text_size)
if (show_M)
table.cell(t, 0, 17, "M", text_color = text_color, text_size =
table_text_size)
table.cell(t, 1, 17, trend_indicator_M, text_color = text_color, text_size
= table_text_size)
if (show_ema_value)
table.cell(t, 2, 17, ema_M, text_color = text_color, text_size =
table_text_size)
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////