// Initialize score tracking for the current situation
score = 0
// Calculate scores for each MA type
for i = 0 to array.size(ma_types) - 1
ma_type = array.get(ma_types, i)
code
// Calculate moving averages of RSI values
ma_rsi14 = ma(rsi14, ma_length, ma_type)
ma_rsi9 = ma(rsi9, ma_length, ma_type)
// Add to score if the condition is met
if (ma_rsi9 > ma_rsi14)
score := score + 1
// Calculate the average of all MA types for RSI 14 and RSI 9
avg_ma_rsi14 = (ema_rsi14 + sma_rsi14 + rma_rsi14 + lsma_rsi14 + wma_rsi14) / 5
avg_ma_rsi9 = (ema_rsi9 + sma_rsi9 + rma_rsi9 + lsma_rsi9 + wma_rsi9) / 5
// Calculate the overall average MA RSI
overall_avg_ma_rsi = (avg_ma_rsi14 + avg_ma_rsi9) / 2
// Set the plot color based on the score
plot_color = score == 0 ? color.red : (score == 5 ? color.green : color.orange)
Assistant
// Plot the overall average MA RSI value
plot(overall_avg_ma_rsi, color=plot_color, title="Overall Average MA RSI")
// Function to get the background color based on score
bg_color(score) =>
switch score
0 => color.red
5 => color.green
=> color.orange
// Display score in a table as a fraction out of 5 or "D/I" if data is insufficient
var table table_id = table.new(position.middle_right, 3, 3)
if bar_index == 0
table.cell(table_id, 1, 1, text="|", text_size=size.normal, text_color=color.new(color.white, 50))
table.cell(table_id, 0, 1, text=bar_index < ma_length ? "D/I" : str.tostring(score) + "/5", text_size=size.normal, bgcolor=bg_color(score), text_color=color.new(color.white, 0))
// Calculate VWMA of RSI
vwma_48 = ta.vwma(ta.rsi(src, rsi_length_14), 48)
// Define the input for the MA type, lengths, and source
ma_type = input.string(title="MA Type", defval="EMA", options=["EMA", "WMA", "SMA", "RMA", "LSMA"], group = "Trend")
ma_source = input.source(title="Source", defval=close, group = "Trend")
ma_5_length = input.int(title="Length for MA 5", defval=5, group = "Trend")
ma_9_length = input.int(title="Length for MA 9", defval=9, group = "Trend")
ma_13_length = input.int(title="Length for MA 13", defval=13, group = "Trend")
// Function to select the MA type
f_get_ma(src, length, ma_type) =>
ma = switch ma_type
"WMA" => ta.wma(src, length)
"SMA" => ta.sma(src, length)
"RMA" => ta.rma(src, length)
"LSMA" => ta.linreg(src, length, 0)
=> ta.ema(src, length) // Default is EMA
ma
// Calculate the MAs
ma_5 = f_get_ma(ma_source, ma_5_length, ma_type)
ma_9 = f_get_ma(ma_source, ma_9_length, ma_type)
ma_13 = f_get_ma(ma_source, ma_13_length, ma_type)
// Calculate the average of the 5, 9, and 13 MAs
ma_avg = (ma_5 + ma_9 + ma_13) / 3
// Determine the line color based on conditions
line_color = color.new(color.orange, 0)
if (ma_5 > ma_9 and ma_9 > ma_13)
line_color := color.new(color.green, 0)
else if (ma_13 > ma_9 and ma_9 > ma_5)
line_color := color.new(color.red, 0)
// Plot the line at 50 with the conditional color
plot(50, color=line_color, linewidth=2, title="Plot Trend")
/////
//Essential Inputs
matype = input.string(title='Moving Average Type', defval='SMA', options=['SMA', 'EMA', 'WMA'], group='Moving Average for Relative Strength')
malength = input(title='Moving Average Length', defval=50, group='Moving Average for Relative Strength')
//Index for Relative Strength
index = input.string(title='Index to compare To', defval='NSE:NIFTY', options=['NSE:NIFTY', 'NSE:CNX500', 'NSE:NIFTYSMLCAP250', 'NSE:CNXSMALLCAP'],group='Index for Relative Strength')
compareToOtherIndex = input.bool(title="Compare to different index?", defval=false, group='Index for Relative Strength', inline='Different compare')
otherIndex = input.symbol(title='', defval='', group='Index for Relative Strength', inline='Different compare')
//Customisation
Easycolors = input(true,'Easy Colors?', group="Customisation", tooltip = 'Easy colors option just changes the histogram colors to either green or red, indicating out-performance or under-performance, respectively.')
//Table options
toggleTable = input(true, group = 'Table Options', title='Show table', tooltip = 'Select this to display the table')
toggleVerboseTable = input.bool(title='Display verbose table?', defval=true, group='Table Options', tooltip = 'Select this to display the table with detailed text')
//Configuring the Moving Average
ma_function(source, length) =>
if matype == 'SMA'
ta.sma(source, length)
else
if matype == 'EMA'
ta.ema(source, length)
else
if matype == 'WMA'
ta.wma(source, length)
priceIsAboveMa = close > ma
priceIsAboveIndexMa = closeIndex > maIndex
indexgreen = request.security(indexToUse, timeframe.period, priceIsAboveIndexMa)
priceIsBelowMa = close <= ma
priceIsBelowIndexMa = closeIndex <= maIndex
indexred = request.security(indexToUse, timeframe.period, priceIsBelowIndexMa)
rop = indexred and priceIsBelowMa and val > 0
gop = indexgreen and priceIsAboveMa and val > 0
aop = indexred and priceIsAboveMa
rup = indexgreen and priceIsAboveMa and val < 0
gup = indexred and priceIsBelowMa and val < 0
aup = indexgreen and priceIsBelowMa
roptext = "both index & stock are bearish, but stock is less bearish"
goptext = "both index & stock are bullish, but stock is more bullish"
aoptext = "index is bearish, but stock is bullish! 🤷♂️"
ruptext = "both index & stock are bullish, but stock is less bullish"
guptext = "both index & stock are bearish, but stock is more bearish"
auptext = "index is bullish, but stock is bearish! 🙈"
// Initialize variables
var float SmoothedTrueRange = na
var float SmoothedDirectionalMovementPlus = na
var float SmoothedDirectionalMovementMinus = na
var float DIPlus = na
var float DIMinus = na
var float DX = na
var float ADX = na
var color current_color = na
var color previous_color = na
// Table
//var table score_table = table.new(position.bottom_right, 1, 1, frame_color=color.black, frame_width=1, border_width=1)
var int previous_score = na
var int current_score = na
// Variables to track transitions to and from score 5 and -5
var bool score_became_5 = false
var bool score_left_5 = false
var bool score_became_neg5 = false
var bool score_left_neg5 = false
if m5
current_score := -5
current_color := color.red
if m4
current_score := -4
current_color := color.red
if m3
current_score := -3
current_color := color.red
if m2
current_score := -2
current_color := color.red
if m1
current_score := -1
current_color := color.red
if m0
current_score := 0
current_color := color.yellow
if p1
current_score := 1
current_color := color.green
if p2
current_score := 2
current_color := color.green
if p3
current_score := 3
current_color := color.green
if p4
current_score := 4
current_color := color.green
if p5
current_score := 5
current_color := color.green
if not na(current_score)
table.cell(table_id, 2, 1, str.tostring(previous_score) + ' -> ' + str.tostring(current_score), bgcolor=current_color, text_color=text_color)
// Check for score transitions to and from 5
score_became_5 := previous_score != 5 and current_score == 5
score_left_5 := previous_score == 5 and current_score != 5
// Check for score transitions to and from -5
score_became_neg5 := previous_score != -5 and current_score == -5
score_left_neg5 := previous_score == -5 and current_score != -5
// Plot line at 0 with color changing according to the score
plot(0, title='Score Line', color=current_color, linewidth=2)
// Plot green circle when score becomes 5 on the 0 line
plotshape(series=score_became_5 ? 0 : na, title='Score Became 5', location=location.absolute, color=color.green, style=shape.circle, size=size.tiny)
// Plot green X when score leaves 5 on the 0 line
plotshape(series=score_left_5 ? 0 : na, title='Score Left 5', location=location.absolute, color=color.green, style=shape.xcross, size=size.tiny)
// Plot red circle when score becomes -5 on the 0 line
plotshape(series=score_became_neg5 ? 0 : na, title='Score Became -5', location=location.absolute, color=color.red, style=shape.circle, size=size.tiny)
// Plot red X when score leaves -5 on the 0 line
plotshape(series=score_left_neg5 ? 0 : na, title='Score Left -5', location=location.absolute, color=color.red, style=shape.xcross, size=size.tiny)
"
help me alalyse this code i want to make it mutitime frame indicaotr
Hello! I'd be happy to help you analyze your code and guide you on how to make it a multitimeframe indicator.
Analysis of Your Code
Your script is quite comprehensive and includes several components:
RSI Calculations with Different Lengths:
Calculates RSI with lengths 14 and 9.
Applies various moving averages (EMA, SMA, WMA, etc.) to the RSI values.
Computes an average of these MAs and assigns scores based on their relationships.
VWMA of RSI and Smoothing Averages:
Calculates the Volume-Weighted Moving Average (VWMA) of RSI.
Applies smoothing techniques using EMA, RMA, LSMA, and Hull MA to the VWMA of RSI.
Plots and fills areas based on comparisons of these smoothed values.
Trend Indicator Using MAs:
Uses moving averages of price with lengths 5, 9, and 13.
Determines the trend based on the relationships between these MAs.
Plots a line at 50 with colors indicating the trend.
MARS Indicator (Relative Strength Comparison):
Compares the stock's performance against a selected index.
Determines outperformance or underperformance.
Displays a table with the results.
RDX Score Line:
Calculates ADX and RSI.
Assigns scores from -5 to +5 based on conditions involving ADX and RSI.
Plots shapes and lines to indicate score transitions.
Objective: Making It a Multitimeframe Indicator
To make your indicator multitimeframe, you need to modify it so that some or all calculations are based on data from different timeframes. In Pine Script, this is achieved using the request.security() function, which allows you to request data from another symbol or timeframe.
Steps to Modify the Code
1. Determine Which Parts to Make Multitimeframe
Decide which indicators or calculations you want to base on different timeframes. Common choices include:
RSI calculations
Moving averages
ADX calculations
Any other indicator you wish to adjust
2. Use request.security() Function
Replace the standard calculations with request.security() calls to fetch data from the desired timeframe.
3. Modify the Code:
Let's go through each major component and see how to adjust it.
Implementation Details
A. Adding Timeframe Inputs
First, let's add inputs to select the higher timeframe:
pine
// Input for higher timeframe
htf_rsi = input.timeframe(title="Higher Timeframe for RSI", defval="")
htf_vwma = input.timeframe(title="Higher Timeframe for VWMA", defval="")
htf_trend = input.timeframe(title="Higher Timeframe for Trend Indicator", defval="")
htf_adx = input.timeframe(title="Higher Timeframe for ADX/RDX", defval="")
By default, if no timeframe is selected (defval=""), the calculations will use the chart's current timeframe.
Data Alignment: When fetching data from higher timeframes, you might encounter alignment issues. It's essential to handle the barstate.isrealtime and barstate.islast conditions to ensure accurate calculations.
Non-Standard Timeframes: Be aware of non-standard timeframes (like 45 minutes) that may not align neatly with higher timeframes.
Complete Example for RSI Calculation
Below is how you can adjust the RSI calculation and plotting:
pine
//@version=5
indicator("21.6) Combined VRS Line with Scoring and VWMA VRS with Big HM + wrt + RDX Score", overlay=false)
// Input for higher timeframe
htf_rsi = input.timeframe(title="Higher Timeframe for RSI", defval="")
// Customizable input parameters
rsi_length_14 = input.int(14, minval=1, title='RSI Length 14', group = "WMA | Score")
rsi_length_9 = input.int(9, minval=1, title='RSI Length 9', group = "WMA | Score")
// Function to get RSI from higher timeframe
htf_rsi_func(length, htf) =>
htf == "" ? ta.rsi(close, length) : request.security(syminfo.tickerid, htf, ta.rsi(close, length))
// Calculate RSI on higher timeframe
rsi14 = htf_rsi_func(rsi_length_14, htf_rsi)
rsi9 = htf_rsi_func(rsi_length_9, htf_rsi)
// Continue with your RSI-based calculations...
Things to Keep in Mind
Timeframe Synchronization:
When working with multiple timeframes, you need to ensure that data aligns correctly.
The request.security() function handles this by default, but be cautious with bar numbers and historical calculations.
Handling na Values:
When accessing higher timeframe data, some bars on the lower timeframe may not have a corresponding higher timeframe bar.
Use nz() or na checks to handle missing data.
Performance:
Using request.security() extensively can impact the performance of your script.
Try to minimize the number of request.security() calls by combining them when possible.
Testing:
After making changes, test your script thoroughly on different symbols and timeframes.
Check if the indicator behaves as expected.
Final Tips
Parameterizing Timeframes:
Allow users to select the timeframes for different components via inputs. This makes your indicator flexible.
Documentation:
Make sure to comment your code, especially where you've made changes for multitimeframe functionality.
User Interface:
Provide clear input options and group them logically. This helps users understand how to adjust the settings.
Conclusion
By wrapping your calculations with request.security() and adjusting your functions to accept higher timeframe data, you can modify your indicator to be multitimeframe. This allows for more sophisticated analysis by combining signals from different timeframes.
Feel free to ask if you have any specific questions or if there's a particular part of the code you'd like more help with!
Example of Adjusted Code Snippet:
pine
//@version=5
indicator("Multitimeframe RSI Example", overlay=false)
// Input for higher timeframe
htf_rsi = input.timeframe(title="Higher Timeframe for RSI", defval="D")
rsi_length = input.int(14, minval=1, title='RSI Length')
// Function to get RSI from higher timeframe
htf_rsi_func(length, htf) =>
htf == "" ? ta.rsi(close, length) : request.security(syminfo.tickerid, htf, ta.rsi(close, length))
// Calculate RSI on higher timeframe
rsi_value = htf_rsi_func(rsi_length, htf_rsi)
// Plot RSI
plot(rsi_value, color=color.blue, title="RSI on Higher Timeframe")
This example shows how to calculate RSI on a higher timeframe and plot it on your chart.
I hope this helps you modify your indicator to be multitimeframe. Let me know if you need further assistance!