[sphx] FWMA
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © zamansiz74

//@version=5
indicator("[sphx] FWMA", overlay = true)

//
periods = input.int(defval = 6, title="Long Period")
periods2 = input.int(defval = 5, title="Short Period")

//
if periods2 > periods
    periods2 := periods - 1

// Fibonacci-Zahlenreihe
fibonacciNumbers = array.new_int(periods)

// Erste beiden Fibonacci-Zahlen
array.set(fibonacciNumbers, 0, 1)
array.set(fibonacciNumbers, 1, 2)

//
var bool direction = false
var bool colorChanged = false

//
for i = 2 to periods - 1
    array.set(fibonacciNumbers, i, array.get(fibonacciNumbers, i - 1) + array.get(fibonacciNumbers, i - 2))

// Funktion zur Berechnung des Fibonacci-Durchschnitts
fibonacciAvg(src, periods) =>
    sum = 0.0
    weightSum = 0.0
    for i = 0 to periods - 1  
        fibNumber = fibonacciNumbers.get(i)
        sum := sum + src[fibNumber - 1]
    sum / periods

// 
fibonacciAvgValue = ta.ema(fibonacciAvg(close, periods),periods)
fibonacciAvgValue2 = ta.ema(fibonacciAvg(close, periods2),periods)
//
distance = fibonacciAvgValue2 - fibonacciAvgValue
distance2 = ta.ema(distance,periods)

// Color
clr =distance < distance2 ? distance > 0  ?  color.rgb(64,255,64,80) :  color.rgb(255,0,0,50)  : distance > 0  ?  color.rgb(0,255,0,50) :  color.rgb(255,0,0,80)

// Plot
fa1 = plot(fibonacciAvgValue, title = "Fibonacci Average 1", color = clr, linewidth = 1)
fa2 = plot(fibonacciAvgValue2, title = "Fibonacci Average 2", color = clr, linewidth = 1)

fill(fa1, fa2, color = clr)