Skip to content

Instantly share code, notes, and snippets.

@andrIvash
Last active January 25, 2024 14:34
Show Gist options
  • Save andrIvash/24014a7796e69bb92cae2f470ba439fb to your computer and use it in GitHub Desktop.
Save andrIvash/24014a7796e69bb92cae2f470ba439fb to your computer and use it in GitHub Desktop.
telecoin
//@version=5
indicator("Telecoin", overlay=true)
// Define colors
// Get risk background color for dashboard
// Somewhat of gradient
get_risk_bgcol(r) =>
risk = r * 100
col = color.green
if risk <= 1
col := #00CC00
if risk <= 4 and risk > 1
col := #269401
if risk <= 5 and risk > 4
col := #33B900
if risk <= 10 and risk > 5
col := #695C00
if risk <= 15 and risk > 10
col := #C26100
if risk > 15
col := #C73601
col
// Define input variables
sma20Period = input.int(20, title="SMA 20 Period", minval=1)
sma50Period = input.int(50, title="SMA 50 Period", minval=1)
sma100Period = input.int(100, title="SMA 100 Period", minval=1)
sma200Period = input.int(200, title="SMA 200 Period", minval=1)
rsiPeriod = input.int(14, title="RSI Period", minval=1)
vwapPeriod = input.int(20, title="VWAP Period", minval=1)
obvPeriod = input.int(1, title="OBV Period", minval=1)
atrPeriod = input.int(14, title="ATR Period", minval=1)
// risk multiplicator
db_sl_mult = input.float(1.0, "ATR Stop-Loss Multiplier (1.0 by default)", 0.1, step = 0.05)
db_tp_mult = input.float(1.5, "ATR Take-Profit Multiplier (1.5 by default)", 0.1, step = 0.05)
// Calculate SMAs
sma20 = ta.sma(close, sma20Period)
sma50 = ta.sma(close, sma50Period)
sma100 = ta.sma(close, sma100Period)
sma200 = ta.sma(close, sma200Period)
// Calculate RSI
rsi = ta.rsi(close, rsiPeriod)
// Calculate VWAP
vwap = ta.vwap(close)
// Calculate OBV
f_obv() =>
ta.cum(math.sign(ta.change(close)) * volume)
obv = f_obv()
// Calculate ATR & risk
atr_last = ta.atr(atrPeriod)
atr_last_150 = atr_last * 150 / 100
atr_daily = request.security(syminfo.tickerid, "D", ta.atr(atrPeriod))
atr_daily_30 = atr_daily * 30 / 100
risk = 1 - (close - atr_last) / close
risk_bgcol_sl = get_risk_bgcol(risk * db_sl_mult)
risk_bgcol_tp = get_risk_bgcol(risk * db_tp_mult)
// Set indicator color
currentColor = if rsi > 50 and sma20 > sma50 and sma50 > sma100
color.orange
else if rsi < 50 and sma20 < sma50 and sma50 < sma100
color.fuchsia
else
color.gray
// check for global trend
is_trend_up = currentColor == color.orange ? true : false
is_trend_down = currentColor == color.fuchsia ? true : false
// show buy/sell signals
upLine = currentColor == color.orange and (close > vwap and obv > 0) ? true : false
downLine = currentColor == color.fuchsia and (close < vwap and obv < 0) ? true : false
// calculate open position position
isCrossMA = ta.cross(close, sma20) or ta.cross(close, sma50) or ta.cross(close, sma100) or ta.cross(close, sma200)
is_cross_up_sma20 = ta.crossover(close, sma20)
is_cross_down_sma20 = ta.crossunder(close, sma20)
is_cross_up_sma50 = ta.crossover(close, sma50)
is_cross_down_sma50 = ta.crossunder(close, sma50)
is_cross_up_sma100 = ta.crossover(close, sma100)
is_cross_down_sma100 = ta.crossunder(close, sma100)
is_cross_up_sma200 = ta.crossover(close, sma200)
is_cross_down_sma200 = ta.crossunder(close, sma200)
is_up_position = is_trend_up and isCrossMA and (is_cross_up_sma20 or is_cross_up_sma50 or is_cross_up_sma100 or is_cross_down_sma200)
is_down_position = is_trend_down and isCrossMA and (is_cross_down_sma20 or is_cross_down_sma50 or is_cross_down_sma100 or is_cross_down_sma200)
//calculate stoploss and takeprofit
stopLossLevel = currentColor == color.orange ? close - atr_last : close + atr_last
takeProfitLevel = currentColor == color.orange ? close + atr_last_150 : close - atr_last_150
// Plot data
plot(sma20, color=upLine or downLine ? currentColor : #00000000, linewidth = 4, title="sma20")
plot(sma20, color=not upLine and not downLine ? currentColor : #00000000, linewidth = 1, title="sma20")
plot(sma50, color=color.black, linewidth = 2, title="sma50")
plot(sma100, color=color.blue, linewidth = 2, title="sma100")
plot(sma200, color=color.red, linewidth = 2, title="sma200")
// Plot an arrow shape for past bars where the condition is true
plotshape(series=is_up_position, title="up", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.tiny)
plotshape(series=is_down_position, title="down", location=location.belowbar, color=color.red, style=shape.triangledown, size=size.tiny)
// create info table
var infoTable = table.new(position = position.top_right, columns = 2, rows = 6, bgcolor = color.gray, border_width = 1)
if barstate.islast
table.cell(table_id = infoTable, column = 0, row = 0, text = "Trend: ", text_color = color.white, bgcolor=color.rgb(86, 122, 94))
table.cell(table_id = infoTable, column = 1, row = 0, text = is_trend_up ? "UP" : is_trend_down ? "DOWN" : "-", text_color = color.white, bgcolor=color.rgb(86, 122, 94))
table.cell(table_id = infoTable, column = 0, row = 1, text = "Pos: ", text_color = color.white, bgcolor=color.rgb(80, 105, 86))
table.cell(table_id = infoTable, column = 1, row = 1, text = is_up_position or is_down_position ? str.tostring(close, '######.#####') : "-", text_color = color.white, bgcolor=color.rgb(86, 122, 94))
table.cell(table_id = infoTable, column = 0, row = 2, text = "TP: ", text_color = color.white, bgcolor=risk_bgcol_tp)
table.cell(table_id = infoTable, column = 1, row = 2, text = is_up_position or is_down_position ? str.tostring(takeProfitLevel, '######.#####') + " (" + str.tostring((db_tp_mult * atr_last) / close, '#####.##%') + ")" : "-", text_color = color.white, bgcolor=risk_bgcol_tp)
table.cell(table_id = infoTable, column = 0, row = 3, text = "SL: ", text_color = color.white, bgcolor=risk_bgcol_sl)
table.cell(table_id = infoTable, column = 1, row = 3, text = is_up_position or is_down_position ? str.tostring(stopLossLevel, '######.#####') + " (" + str.tostring((db_sl_mult * atr_last) / close, '#####.##%') + ")" : "-", text_color = color.white, bgcolor=risk_bgcol_sl)
table.cell(table_id = infoTable, column = 0, row = 4, text = "ATR Daily: ")
table.cell(table_id = infoTable, column = 1, row = 4, text = str.tostring(atr_daily, '######.#####'))
table.cell(table_id = infoTable, column = 0, row = 5, text = "ATR Daily (30%): ", bgcolor=color.teal)
table.cell(table_id = infoTable, column = 1, row = 5, text = str.tostring(atr_daily_30, '######.#####'), bgcolor=color.teal)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment