Tribute to David Paul
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © LOKEN94

//  I made this indicator as a tribute to the late David Paul. 
//  He mentioned quite a lot about 89 periods moving average, also the 21 and 55. 

//@version=5
indicator("Tribute to David Paul",shorttitle="MA",overlay=true,timeframe = "",timeframe_gaps = false)
ma21=ta.sma(close,21)
ma55=ta.sma(close,55)
ma89=ta.sma(close,89)
//

//
float BULL=0
if ma21<=close and ma55<=close and ma89<=close
    BULL:=1
float BEAR=0
if ma21>close and ma55>close and ma89>close
    BEAR:=1
//
float lowest_ma=ma89
if ma21<=ma55 and ma21<=ma89
    lowest_ma:=ma21
if ma55<=ma21 and ma55<=ma89
    lowest_ma:=ma55
//
float highest_ma=ma89
if ma21>=ma55 and ma21>=ma89
    highest_ma:=ma21
if ma55>=ma21 and ma55>=ma89
    highest_ma:=ma55
//
float IDEAL_LONG_ENTRY=na 
if BULL==1 and BULL[1]==0
    IDEAL_LONG_ENTRY:=low
//
float IDEAL_SHORT_ENTRY=na 
if BEAR==1 and BEAR[1]==0
    IDEAL_SHORT_ENTRY:=high
//
float REAL_LONG_ENTRY=na 
if BULL==1 and BULL[1]==0
    REAL_LONG_ENTRY:=highest_ma
//
float REAL_SHORT_ENTRY=na 
if BEAR==1 and BEAR[1]==0
    REAL_SHORT_ENTRY:=lowest_ma
//
iLP=ta.sma(IDEAL_LONG_ENTRY,1)
iSP=ta.sma(IDEAL_SHORT_ENTRY,1)
rLP=ta.sma(REAL_LONG_ENTRY,1)
rSP=ta.sma(REAL_SHORT_ENTRY,1)
float ilong=na 
if BULL==1
    ilong:=iLP
float ishort=na
if BEAR==1
    ishort:=iSP
//
float rlong=na 
if BULL==1
    rlong:=rLP
float rshort=na
if BEAR==1
    rshort:=rSP
//
IR=input.int(2,minval=-1,maxval=2,title="Show ideal entries rather than pragmatic ones.",tooltip="If -1, no entries zone will be displayed.                                                                                If 0, entries will be ploted on the crossing level of the highest or lowest sma.                                                                                If 1, entries will be ploted on high of the canlde that crossunder the lowest ma or low of the candle that crossover the highest.                                                                                If 2, the two previous will be ploted, making an order block area.")
botdemand=plot(IR==1 or IR==2 ? ilong:na,"Ideal Long entry",color=color.blue,style=plot.style_linebr)
topsupply=plot(IR==1 or IR==2 ? ishort:na,"Ideal Short entry",color=color.red,style=plot.style_linebr)
topdemand=plot(IR==0 or IR==2 ? rlong:na,"Real Long entry",color=color.blue,style=plot.style_linebr)
botsupply=plot(IR==0 or IR==2 ? rshort:na,"Real Short entry",color=color.red,style=plot.style_linebr)
SDZ=input(true,title="Show Supply & Demand zone",tooltip="Previous setting need to be set on 2.")
fill(botdemand,topdemand,title="Fill Demand Zone",color=SDZ==true?color.rgb(33, 149, 243, 60):na)
fill(topsupply,botsupply,title="Fill Supply Zone",color=SDZ==true?color.rgb(255, 82, 82, 60):na)
//
ama=(ma21+ma55+ma89)/3
float AC=0
if close>ama
    AC:=1
if close<ama
    AC:=-1
//
float FLAT=na 
if AC==1 and AC[1]!=1
    FLAT:=ama
if AC==-1 and AC[1]!=-1
    FLAT:=ama
//
float entries=rlong+rshort
if IR==true
    entries:=ilong+ishort
//
float FLAT1=ta.sma(FLAT,1)
if FLAT1!=FLAT1[1]
    FLAT1:=na 
//

MA=input.int(4,minval=-1,maxval=5,title="Show ma",tooltip="If -1, no MA will be displayed.                                                                                If 0, only sma21,sma55,sma89 will be.                                                                                If 1, only AMA will be.                                                                                If 2, the lowest ma in uptrend and the highest in downtrend, so the furthest.                                                                                If 3, the highest ma in uptrend and the lowest in downtrend will be displayed, so the closest.                                                                                If 4, all MA will be displayed.                                                                                If 5, ma55 and ama will be displayed.")
plot(MA==1 or MA==4 or MA==5?ama:na,"AMA",color=ama<=close?color.rgb(0, 25, 45):color.rgb(10, 0, 0))
FA=plot(FLAT1,color=ama<=close?color.blue:color.red,style=plot.style_linebr,display=display.none,editable=false)
FHL=plot(hl2,display=display.none,editable=false)
fill(FA,FHL,color=ama<=close?(ilong==iLP?color.rgb(33, 149, 243, 80):color.rgb(19, 92, 151, 80)):(ishort==iSP?color.rgb(255, 82, 82, 80):color.rgb(155, 28, 28, 80)),title="Fill: trend based on AMA")
//
plot(MA==0 or MA==4?ma21:na,"sma21",color=ma21<=close?color.blue:color.red,transp=60)
plot(MA==0 or MA==4 or MA==5?ma55:na,"sma55",color=ma55<=close?color.blue:color.red,transp=60)
plot(MA==0 or MA==4?ma89:na,"sma89",color=ma89<=close?color.blue:color.red,transp=60)
plot((MA==3 and (BULL==1 or ama<=close)) or (MA==2 and (BEAR==1 or ama>=close))?highest_ma:na,"highest ma",color=highest_ma<=close?color.blue:color.red,transp=60,style=plot.style_linebr)
plot((MA==3 and (BEAR==1 or ama>=close)) or (MA==2 and (BULL==1 or ama<=close))?lowest_ma:na,"lowest ma",color=lowest_ma<=close?color.blue:color.red,transp=60,style=plot.style_linebr)
//
enableTrendColors = input(true, title='Enable sma based candles colors ?',group="Color Code settings :",tooltip="Color candles in blue/red if close is simultaneously above/below ma21, ma55, ma89, and in gray if not, generally meaning sideway.")
barcolor(enableTrendColors ? ilong==iLP ? #00e2ff99 : na : na, title='Bullish candle color')
barcolor(enableTrendColors ? ishort==iSP ? #ff000099 : na : na, title='Bearish candle color')
barcolor(enableTrendColors ? BULL==0 and BEAR==0 ? #84848d99 : na : na, title='Neutral candle color')
