OPEN-SOURCE SCRIPT

RSI OB/OS THEDU 999

143
//version=6
indicator("RSI OB/OS THEDU 999", overlay=false)

//#region Inputs Section
// ================================
// Inputs Section
// ================================

// Time Settings Inputs
startTime = input.time(timestamp("1 Jan 1900"), "Start Time", group="Time Settings")
endTime = input.time(timestamp("1 Jan 2099"), "End Time", group="Time Settings")
isTimeWindow = time >= startTime and time <= endTime

// Table Settings Inputs
showTable = input.bool(true, "Show Table", group="Table Settings")
fontSize = input.string("Auto", "Font Size", options=["Auto", "Small", "Normal", "Large"], group="Table Settings")

// Strategy Settings Inputs
tradeDirection = input.string("Long", "Trade Direction", options=["Long", "Short"], group="Strategy Settings")
entryStrategy = input.string("Revert Cross", "Entry Strategy", options=["Cross Threshold", "Revert Cross"], group="Strategy Settings")
barLookback = input.int(10, "Bar Lookback", minval=1, maxval=20, group="Strategy Settings")

// RSI Settings Inputs
rsiPeriod = input.int(14, "RSI Period", minval=1, group="RSI Settings")
overboughtLevel = input.int(70, "Overbought Level", group="RSI Settings")
oversoldLevel = input.int(30, "Oversold Level", group="RSI Settings")
//#endregion

//#region Font Size Mapping
// ================================
// Font Size Mapping
// ================================
fontSizeMap = fontSize == "Auto" ? size.auto : fontSize == "Small" ? size.small : fontSize == "Normal" ? size.normal : fontSize == "Large" ? size.large : na
//#endregion

//#region RSI Calculation
// ================================
// RSI Calculation
// ================================
rsiValue = ta.rsi(close, rsiPeriod)

plot(rsiValue, "RSI", color=color.yellow)
hline(overboughtLevel, "OB Level", color=color.gray)
hline(oversoldLevel, "OS Level", color=color.gray)
//#endregion

//#region Entry Conditions
// ================================
// Entry Conditions
// ================================
buyCondition = entryStrategy == "Revert Cross" ? ta.crossover(rsiValue, oversoldLevel) : ta.crossunder(rsiValue, oversoldLevel)
sellCondition = entryStrategy == "Revert Cross" ? ta.crossunder(rsiValue, overboughtLevel) : ta.crossover(rsiValue, overboughtLevel)

// Plotting buy/sell signals
plotshape(buyCondition ? oversoldLevel : na, title="Buy", location=location.absolute, color=color.green, style=shape.labelup, text="BUY", textcolor=color.white, size=size.small)
plotshape(sellCondition ? overboughtLevel : na, title="Sell", location=location.absolute, color=color.red, style=shape.labeldown, text="SELL", textcolor=color.white, size=size.small)

// Plotting buy/sell signals on the chart
plotshape(buyCondition, title="Buy", location=location.belowbar, color=color.green, style=shape.triangleup, text="BUY", textcolor=color.white, size=size.small , force_overlay = true)
plotshape(sellCondition, title="Sell", location=location.abovebar, color=color.red, style=shape.triangledown, text="SELL", textcolor=color.white, size=size.small, force_overlay = true)
//#endregion

//#region Returns Matrix Calculation
// ================================
// Returns Matrix Calculation
// ================================
var returnsMatrix = matrix.new<float>(0, barLookback, 0.0)

if (tradeDirection == "Long" ? buyCondition[barLookback] : sellCondition[barLookback]) and isTimeWindow
newRow = array.new_float(barLookback)
for i = 0 to barLookback - 1
entryPrice = close[barLookback]
futurePrice = close[barLookback - 1 - i]
ret = (futurePrice - entryPrice) / entryPrice * 100
array.set(newRow, i, math.round(ret, 4))
matrix.add_row(returnsMatrix, matrix.rows(returnsMatrix), newRow)
//#endregion

//#region Display Table
// ================================
// Display Table
// ================================
var table statsTable = na
if barstate.islastconfirmedhistory and showTable
statsTable := table.new(position.top_right, barLookback + 1, 4, border_width=1, force_overlay=true)

// Table Headers
table.cell(statsTable, 0, 1, "Win Rate %", bgcolor=color.rgb(45, 45, 48), text_color=color.white, text_size=fontSizeMap)
table.cell(statsTable, 0, 2, "Mean Return %", bgcolor=color.rgb(45, 45, 48), text_color=color.white, text_size=fontSizeMap)
table.cell(statsTable, 0, 3, "Median Return %", bgcolor=color.rgb(45, 45, 48), text_color=color.white, text_size=fontSizeMap)

// Row Headers
for i = 1 to barLookback
table.cell(statsTable, i, 0, str.format("{0} Bar Return", i), bgcolor=color.rgb(45, 45, 48), text_color=color.white, text_size=fontSizeMap)

// Calculate Statistics
meanReturns = array.new_float()
medianReturns = array.new_float()
for col = 0 to matrix.columns(returnsMatrix) - 1
colData = matrix.col(returnsMatrix, col)
array.push(meanReturns, array.avg(colData))
array.push(medianReturns, array.median(colData))

// Populate Table
for col = 0 to matrix.columns(returnsMatrix) - 1
colData = matrix.col(returnsMatrix, col)
positiveCount = 0
for val in colData
if val > 0
positiveCount += 1
winRate = positiveCount / array.size(colData)
meanRet = array.avg(colData)
medianRet = array.median(colData)

// Color Logic
winRateColor = winRate == 0.5 ? color.rgb(58, 58, 60) : (winRate > 0.5 ? color.rgb(76, 175, 80) : color.rgb(244, 67, 54))
meanBullCol = color.from_gradient(meanRet, 0, array.max(meanReturns), color.rgb(76, 175, 80), color.rgb(0, 128, 0))
meanBearCol = color.from_gradient(meanRet, array.min(meanReturns), 0, color.rgb(255, 0, 0), color.rgb(255, 99, 71))
medianBullCol = color.from_gradient(medianRet, 0, array.max(medianReturns), color.rgb(76, 175, 80), color.rgb(0, 128, 0))
medianBearCol = color.from_gradient(medianRet, array.min(medianReturns), 0, color.rgb(255, 0, 0), color.rgb(255, 99, 71))

table.cell(statsTable, col + 1, 1, str.format("{0,number,#.##%}", winRate), text_color=color.white, bgcolor=winRateColor, text_size=fontSizeMap)
table.cell(statsTable, col + 1, 2, str.format("{0,number,#.###}%", meanRet), text_color=color.white, bgcolor=meanRet > 0 ? meanBullCol : meanBearCol, text_size=fontSizeMap)
table.cell(statsTable, col + 1, 3, str.format("{0,number,#.###}%", medianRet), text_color=color.white, bgcolor=medianRet > 0 ? medianBullCol : medianBearCol, text_size=fontSizeMap)
//#endregion

// Background color for OB/OS regions
bgcolor(rsiValue >= overboughtLevel ? color.new(color.red, 90) : rsiValue <= oversoldLevel ? color.new(color.green, 90) : na)

免責聲明

這些資訊和出版物並不意味著也不構成TradingView提供或認可的金融、投資、交易或其他類型的意見或建議。請在使用條款閱讀更多資訊。