PHP Code:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © vanAmsen
//@version=5
indicator(".", overlay = true)
atrPeriod = input.int(10, title="ATR Length", tooltip="Specifies the period over which the Average True Range (ATR) is calculated.", minval=1)
factor = input.float(9.9, title="Factor", tooltip="Multiplier factor for the ATR to define the distance of the Supertrend line from the price.", minval=0.1, step=0.1)
upColor = input.color(color.rgb(122,244,122), title="Up Color", tooltip="Color for the Supertrend line when the trend is upwards or bullish.")
downColor = input.color(color.rgb(244,122,122), title="Down Color", tooltip="Color for the Supertrend line when the trend is downwards or bearish.")
// Add switchers
showLabel = input.bool(true, title="Show Label", tooltip="Toggle to show or hide labels indicating statistical metrics like win rate and expected return.")
showTable = input.bool(false, title="Show Table", tooltip="Toggle to show or hide the interactive table summarizing key metrics.")
showLine = input.bool(false, title="Show Line", tooltip="Toggle to show or hide the Supertrend lines on the chart.")
[supertrend, direction] = ta.supertrend(factor, atrPeriod)
// Lists to store durations and percentage changes for up and down trends
var int[] durations_up_win = array.new_int(0)
var int[] durations_up_loss = array.new_int(0)
var int[] durations_down_win = array.new_int(0)
var int[] durations_down_loss = array.new_int(0)
var float[] pct_changes_up_win = array.new_float(0)
var float[] pct_changes_up_loss = array.new_float(0)
var float[] pct_changes_down_win = array.new_float(0)
var float[] pct_changes_down_loss = array.new_float(0)
// Additional arrays to store max running profit for uptrend and min running profit for downtrend
var float[] pct_changes_up_max = array.new_float(0)
var float[] pct_changes_down_min = array.new_float(0)
// Track the starting bar and price of the current trend
var int startBar = na
var float startPrice = na
// Track max running profit for uptrend and min running profit for downtrend
var float max_up = na
var float min_down = na
float pct_change = (close / startPrice) - 1
// Tracking max and min running profits for the current trend
if (direction[1] < 0) // Uptrend
max_up := math.max(nz(max_up), pct_change)
else
min_down := math.min(nz(min_down), pct_change)
if (direction != direction[1])
if (na(startBar) == false)
if (direction[1] < 0) // Uptrend
array.push(pct_changes_up_max, max_up)
if (pct_change > 0) // Win scenario for uptrend
array.push(pct_changes_up_win, pct_change)
array.push(durations_up_win, bar_index - startBar)
else // Loss scenario for uptrend
array.push(pct_changes_up_loss, pct_change)
array.push(durations_up_loss, bar_index - startBar)
else // Downtrend
array.push(pct_changes_down_min, min_down)
if (pct_change < 0) // Win scenario for downtrend
array.push(pct_changes_down_win, pct_change)
array.push(durations_down_win, bar_index - startBar)
else // Loss scenario for downtrend
array.push(pct_changes_down_loss, pct_change)
array.push(durations_down_loss, bar_index - startBar)
// Reset max and min values when the direction changes
max_up := na
min_down := na
// Start of new trend
startBar := bar_index
startPrice := close
// Calculate average durations for up and down trends
avg_duration_up_win = bar_index + array.avg(durations_up_win)
avg_duration_up_loss = bar_index + array.avg(durations_up_loss)
avg_duration_down_win = bar_index + array.avg(durations_down_win)
avg_duration_down_loss = bar_index + array.avg(durations_down_loss)
// Calculate average percentage changes for wins and losses
float avg_pct_change_up_win = array.avg(pct_changes_up_win)
float avg_pct_change_up_loss = array.avg(pct_changes_up_loss)
float avg_pct_change_down_win = array.avg(pct_changes_down_win)
float avg_pct_change_down_loss = array.avg(pct_changes_down_loss)
// Calculate the average max percentage change for uptrends and the average min percentage change for downtrends
float avg_max_pct_change_up = array.avg(pct_changes_up_max)
float avg_min_pct_change_down = array.avg(pct_changes_down_min)
avg_pct_up_win = close * (1 + avg_pct_change_up_win)
avg_pct_up_loss = close * (1 + avg_pct_change_up_loss)
avg_pct_down_win = close * (1 + avg_pct_change_down_win)
avg_pct_down_loss = close * (1 + avg_pct_change_down_loss)
// Calculate win rates
float win_rate_up = array.size(pct_changes_up_win) / (array.size(pct_changes_up_win) + array.size(pct_changes_up_loss))
float win_rate_down = array.size(pct_changes_down_win) / (array.size(pct_changes_down_win) + array.size(pct_changes_down_loss))
// Calculate expected returns
float expected_return_up = win_rate_up * avg_pct_change_up_win + (1 - win_rate_up) * avg_pct_change_up_loss
float expected_return_down = win_rate_down * avg_pct_change_down_win + (1 - win_rate_down) * avg_pct_change_down_loss
// Draw the lines for win and loss scenarios and display win rate and expected return
if (direction != direction[1])
label_text_up = "WR:" + str.tostring(win_rate_up * 100, "#.#") + "% / ER:" + str.tostring(expected_return_up * 100, "#.#") + "%\nEW:" + str.tostring(avg_pct_change_up_win * 100, "#.#") + "% / EL:" + str.tostring(avg_pct_change_up_loss * 100, "#.#") + "%"
label_text_down = "WR:" + str.tostring(win_rate_down * 100, "#.#") + "% / ER:" + str.tostring(expected_return_down * 100, "#.#") + "%\nEW:" + str.tostring(avg_pct_change_down_win * 100, "#.#") + "% / EL:" + str.tostring(avg_pct_change_down_loss * 100, "#.#") + "%"
if (direction < 0) // Uptrend
if showLine
line.new(x1=startBar, y1=startPrice, x2=avg_duration_up_win, y2=avg_pct_up_win, width=1, color=upColor, style=line.style_dashed)
line.new(x1=startBar, y1=startPrice, x2=avg_duration_up_loss, y2=avg_pct_up_loss, width=1, color=downColor, style=line.style_dashed)
if showLabel
// Add label for win rate, expected return, and average percentage changes
label.new(x=startBar, y=supertrend , text=label_text_up, style=label.style_label_up, color=upColor)
else // Downtrend
if showLine
line.new(x1=startBar, y1=startPrice, x2=avg_duration_down_win, y2=avg_pct_down_win, width=1, color=downColor, style=line.style_dashed)
line.new(x1=startBar, y1=startPrice, x2=avg_duration_down_loss, y2=avg_pct_down_loss, width=1, color=upColor, style=line.style_dashed)
if showLabel
// Add label for win rate, expected return, and average percentage changes
label.new(x=startBar, y=supertrend, text=label_text_down, style=label.style_label_down, color=downColor)
if showTable
// Create a table with 3 columns and 4 rows in the top right corner of the chart
var table t = table.new(position = position.top_right, columns = 3, rows = 4, bgcolor = color.new(color.gray, 90))
// Update the table data each bar with the latest metrics
// Define the text to be displayed in the cells
string wr_text_up = str.tostring(win_rate_up * 100, "#.#") + "%"
string er_text_up = str.tostring(expected_return_up * 100, "#.#") + "%"
string ew_text_up = str.tostring(avg_pct_change_up_win * 100, "#.#") + "%"
string el_text_up = str.tostring(avg_pct_change_up_loss * 100, "#.#") + "%"
string wr_text_down = str.tostring(win_rate_down * 100, "#.#") + "%"
string er_text_down = str.tostring(expected_return_down * 100, "#.#") + "%"
string ew_text_down = str.tostring(avg_pct_change_down_win * 100, "#.#") + "%"
string el_text_down = str.tostring(avg_pct_change_down_loss * 100, "#.#") + "%"
// Update the table every bar, so the table always displays the latest metrics
table.cell(t, 0, 0, "WR:", bgcolor=color.silver)
table.cell(t, 1, 0, wr_text_up, bgcolor=upColor)
table.cell(t, 2, 0, wr_text_down, bgcolor=downColor)
table.cell(t, 0, 1, "ER:", bgcolor=color.silver)
table.cell(t, 1, 1, er_text_up, bgcolor=upColor)
table.cell(t, 2, 1, er_text_down, bgcolor=downColor)
table.cell(t, 0, 2, "EW:", bgcolor=color.silver)
table.cell(t, 1, 2, ew_text_up, bgcolor=upColor)
table.cell(t, 2, 2, ew_text_down, bgcolor=downColor)
table.cell(t, 0, 3, "EL:", bgcolor=color.silver)
table.cell(t, 1, 3, el_text_up, bgcolor=upColor)
table.cell(t, 2, 3, el_text_down, bgcolor=downColor)
plot(direction < 0 ? supertrend : na, "Up Trend", color = upColor, style = plot.style_linebr)
plot(direction > 0 ? supertrend : na, "Down Trend", color = downColor, style = plot.style_linebr)
////
//@version=5
wicks = input(true, "Take Wicks into Account?")
highlightState = input(true, "Highlight State?")
ma(source, length, type) =>
type == "SMA" ? ta.sma(source, length) :
type == "EMA" ? ta.ema(source, length) :
type == "SMMA (RMA)" ? ta.rma(source, length) :
type == "WMA" ? ta.wma(source, length) :
type == "VWMA" ? ta.vwma(source, length) :
na
show_ma1 = input(true , "MA High", inline="MA #1", group="Channel №1")
ma1_type = input.string("SMA" , "" , inline="MA #1", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="Channel №1")
ma1_source = input(high , "" , inline="MA #1", group="Channel №1")
ma1_length = input.int(50 , "" , inline="MA #1", minval=1, group="Channel №1")
ma1_color = input(color.green, "" , inline="MA #1", group="Channel №1")
ma1 = ta.ema(ma1_source, ma1_length)
show_ma2 = input(true , "MA Low", inline="MA #2", group="Channel №1")
ma2_type = input.string("SMA" , "" , inline="MA #2", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="Channel №1")
ma2_source = input(low , "" , inline="MA #2", group="Channel №1")
ma2_length = input.int(200 , "" , inline="MA #2", minval=1, group="Channel №1")
ma2_color = input(color.red, "" , inline="MA #2", group="Channel №1")
ma2 = ta.ema(ma2_source, ma2_length)
showLabels1 = input(true, "Show Buy/Sell Labels?", group="Channel №1")
show_ma3 = input(true , "MA High", inline="MA #3", group="Channel №2")
ma3_type = input.string("WMA" , "" , inline="MA #3", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="Channel №2")
ma3_source = input(high , "" , inline="MA #3", group="Channel №2")
ma3_length = input.int(50 , "" , inline="MA #3", minval=1, group="Channel №2")
ma3_color = input(color.orange, "" , inline="MA #3", group="Channel №2")
ma3 = ma(ma3_source, ma3_length, ma3_type)
show_ma4 = input(true , "MA Low", inline="MA #4", group="Channel №2")
ma4_type = input.string("WMA" , "" , inline="MA #4", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="Channel №2")
ma4_source = input(low , "" , inline="MA #4", group="Channel №2")
ma4_length = input.int(200 , "" , inline="MA #4", minval=1, group="Channel №2")
ma4_color = input(color.blue, "" , inline="MA #4", group="Channel №2")
ma4 = ma(ma4_source, ma4_length, ma4_type)
showLabels2 = input(true, "Show Buy/Sell Labels?", group="Channel №2")
Hlv1 = float(na)
Hlv1 := (wicks ? high : close) > ma1 ? 1 : (wicks ? low : close) < ma2 ? -1 : Hlv1[1]
sslUp1 = Hlv1 < 0 ? ma2 : ma1
sslDown1 = Hlv1 < 0 ? ma1 : ma2
Color1 = Hlv1 == 1 ? ma1_color : ma2_color
fillColor1 = highlightState ? (color.new(Color1, 90)) : na
highLine1 = plot(show_ma1 ? sslUp1 : na, title="UP", linewidth=2, color = Color1)
lowLine1 = plot(show_ma2 ? sslDown1 : na, title="DOWN", linewidth=2, color = Color1)
// Hide labels above MA
buySignal1 = show_ma1 and showLabels1 and Hlv1 == 1 and Hlv1[1] == -1
sellSignal1 = show_ma2 and showLabels1 and Hlv1 == -1 and Hlv1[1] == 1
plotshape(buySignal1 and close > ta.ema(close, 20), title="Buy Label", text="Buy", location=location.belowbar, style=shape.labelup, size=size.tiny, color=Color1, textcolor=color.white)
plotshape(sellSignal1 and close < ta.ema(close, 20), title="Sell Label", text="Sell", location=location.abovebar, style=shape.labeldown, size=size.tiny, color=Color1, textcolor=color.white)
//fill(highLine1, lowLine1, color = fillColor1)
Hlv2 = float(na)
Hlv2 := (wicks ? high : close) > ma3 ? 1 : (wicks ? low : close) < ma4 ? -1 : Hlv2[1]
sslUp2 = Hlv2 < 0 ? ma4 : ma3
sslDown2 = Hlv2 < 0 ? ma3 : ma4
Color2 = Hlv2 == 1 ? ma3_color : ma4_color
fillColor2 = highlightState ? (color.new(Color2, 90)) : na
highLine2 = plot(show_ma3 ? sslUp2 : na, title="UP", linewidth=2, color = Color2)
lowLine2 = plot(show_ma4 ? sslDown2 : na, title="DOWN", linewidth=2, color = Color2)
// Hide labels below MA
buySignal2 = show_ma3 and showLabels2 and Hlv2 == 1 and Hlv2[1] == -1
sellSignal2 = show_ma4 and showLabels2 and Hlv2 == -1 and Hlv2[1] == 1
plotshape(buySignal2 and close < ta.ema(close, 200), title="Buy Label", text="Buy", location=location.belowbar, style=shape.labelup, size=size.tiny, color=Color2, textcolor=color.white)
plotshape(sellSignal2 and close > ta.ema(close, 200), title="Sell Label", text="Sell", location=location.abovebar, style=shape.labeldown, size=size.tiny, color=Color2, textcolor=color.white)
//fill(highLine2, lowLine2, color = fillColor2)
////
//@version=5
/////////GENIAL LINE
mostrarGenial = input(true, title='Mostrar genial line')
src = input(close)
genial = input(32, title='32')
pgenial = ta.sma(src, genial)
genialplot = plot(mostrarGenial ? pgenial : na, color=color.rgb(3, 68, 247), style=plot.style_cross, ****=false, linewidth=2, title='32')
/////////FIN GENIAL LINE
//////ZONA DE CORRECION
ema8 = ta.ema(src, input(8,title='8'))
///////WILDER 8
f_wwma(l, p) =>
wwma = 0.
wwma := (nz(wwma[1]) * (l - 1) + p) / l
wilder8 = f_wwma(8, close)
// ///// FIN WILDER 8
fill_color = ema8> wilder8 ? color.new(#09ee11, 100) : color.new(#f70202, 100)
plotema8 = plot(ema8,color=fill_color, linewidth=1,title='8')
plotwilder8 = plot(wilder8,color=color.rgb(252, 248, 3), linewidth=1,style=plot.style_circles,title='8')
// ////// Colorear el fondo entre las EMAs
//fill(plotema8,plotwilder8,color=fill_color,title ='TUNEL DE CORRECCION',editable = true)
///////FIN ZONA DE CORRECION
/////TUNEL DOMENEC
ema123 = input(123,title='EMA-123')
ema188 = input(188,title='EMA-188')
ema416 = input(416,title='EMA-416')
ema618 = input(618,title='EMA-618')
ema882 = input(882,title='EMA-882')
ema1223 = input(1223,title='EMA-1223')
pema123 = ta.ema(src, ema123)
pema188 = ta.ema(src, ema188)
pema416 = ta.ema(src, ema416)
pema618 = ta.ema(src, ema618)
pema882 = ta.ema(src, ema882)
pema1223 = ta.ema(src, ema1223)
//cinta1 = plot(pema123,title='EMA-123 TUNEL 1',color=color.blue, linewidth=1,editable = true)
//cinta1_1 = plot(pema188,title='EMA 188 TUNEL 1',color=color.blue, linewidth=1,editable = true)
//cinta2 = plot(pema416,title='EMA-416 TUNEL 2',color=color.yellow, linewidth=1,editable = true)
//cinta2_1 = plot(pema618,title='EMA 618 TUNEL 2',color=color.yellow, linewidth=1,editable = true)
//cinta3 = plot(pema882,title='EMA-882 TUNEL 3', color=#FF1493, linewidth=1,editable = true)
//cinta3_1 = plot(pema1223,title='EMA 1223 TUNEL 3', color=#FF1493, linewidth=1,editable = true)
//fill(cinta1,cinta1_1,color.new(color.blue,transp = 75),title='COLOR TUNEL 1', editable=true)
//fill(cinta2,cinta2_1,color.new(color.yellow,transp=75),title='COLOR TUNEL 2', editable=true)
//fill(cinta3,cinta3_1,color.new(color=#FF1493, transp=75),title='COLOR TUNEL 3', editable=true)
//////FIN TUNEL DOMENEC
Yer İmleri