Moving Average - TREND POWER v1.1- (AS)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Adam-Szafranski

//@version=5
indicator("Moving Average - TREND POWER v1.1- (AS)",shorttitle = 'TR_POW - (AS)',overlay = false)
///////////////////TOOLTIPS{
TT_0X = 'MAIN IDEA: \n This is Trend Indicator that rises/falls if moving avg is below/above price and resets when there is a cross (cross of what will be defined later).If you have trouble with grasping how it works either look into "STRATEGY LOGIC" part of the code or in case you dont know pine script select SIMPLE preset and define values of MA1(how SIMPLE works described in PRESETS tooltip)\n!!!!!!-defval settings for MA1 and MA2 are not optimalized for now'
//TT_0Y = 'CROSS TYPES LIST: \n-1 means that its cross of MA1 \n-2 means its cross of MA2 \n-3 is a cross of MA1 and MA2 \n-H - cross with high \n-L - cross with low \n"So for example 1L is cross of MA1 with low or 2H is cross of MA2 with high \n-1HL-cross of MA1 with high or low 12HL cross of MA1 with high or MA1 with low or MA2 with high or MA2 with low \nLIST: \n -1L MA1+low \n -1H MA1+high\n -2L MA2+low\n -1H MA2+high\n -1L MA1+low\n -1L MA1+low'
TT_01 = 'SOURCE: \nSource of MA1 and MA2  \nOVERLAY: \n(off by def) - if you want to see MA1/2 on chart turn ON and change overlay to true in the code'
TT_02 = 'TRESHOLDS: \nvalue od upper/lower treshold/Use TSHs? \nIF blw upper and above lower - consolidation'
TT_03 = 'MA1: \nLength/type/Offset(only if LSMA) \n faster MAs for short trends and slower/bigger periods for larger trends'
TT_04 = 'MA2(ALMA):\nLength/offset/sigma \n Use setting in which MA2>MA1 in Downtrend and MA2<MA1 in uptrend'
TT_05 = 'CROSS TYPE: \nchoose type of cross to plot on chart(Overlay=true) or to use in counting crosses \nPlot cross on chart? '
TT_06 = 'LOOKBACK: \nSet lookback for counting choosen cross type above /npPLOT number of crosses on chart (Overlay=false)'
TT_07 = 'PRESETS: \n-SIMPLE-most basic version using only MA1 and CROSS_1_ULT type(all crosses of MA1(low,high,close,ohlc4 etc...)) \n-ABSOLUTE-shows only positive values if trend(UP/DN), uses CROSS_123_ULT type(all crosses)\n-CUSTOM-pick this to define your own conditions below (take look into stratey code if more help needed)\nSOURCE: \nabv/blw to calculate value \n-HL-MA<high for UPtrend and MA>low for DNtren \n-OHLC4- MA > or < from OHLC4'
TT_08 = '(this part only if PRESET choosen above is CUST\nCHOOSE MA: \nChoosing which MA to use (if PRESET=CUST). \nMA1 - only use MA1 - grows if ma1 below defined source and falls if above \nMA2 - only use MA2 - smae as MA1 but MA2 instead \nBOTH - Use MA1 and MA2 - grows if MA1 or MA2 below, grows faster if MA1 and MA2 below, growws fastest if MA1 and MA2 blelow and MA2<MA1 \nCRS:\nWhat Conditions of cross to use for going back to 0\n>/< - 0 if uptrend and cross with high (and the other way around)\nALL - 0 if cross with high or low\nULT - 0 if any cross'
/////TOOLTIPS}
///////////////////FUNCTIONS{

ma(type, src, len,off) =>
    e = ta.ema(src,len)
    switch type
        'SMA'   =>    ta.sma(src, len)
        'EMA'   =>    ta.ema(src, len)
        '2MA'   =>    2 * e - ta.ema(e, len)
        '3MA'   =>    3 * (e - ta.ema(e, len)) + ta.ema(ta.ema(e, len), len)
        'WMA'   =>    ta.wma(src, len)
        'LSM'   =>    ta.linreg(src,len,off)
        'HMA'   =>    ta.hma(src,len)
        'RMA'   =>    ta.rma(src,len)
///////////////////FUNCTIONS}
///////////////////INPUTS{
var G_STRT = '---Starting settings---'
STARTINFO   = input.bool    (true   ,group = G_STRT,inline = 'X',title ='HOW TO USE AND SET - READ ME!',tooltip=TT_0X)
//LISTSINFO   = input.bool    (true   ,group = G_STRT,inline = 'Y',title ='TYPE OF CROSS AND COND - LIST',tooltip=TT_0Y)
INPSOURCE   = input.source  (ohlc4  ,group = G_STRT,inline = '1',title ='SOURCE/OVERLAY->')
P_OVERLAY   = input.bool    (false  ,group = G_STRT,inline = '1',title ='',tooltip = TT_01)
TSHOLD_UP   = input.float   (0.5    ,group = G_STRT,inline = '2',title ='TSHL-UP/DN/USE?->')
TSHOLD_DN   = input.float   (-0.5   ,group = G_STRT,inline = '2',title ='')
P_TSHOLDS   = input.bool    (false  ,group = G_STRT,inline = '2',title ='',tooltip = TT_02)
var G_MOVA = 'Moving avgs settings'
INPMA1LEN   = input.int     (120    ,group = G_MOVA,inline = '3',title ='MA1-LEN/TYP/OFF')
INPMA1TYP   = input.string  ('LSM'  ,group = G_MOVA,inline = '3',title ='', options = ['EMA','HMA','2MA','3MA','WMA','SMA','LSM','RMA'])
INPLSMAOF   = input         (36     ,group = G_MOVA,inline = '3',title ='',tooltip = TT_03)
INPMA2LEN   = input.int     (120    ,group = G_MOVA,inline = '4',title ='MA2-LEN/OFF/SIG')
INPMA2OFF   = input.float   (1.5    ,group = G_MOVA,inline = '4',title ='')
INPMA2SIG   = input.int     (1      ,group = G_MOVA,inline = '4',title ='',tooltip = TT_04)
var G_CROS = 'Crosses settings'
P_CRS_SIG   = input.string  ('NONE' ,group = G_CROS,inline = '5',title ='CROSS TO PLOT/COUNT',options = ['NONE','1L','1H','1HL','2L','2H','2HL','12L','12H','12HL','3','123HL','1-ULT','2-ULT','123-ULT'])
P_CRSHAPE   = input.bool    (false  ,group = G_CROS,inline = '5',title ='PLOT?',tooltip = TT_05)
INPLOOKBC   = input.int     (25     ,group = G_CROS,inline = '6',title ='CROSS COUNT LOOKBC')
P_CRSNUMB   = input.bool    (false  ,group = G_CROS,inline = '6',title ='PLOT?',tooltip = TT_06)
var G_COND = 'Define Conditions for calculation'
PRESETS     = input.string  ('CUST' ,group = G_COND,inline = '7',title ='PRESETS/SOURCE',options = ['CUST','SIMPLE','ABSOLUTE'])
S_SRC_CD    = input.string  ('H/L'  ,group = G_COND,inline = '7',title ='/',options = ['H/L','OHLC'],tooltip = TT_07)
S_USE_MA    = input.string  ('MA1'  ,group = G_COND,inline = '8',title ='IF CUST - MA/CRS',options = ['MA1','MA2','BOTH'])
S_CRS_CD    = input.string  ('>/<'  ,group = G_COND,inline = '8',title ='/',options = ['>/<','ALL','ULT'],tooltip = TT_08)
///////////////////INPUTS}
///////////////////CROSSES{
MA1          = ma        (INPMA1TYP, INPSOURCE, INPMA1LEN, INPLSMAOF)
MA2          = ta.alma   (INPSOURCE, INPMA2LEN, INPMA2OFF, INPMA2SIG)

CROSS_1_L     = ta.cross(MA1,low)             ,CROSS_1_H = ta.cross(MA1,high) ,CROSS_3_M  = ta.cross(MA1,MA2)
CROSS_2_L     = ta.cross(MA2,low)             ,CROSS_2_H = ta.cross(MA2,high) 
CROSS_12_L    = CROSS_1_L or CROSS_2_L        ,CROSS_12_H      = CROSS_1_H or CROSS_2_H 
CROSS_1_HL    = CROSS_1_L or CROSS_1_H        ,CROSS_2_HL      = CROSS_2_L or CROSS_2_H
CROSS_12_HL   = CROSS_1_L or CROSS_2_L or CROSS_1_H or CROSS_2_H
CROSS_123_HL  = CROSS_1_L or CROSS_2_L or CROSS_1_H or CROSS_2_H or CROSS_3_M
CROSS_1_ULT   = ta.cross(MA1,close) or ta.cross(MA1,ohlc4) or ta.cross(MA1,hl2) or ta.cross(MA1,high) or ta.cross(MA1,low) or ta.cross(MA1,open) 
CROSS_2_ULT   = ta.cross(MA2,close) or ta.cross(MA2,ohlc4) or ta.cross(MA2,hl2) or ta.cross(MA2,high) or ta.cross(MA2,low) or ta.cross(MA2,open)
CROSS_123_ULT   = 
 ta.cross(MA1,close) or
 ta.cross(MA1,ohlc4) or ta.cross(MA1,hl2)   or
 ta.cross(MA1,high)  or ta.cross(MA1,low)   or
 ta.cross(MA1,open)  or ta.cross(MA2,close) or 
 ta.cross(MA2,ohlc4) or ta.cross(MA2,hl2)   or
 ta.cross(MA2,high)  or ta.cross(MA2,low)   or
 ta.cross(MA2,open)  or ta.cross(MA1,MA2) 

CROSStoCOUNT    =
 P_CRS_SIG=='1L'    ?CROSS_1_L   :P_CRS_SIG=='1H'      ?CROSS_1_H     :P_CRS_SIG=='1HL'   ?CROSS_1_HL   :
 P_CRS_SIG=='2L'    ?CROSS_2_L   :P_CRS_SIG=='2H'      ?CROSS_2_H     :P_CRS_SIG=='2HL'   ?CROSS_2_HL   :
 P_CRS_SIG=='12L'   ?CROSS_12_L  :P_CRS_SIG=='12H'     ?CROSS_12_H    :P_CRS_SIG=='12HL'  ?CROSS_12_HL  :
 P_CRS_SIG=='3'     ?CROSS_3_M   :P_CRS_SIG=='123HL'   ?CROSS_123_HL  :P_CRS_SIG=='1-ULT' ?CROSS_1_ULT  :
 P_CRS_SIG=='2-ULT' ?CROSS_2_ULT :P_CRS_SIG=='123-ULT' ?CROSS_123_ULT :na
CROSScount      = 0
for i = 0 to (INPLOOKBC-1)
    if CROSStoCOUNT[i]
        CROSScount+= 1
///////////////////CROSSES} 
///////////////////CONDITIONS{
COND_UP_1         = S_SRC_CD=='H/L'?MA1<high:MA1<ohlc4     ,COND_UP_2 = S_SRC_CD=='H/L'?MA2<high:MA2<ohlc4      ,COND_UP_3 = MA2<MA1
COND_DN_1         = S_SRC_CD=='H/L'?MA1>low :MA1>ohlc4     ,COND_DN_2 = S_SRC_CD=='H/L'?MA2>low :MA2>ohlc4      ,COND_DN_3 = MA2>MA1
COND_UP_1and2     = COND_UP_1 and COND_UP_2                ,COND_UP_1or2 = COND_UP_1 or COND_UP_2   
COND_DN_1and2     = COND_DN_1 and COND_DN_2                ,COND_DN_1or2 = COND_DN_1 or COND_DN_2
COND_UP_1and2and3 = COND_UP_1 and COND_UP_2 and COND_UP_3
COND_DN_1and2and3 = COND_DN_1 and COND_DN_2 and COND_DN_3
///////////////////CONDITIONS}
///////////////////INDICATOR LOGIC{
var TREND_POINTS_C = 0.0
var TREND_POINTS_S = 0.0
var TREND_POINTS_A = 0.0
if PRESETS == 'CUST'
    if S_USE_MA=='MA1'
        if COND_UP_1
            TREND_POINTS_C+=0.02
        if COND_DN_1
            TREND_POINTS_C-=0.02
    if S_USE_MA=='MA2'
        if COND_UP_2
            TREND_POINTS_C+=0.02
        if COND_DN_2
            TREND_POINTS_C-=0.02
    if S_USE_MA=='BOTH'
        if COND_UP_1and2and3            //123
            TREND_POINTS_C+=0.04
        else if COND_UP_1and2
            TREND_POINTS_C+=0.03          //12
        else if COND_UP_1or2
            TREND_POINTS_C+=0.02          //1
        if COND_DN_1and2and3 
            TREND_POINTS_C-=0.04          //3
        else if COND_DN_1and2           //12
            TREND_POINTS_C-=0.03
        else if COND_DN_1or2        //1
            TREND_POINTS_C-=0.02
    if S_CRS_CD == '>/<'
        if S_USE_MA=='MA1'
            if TREND_POINTS_C>0 and CROSS_1_H
                TREND_POINTS_C := 0
            if TREND_POINTS_C<0 and CROSS_1_L
                TREND_POINTS_C := 0
        if S_USE_MA=='MA2'
            if TREND_POINTS_C>0 and CROSS_2_H
                TREND_POINTS_C := 0
            if TREND_POINTS_C<0 and CROSS_2_L
                TREND_POINTS_C := 0
        if S_USE_MA=='BOTH'
            if TREND_POINTS_C>0 and CROSS_12_H 
                TREND_POINTS_C := 0
            if TREND_POINTS_C<0 and CROSS_12_L
                TREND_POINTS_C := 0
    if S_CRS_CD == 'ALL'
        if S_USE_MA=='MA1'
            if CROSS_1_HL               
                TREND_POINTS_C := 0
        if S_USE_MA=='MA2'
            if CROSS_2_HL
                TREND_POINTS_C := 0
        if S_USE_MA=='BOTH'
            if CROSS_123_HL                 
                TREND_POINTS_C := 0
    if S_CRS_CD == 'ULT'
        if S_USE_MA=='MA1'
            if CROSS_1_ULT
                TREND_POINTS_C := 0
        if S_USE_MA=='MA2'
            if CROSS_2_ULT
                TREND_POINTS_C := 0
        if S_USE_MA=='BOTH'
            if CROSS_123_ULT
                TREND_POINTS_C := 0
if PRESETS == 'SIMPLE'
    if COND_UP_1
        TREND_POINTS_S+=0.02
    if COND_DN_1
        TREND_POINTS_S-=0.02
    if CROSS_1_ULT
        TREND_POINTS_S:=0
if PRESETS == 'ABSOLUTE'
    if COND_DN_1and2and3 or COND_UP_1and2and3
        TREND_POINTS_A += 0.02
    if CROSS_123_ULT
        TREND_POINTS_A :=0
VALUE = PRESETS == 'ABSOLUTE'?TREND_POINTS_A:PRESETS == 'SIMPLE'?TREND_POINTS_S:PRESETS == 'CUST'?TREND_POINTS_C:na
///////////////////INDICATOR LOGIC}
///////////////////PLOTS{
TREND_VALUE_COLOR = 
 PRESETS == 'ABSOLUTE'  ?color.rgb(0, 116,  211):
 PRESETS == 'SIMPLE'    ?color.rgb(216, 194 , 0):
 PRESETS == 'CUST'      ?color.rgb(220, 35, 112):na
TREND_P     = plot   (not P_OVERLAY?VALUE:na  ,'TREND_PLOT_V' ,TREND_VALUE_COLOR)
ZERO1_P     = hline  (not P_OVERLAY?0:na      ,'ZERO1_PLOT'   ,color.rgb(123, 128, 146)     ,linestyle = hline.style_dashed )
ZERO2_P     = plot   (not P_OVERLAY?0:na      ,'ZERO2_PLOT'   ,color.rgb(33, 149 , 243,100) ,display   = display.none)  
CROSS_P     = plot   (P_CRSNUMB and not P_OVERLAY?CROSScount:na  ,'CROSS',color.yellow,1,plot.style_columns)
TSHUP_P     = hline  (P_TSHOLDS and not P_OVERLAY?TSHOLD_UP :na  ,'TSHUP',color.gray,hline.style_dashed)
TSHDN_P     = hline  (P_TSHOLDS and not P_OVERLAY?TSHOLD_DN :na  ,'TSHSN',color.gray,hline.style_dashed)
MA1_P       = plot   (P_OVERLAY and S_USE_MA!='MA2'?MA1:na,'MA1',color.rgb(46, 92, 231))
MA2_P       = plot   (P_OVERLAY and S_USE_MA!='MA1'?MA2:na,'MA2',color.rgb(255, 26, 106))

fill    (ZERO2_P,TREND_P ,P_OVERLAY?na:(VALUE>=0?#00ff0040:VALUE<0?#e9377e3f:color.blue))
fill    (MA1_P,MA2_P ,P_OVERLAY?(COND_UP_1and2and3?#00ff0040:COND_DN_1and2and3?#e9377e3f:na):na)

plotshape(P_CRS_SIG == '1L'      and P_CRSHAPE and P_OVERLAY?CROSS_1_L     :na,'',shape.cross,location.abovebar,color.rgb(213, 40, 144, 58),size=size.small)
plotshape(P_CRS_SIG == '1H'      and P_CRSHAPE and P_OVERLAY?CROSS_1_H     :na,'',shape.cross,location.abovebar,color.rgb(40, 213, 75 , 58),size=size.small)
plotshape(P_CRS_SIG == '1HL'     and P_CRSHAPE and P_OVERLAY?CROSS_1_HL    :na,'',shape.cross,location.abovebar,color.rgb(35, 170, 233, 58),size=size.small)

plotshape(P_CRS_SIG == '2L'      and P_CRSHAPE and P_OVERLAY?CROSS_2_L     :na,'',shape.cross,location.abovebar,color.rgb(213, 40, 144, 58),size=size.small)
plotshape(P_CRS_SIG == '2H'      and P_CRSHAPE and P_OVERLAY?CROSS_2_H     :na,'',shape.cross,location.abovebar,color.rgb(40, 213, 75 , 58),size=size.small)
plotshape(P_CRS_SIG == '2HL'     and P_CRSHAPE and P_OVERLAY?CROSS_2_HL    :na,'',shape.cross,location.abovebar,color.rgb(35, 170, 233, 58),size=size.small)

plotshape(P_CRS_SIG == '12L'     and P_CRSHAPE and P_OVERLAY?CROSS_12_L    :na,'',shape.cross,location.abovebar,color.rgb(213, 40, 144, 58),size=size.small)
plotshape(P_CRS_SIG == '12H'     and P_CRSHAPE and P_OVERLAY?CROSS_12_H    :na,'',shape.cross,location.abovebar,color.rgb(40, 213, 75 , 58),size=size.small)
plotshape(P_CRS_SIG == '12HL'    and P_CRSHAPE and P_OVERLAY?CROSS_12_HL   :na,'',shape.cross,location.abovebar,color.rgb(35, 170, 233, 58),size=size.small)

plotshape(P_CRS_SIG == '3'       and P_CRSHAPE and P_OVERLAY?CROSS_3_M     :na,'',shape.cross,location.abovebar,color.rgb(35, 170, 233, 58),size=size.small)
plotshape(P_CRS_SIG == '123HL'   and P_CRSHAPE and P_OVERLAY?CROSS_123_HL  :na,'',shape.cross,location.abovebar,color.rgb(35, 170, 233, 58),size=size.small)
plotshape(P_CRS_SIG == '1-ULT'   and P_CRSHAPE and P_OVERLAY?CROSS_1_ULT   :na,'',shape.cross,location.abovebar,color.rgb(35, 170, 233, 58),size=size.small)
plotshape(P_CRS_SIG == '2-ULT'   and P_CRSHAPE and P_OVERLAY?CROSS_2_ULT   :na,'',shape.cross,location.abovebar,color.rgb(35, 170, 233, 58),size=size.small)
plotshape(P_CRS_SIG == '123-ULT' and P_CRSHAPE and P_OVERLAY?CROSS_123_ULT :na,'',shape.cross,location.abovebar,color.rgb(35, 170, 233, 58),size=size.small)

///////////////////PLOTS}



