100% found this document useful (1 vote)
2K views9 pages

The Real-Gains Algo

Uploaded by

venkatesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
2K views9 pages

The Real-Gains Algo

Uploaded by

venkatesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

//@version=5

indicator(title="Range Filter Buy and Sell 5min", shorttitle="The Real GainsAlgo",


overlay=true)

// 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")

// Smooth Average Range


smoothrng(x, t, m) =>
wper = t * 2 - 1
avrng = ta.ema(math.abs(x - x[1]), t)
smoothrng = ta.ema(avrng, wper) * m
smoothrng
smrng = smoothrng(src, per, mult)

// 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

filtplot = plot(filt, color=filtcolor, linewidth=2, title="Range Filter")

// 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))

alertcondition(longCondition, title="Buy alert on Range Filter", message="Buy alert


on Range Filter")
alertcondition(shortCondition, title="Sell alert on Range Filter", message="Sell
alert on Range Filter")
alertcondition(longCondition or shortCondition, title="Buy and Sell alert on Range
Filter", message="Buy and Sell alert on Range Filter")

//* TTD BAR SETTINGS *//

paint_bars = input(false, title = "Paint bars?", group = 'Bars Settings')


catch_flat = input(false, title = "Try to catch flat?", group = 'Bars Settings')
uptrend_colour = input.color(defval = color.green, title = "Uptrend colour", group
= 'Bars Settings')
downtrend_colour = input.color(defval = color.red, title = "Downtrend colour",
group = 'Bars Settings')
neutraltrend_colour = input.color(defval = color.gray, title = "Downtrend colour",
tooltip = 'Note that this value is ignored if the setting to catch flat is switched
off.', group = 'Bars Settings')

///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////

//* TABLE SETTINGS *//

show_header = input(false, title = "Show header?", group = 'Table Settings')


show_ema_value = input(false, title = "Show EMA value?", group = 'Table Settings')
dashboard_position = input.session("Middle right", "Position", ["Top right",
"Bottom right", "Top left", "Bottom left", "Top center", "Bottom center", "Middle
right"], group = 'Table Settings')
text_size = input.session('Normal', "Size", options = ["Tiny", "Small", "Normal",
"Large"], group = 'Table Settings')
text_color = input(#d1d4dc, title = "Text colour", group = 'Table Settings')
table_color = input(color.gray, title = "Border colour", group = 'Table Settings')
uptrend_indicator = input("🟢", title = "Uptrend indicator", group = 'Table
Settings')
downtrend_indicator = input("🔴", title = "Downtrend indicator", group = 'Table
Settings')
neutraltrend_indicator = input("⚫️", title = "Neutraltrend indicator", tooltip =
'Note that this value is ignored if the setting to catch flat is switched off.',
group = 'Table Settings')

///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////

//* EMA SETTINGS *//

trend_identification_approach = input.session("Direction of a single EMA", "Trend


identification approach", ["Direction of a single EMA", "Comparison of the two
EMAs"], group = 'EMA Settings')
ema1_length = input.int(title = "EMA length", defval = 50, minval = 1, maxval =
800, group = 'EMA Settings')
ema2_length = input.int(title = "Additional EMA length", defval = 200, minval = 20,
maxval = 800, tooltip = 'Note that the single EMA trend identification approach
ignores this value.', group = 'EMA Settings')

///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////

//* TIMEFRAME SETTINGS *//

show_1m = input(true, title = 'Show 1m', group = 'Timeframe Settings')


show_2m = input(false, title = 'Show 2m ', group = 'Timeframe Settings')
show_3m = input(true, title = 'Show 3m ', group = 'Timeframe Settings')
show_5m = input(true, title = 'Show 5m', group = 'Timeframe Settings')
show_10m = input(false, title = 'Show 10m', group = 'Timeframe Settings')
show_15m = input(true, title = 'Show 15m', group = 'Timeframe Settings')
show_30m = input(false, title = 'Show 30m', group = 'Timeframe Settings')
show_45m = input(false, title = 'Show 45m', group = 'Timeframe Settings')
show_1h = input(true, title = 'Show 1h', group = 'Timeframe Settings')
show_2h = input(false, title = 'Show 2h', group = 'Timeframe Settings')
show_3h = input(false, title = 'Show 3h', group = 'Timeframe Settings')
show_4h = input(true, title = 'Show 4h', group = 'Timeframe Settings')
show_12h = input(false, title = 'Show 12h', group = 'Timeframe Settings')
show_D = input(true, title = 'Show D', group = 'Timeframe Settings')
show_3D = input(false, title = 'Show 3D', group = 'Timeframe Settings')
show_W = input(false, title = 'Show W', group = 'Timeframe Settings')
show_M = input(false, title = 'Show M', group = 'Timeframe Settings')

///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////

//* CALCULATION *//

var table_position = dashboard_position == 'Top right' ? position.top_right :


dashboard_position == 'Top left' ? position.top_left :
dashboard_position == 'Top center' ? position.top_center :
dashboard_position == 'Bottom right' ? position.bottom_right :
dashboard_position == 'Bottom left' ? position.bottom_left :
dashboard_position == 'Bottom center' ? position.bottom_center :
dashboard_position == 'Middle right' ? position.middle_right :
dashboard_position == 'Middle left' ? position.middle_left :
position.middle_right

var table_text_size = text_size == 'Normal' ? size.normal :


text_size == 'Large' ? size.large :
text_size == 'Tiny' ? size.tiny :
text_size == 'Small' ? size.small : size.normal

var t = table.new(position = table_position,


columns = 3,
rows = 20,
frame_color = table_color,
frame_width = 2,
border_color = table_color,
border_width = 2)

calc_smma(src, len) =>


var float smma = na
smma := na(smma) ? ta.sma(src, len) : (smma[1] * (len - 1) + src) / len
smma

calc_zlema(src, len) =>


ema1 = ta.ema(src, len)
ema2 = ta.ema(ema1, len)
d = ema1 - ema2
ema1 + d

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

is_impulse = sh != 0 and sh_prev != 0


is_impulse

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]

///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////

//* TABLE *//

[ema_1m, trend_indicator_1m, _] = request.security(syminfo.tickerid, "1",


get_trend_status())
[ema_2m, trend_indicator_2m, _] = request.security(syminfo.tickerid, "2",
get_trend_status())
[ema_3m, trend_indicator_3m, _] = request.security(syminfo.tickerid, "3",
get_trend_status())
[ema_5m, trend_indicator_5m, _] = request.security(syminfo.tickerid, "5",
get_trend_status())
[ema_10m, trend_indicator_10m, _] = request.security(syminfo.tickerid, "10",
get_trend_status())
[ema_15m, trend_indicator_15m, _] = request.security(syminfo.tickerid, "15",
get_trend_status())
[ema_30m, trend_indicator_30m, _] = request.security(syminfo.tickerid, "30",
get_trend_status())
[ema_45m, trend_indicator_45m, _] = request.security(syminfo.tickerid, "45",
get_trend_status())
[ema_1h, trend_indicator_1h, _] = request.security(syminfo.tickerid, "60",
get_trend_status())
[ema_2h, trend_indicator_2h, _] = request.security(syminfo.tickerid, "120",
get_trend_status())
[ema_3h, trend_indicator_3h, _] = request.security(syminfo.tickerid, "180",
get_trend_status())
[ema_4h, trend_indicator_4h, _] = request.security(syminfo.tickerid, "240",
get_trend_status())
[ema_12h, trend_indicator_12h, _] = request.security(syminfo.tickerid, "720",
get_trend_status())
[ema_D, trend_indicator_D, _] = request.security(syminfo.tickerid, "D",
get_trend_status())
[ema_3D, trend_indicator_3D, _] = request.security(syminfo.tickerid, "3D",
get_trend_status())
[ema_W, trend_indicator_W, _] = request.security(syminfo.tickerid, "W",
get_trend_status())
[ema_M, trend_indicator_M, _] = request.security(syminfo.tickerid, "M",
get_trend_status())

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)

///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////

//* Simple moving averages *//

len1 = input.int(20, minval=1, title='SMA #1')


src1 = input(close, title='SMA Source #1')
out1 = ta.sma(src1, len1)
plot(out1, title='SMA #1', color=close >= out1 ? color.blue : color.orange)

len2 = input.int(50, minval=1, title='SMA #2')


src2 = input(close, title='SMA Source #2')
out2 = ta.sma(src2, len2)
plot(out2, title='SMA #2', color=close >= out2 ? color.blue : color.orange,
linewidth=2)

len3 = input.int(100, minval=1, title='SMA #3')


src3 = input(close, title='SMA Source #3')
out3 = ta.sma(src3, len3)
plot(out3, title='SMA #3', color=close >= out3 ? color.blue : color.orange)

len4 = input.int(150, minval=1, title='SMA #4')


src4 = input(close, title='SMA Source #4')
out4 = ta.sma(src4, len4)
plot(out4, title='SMA #4', color=close >= out4 ? color.blue : color.orange)

len5 = input.int(200, minval=1, title='SMA #5')


src5 = input(close, title='SMA Source #5')
out5 = ta.sma(src5, len5)
plot(out5, title='SMA #5', color=close >= out5 ? color.blue : color.orange,
linewidth=4)

len6 = input.int(250, minval=1, title='SMA #6')


src6 = input(close, title='SMA Source #6')
out6 = ta.sma(src6, len6)
plot(out6, title='SMA #6', color=close >= out6 ? color.green : color.red)

len7 = input.int(300, minval=1, title='SMA #7')


src7 = input(close, title='SMA Source #7')
out7 = ta.sma(src7, len7)
plot(out7, title='SMA #7', color=close >= out7 ? color.green : color.red,
linewidth=2)

len8 = input.int(400, minval=1, title='SMA #8')


src8 = input(close, title='SMA Source #8')
out8 = ta.sma(src8, len8)
plot(out8, title='SMA #8', color=close >= out8 ? color.green : color.red)

len9 = input.int(500, minval=1, title='SMA #9')


src9 = input(close, title='SMA Source #9')
out9 = ta.sma(src9, len9)
plot(out9, title='SMA #9', color=close >= out9 ? color.green : color.red)

len10 = input.int(600, minval=1, title='SMA #10')


src10 = input(close, title='SMA Source #10')
out10 = ta.sma(src10, len10)
plot(out10, title='SMA #10', color=close >= out10 ? color.green : color.red,
linewidth=4)

///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////

You might also like