Binomial Moving Average Strategy
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © HosseinDaftary

//@version=4
strategy("Binomial Moving Average","BMA", overlay=true, margin_long=100, margin_short=100 ,max_bars_back=96)
//Binomial Moving Average:This type of moving average that is made by myself and i did not see anywhere before uses the half of binomial cofficients for
//averaging the prices for example if the period be 5 then we use the 9 degree binomial cofficients(that yields 10 cofficients) and use half of them.
//we use 126/256 for last bar,84/256,36/256,9/256 and finally use 1/256 for 5th bar. Seemingly this MA works better than EMA.
fa_ma=input(title='Fast MA',defval=10)
sl_ma=input(title='Slow MA',defval=30)

fac(n)=>
    fact=1
    for i= 1 to n
        fact:=fact*i
    fact
cof= array.new_float(sl_ma) 

hn_ma(price,length)=>
    sum=1.0
    sum1=0.0
    array.set(cof,length-1,1)
    for i=2 to length
        array.set(cof,length-i,fac(2*length-1)/(fac(i-1)*fac(2*length-i)))
        sum:=sum+array.get(cof,length-i)
    for i=0 to length-1
        array.set(cof,i,array.get(cof,i)/sum)
        sum1:=sum1+array.get(cof,i)*price[i]
    sum1
hn1=plot(hn_ma(close,sl_ma) , color=#00ff00)
hn2=plot(hn_ma(close,fa_ma) ,color=#ff0000)
fill(hn1,hn2,color=hn_ma(close,fa_ma)>hn_ma(close,sl_ma)?color.green:color.red)


longCondition = crossover(hn_ma(close, fa_ma), hn_ma(close, sl_ma))
if (longCondition)
    strategy.entry("Long", strategy.long)

shortCondition = crossunder(hn_ma(close, fa_ma), hn_ma(close, sl_ma))
if (shortCondition)
    strategy.entry("Short", strategy.short)