Advertisement
xmd79

Fibonacci Averages Trend Oscillator

Jan 14th, 2024
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. //@version=5
  2. indicator("Fibonacci Averages Trend Oscillator", shorttitle = 'Fib Avg Trend Osc', precision = 4)
  3.  
  4. //Tooltips
  5. 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."
  6. 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."
  7. // Inputs
  8. maxFibNumber = input.string("610", "Max fib number", options = ["21", "34", "55", "89", "144", "233", "377", "610", "987", "1597"], tooltip = maxFibNumberTooltip)
  9. smoothLevel = input.int(30, "Smooth", minval = 2, maxval = 70, tooltip = smoothLevelTooltip)
  10. oversoldLevel = input.float(0.2, "Oversold level", minval = 0, maxval = 0.5, step = 0.01)
  11. overboughtLevel = input.float(0.8, "Overbought level", minval = 0.5, maxval = 1, step = 0.01)
  12. // Array of Fibonacci numbers
  13. fibNumbers = array.from(2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597)
  14. maxFibIndex = int(str.tonumber(maxFibNumber))
  15. arrayEndIndex = array.lastindexof(fibNumbers, maxFibIndex)
  16. fibNumbers := array.slice(fibNumbers, 0, arrayEndIndex)
  17.  
  18. // Custom type for storing arrays of closes and opens
  19. type ArraySet
  20. array<float> closes
  21. array<float> opens
  22.  
  23. // Initializing ArraySet
  24. arrayData = ArraySet.new(array.new_float(0), array.new_float(0))
  25. trendArray = array.new_float()
  26.  
  27. // Gathering historical data for closes and opens
  28. for i = 0 to array.max(fibNumbers) - 1
  29. array.push(arrayData.closes, close[i])
  30. array.push(arrayData.opens, open[i])
  31.  
  32. // Normalizing data samples to binary (1 or 0) based on average closes and opens
  33. for i = 0 to array.size(fibNumbers) - 1
  34. arraySize = array.get(fibNumbers, i) - 1
  35. slicedCloses = array.slice(arrayData.closes, 0, arraySize)
  36. slicedOpens = array.slice(arrayData.opens, 0, arraySize)
  37. array.push(trendArray, (array.avg(slicedCloses) > array.avg(slicedOpens)) ? 1 : 0)
  38.  
  39. // Calculating the average trend and applying smoothing
  40. fibTrend = ta.ema(array.avg(trendArray), smoothLevel)
  41.  
  42. // Determining plot color based on trend values
  43. plotColor = color.from_gradient(fibTrend, oversoldLevel, overboughtLevel, color.rgb(179, 0, 72), color.rgb(169, 231, 0))
  44.  
  45. // Setting background color based on trend strength
  46. bgcolor(fibTrend > overboughtLevel ? color.new(color.green, 80) : fibTrend < oversoldLevel ? color.new(color.red, 80) : na)
  47.  
  48. // Plotting the trend line
  49. plot(fibTrend, "Trend", color = plotColor, linewidth = 3)
  50.  
  51. // Drawing a middle line for reference
  52. hline(0.5, "Middle Line", linestyle = hline.style_dotted)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement