Last active
October 5, 2024 04:41
-
-
Save alexeyismirnov/ca13debc0daed621e15331f61d513982 to your computer and use it in GitHub Desktop.
SUPERTREND + CORAL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//@version=5 | |
strategy("SUPERTREND", overlay=true, initial_capital = 1000, currency = "USD", default_qty_type = strategy.cash, default_qty_value = 1000, commission_value = 0.04, calc_on_every_tick=true) | |
// Timeframe=30m, smoothing period = 130 | |
// CORAL trend indicator, used as "global trend" | |
src2=close | |
sm =input(21, title="Smoothing Period") | |
cd = input(0.4, title="Constant D") | |
ebc=input(false, title="Color Bars") | |
ribm=input(false, title="Ribbon Mode") | |
di = (sm - 1.0) / 2.0 + 1.0 | |
c1 = 2 / (di + 1.0) | |
c2 = 1 - c1 | |
c3 = 3.0 * (cd * cd + cd * cd * cd) | |
c4 = -3.0 * (2.0 * cd * cd + cd + cd * cd * cd) | |
c5 = 3.0 * cd + 1.0 + cd * cd * cd + 3.0 * cd * cd | |
float i1 = 0.0 | |
float i2 = 0.0 | |
float i3 = 0.0 | |
float i4 = 0.0 | |
float i5 = 0.0 | |
float i6 = 0.0 | |
i1 := c1*src2 + c2*nz(i1[1]) | |
i2 := c1*i1 + c2*nz(i2[1]) | |
i3 := c1*i2 + c2*nz(i3[1]) | |
i4 := c1*i3 + c2*nz(i4[1]) | |
i5 := c1*i4 + c2*nz(i5[1]) | |
i6 := c1*i5 + c2*nz(i6[1]) | |
bfr = -cd*cd*cd*i6 + c3*(i5) + c4*(i4) + c5*(i3) | |
bfrC = bfr > nz(bfr[1]) ? color.green : bfr < nz(bfr[1]) ? color.red : color.blue | |
plot(bfr, title="Trend", linewidth=3, style=plot.style_circles, color=bfrC) | |
// SUPERTREND indicator, used as "local trend" | |
Periods = input(title='ATR Period', defval=10) | |
src = input(hl2, title='Source') | |
Multiplier = input.float(title='ATR Multiplier', step=0.1, defval=3.0) | |
changeATR = input(title='Change ATR Calculation Method ?', defval=true) | |
showsignals = input(title='Show Buy/Sell Signals ?', defval=false) | |
highlighting = input(title='Highlighter On/Off ?', defval=true) | |
atr2 = ta.sma(ta.tr, Periods) | |
atr = changeATR ? ta.atr(Periods) : atr2 | |
up = src - Multiplier * atr | |
up1 = nz(up[1], up) | |
up := close[1] > up1 ? math.max(up, up1) : up | |
dn = src + Multiplier * atr | |
dn1 = nz(dn[1], dn) | |
dn := close[1] < dn1 ? math.min(dn, dn1) : dn | |
trend = 1 | |
trend := nz(trend[1], trend) | |
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend | |
upPlot = plot(trend == 1 ? up : na, title='Up Trend', style=plot.style_linebr, linewidth=2, color=color.new(color.green, 0)) | |
buySignal = trend == 1 and trend[1] == -1 | |
plotshape(buySignal ? up : na, title='UpTrend Begins', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.green, 0)) | |
plotshape(buySignal and showsignals ? up : na, title='Buy', text='Buy', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.green, 0), textcolor=color.new(color.white, 0)) | |
dnPlot = plot(trend == 1 ? na : dn, title='Down Trend', style=plot.style_linebr, linewidth=2, color=color.new(color.red, 0)) | |
sellSignal = trend == -1 and trend[1] == 1 | |
plotshape(sellSignal ? dn : na, title='DownTrend Begins', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.red, 0)) | |
plotshape(sellSignal and showsignals ? dn : na, title='Sell', text='Sell', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.red, 0), textcolor=color.new(color.white, 0)) | |
mPlot = plot(ohlc4, title='', style=plot.style_circles, linewidth=0) | |
longFillColor = highlighting ? trend == 1 ? color.green : color.white : color.white | |
shortFillColor = highlighting ? trend == -1 ? color.red : color.white : color.white | |
fill(mPlot, upPlot, title='UpTrend Highligter', color=longFillColor, transp=90) | |
fill(mPlot, dnPlot, title='DownTrend Highligter', color=shortFillColor, transp=90) | |
// calculating strategy entry/exit points as a combination of global+local trends | |
if barstate.isconfirmed | |
if (strategy.position_size == 0) and buySignal and (bfr > nz(bfr[1])) | |
strategy.entry("Long", strategy.long) | |
else if (strategy.position_size == 0) and sellSignal and (bfr < nz(bfr[1])) | |
strategy.entry("Short", strategy.short) | |
else if (strategy.position_size > 0) and sellSignal | |
if bfr < nz(bfr[1]) | |
strategy.entry("Short", strategy.short, comment = "RShort") | |
else | |
strategy.close("Long", comment = "ExitL") | |
else if (strategy.position_size < 0) and buySignal | |
if bfr > nz(bfr[1]) | |
strategy.entry("Long", strategy.long, comment = "RLong") | |
else | |
strategy.close("Short", comment = "ExitS") | |
/////////////////////////////////////////////////////////////////////////////// | |
// MONTHLY TABLE // | |
/////////////////////////////////////////////////////////////////////////////// | |
prec = 2 | |
new_month = month(time) != month(time[1]) | |
new_year = year(time) != year(time[1]) | |
eq = strategy.equity | |
bar_pnl = eq / eq[1] - 1 | |
bar_eq = eq - eq[1] | |
cur_month_pnl = 0.0 | |
cur_month_eq = 0.0 | |
cur_year_pnl = 0.0 | |
// Current Monthly P&L | |
cur_month_pnl := new_month ? 0.0 : (1 + cur_month_pnl[1]) * (1 + bar_pnl) - 1 | |
cur_month_eq := new_month ? 0.0 : (cur_month_eq[1] + bar_eq) | |
// Current Yearly P&L | |
cur_year_pnl := new_year ? 0.0 : (1 + cur_year_pnl[1]) * (1 + bar_pnl) - 1 | |
// Arrays to store Yearly and Monthly P&Ls | |
var month_pnl = array.new_float(0) | |
var month_eq = array.new_float(0) | |
var month_time = array.new_int(0) | |
var year_pnl = array.new_float(0) | |
var year_time = array.new_int(0) | |
last_computed = false | |
if not na(cur_month_pnl[1]) and (new_month or barstate.islast) | |
if last_computed[1] | |
array.pop(month_pnl) | |
array.pop(month_eq) | |
array.pop(month_time) | |
array.push(month_pnl, cur_month_pnl[1]) | |
array.push(month_eq, cur_month_eq[1]) | |
array.push(month_time, time[1]) | |
if not na(cur_year_pnl[1]) and (new_year or barstate.islast) | |
if last_computed[1] | |
array.pop(year_pnl) | |
array.pop(year_time) | |
array.push(year_pnl, cur_year_pnl[1]) | |
array.push(year_time, time[1]) | |
last_computed := barstate.islast ? true : nz(last_computed[1]) | |
// Monthly P&L Table | |
var monthly_table = table(na) | |
if barstate.islast | |
monthly_table := table.new(position.bottom_right, columns=14, rows=array.size(year_pnl) + 1, border_width=1) | |
table.cell(monthly_table, 0, 0, '', bgcolor=#cccccc) | |
table.cell(monthly_table, 1, 0, 'Jan', bgcolor=#cccccc) | |
table.cell(monthly_table, 2, 0, 'Feb', bgcolor=#cccccc) | |
table.cell(monthly_table, 3, 0, 'Mar', bgcolor=#cccccc) | |
table.cell(monthly_table, 4, 0, 'Apr', bgcolor=#cccccc) | |
table.cell(monthly_table, 5, 0, 'May', bgcolor=#cccccc) | |
table.cell(monthly_table, 6, 0, 'Jun', bgcolor=#cccccc) | |
table.cell(monthly_table, 7, 0, 'Jul', bgcolor=#cccccc) | |
table.cell(monthly_table, 8, 0, 'Aug', bgcolor=#cccccc) | |
table.cell(monthly_table, 9, 0, 'Sep', bgcolor=#cccccc) | |
table.cell(monthly_table, 10, 0, 'Oct', bgcolor=#cccccc) | |
table.cell(monthly_table, 11, 0, 'Nov', bgcolor=#cccccc) | |
table.cell(monthly_table, 12, 0, 'Dec', bgcolor=#cccccc) | |
table.cell(monthly_table, 13, 0, 'Year', bgcolor=#999999) | |
for yi = 0 to array.size(year_pnl) - 1 by 1 | |
table.cell(monthly_table, 0, yi + 1, str.tostring(year(array.get(year_time, yi))), bgcolor=#cccccc) | |
y_color = array.get(year_pnl, yi) > 0 ? color.new(color.green, transp=50) : color.new(color.red, transp=50) | |
table.cell(monthly_table, 13, yi + 1, str.tostring(math.round(array.get(year_pnl, yi) * 100, prec)), bgcolor=y_color, text_color=color.white) | |
for mi = 0 to array.size(month_time) - 1 by 1 | |
m_row = year(array.get(month_time, mi)) - year(array.get(year_time, 0)) + 1 | |
m_col = month(array.get(month_time, mi)) | |
m_color = array.get(month_pnl, mi) > 0 ? color.new(color.green, transp=70) : color.new(color.red, transp=70) | |
// table.cell(monthly_table, m_col, m_row, str.tostring(math.round(array.get(month_pnl, mi) * 100, prec)), bgcolor=m_color, text_color=color.white) | |
table.cell(monthly_table, m_col, m_row, str.tostring(math.round(array.get(month_eq, mi), prec)), bgcolor = m_color, text_color = color.white) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment