حتماً، برای اینکه امکان تغییر رنگ خط اصلی RSI را اضافه کنیم، باید یک ورودی جدید از نوع رنگ به بخش تنظیمات اضافه کنیم و آن را در تابع plot برای رسم خط RSI استفاده کنیم.
این تغییر در کدی که دو سطح Overbought/Oversold داشت اعمال میشود:
✅ کد آپدیت شده با امکان تغییر رنگ خط RSI
pinescript
//@version=5
indicator("RSI with Dual Overbought/Oversold Levels", shorttitle="RSI Dual Zones", overlay=false)
// -----------------------------------------------------------------------------
// Customizable User Inputs
// -----------------------------------------------------------------------------
// RSI period length (usually 14)
int rsiLength = input.int(14, title="RSI Length", minval=1)
// Input for the color of the RSI line itself
color rsiColor = input.color(color.blue, title="RSI Line Color") // <-- ورودی جدید برای رنگ خط RSI
// First set of OB/OS levels (e.g., 70 / 30)
float obLevel1 = input.float(70.0, title="Overbought Level 1", minval=0, maxval=100)
float osLevel1 = input.float(30.0, title="Oversold Level 1", minval=0, maxval=100)
color obColor1 = input.color(color.red, title="Overbought Zone Color 1")
color osColor1 = input.color(color.green, title="Oversold Zone Color 1")
// Second set of OB/OS levels (e.g., 80 / 20)
float obLevel2 = input.float(80.0, title="Overbought Level 2", minval=0, maxval=100)
float osLevel2 = input.float(20.0, title="Oversold Level 2", minval=0, maxval=100)
color obColor2 = input.color(color.orange, title="Overbought Zone Color 2")
color osColor2 = input.color(color.lime, title="Oversold Zone Color 2")
// -----------------------------------------------------------------------------
// Indicator Calculations
// -----------------------------------------------------------------------------
// Calculate RSI value based on closing price
float rsiValue = ta.rsi(close, rsiLength)
// -----------------------------------------------------------------------------
// Plotting the Indicator and Levels
// -----------------------------------------------------------------------------
// Plot the RSI line using the new input color
plot(rsiValue, "RSI", color=rsiColor, linewidth=2) // <-- استفاده از ورودی جدید
// Plot horizontal lines for Overbought/Oversold levels (Set 1)
hline(obLevel1, "Overbought Level 1", color=obColor1, linestyle=hline.style_dotted)
hline(osLevel1, "Oversold Level 1", color=osColor1, linestyle=hline.style_dotted)
// Plot horizontal lines for Overbought/Oversold levels (Set 2)
hline(obLevel2, "Overbought Level 2", color=obColor2, linestyle=hline.style_dashed)
hline(osLevel2, "Oversold Level 2", color=osColor2, linestyle=hline.style_dashed)
// Mid-line
hline(50, "Mid-Line (50)", color=color.gray, linestyle=hline.style_dashed)
// -----------------------------------------------------------------------------
// Background Coloring for Zones
// -----------------------------------------------------------------------------
// Overbought Zones
bgcolor(rsiValue > obLevel2 ? color.new(obColor2, 80) : na, title="OB Zone 2")
bgcolor(rsiValue <= obLevel2 and rsiValue > obLevel1 ? color.new(obColor1, 85) : na, title="OB Zone 1")
// Oversold Zones
bgcolor(rsiValue < osLevel2 ? color.new(osColor2, 80) : na, title="OS Zone 2")
bgcolor(rsiValue >= osLevel2 and rsiValue < osLevel1 ? color.new(osColor1, 85) : na, title="OS Zone 1")
// -----------------------------------------------------------------------------
// Generating Buy/Sell Signals (Crossovers)
// -----------------------------------------------------------------------------
// Buy Signal: When RSI crosses Oversold level from below
bool crossUpOS1 = ta.crossover(rsiValue, osLevel1)
bool crossUpOS2 = ta.crossover(rsiValue, osLevel2)
// Sell Signal: When RSI crosses Overbought level from above
bool crossDownOB1 = ta.crossunder(rsiValue, obLevel1)
bool crossDownOB2 = ta.crossunder(rsiValue, obLevel2)
// -----------------------------------------------------------------------------
// Plotting Shapes for Signals on the Main Chart
// -----------------------------------------------------------------------------
plotshape(crossUpOS1, title="Buy Signal (OS1)", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small, text="Buy1")
plotshape(crossUpOS2, title="Buy Signal (OS2)", location=location.belowbar, color=color.lime, style=shape.triangleup, size=size.small, text="Buy2")
plotshape(crossDownOB1, title="Sell Signal (OB1)", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small, text="Sell1")
plotshape(crossDownOB2, title="Sell Signal (OB2)", location=location.abovebar, color=color.orange, style=shape.triangledown, size=size.small, text="Sell2")
// -----------------------------------------------------------------------------
// Defining Alert Conditions for TradingView
// -----------------------------------------------------------------------------
alertcondition(crossUpOS1 or crossUpOS2, title="RSI Buy Alert", message="RSI crossed above Oversold level.")
alertcondition(crossDownOB1 or crossDownOB2, title="RSI Sell Alert", message="RSI crossed below Overbought level.")
📌 تغییرات اعمال شده:
اضافه شدن ورودی رنگ خط RSI:
pinescript
color rsiColor = input.color(color.blue, title="RSI Line Color")
این خط یک گزینه جدید به تنظیمات اندیکاتور اضافه میکند که به شما اجازه میدهد رنگ خط اصلی RSI را تغییر دهید. مقدار پیشفرض آن آبی (color.blue) تنظیم شده است.
در این خط، به جای color.blue که رنگ ثابت بود، از متغیر rsiColor استفاده شده است. حالا هر رنگی که در تنظیمات برای "RSI Line Color" انتخاب کنید، رنگ خط RSI خواهد شد.
با این تغییر، حالا میتوانید رنگ خط RSI را از طریق منوی تنظیمات اندیکاتور در TradingView به دلخواه خود تغییر دهید.