Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=5
- indicator("Fibonacci Averages Trend Oscillator", shorttitle = 'Fib Avg Trend Osc', precision = 4)
- //Tooltips
- string maxFibNumberTooltip = "Select the maximum Fibonacci number to be used in the oscillator calculations. Higher numbers result in longer periods being considered, which may affect the sensitivity and responsiveness of the indicator to market changes."
- string smoothLevelTooltip = "Sets the smoothness of the oscillator line. A higher value will result in a smoother line, which may help in identifying the overall trend direction more clearly but could delay the detection of short-term market movements."
- // Inputs
- maxFibNumber = input.string("610", "Max fib number", options = ["21", "34", "55", "89", "144", "233", "377", "610", "987", "1597"], tooltip = maxFibNumberTooltip)
- smoothLevel = input.int(30, "Smooth", minval = 2, maxval = 70, tooltip = smoothLevelTooltip)
- oversoldLevel = input.float(0.2, "Oversold level", minval = 0, maxval = 0.5, step = 0.01)
- overboughtLevel = input.float(0.8, "Overbought level", minval = 0.5, maxval = 1, step = 0.01)
- // Array of Fibonacci numbers
- fibNumbers = array.from(2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597)
- maxFibIndex = int(str.tonumber(maxFibNumber))
- arrayEndIndex = array.lastindexof(fibNumbers, maxFibIndex)
- fibNumbers := array.slice(fibNumbers, 0, arrayEndIndex)
- // Custom type for storing arrays of closes and opens
- type ArraySet
- array<float> closes
- array<float> opens
- // Initializing ArraySet
- arrayData = ArraySet.new(array.new_float(0), array.new_float(0))
- trendArray = array.new_float()
- // Gathering historical data for closes and opens
- for i = 0 to array.max(fibNumbers) - 1
- array.push(arrayData.closes, close[i])
- array.push(arrayData.opens, open[i])
- // Normalizing data samples to binary (1 or 0) based on average closes and opens
- for i = 0 to array.size(fibNumbers) - 1
- arraySize = array.get(fibNumbers, i) - 1
- slicedCloses = array.slice(arrayData.closes, 0, arraySize)
- slicedOpens = array.slice(arrayData.opens, 0, arraySize)
- array.push(trendArray, (array.avg(slicedCloses) > array.avg(slicedOpens)) ? 1 : 0)
- // Calculating the average trend and applying smoothing
- fibTrend = ta.ema(array.avg(trendArray), smoothLevel)
- // Determining plot color based on trend values
- plotColor = color.from_gradient(fibTrend, oversoldLevel, overboughtLevel, color.rgb(179, 0, 72), color.rgb(169, 231, 0))
- // Setting background color based on trend strength
- bgcolor(fibTrend > overboughtLevel ? color.new(color.green, 80) : fibTrend < oversoldLevel ? color.new(color.red, 80) : na)
- // Plotting the trend line
- plot(fibTrend, "Trend", color = plotColor, linewidth = 3)
- // Drawing a middle line for reference
- hline(0.5, "Middle Line", linestyle = hline.style_dotted)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement