can you add this strategy //@version=5
//The Average Daily Range is a simple concept, calculated as the difference between daily highs and lows averaged over some period.
//This indicator uses that range in conjunction with Fibonacci ratios to create zones centered on the day's open that tends to act as areas of support and resistance.
//
//A simpler version of this concept can be seen in my other script, Average Daily Range Zones,
//which does not include the Fibonacci ratio zones.
//
//Thanks to @Hank Hill for the original idea and ToS script this is based on, and
//TV for the feedback and support on the use of the security() function.
//Also thanks to @GoldenCross for the Fibonacci obsession.
//For future reference; an sma() on a security return is not the same as a security() with an sma argument.
// i.e., security(tickerid, 'D', sma(dayRange, length)) != sma(security(tickerid, 'D', dayRange), length)
//Includes TradingView's own f_security function for better historical data.
error line 61 The arguments 'maxval', 'minval', and 'step' cannot be used with the input() function. You can use the input.int() or input.float() functions to specify a range of input data values
isToday = false
if year(timenow) == year(time) and month(timenow) == month(time) and dayofmonth(timenow) == dayofmonth(time)
isToday := true
if not showTodayOnly
isToday := true
Here's how you can integrate your 'Daily Fibs Levels' script into the existing 'Magic MA' script. The new script combines the functionalities of both indicators:
pinescript
//@version=5
indicator('Magic MA with Daily Fibs Levels', shorttitle='Magic MA + Daily Fibs', overlay=true, max_bars_back=2000)
// Averages and Fibonacci Levels Setup
firstLength = input(50, title='ADR Calculation Period (Days)')
calcType = input.string("Basic", title="Fibs Calculation", options=["Basic", "Standard Deviation", "Variance"])
// Fibonacci Levels Inputs
showFib1 = input.bool(true, title='', group='Fibonacci Levels', inline='1')
fibLevel1 = input.float(0.25, title='', group='Fibonacci Levels', inline='1')
fib1Color = input.color(#F44336, title='', group='Fibonacci Levels', inline='1')
showFib1_line = input.bool(true, title='Line', group='Fibonacci Levels', inline='1')
// (other Fibonacci Input Settings similarly defined here...)
dynamic = input(false, title='Dynamically Show Zones Outside ADR')
// Open, Scale, and Visibility Inputs
openColor = input.color(#ffffff, title='Open Plot Color')
fibScale = input.float(1, step=0.1, title='Fibonacci Scale', tooltip="Allows custom scale of the fib levels; 1.0 is default.")
lineVisibility = 100 - input.int(70, minval=0, maxval=100, step=5, title='Line Visibility % (Transparency)')
fillVisibility = 100 - input.int(20, minval=0, maxval=100, step=5, title='Fill Visibility % (Transparency)')
showPrices = input(false, title='Show Price Lines')
showZonesFill = input(true, title='Fill Zones')
showZonesOnly = input(false, title='Show Zones Only')
showTodayOnly = input(false, title='Plot For Today Only')
disableNonIntraday = input(true, title='Disable On Daily or Above')
var string ticker = syminfo.tickerid
enabled = not (disableNonIntraday and not timeframe.isintraday)
// Custom Timeframe Inputs
i_timeframe = input.timeframe(title='Custom Timeframe', defval='D')
// Getting Day Open, High, Low
dayOpen = request.security(ticker, i_timeframe, open)
dayHigh = request.security(ticker, i_timeframe, high)
dayLow = request.security(ticker, i_timeframe, low)
// Calculate Daily Range
dayRange = dayHigh - dayLow
avgRange_1 = request.security(ticker, i_timeframe, ta.sma(dayRange, firstLength))
// Calculate Fibonacci Ranges
fibRange_1 = avgRange_1 * fibLevel1 * fibScale / 2
// (Other Fibonacci Ranges calculations...)
isToday = year(timenow) == year(time) and month(timenow) == month(time) and dayofmonth(timenow) == dayofmonth(time)
if not showTodayOnly
isToday := true
// Plot the Fibonacci Levels
plot(enabled and isToday and showFib1 ? dayOpen + fibRange_1 : na, color=showZonesOnly or not showFib1_line or dayOpen != dayOpen[1] ? na : color.new(fib1Color, lineVisibility), trackprice=showPrices, title="ADR Fib 1 High")
// (Other plot functions for Fibonacci lines...)
// Magic MA calculation from original script
type3 = input(defval="HullMA", title="Slow MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"])
len3 = input(defval=34, title="Slow Length", minval=1)
// Function to return MA variant (as defined in original)
variant(type, src, len) =>
v1 = ta.sma(src, len)
v2 = ta.ema(src, len)
v3 = ta.wma(src, len)
v4 = ta.vwma(src, len)
v5 = (v5[1] * (len - 1) + src) / len
v6 = 2 * v2 - ta.ema(v2, len)
v7 = 3 * (v2 - ta.ema(v2, len)) + ta.ema(ta.ema(v2, len), len)
v8 = ta.wma(2 * ta.wma(src, len / 2) - ta.wma(src, len), round(math.sqrt(len)))
v11 = ta.sma(ta.sma(src, len), len)
a1 = math.exp(-1.414 * 3.14159 / len)
b1 = 2*a1*math.cos(1.414 * 3.14159 / len)
c2 = b1
c3 = (-a1) * a1
c1 = 1 - c2 - c3
v9 = c1 * (src + nz(src[1])) / 2 + c2 * nz(v9[1]) + c3 * nz(v9[2])
e = ta.ema(v1, len)
v10 = v1 + (v1 - e)
type == "EMA" ? v2 : type == "WMA" ? v3 : type == "VWMA" ? v4 : type == "SMMA" ? v5 : type == "DEMA" ? v6 : type == "TEMA" ? v7 : type == "HullMA" ? v8 : type == "SSMA" ? v9 : type == "ZEMA" ? v10 : type == "TMA" ? v11 : v1
// Series variable for slow MA
srcclose = close
ma_slow = variant(type3, srcclose, len3)
// Plotting the Slow MA
plot(ma_slow, title="MA Slow", color=color.new(color.green, 0), linewidth=2)
This integrated script combines the functionality of the 'Magic MA' with the 'Daily Fibs Levels'. Please note that I have maintained the calculation methods and settings from both scripts and attempted to integrate them logically. You may need to further customize the script, especially the plotting and input sections, to meet specific requirements.
It seems the error arises because in Pine Script version 5, input integers and floats should use input.int() or input.float(), and not input(). Here's the revised script removing the old style input() and replacing it with the correct function calls for specifying input values with constraints:
pinescript
//@version=5
indicator('Magic MA with Daily Fibs Levels', shorttitle='Magic MA + Daily Fibs', overlay=true, max_bars_back=2000)
// Averages and Fibonacci Levels Setup
firstLength = input.int(50, title='ADR Calculation Period (Days)')
calcType = input.string("Basic", title="Fibs Calculation", options=["Basic", "Standard Deviation", "Variance"])
// Fibonacci Levels Inputs
showFib1 = input.bool(true, title='', group='Fibonacci Levels', inline='1')
fibLevel1 = input.float(0.25, title='', group='Fibonacci Levels', inline='1')
fib1Color = input.color(#F44336, title='', group='Fibonacci Levels', inline='1')
showFib1_line = input.bool(true, title='Line', group='Fibonacci Levels', inline='1')
// (other Fibonacci Input Settings similarly defined here...)
dynamic = input(false, title='Dynamically Show Zones Outside ADR')
// Open, Scale, and Visibility Inputs
openColor = input.color(#ffffff, title='Open Plot Color')
fibScale = input.float(1, step=0.1, title='Fibonacci Scale', tooltip="Allows custom scale of the fib levels; 1.0 is default.")
lineVisibility = 100 - input.int(70, minval=0, maxval=100, step=5, title='Line Visibility % (Transparency)')
fillVisibility = 100 - input.int(20, minval=0, maxval=100, step=5, title='Fill Visibility % (Transparency)')
showPrices = input(false, title='Show Price Lines')
showZonesFill = input(true, title='Fill Zones')
showZonesOnly = input(false, title='Show Zones Only')
showTodayOnly = input(false, title='Plot For Today Only')
disableNonIntraday = input(true, title='Disable On Daily or Above')
var string ticker = syminfo.tickerid
enabled = not (disableNonIntraday and not timeframe.isintraday)
// Custom Timeframe Inputs
i_timeframe = input.timeframe(title='Custom Timeframe', defval='D')
// Getting Day Open, High, Low
dayOpen = request.security(ticker, i_timeframe, open)
dayHigh = request.security(ticker, i_timeframe, high)
dayLow = request.security(ticker, i_timeframe, low)
// Calculate Daily Range
dayRange = dayHigh - dayLow
avgRange_1 = request.security(ticker, i_timeframe, ta.sma(dayRange, firstLength))
// Calculate Fibonacci Ranges
fibRange_1 = avgRange_1 * fibLevel1 * fibScale / 2
// (Other Fibonacci Ranges calculations...)
isToday = year(timenow) == year(time) and month(timenow) == month(time) and dayofmonth(timenow) == dayofmonth(time)
if not showTodayOnly
isToday := true
// Plot the Fibonacci Levels
plot(enabled and isToday and showFib1 ? dayOpen + fibRange_1 : na, color=showZonesOnly or not showFib1_line or dayOpen != dayOpen[1] ? na : color.new(fib1Color, lineVisibility), trackprice=showPrices, title="ADR Fib 1 High")
// (Other plot functions for Fibonacci lines...)
// Magic MA calculation from original script
type3 = input.string(defval="HullMA", title="Slow MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"])
len3 = input.int(defval=34, title="Slow Length", minval=1)
// Function to return MA variant (as defined in original)
variant(type, src, len) =>
v1 = ta.sma(src, len)
v2 = ta.ema(src, len)
v3 = ta.wma(src, len)
v4 = ta.vwma(src, len)
v5 = (v5[1] * (len - 1) + src) / len
v6 = 2 * v2 - ta.ema(v2, len)
v7 = 3 * (v2 - ta.ema(v2, len)) + ta.ema(ta.ema(v2, len), len)
v8 = ta.wma(2 * ta.wma(src, len / 2) - ta.wma(src, len), round(math.sqrt(len)))
v11 = ta.sma(ta.sma(src, len), len)
a1 = math.exp(-1.414 * 3.14159 / len)
b1 = 2*a1*math.cos(1.414 * 3.14159 / len)
c2 = b1
c3 = (-a1) * a1
c1 = 1 - c2 - c3
v9 = c1 * (src + nz(src[1])) / 2 + c2 * nz(v9[1]) + c3 * nz(v9[2])
e = ta.ema(v1, len)
v10 = v1 + (v1 - e)
type == "EMA" ? v2 : type == "WMA" ? v3 : type == "VWMA" ? v4 : type == "SMMA" ? v5 : type == "DEMA" ? v6 : type == "TEMA" ? v7 : type == "HullMA" ? v8 : type == "SSMA" ? v9 : type == "ZEMA" ? v10 : type == "TMA" ? v11 : v1
// Series variable for slow MA
srcclose = close
ma_slow = variant(type3, srcclose, len3)
// Plotting the Slow MA
plot(ma_slow, title="MA Slow", color=color.new(color.green, 0), linewidth=2)
This version of the script should resolve input-related errors by ensuring the correct usage of input.int() and similar functions for constraints. Adjust other input settings as needed to fit the intended use case.
The error "Undeclared identifier 'v5'" occurs because the v5 variable is not initialized correctly before being used in its recursive definition. To address this, ensure that such variables are initialized properly before using them in recursive operations. Here’s how you can fix this in the script:
pinescript
//@version=5
indicator('Magic MA with Daily Fibs Levels', shorttitle='Magic MA + Daily Fibs', overlay=true, max_bars_back=2000)
// Averages and Fibonacci Levels Setup
firstLength = input.int(50, title='ADR Calculation Period (Days)')
calcType = input.string("Basic", title="Fibs Calculation", options=["Basic", "Standard Deviation", "Variance"])
// Fibonacci Levels Inputs
showFib1 = input.bool(true, title='', group='Fibonacci Levels', inline='1')
fibLevel1 = input.float(0.25, title='', group='Fibonacci Levels', inline='1')
fib1Color = input.color(#F44336, title='', group='Fibonacci Levels', inline='1')
showFib1_line = input.bool(true, title='Line', group='Fibonacci Levels', inline='1')
// (other Fibonacci Input Settings similarly defined here...)
dynamic = input(false, title='Dynamically Show Zones Outside ADR')
// Open, Scale, and Visibility Inputs
openColor = input.color(#ffffff, title='Open Plot Color')
fibScale = input.float(1, step=0.1, title='Fibonacci Scale', tooltip="Allows custom scale of the fib levels; 1.0 is default.")
lineVisibility = 100 - input.int(70, minval=0, maxval=100, step=5, title='Line Visibility % (Transparency)')
fillVisibility = 100 - input.int(20, minval=0, maxval=100, step=5, title='Fill Visibility % (Transparency)')
showPrices = input(false, title='Show Price Lines')
showZonesFill = input(true, title='Fill Zones')
showZonesOnly = input(false, title='Show Zones Only')
showTodayOnly = input(false, title='Plot For Today Only')
disableNonIntraday = input(true, title='Disable On Daily or Above')
var string ticker = syminfo.tickerid
enabled = not (disableNonIntraday and not timeframe.isintraday)
// Custom Timeframe Inputs
i_timeframe = input.timeframe(title='Custom Timeframe', defval='D')
// Getting Day Open, High, Low
dayOpen = request.security(ticker, i_timeframe, open)
dayHigh = request.security(ticker, i_timeframe, high)
dayLow = request.security(ticker, i_timeframe, low)
// Calculate Daily Range
dayRange = dayHigh - dayLow
avgRange_1 = request.security(ticker, i_timeframe, ta.sma(dayRange, firstLength))
// Calculate Fibonacci Ranges
fibRange_1 = avgRange_1 * fibLevel1 * fibScale / 2
// (Other Fibonacci Ranges calculations...)
isToday = year(timenow) == year(time) and month(timenow) == month(time) and dayofmonth(timenow) == dayofmonth(time)
if not showTodayOnly
isToday := true
// Plot the Fibonacci Levels
plot(enabled and isToday and showFib1 ? dayOpen + fibRange_1 : na, color=showZonesOnly or not showFib1_line or dayOpen != dayOpen[1] ? na : color.new(fib1Color, lineVisibility), trackprice=showPrices, title="ADR Fib 1 High")
// (Other plot functions for Fibonacci lines...)
// Magic MA calculation from original script
type3 = input.string(defval="HullMA", title="Slow MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"])
len3 = input.int(defval=34, title="Slow Length", minval=1)
// Function to return MA variant (as defined in original)
variant(type, src, len) =>
v1 = ta.sma(src, len)
v2 = ta.ema(src, len)
v3 = ta.wma(src, len)
v4 = ta.vwma(src, len)
// Initialize v5 for recursive computation
var float v5 = na
v5 := na(v5) ? ta.sma(src, len) : (v5 * (len - 1) + src) / len
v6 = 2 * v2 - ta.ema(v2, len)
v7 = 3 * (v2 - ta.ema(v2, len)) + ta.ema(ta.ema(v2, len), len)
v8 = ta.wma(2 * ta.wma(src, len / 2) - ta.wma(src, len), math.round(math.sqrt(len)))
v11 = ta.sma(ta.sma(src, len), len)
a1 = math.exp(-1.414 * 3.14159 / len)
b1 = 2*a1*math.cos(1.414 * 3.14159 / len)
c2 = b1
c3 = (-a1) * a1
c1 = 1 - c2 - c3
var float v9 = na
v9 := na(v9) ? na : c1 * (src + nz(src[1])) / 2 + c2 * nz(v9[1]) + c3 * nz(v9[2])
e = ta.ema(v1, len)
v10 = v1 + (v1 - e)
type == "EMA" ? v2 : type == "WMA" ? v3 : type == "VWMA" ? v4 : type == "SMMA" ? v5 : type == "DEMA" ? v6 : type == "TEMA" ? v7 : type == "HullMA" ? v8 : type == "SSMA" ? v9 : type == "ZEMA" ? v10 : type == "TMA" ? v11 : v1
// Series variable for slow MA
srcclose = close
ma_slow = variant(type3, srcclose, len3)
// Plotting the Slow MA
plot(ma_slow, title="MA Slow", color=color.new(color.green, 0), linewidth=2)
In the variant function, v5 and v9 variables are initialized properly using the var keyword, ensuring they are declared once and maintain their values between function calls, allowing the rolling calculation to work correctly. This modification should prevent any runtime errors related to undeclared identifiers.