Strategy · Pine Script v4 · Own pane

CMYK RMI SMA

by myn

CMYK RMI SMA is a multi-period strategy combining three distinct Relative Momentum Index (RMI) oscillators—Alpha, Beta, and Gamma—with Simple Moving Average (SMA) trend filters.

View on GitHub

GitHub stars4on the repository
Pine versionv4older, still runs
Lines of code1,127
Settings33inputs
LicenceApache-2.0
Last update6 years ago15 Apr 2020

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

1,127 lines of Pine Script v4 from Strategies/CMYX RMI SMA.pine, published under Apache-2.0. Keep the author's credit and licence lines when you reuse it.

CMYX RMI SMA.pine
//@version=4
//
//▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
//                ██████    ██      ██   ██      ██   ██    ██                 ██                  ██████       ██      ██   ██████   ██████
//              ██          ████  ████    ██    ██    ██   ██                ██████                ██    ██     ████  ████     ██           ██ 
//              ██          ██  ██  ██      █  █      █████                 ████████               ██████       ██  ██  ██     ██     ██████
//              ██          ██      ██       ██       ██   ██                ██████                ██    ██     ██      ██     ██           ██
//                ██████    ██      ██       ██       ██    ██                 ██                  ██      ██   ██      ██   ██████   ██████
//                                             Experimental Triple RMI Indicator, prepared for automation
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
//▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
//
//                                                                              STRATEGY SETTINGS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
strategy(overlay=false, pyramiding=1, default_qty_type=strategy.percent_of_equity, default_qty_value=100, initial_capital=20000, currency='USD', slippage=1, commission_type=strategy.commission.percent, commission_value=0.075, precision=1, scale=scale.right, title='▼ █▓▒░ CMYK ♦ RMI ♦ SMA ░▒▓█', shorttitle='▼ █▓▒░ CMYK ♦ RMI ♦ SMA ░▒▓█')
// study           ( title='▲ █▓▒░ CMYK ♦ RMI ♦ TRIPLE ░▒▓█', shorttitle='▲ █▓▒░ CMYK ♦ RMI ♦ TRIPLE ░▒▓█', overlay=false , precision=1 ,scale=scale.right)
//                                                                              COLORSPACE
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
C1A = #bfe9e9
C2A = #83e2df
C3A = #4de3dd
C4A = #07ddcf
C5A = #00b2a3
C6A = #009483
C7A = #007a60
C8A = #084e38
C9A = #103023
M1A = #fdc5dc
M2A = #f9a1d0
M3A = #f170bf
M4A = #ea3ab4
M5A = #df1ec2
M6A = #d009c7
M7A = #ae02bb
M8A = #830899
M9A = #500968
Y1A = #f9ffb1
Y2A = #fff489
Y3A = #ffe766
Y4A = #ffd536
Y5A = #f7b121
Y6A = #eb8e0d
Y7A = #eb8e0d
Y8A = #a93f0a
Y9A = #681a06
K1A = #adadad
K2A = #929292
K3A = #7b7b7b
K4A = #6e6e6e
K5A = #606060
K6A = #505050
K7A = #3f3f3f
K8A = #2c2c2c
K9A = #171717

C1B = #8fafaf
C2B = #62a9a7
C3B = #3aaaa6
C4B = #05a69b
C5B = #00857a
C6B = #006f62
C7B = #005b48
C8B = #063a2a
C9B = #0c241a
M1B = #be94a5
M2B = #bb799c
M3B = #b5548f
M4B = #af2b87
M5B = #a71691
M6B = #9c0795
M7B = #82018c
M8B = #620673
M9B = #3c074e
Y1B = #bbbf85
Y2B = #bfb767
Y3B = #bfad4c
Y4B = #bfa028
Y5B = #b98519
Y6B = #af6b0a
Y7B = #9b5009
Y8B = #7f3007
Y9B = #4e1406
K1B = #828282
K2B = #6d6d6d
K3B = #5c5c5c
K4B = #525252
K5B = #484848
K6B = #3c3c3c
K7B = #2f2f2f
K8B = #212121
K9B = #111111

C1C = #607473
C2C = #41726f
C3C = #26716d
C4C = #036e68
C5C = #005a51
C6C = #014a41
C7C = #003d30
C8C = #05261b
C9C = #091811
M1C = #7e626e
M2C = #7c5069
M3C = #793860
M4C = #751d5a
M5C = #6e0f61
M6C = #680464
M7C = #57005e
M8C = #42044d
M9C = #280434
Y1C = #7d7f58
Y2C = #7f7a43
Y3C = #7e7333
Y4C = #7f6a1b
Y5C = #7a5810
Y6C = #744706
Y7C = #673506
Y8C = #551f05
Y9C = #330d03
K1C = #565656
K2C = #494949
K3C = #3d3d3d
K4C = #373737
K5C = #303030
K6C = #282828
K7C = #1f1f1f
K8C = #161616
K9C = #0b0b0b

//                                                                              FUNCTIONS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
//                                                              RMI
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 
rmi(SOURCE, LENGHT, MOMENTUM) =>
    INC = ema(max(SOURCE - SOURCE[MOMENTUM], 0), LENGHT)
    DEC = ema(max(SOURCE[MOMENTUM] - SOURCE, 0), LENGHT)
    RMI = DEC == 0 ? 0 : 50 - 100 / (1 + INC / DEC)
    RMI

//                                                              SWEEP
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
sweep(SOURCE, DELAY) =>
    PULSE = iff(SOURCE and not SOURCE[1], 1, 0)
    SWEEP = sma(PULSE, DELAY) > 0 ? 1 : 0
    OUTPUT = offset(SWEEP, 1) < PULSE ? 1 : 0
    OUTPUT

//                                                              MULTIFIREFIX
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒    
multifirefix(SOURCE) =>
    OUTPUT = fixnan(SOURCE and not SOURCE[1] ? 1 : SOURCE[1] and not SOURCE ? 0 : na)
    OUTPUT

//                                                             PULSE
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒    
pulse(SOURCE) =>
    OUTPUT = iff(SOURCE and not SOURCE[1], 1, 0)
    OUTPUT

//                                                             PULSEDELAY
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒    
pulsedelay(SOURCE, DELAY) =>
    PULSE = iff(SOURCE and not SOURCE[1], 1, 0)
    OUTPUT = PULSE[DELAY]
    OUTPUT


//                                                                              INPUTS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
RESOLUTION = input(defval=3, minval=1, type=input.integer, title="Resolution")
//                                                              SOURCE
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
SOURCE = input(defval=close, type=input.source, title="Source")
//                                                              RMI
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
RMI_ALPHA_ON = input(defval=true, type=input.bool, title="Alpha   RMI On —————————————————————")
ALPHA_PERIOD = input(defval=29, minval=1, type=input.integer, title="Alpha   Period") / 
   RESOLUTION
ALPHA_MOMENTUM = input(defval=7, minval=1, type=input.integer, title="Alpha   Momentum") / 
   RESOLUTION

RMI_BETA_ON = input(defval=true, type=input.bool, title="Beta    RMI On ——————————————————————")
BETA_PERIOD = input(defval=239, minval=1, type=input.integer, title="Beta    Period") / 
   RESOLUTION
BETA_MOMENTUM = input(defval=23, minval=1, type=input.integer, title="Beta    Momentum") / 
   RESOLUTION

RMI_GAMMA_ON = input(defval=true, type=input.bool, title="Gamma   RMI On ————————————————————")
GAMMA_PERIOD = input(defval=2467, minval=1, type=input.integer, title="Gamma   Period") / 
   RESOLUTION
GAMMA_MOMENTUM = input(defval=239, minval=1, type=input.integer, title="Gamma   Momentum") / 
   RESOLUTION

//                    RMI boundary correction
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
RMI_TREND_CORRECTION = input(defval=true, type=input.bool, title="Gamma Boundary Correction ————————————")
RMI_TREND_FACTOR = input(defval=0.2, minval=00, maxval=250, step=0.1, type=input.float, title="Correction Factor")
RMI_TREND_PERIOD = input(defval=1400, minval=00, maxval=2500, type=input.integer, title="Correction Period") / 
   RESOLUTION

//                    SMA PHASING
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
SMA_TREND_CORRECTION = input(defval=true, type=input.bool, title="SMA Phasing ——————————————————————")
SMA_TREND_FACTOR = input(defval=3.3, minval=00, maxval=250, step=0.1, type=input.float, title="SMA Multiplier")
SMA_TREND_ENTRIES = input(defval=true, type=input.bool, title="SMA Trend Entries ——————————————————")
SMA_TREND_PERIOD = input(defval=10080, minval=00, maxval=10080, type=input.integer, title="SMA Trend Period") / 
   RESOLUTION
SMA_TREND_THRESHOLD = input(defval=2.1, minval=00, maxval=100, step=0.1, type=input.float, title="SMA Trend Threshold %") / 
   100
// SMA_TREND_SCALP         = input ( defval=0.08   , minval=00     , maxval=1  , step=0.01         , type=float                    , title="SMA Trending Scalp"  )

//                    RMI boundaries
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
LINE_BREAK = input(defval=false, type=input.bool, title="——————————————————————————————")
RMI_ALPHA_HIGH_BNDR = input(defval=19, minval=00, maxval=50, type=input.integer, title="Alpha   HIGH Boundary")
RMI_BETA_HIGH_BNDR = input(defval=18, minval=00, maxval=50, type=input.integer, title="Beta    HIGH Boundary")
RMI_GAMMA_HIGH_BNDR = input(defval=17, minval=00, maxval=50, type=input.integer, title="Gamma   HIGH Boundary")

RMI_NEUTRALITY = input(defval=27, minval=0, maxval=73, type=input.integer, title="RMI Nuetrality width")

RMI_GAMMA_LOW_BNDR = input(defval=-17, minval=-50, maxval=00, type=input.integer, title="Gamma   LOW  Boundary")
RMI_BETA_LOW_BNDR = input(defval=-18, minval=-50, maxval=00, type=input.integer, title="Beta    LOW  Boundary")
RMI_ALPHA_LOW_BNDR = input(defval=-19, minval=-50, maxval=00, type=input.integer, title="Alpha   LOW  Boundary")

//                                                              AUTOMATION
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
STRATEGY = input(defval="Margin", options=["Margin", "Regular"], title="Strategy Type —————————————————————")
PYRAMIDING = input(defval=1, minval=1, maxval=9, type=input.integer, title="Pyramiding")
DELAY_BETWEEN_ENTRIES = input(defval=69, minval=0, maxval=409, title="Delay Between Entries") / 
   RESOLUTION
COMISSION_VALUE = input(defval=0.075, minval=0, maxval=1, step=0.1, type=input.float, title="Comission value %")
DISTANCE_EXIT_OPEN = 0  // input ( defval=1      , minval=0      , maxval=3                      , type=integer                  , title="Distance between Close Positions and First Open" )

//                    RISK MANAGEMENT
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
TAKEPROFIT = input(defval=6, minval=0, step=0.1, type=input.float, title="Take Profit            %")
STOPLOSS = input(defval=6, minval=0, maxval=100, step=0.1, type=input.float, title="Stop Loss              %")



VIEWPOSITIONS = false  // input ( defval=false                                                  , type=bool                     , title="Overlay Mode"                           )
BLACKGROUND = true  // input ( defval=true                                                   , type=bool                     , title="BlackGround"                            )




CLOSE_ALERT = int(na)


//                                                                              CALCULATION
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
//                                                              MOVING AVERAGE
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
SMA_ALPHA = sma(close, round(ALPHA_PERIOD * SMA_TREND_FACTOR))
SMA_BETA = sma(close, round(BETA_PERIOD * SMA_TREND_FACTOR))
SMA_GAMMA = sma(close, round(GAMMA_PERIOD * SMA_TREND_FACTOR))


//                      MOVING AVERAGE RELATIVE
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
SMA_SOURCE_ALPHA = SOURCE > SMA_ALPHA ? 1 : 0
SMA_ALPHA_BETA = SMA_ALPHA > SMA_BETA ? 1 : 0
SMA_BETA_GAMMA = SMA_BETA > SMA_GAMMA ? 1 : 0

// //                      MOVING AVERAGE SCALP
// //░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
// SMA_GAMMA_TOP       = SMA_GAMMA * ( 1 + SMA_TREND_SCALP )
// SMA_GAMMA_DIP       = SMA_GAMMA * ( 1 - SMA_TREND_SCALP )

// PLOT_SMA_GAMMA_TOP              = plot  ( VIEWPOSITIONS ? SMA_GAMMA_TOP      :na         , color=Y2A     )
// PLOT_SMA_GAMMA_DIP              = plot  ( VIEWPOSITIONS ? SMA_GAMMA_DIP     :na         , color=C2A     )

// //                      MOVING AVERAGE CROSSVALUES
// //░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
// SMA_CROSS_TOP           = fixnan    ( crossover     ( SMA_BETA , SMA_GAMMA ) ? SMA_BETA : na )
// SMA_CROSS_DIP           = fixnan    ( crossover     ( SMA_GAMMA , SMA_BETA ) ? SMA_BETA : na )

// plot                            ( SMA_CROSS_TOP         , color=white   , title='MOVING ALPHA ANGLE'    )
// plot                            ( SMA_CROSS_DIP         , color=white   , title='MOVING BETA ANGLE'     )

//                      MOVING AVERAGE DIRECTIONS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
SMA_SAMPLING_SIZE = SMA_TREND_PERIOD / RESOLUTION

SMA_ALPHA_DIR = (SMA_ALPHA - SMA_ALPHA[ALPHA_MOMENTUM]) / ALPHA_MOMENTUM / 
   SOURCE[ALPHA_MOMENTUM] * SMA_SAMPLING_SIZE
SMA_BETA_DIR = (SMA_BETA - SMA_BETA[BETA_MOMENTUM]) / BETA_MOMENTUM / SOURCE[BETA_MOMENTUM] * 
   SMA_SAMPLING_SIZE
SMA_GAMMA_DIR = (SMA_GAMMA - SMA_GAMMA[GAMMA_MOMENTUM]) / GAMMA_MOMENTUM / 
   SOURCE[ALPHA_MOMENTUM] * SMA_SAMPLING_SIZE

SMA_TRENDING_UP = SMA_GAMMA_DIR > SMA_TREND_THRESHOLD
SMA_TRENDING_DOWN = SMA_GAMMA_DIR < -SMA_TREND_THRESHOLD

SMA_SHORT = SMA_SOURCE_ALPHA and SMA_ALPHA_BETA and SMA_BETA_GAMMA ? 1 : 0
SMA_LONG = not SMA_SOURCE_ALPHA and not SMA_ALPHA_BETA and not SMA_BETA_GAMMA ? 1 : 0

SMA_SHORT_TRENDING = SMA_SOURCE_ALPHA and SMA_ALPHA_BETA and SMA_TRENDING_DOWN and 
   not(sma(SMA_LONG, 69) > 0) ? 1 : 0
SMA_LONG_TRENDING = not SMA_SOURCE_ALPHA and not SMA_ALPHA_BETA and SMA_TRENDING_UP and 
   not(sma(SMA_SHORT, 69) > 0) ? 1 : 0



// //                      MOVING AVERAGE DIRECTION CHANGE
// //░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
// SMA_ALPHA_VAR           = sma ( change ( SMA_ALPHA_DIR , 1 ) , ALPHA_PERIOD )
// SMA_BETA_VAR            = sma ( change ( SMA_BETA_DIR , 1  ) , BETA_PERIOD  )
// SMA_GAMMA_VAR           = sma ( change ( SMA_GAMMA_DIR , 1 ) , GAMMA_PERIOD )

// plot                            ( VIEWPOSITIONS ? SMA_ALPHA_VAR * 100 :na         , color=yellow   , title='MOVING ALPHA ANGLE'    )
// plot                            ( VIEWPOSITIONS ? SMA_BETA_VAR  * 100 :na         , color=yellow   , title='MOVING BETA ANGLE'     )
// plot                            ( VIEWPOSITIONS ? SMA_GAMMA_VAR * 100 :na         , color=yellow   , title='MOVING GAMMA ANGLE'    )




//                                                              RMI
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
RMI_ALPHA = rmi(SOURCE, ALPHA_PERIOD, ALPHA_MOMENTUM)

RMI_BETA = rmi(SOURCE, BETA_PERIOD, BETA_MOMENTUM)

RMI_GAMMA = rmi(SOURCE, GAMMA_PERIOD, GAMMA_MOMENTUM)

//                      TREND CORRECTION
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
RMI_TREND = sma(RMI_ALPHA + RMI_BETA + RMI_GAMMA, RMI_TREND_PERIOD) / 
   (1 / (RMI_TREND_FACTOR / 10))
RMI_CORRECTION = iff(RMI_TREND_CORRECTION, RMI_TREND, 0)

RMI_ALPHA_HIGH = RMI_ALPHA_HIGH_BNDR
RMI_BETA_HIGH = RMI_BETA_HIGH_BNDR
RMI_GAMMA_HIGH = RMI_GAMMA_HIGH_BNDR + iff(RMI_CORRECTION < 0, RMI_CORRECTION, 0)

RMI_ALPHA_LOW = RMI_ALPHA_LOW_BNDR
RMI_BETA_LOW = RMI_BETA_LOW_BNDR
RMI_GAMMA_LOW = RMI_GAMMA_LOW_BNDR + iff(RMI_CORRECTION > 0, RMI_CORRECTION, 0)

//                                                                              CONDITIONS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
//                                                              RMI
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
//                      TOPS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
RMI_ALPHA_TOP = RMI_ALPHA >= RMI_ALPHA_HIGH ? 1 : not RMI_ALPHA_ON ? 1 : 0
RMI_BETA_TOP = RMI_BETA >= RMI_BETA_HIGH ? 1 : not RMI_BETA_ON ? 1 : 0
RMI_GAMMA_TOP = RMI_GAMMA >= RMI_GAMMA_HIGH ? 1 : not RMI_GAMMA_ON ? 1 : 0

//                      DIPS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
RMI_ALPHA_DIP = RMI_ALPHA <= RMI_ALPHA_LOW ? 1 : not RMI_ALPHA_ON ? 1 : 0
RMI_BETA_DIP = RMI_BETA <= RMI_BETA_LOW ? 1 : not RMI_BETA_ON ? 1 : 0
RMI_GAMMA_DIP = RMI_GAMMA <= RMI_GAMMA_LOW ? 1 : not RMI_GAMMA_ON ? 1 : 0

//                      3 RMI ABG
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
RMI_TOP_3 = RMI_ALPHA_TOP and RMI_BETA_TOP and RMI_GAMMA_TOP ? 1 : 0
RMI_DIP_3 = RMI_ALPHA_DIP and RMI_BETA_DIP and RMI_GAMMA_DIP ? 1 : 0

//                      2 RMI AB
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
RMI_TOP_2 = RMI_ALPHA_TOP and RMI_BETA_TOP and not iff(RMI_GAMMA >= RMI_GAMMA_HIGH, 1, 0) ? 
   1 : 0
RMI_DIP_2 = RMI_ALPHA_DIP and RMI_BETA_DIP and not iff(RMI_GAMMA <= RMI_GAMMA_LOW, 1, 0) ? 1 : 
   0

//                      1 RMI A
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

//                                                              CROSSINGS
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
//                      TOPS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
RMI_ALPHA_TOP_IN = crossover(RMI_ALPHA, RMI_ALPHA_HIGH)
RMI_ALPHA_TOP_OUT = crossunder(RMI_ALPHA, RMI_ALPHA_HIGH)

RMI_BETA_TOP_IN = crossover(RMI_BETA, RMI_BETA_HIGH)
RMI_BETA_TOP_OUT = crossunder(RMI_BETA, RMI_BETA_HIGH)

RMI_GAMMA_TOP_IN = crossover(RMI_GAMMA, RMI_GAMMA_HIGH)
RMI_GAMMA_TOP_OUT = crossunder(RMI_GAMMA, RMI_GAMMA_HIGH)

//                      DIPS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
RMI_ALPHA_DIP_IN = crossunder(RMI_ALPHA, RMI_ALPHA_LOW)
RMI_ALPHA_DIP_OUT = crossover(RMI_ALPHA, RMI_ALPHA_LOW)

RMI_BETA_DIP_IN = crossunder(RMI_BETA, RMI_BETA_LOW)
RMI_BETA_DIP_OUT = crossover(RMI_BETA, RMI_BETA_LOW)

RMI_GAMMA_DIP_IN = crossunder(RMI_GAMMA, RMI_GAMMA_LOW)
RMI_GAMMA_DIP_OUT = crossover(RMI_GAMMA, RMI_GAMMA_LOW)


//                                                                              ANALYTHICAL
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
//                                                              PHASE
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
PHASE_LONG = fixnan(RMI_TOP_3 ? 0 : RMI_DIP_3 ? 1 : na)
PHASE_SHORT = fixnan(RMI_TOP_3 ? 1 : RMI_DIP_3 ? 0 : na)

PHASE_LONG_IV = fixnan(PHASE_LONG ? close : na)
PHASE_SHORT_IV = fixnan(PHASE_SHORT ? close : na)

//                                                              JUMPS
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
RMI_PUMP = RMI_BETA > RMI_ALPHA_HIGH
RMI_DUMP = RMI_BETA < RMI_ALPHA_LOW

RMI_PUMP_OUT = crossunder(RMI_BETA, RMI_ALPHA_HIGH)
RMI_DUMP_OUT = crossover(RMI_BETA, RMI_ALPHA_LOW)

//                                                      HIGHEST/LOWEST ENTRY
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
//                    ON ENTRY IF LARGER
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
PHASE_SHORT_LV = fixnan(RMI_TOP_3 or RMI_TOP_2 and close < PHASE_LONG_IV ? close : PHASE_LONG ? PHASE_LONG_IV : na)
PHASE_LONG_LV = fixnan(RMI_DIP_3 or RMI_DIP_2 and close > PHASE_SHORT_IV ? close : PHASE_SHORT ? PHASE_SHORT_IV : na)

PHASE_LONG_XV = fixnan(CLOSE_ALERT[1] and PHASE_LONG ? PHASE_LONG_LV : na)
PHASE_SHORT_XV = fixnan(CLOSE_ALERT[1] and PHASE_SHORT ? PHASE_SHORT_LV : na)

//                                                                              ADDITIONAL ENRTRIES
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
RMI_NEUTRALITY_MAX = 0 + RMI_NEUTRALITY / 2
RMI_NEUTRALITY_MIN = 0 - RMI_NEUTRALITY / 2

//                                                              SHORT
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
SHORT_ALT = RMI_GAMMA > RMI_NEUTRALITY_MAX and RMI_TOP_2 and close > PHASE_SHORT_XV  /// -3 3 NEUTRALITY BANDWIDTH FOR CONDITION

//                                                              LONG
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
LONG_ALT = RMI_GAMMA < RMI_NEUTRALITY_MIN and RMI_DIP_2 and close < PHASE_LONG_XV

//                                                                              ENRTRIES SUM
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

MUL_SHORT = SMA_SHORT and (RMI_TOP_3 or SHORT_ALT) or 
   SMA_SHORT_TRENDING and (RMI_TOP_2 or SHORT_ALT) and 
   SMA_TREND_CORRECTION and SMA_TREND_ENTRIES ? 1 : 
   (RMI_TOP_3 or SHORT_ALT) and not SMA_TREND_CORRECTION ? 1 : 
   (RMI_TOP_3 or SHORT_ALT) and 
   SMA_SHORT and SMA_TREND_CORRECTION and not SMA_TREND_ENTRIES ? 1 : 0
MUL_LONG = SMA_LONG and (RMI_DIP_3 or LONG_ALT) or 
   SMA_LONG_TRENDING and (RMI_DIP_2 or LONG_ALT) and 
   SMA_TREND_CORRECTION and SMA_TREND_ENTRIES ? 1 : 
   (RMI_DIP_3 or LONG_ALT) and not SMA_TREND_CORRECTION ? 1 : 
   (RMI_DIP_3 or LONG_ALT) and 
   SMA_LONG and SMA_TREND_CORRECTION and not SMA_TREND_ENTRIES ? 1 : 0

//                                                                              SWEEP
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
SHORT = int(na)
PULSE_SHORT = pulse(MUL_SHORT)
SWEEP_SHORT = sma(PULSE_SHORT, DELAY_BETWEEN_ENTRIES) > 0 ? 1 : 0
SHORTS = offset(SWEEP_SHORT, 1) < PULSE_SHORT ? 1 : 0
FIX_SHORT = fixnan(SHORTS and RMI_PUMP ? 1 : SHORT[1] ? 0 : na)
SHORT := FIX_SHORT and RMI_PUMP_OUT or SHORTS and not FIX_SHORT ? 1 : 0  //and  ( close > nz(PHASE_SHORT_XV,1) )  and  ( close > nz(PHASE_LONG_XV,1)


LONG = int(na)
PULSE_LONG = pulse(MUL_LONG)
SWEEP_LONG = sma(PULSE_LONG, DELAY_BETWEEN_ENTRIES) > 0 ? 1 : 0
LONGS = offset(SWEEP_LONG, 1) < PULSE_LONG
FIX_LONG = fixnan(LONGS and RMI_DUMP ? 1 : LONG[1] ? 0 : na)
LONG := FIX_LONG and RMI_DUMP_OUT or LONGS and not FIX_LONG ? 1 : 0  // and  ( close < nz(PHASE_LONG_XV,1) )   and  ( close < nz(PHASE_SHORT_XV,1)



// //                    ADJUSTED VALUE AFTER ADDITIONAL ENTRY
// //░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
// // REQUIRES REBUILD  :: ORDER OF LOGIC INCAPABLE OR POST ADJUSTMENT



// // plot                    (PHASE_LONG_IV , color=Y3A  , transp=00)
// plot                    (PHASE_LONG_LV , color=C8A  , transp=00)
// plot                    (PHASE_LONG_XV , color=C6A  , transp=00)

// // plot                    (PHASE_SHORT_IV , color=C3A , transp=00)
// plot                    (PHASE_SHORT_LV , color=Y8A , transp=00)
// plot                    (PHASE_SHORT_XV , color=Y6A , transp=00)



//                                                                              ASSET MANAGEMENT 
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
//                                              STOPLOSS VARIABLE DECLARATION
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
STOPLOSS_SHORT_A = int(na)
STOPLOSS_SHORT_B = int(na)
STOPLOSS_SHORT_C = int(na)
STOPLOSS_SHORT_D = int(na)
STOPLOSS_SHORT_E = int(na)
STOPLOSS_SHORT_F = int(na)
STOPLOSS_SHORT_G = int(na)
STOPLOSS_SHORT_H = int(na)
STOPLOSS_SHORT_I = int(na)
STOPLOSS_LONG_A = int(na)
STOPLOSS_LONG_B = int(na)
STOPLOSS_LONG_C = int(na)
STOPLOSS_LONG_D = int(na)
STOPLOSS_LONG_E = int(na)
STOPLOSS_LONG_F = int(na)
STOPLOSS_LONG_G = int(na)
STOPLOSS_LONG_H = int(na)
STOPLOSS_LONG_I = int(na)

//                                              TAKEPROFIT VARIABLE DECLARATION
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
TAKEPROFIT_SHORT_A = int(na)
TAKEPROFIT_SHORT_B = int(na)
TAKEPROFIT_SHORT_C = int(na)
TAKEPROFIT_SHORT_D = int(na)
TAKEPROFIT_SHORT_E = int(na)
TAKEPROFIT_SHORT_F = int(na)
TAKEPROFIT_SHORT_G = int(na)
TAKEPROFIT_SHORT_H = int(na)
TAKEPROFIT_SHORT_I = int(na)
TAKEPROFIT_LONG_A = int(na)
TAKEPROFIT_LONG_B = int(na)
TAKEPROFIT_LONG_C = int(na)
TAKEPROFIT_LONG_D = int(na)
TAKEPROFIT_LONG_E = int(na)
TAKEPROFIT_LONG_F = int(na)
TAKEPROFIT_LONG_G = int(na)
TAKEPROFIT_LONG_H = int(na)
TAKEPROFIT_LONG_I = int(na)

//                                              STOPLOSS VARIABLE SUM
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
STOPLOSS_SHORT = STOPLOSS_SHORT_A or STOPLOSS_SHORT_B or STOPLOSS_SHORT_C or STOPLOSS_SHORT_D or 
   STOPLOSS_SHORT_E or STOPLOSS_SHORT_F or STOPLOSS_SHORT_G or STOPLOSS_SHORT_H or 
   STOPLOSS_SHORT_I ? 1 : 0
STOPLOSS_LONG = STOPLOSS_LONG_A or STOPLOSS_LONG_B or STOPLOSS_LONG_C or STOPLOSS_LONG_D or 
   STOPLOSS_LONG_E or STOPLOSS_LONG_F or STOPLOSS_LONG_G or STOPLOSS_LONG_H or 
   STOPLOSS_LONG_I ? 1 : 0

//                                              TAKEPROFIT VARIABLE SUM
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
TAKEPROFIT_SHORT = TAKEPROFIT_SHORT_A or TAKEPROFIT_SHORT_B or TAKEPROFIT_SHORT_C or 
   TAKEPROFIT_SHORT_D or TAKEPROFIT_SHORT_E or TAKEPROFIT_SHORT_F or 
   TAKEPROFIT_SHORT_G or TAKEPROFIT_SHORT_H or TAKEPROFIT_SHORT_I ? 1 : 0
TAKEPROFIT_LONG = TAKEPROFIT_LONG_A or TAKEPROFIT_LONG_B or TAKEPROFIT_LONG_C or TAKEPROFIT_LONG_D or 
   TAKEPROFIT_LONG_E or TAKEPROFIT_LONG_F or TAKEPROFIT_LONG_G or TAKEPROFIT_LONG_H or 
   TAKEPROFIT_LONG_I ? 1 : 0



//                                                              PYRAMIDING
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
//                      COUNTER OPEN SHORTS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
PYR_SHORTS_OPEN = 0
PYR_SHORTS_OPEN := nz(PYR_SHORTS_OPEN[1])

PYR_SHORTS_CLOSED = 0
PYR_SHORTS_CLOSED := nz(PYR_SHORTS_CLOSED[1])

if SHORT
    PYR_SHORTS_OPEN := PYR_SHORTS_OPEN + 1
    PYR_SHORTS_CLOSED := 0
    PYR_SHORTS_CLOSED

if LONG or TAKEPROFIT_SHORT or STOPLOSS_SHORT
    PYR_SHORTS_CLOSED := PYR_SHORTS_CLOSED + PYR_SHORTS_OPEN
    PYR_SHORTS_OPEN := 0
    PYR_SHORTS_OPEN

//                      LAST POSITION VALUE
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
PYR_SHORTS_LAST = float(na)
PYR_SHORTS_LAST := SHORT ? close : nz(PYR_SHORTS_LAST[1])


//                      COUNTER OPEN LONGS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
PYR_LONGS_OPEN = 0
PYR_LONGS_OPEN := nz(PYR_LONGS_OPEN[1])

PYR_LONGS_CLOSED = 0
PYR_LONGS_CLOSED := nz(PYR_LONGS_CLOSED[1])

if LONG
    PYR_LONGS_OPEN := PYR_LONGS_OPEN + 1
    PYR_LONGS_CLOSED := 0
    PYR_LONGS_CLOSED

if SHORT or TAKEPROFIT_LONG or STOPLOSS_LONG
    PYR_LONGS_CLOSED := PYR_LONGS_CLOSED + PYR_LONGS_OPEN
    PYR_LONGS_OPEN := 0
    PYR_LONGS_OPEN

//                      LAST POSITION VALUE
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
PYR_LONGS_LAST = float(na)
PYR_LONGS_LAST := LONG ? close : nz(PYR_LONGS_LAST[1])

//                                                              POSITIONS
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
//                     SHORTS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

PYR_SHORTS_A = fixnan(PYR_SHORTS_OPEN <= PYRAMIDING and PYR_SHORTS_OPEN == 1 and SHORT[1 + DISTANCE_EXIT_OPEN] and STRATEGY == "Margin" ? close : TAKEPROFIT_SHORT_A[1] or STOPLOSS_SHORT_A[1] or CLOSE_ALERT[1] ? 0 : na)
OPEN_SHORT_A = PYR_SHORTS_OPEN <= PYRAMIDING and 
   PYR_SHORTS_OPEN == 1 and SHORT[DISTANCE_EXIT_OPEN] and STRATEGY == "Margin" ? 1 : 
   0
SELL_LONGS = PYR_SHORTS_OPEN <= PYRAMIDING and 
   PYR_SHORTS_OPEN == 1 and SHORT[DISTANCE_EXIT_OPEN] and STRATEGY == "Regular" ? 1 : 
   0

PYR_SHORTS_B = fixnan(PYR_SHORTS_OPEN <= PYRAMIDING and PYR_SHORTS_OPEN == 2 and SHORT[1] and STRATEGY == "Margin" ? close : TAKEPROFIT_SHORT_B[1] or STOPLOSS_SHORT_B[1] or CLOSE_ALERT[1] ? 0 : na)
OPEN_SHORT_B = PYR_SHORTS_OPEN <= PYRAMIDING and PYR_SHORTS_OPEN == 2 and SHORT and 
   STRATEGY == "Margin" ? 1 : 0

PYR_SHORTS_C = fixnan(PYR_SHORTS_OPEN <= PYRAMIDING and PYR_SHORTS_OPEN == 3 and SHORT[1] and STRATEGY == "Margin" ? close : TAKEPROFIT_SHORT_C[1] or STOPLOSS_SHORT_C[1] or CLOSE_ALERT[1] ? 0 : na)
OPEN_SHORT_C = PYR_SHORTS_OPEN <= PYRAMIDING and PYR_SHORTS_OPEN == 3 and SHORT and 
   STRATEGY == "Margin" ? 1 : 0

PYR_SHORTS_D = fixnan(PYR_SHORTS_OPEN <= PYRAMIDING and PYR_SHORTS_OPEN == 4 and SHORT[1] and STRATEGY == "Margin" ? close : TAKEPROFIT_SHORT_D[1] or STOPLOSS_SHORT_D[1] or CLOSE_ALERT[1] ? 0 : na)
OPEN_SHORT_D = PYR_SHORTS_OPEN <= PYRAMIDING and PYR_SHORTS_OPEN == 4 and SHORT and 
   STRATEGY == "Margin" ? 1 : 0

PYR_SHORTS_E = fixnan(PYR_SHORTS_OPEN <= PYRAMIDING and PYR_SHORTS_OPEN == 5 and SHORT[1] and STRATEGY == "Margin" ? close : TAKEPROFIT_SHORT_E[1] or STOPLOSS_SHORT_E[1] or CLOSE_ALERT[1] ? 0 : na)
OPEN_SHORT_E = PYR_SHORTS_OPEN <= PYRAMIDING and PYR_SHORTS_OPEN == 5 and SHORT and 
   STRATEGY == "Margin" ? 1 : 0

PYR_SHORTS_F = fixnan(PYR_SHORTS_OPEN <= PYRAMIDING and PYR_SHORTS_OPEN == 6 and SHORT[1] and STRATEGY == "Margin" ? close : TAKEPROFIT_SHORT_F[1] or STOPLOSS_SHORT_F[1] or CLOSE_ALERT[1] ? 0 : na)
OPEN_SHORT_F = PYR_SHORTS_OPEN <= PYRAMIDING and PYR_SHORTS_OPEN == 6 and SHORT and 
   STRATEGY == "Margin" ? 1 : 0

PYR_SHORTS_G = fixnan(PYR_SHORTS_OPEN <= PYRAMIDING and PYR_SHORTS_OPEN == 7 and SHORT[1] and STRATEGY == "Margin" ? close : TAKEPROFIT_SHORT_G[1] or STOPLOSS_SHORT_G[1] or CLOSE_ALERT[1] ? 0 : na)
OPEN_SHORT_G = PYR_SHORTS_OPEN <= PYRAMIDING and PYR_SHORTS_OPEN == 7 and SHORT and 
   STRATEGY == "Margin" ? 1 : 0

PYR_SHORTS_H = fixnan(PYR_SHORTS_OPEN <= PYRAMIDING and PYR_SHORTS_OPEN == 8 and SHORT[1] and STRATEGY == "Margin" ? close : TAKEPROFIT_SHORT_H[1] or STOPLOSS_SHORT_H[1] or CLOSE_ALERT[1] ? 0 : na)
OPEN_SHORT_H = PYR_SHORTS_OPEN <= PYRAMIDING and PYR_SHORTS_OPEN == 8 and SHORT and 
   STRATEGY == "Margin" ? 1 : 0

PYR_SHORTS_I = fixnan(PYR_SHORTS_OPEN <= PYRAMIDING and PYR_SHORTS_OPEN == 9 and SHORT[1] and STRATEGY == "Margin" ? close : TAKEPROFIT_SHORT_I[1] or STOPLOSS_SHORT_I[1] or CLOSE_ALERT[1] ? 0 : na)
OPEN_SHORT_I = PYR_SHORTS_OPEN <= PYRAMIDING and PYR_SHORTS_OPEN == 9 and SHORT and 
   STRATEGY == "Margin" ? 1 : 0

//                      LONGS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

PYR_LONGS_A = fixnan(PYR_LONGS_OPEN <= PYRAMIDING and PYR_LONGS_OPEN == 1 and LONG[1 + DISTANCE_EXIT_OPEN] ? close : TAKEPROFIT_LONG_A[1] or STOPLOSS_LONG_A[1] or CLOSE_ALERT[1] ? 0 : na)
OPEN_LONG_A = PYR_LONGS_OPEN <= PYRAMIDING and 
   PYR_LONGS_OPEN == 1 and LONG[DISTANCE_EXIT_OPEN] ? 1 : 0

PYR_LONGS_B = fixnan(PYR_LONGS_OPEN <= PYRAMIDING and PYR_LONGS_OPEN == 2 and LONG[1] ? close : TAKEPROFIT_LONG_B[1] or STOPLOSS_LONG_B[1] or CLOSE_ALERT[1] ? 0 : na)
OPEN_LONG_B = PYR_LONGS_OPEN <= PYRAMIDING and PYR_LONGS_OPEN == 2 and LONG ? 1 : 0

PYR_LONGS_C = fixnan(PYR_LONGS_OPEN <= PYRAMIDING and PYR_LONGS_OPEN == 3 and LONG[1] ? close : TAKEPROFIT_LONG_C[1] or STOPLOSS_LONG_C[1] or CLOSE_ALERT[1] ? 0 : na)
OPEN_LONG_C = PYR_LONGS_OPEN <= PYRAMIDING and PYR_LONGS_OPEN == 3 and LONG ? 1 : 0

PYR_LONGS_D = fixnan(PYR_LONGS_OPEN <= PYRAMIDING and PYR_LONGS_OPEN == 4 and LONG[1] ? close : TAKEPROFIT_LONG_D[1] or STOPLOSS_LONG_D[1] or CLOSE_ALERT[1] ? 0 : na)
OPEN_LONG_D = PYR_LONGS_OPEN <= PYRAMIDING and PYR_LONGS_OPEN == 4 and LONG ? 1 : 0

PYR_LONGS_E = fixnan(PYR_LONGS_OPEN <= PYRAMIDING and PYR_LONGS_OPEN == 5 and LONG[1] ? close : TAKEPROFIT_LONG_E[1] or STOPLOSS_LONG_E[1] or CLOSE_ALERT[1] ? 0 : na)
OPEN_LONG_E = PYR_LONGS_OPEN <= PYRAMIDING and PYR_LONGS_OPEN == 5 and LONG ? 1 : 0

PYR_LONGS_F = fixnan(PYR_LONGS_OPEN <= PYRAMIDING and PYR_LONGS_OPEN == 6 and LONG[1] ? close : TAKEPROFIT_LONG_F[1] or STOPLOSS_LONG_F[1] or CLOSE_ALERT[1] ? 0 : na)
OPEN_LONG_F = PYR_LONGS_OPEN <= PYRAMIDING and PYR_LONGS_OPEN == 6 and LONG ? 1 : 0

PYR_LONGS_G = fixnan(PYR_LONGS_OPEN <= PYRAMIDING and PYR_LONGS_OPEN == 7 and LONG[1] ? close : TAKEPROFIT_LONG_G[1] or STOPLOSS_LONG_G[1] or CLOSE_ALERT[1] ? 0 : na)
OPEN_LONG_G = PYR_LONGS_OPEN <= PYRAMIDING and PYR_LONGS_OPEN == 7 and LONG ? 1 : 0

PYR_LONGS_H = fixnan(PYR_LONGS_OPEN <= PYRAMIDING and PYR_LONGS_OPEN == 8 and LONG[1] ? close : TAKEPROFIT_LONG_H[1] or STOPLOSS_LONG_H[1] or CLOSE_ALERT[1] ? 0 : na)
OPEN_LONG_H = PYR_LONGS_OPEN <= PYRAMIDING and PYR_LONGS_OPEN == 8 and LONG ? 1 : 0

PYR_LONGS_I = fixnan(PYR_LONGS_OPEN <= PYRAMIDING and PYR_LONGS_OPEN == 9 and LONG[1] ? close : TAKEPROFIT_LONG_I[1] or STOPLOSS_LONG_I[1] or CLOSE_ALERT[1] ? 0 : na)
OPEN_LONG_I = PYR_LONGS_OPEN <= PYRAMIDING and PYR_LONGS_OPEN == 9 and LONG ? 1 : 0


//                                                              STOP LOSS
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
//                      FACTOR
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
STOPLOSS_LONG_FACTOR = (100 - STOPLOSS) / 100 - COMISSION_VALUE / 100
STOPLOSS_SHORT_FACTOR = (100 + STOPLOSS) / 100 + COMISSION_VALUE / 100

//                      SHORTS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
STOPLOSS_SHORT_A := PYR_SHORTS_A * STOPLOSS_SHORT_FACTOR < close and PYR_SHORTS_A > 0 ? 1 : 
   LONG ? 0 : nz(STOPLOSS_SHORT_A[1])
STOPLOSS_SHORT_B := PYR_SHORTS_B * STOPLOSS_SHORT_FACTOR < close and PYR_SHORTS_B > 0 ? 1 : 
   LONG ? 0 : nz(STOPLOSS_SHORT_B[1])
STOPLOSS_SHORT_C := PYR_SHORTS_C * STOPLOSS_SHORT_FACTOR < close and PYR_SHORTS_C > 0 ? 1 : 
   LONG ? 0 : nz(STOPLOSS_SHORT_C[1])
STOPLOSS_SHORT_D := PYR_SHORTS_D * STOPLOSS_SHORT_FACTOR < close and PYR_SHORTS_D > 0 ? 1 : 
   LONG ? 0 : nz(STOPLOSS_SHORT_D[1])
STOPLOSS_SHORT_E := PYR_SHORTS_E * STOPLOSS_SHORT_FACTOR < close and PYR_SHORTS_E > 0 ? 1 : 
   LONG ? 0 : nz(STOPLOSS_SHORT_E[1])
STOPLOSS_SHORT_F := PYR_SHORTS_F * STOPLOSS_SHORT_FACTOR < close and PYR_SHORTS_F > 0 ? 1 : 
   LONG ? 0 : nz(STOPLOSS_SHORT_F[1])
STOPLOSS_SHORT_G := PYR_SHORTS_G * STOPLOSS_SHORT_FACTOR < close and PYR_SHORTS_G > 0 ? 1 : 
   LONG ? 0 : nz(STOPLOSS_SHORT_G[1])
STOPLOSS_SHORT_H := PYR_SHORTS_H * STOPLOSS_SHORT_FACTOR < close and PYR_SHORTS_H > 0 ? 1 : 
   LONG ? 0 : nz(STOPLOSS_SHORT_H[1])
STOPLOSS_SHORT_I := PYR_SHORTS_I * STOPLOSS_SHORT_FACTOR < close and PYR_SHORTS_I > 0 ? 1 : 
   LONG ? 0 : nz(STOPLOSS_SHORT_I[1])

//                      LONGS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
STOPLOSS_LONG_A := PYR_LONGS_A * STOPLOSS_LONG_FACTOR > close and PYR_LONGS_A > 0 ? 1 : 
   SHORT ? 0 : nz(STOPLOSS_LONG_A[1])
STOPLOSS_LONG_B := PYR_LONGS_B * STOPLOSS_LONG_FACTOR > close and PYR_LONGS_B > 0 ? 1 : 
   SHORT ? 0 : nz(STOPLOSS_LONG_B[1])
STOPLOSS_LONG_C := PYR_LONGS_C * STOPLOSS_LONG_FACTOR > close and PYR_LONGS_C > 0 ? 1 : 
   SHORT ? 0 : nz(STOPLOSS_LONG_C[1])
STOPLOSS_LONG_D := PYR_LONGS_D * STOPLOSS_LONG_FACTOR > close and PYR_LONGS_D > 0 ? 1 : 
   SHORT ? 0 : nz(STOPLOSS_LONG_D[1])
STOPLOSS_LONG_E := PYR_LONGS_E * STOPLOSS_LONG_FACTOR > close and PYR_LONGS_E > 0 ? 1 : 
   SHORT ? 0 : nz(STOPLOSS_LONG_E[1])
STOPLOSS_LONG_F := PYR_LONGS_F * STOPLOSS_LONG_FACTOR > close and PYR_LONGS_F > 0 ? 1 : 
   SHORT ? 0 : nz(STOPLOSS_LONG_F[1])
STOPLOSS_LONG_G := PYR_LONGS_G * STOPLOSS_LONG_FACTOR > close and PYR_LONGS_G > 0 ? 1 : 
   SHORT ? 0 : nz(STOPLOSS_LONG_G[1])
STOPLOSS_LONG_H := PYR_LONGS_H * STOPLOSS_LONG_FACTOR > close and PYR_LONGS_H > 0 ? 1 : 
   SHORT ? 0 : nz(STOPLOSS_LONG_H[1])
STOPLOSS_LONG_I := PYR_LONGS_I * STOPLOSS_LONG_FACTOR > close and PYR_LONGS_I > 0 ? 1 : 
   SHORT ? 0 : nz(STOPLOSS_LONG_I[1])

//                                              STOPLOSS VARIABLE SUM
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
//                      SHORTS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
STOPLOSS_SHORT := multifirefix(pulse(STOPLOSS_SHORT_A) or pulse(STOPLOSS_SHORT_B) or pulse(STOPLOSS_SHORT_C) or pulse(STOPLOSS_SHORT_D) or pulse(STOPLOSS_SHORT_E) or pulse(STOPLOSS_SHORT_F) or pulse(STOPLOSS_SHORT_G) or pulse(STOPLOSS_SHORT_H) or pulse(STOPLOSS_SHORT_I) ? 1 : 0)

//                      LONGS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
STOPLOSS_LONG := multifirefix(pulse(STOPLOSS_LONG_A) or pulse(STOPLOSS_LONG_B) or pulse(STOPLOSS_LONG_C) or pulse(STOPLOSS_LONG_D) or pulse(STOPLOSS_LONG_E) or pulse(STOPLOSS_LONG_F) or pulse(STOPLOSS_LONG_G) or pulse(STOPLOSS_LONG_H) or pulse(STOPLOSS_LONG_I) ? 1 : 0)


//                                                              TAKE PROFIT
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
//                      FACTOR
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
TAKEPROFIT_LONG_FACTOR = (100 + TAKEPROFIT) / 100 + COMISSION_VALUE / 100
TAKEPROFIT_SHORT_FACTOR = (100 - TAKEPROFIT) / 100 - COMISSION_VALUE / 100

//                      SHORTS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
TAKEPROFIT_SHORT_A := PYR_SHORTS_A * TAKEPROFIT_SHORT_FACTOR > close and PYR_SHORTS_A > 0 ? 1 : 
   LONG ? 0 : nz(TAKEPROFIT_SHORT_A[1])
TAKEPROFIT_SHORT_B := PYR_SHORTS_B * TAKEPROFIT_SHORT_FACTOR > close and PYR_SHORTS_B > 0 ? 1 : 
   LONG ? 0 : nz(TAKEPROFIT_SHORT_B[1])
TAKEPROFIT_SHORT_C := PYR_SHORTS_C * TAKEPROFIT_SHORT_FACTOR > close and PYR_SHORTS_C > 0 ? 1 : 
   LONG ? 0 : nz(TAKEPROFIT_SHORT_C[1])
TAKEPROFIT_SHORT_D := PYR_SHORTS_D * TAKEPROFIT_SHORT_FACTOR > close and PYR_SHORTS_D > 0 ? 1 : 
   LONG ? 0 : nz(TAKEPROFIT_SHORT_D[1])
TAKEPROFIT_SHORT_E := PYR_SHORTS_E * TAKEPROFIT_SHORT_FACTOR > close and PYR_SHORTS_E > 0 ? 1 : 
   LONG ? 0 : nz(TAKEPROFIT_SHORT_E[1])
TAKEPROFIT_SHORT_F := PYR_SHORTS_F * TAKEPROFIT_SHORT_FACTOR > close and PYR_SHORTS_F > 0 ? 1 : 
   LONG ? 0 : nz(TAKEPROFIT_SHORT_F[1])
TAKEPROFIT_SHORT_G := PYR_SHORTS_G * TAKEPROFIT_SHORT_FACTOR > close and PYR_SHORTS_G > 0 ? 1 : 
   LONG ? 0 : nz(TAKEPROFIT_SHORT_G[1])
TAKEPROFIT_SHORT_H := PYR_SHORTS_H * TAKEPROFIT_SHORT_FACTOR > close and PYR_SHORTS_H > 0 ? 1 : 
   LONG ? 0 : nz(TAKEPROFIT_SHORT_H[1])
TAKEPROFIT_SHORT_I := PYR_SHORTS_I * TAKEPROFIT_SHORT_FACTOR > close and PYR_SHORTS_I > 0 ? 1 : 
   LONG ? 0 : nz(TAKEPROFIT_SHORT_I[1])


//                      LONGS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
TAKEPROFIT_LONG_A := PYR_LONGS_A * TAKEPROFIT_LONG_FACTOR < close and PYR_LONGS_A > 0 ? 1 : 
   SHORT ? 0 : nz(TAKEPROFIT_LONG_A[1])
TAKEPROFIT_LONG_B := PYR_LONGS_B * TAKEPROFIT_LONG_FACTOR < close and PYR_LONGS_B > 0 ? 1 : 
   SHORT ? 0 : nz(TAKEPROFIT_LONG_B[1])
TAKEPROFIT_LONG_C := PYR_LONGS_C * TAKEPROFIT_LONG_FACTOR < close and PYR_LONGS_C > 0 ? 1 : 
   SHORT ? 0 : nz(TAKEPROFIT_LONG_C[1])
TAKEPROFIT_LONG_D := PYR_LONGS_D * TAKEPROFIT_LONG_FACTOR < close and PYR_LONGS_D > 0 ? 1 : 
   SHORT ? 0 : nz(TAKEPROFIT_LONG_D[1])
TAKEPROFIT_LONG_E := PYR_LONGS_E * TAKEPROFIT_LONG_FACTOR < close and PYR_LONGS_E > 0 ? 1 : 
   SHORT ? 0 : nz(TAKEPROFIT_LONG_E[1])
TAKEPROFIT_LONG_F := PYR_LONGS_F * TAKEPROFIT_LONG_FACTOR < close and PYR_LONGS_F > 0 ? 1 : 
   SHORT ? 0 : nz(TAKEPROFIT_LONG_F[1])
TAKEPROFIT_LONG_G := PYR_LONGS_G * TAKEPROFIT_LONG_FACTOR < close and PYR_LONGS_G > 0 ? 1 : 
   SHORT ? 0 : nz(TAKEPROFIT_LONG_G[1])
TAKEPROFIT_LONG_H := PYR_LONGS_H * TAKEPROFIT_LONG_FACTOR < close and PYR_LONGS_H > 0 ? 1 : 
   SHORT ? 0 : nz(TAKEPROFIT_LONG_H[1])
TAKEPROFIT_LONG_I := PYR_LONGS_I * TAKEPROFIT_LONG_FACTOR < close and PYR_LONGS_I > 0 ? 1 : 
   SHORT ? 0 : nz(TAKEPROFIT_LONG_I[1])


//                                              TAKEPROFIT VARIABLE SUM
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
//                      SHORTS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
TAKEPROFIT_SHORT := multifirefix(pulse(TAKEPROFIT_SHORT_A) or pulse(TAKEPROFIT_SHORT_B) or pulse(TAKEPROFIT_SHORT_C) or pulse(TAKEPROFIT_SHORT_D) or pulse(TAKEPROFIT_SHORT_E) or pulse(TAKEPROFIT_SHORT_F) or pulse(TAKEPROFIT_SHORT_G) or pulse(TAKEPROFIT_SHORT_H) or pulse(TAKEPROFIT_SHORT_I) ? 1 : 0)

//                      LONGS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
TAKEPROFIT_LONG := multifirefix(pulse(TAKEPROFIT_LONG_A) or pulse(TAKEPROFIT_LONG_B) or pulse(TAKEPROFIT_LONG_C) or pulse(TAKEPROFIT_LONG_D) or pulse(TAKEPROFIT_LONG_E) or pulse(TAKEPROFIT_LONG_F) or pulse(TAKEPROFIT_LONG_G) or pulse(TAKEPROFIT_LONG_H) or pulse(TAKEPROFIT_LONG_I) ? 1 : 0)

//                                                                              AUTOTVIEW ALERTS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
//                      OFFSET A TO CLOSE ON 1 TICK BEFORE
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
SHORT_CORRECTED = OPEN_SHORT_A or OPEN_SHORT_B or OPEN_SHORT_C or OPEN_SHORT_D or OPEN_SHORT_E or 
   OPEN_SHORT_F or OPEN_SHORT_G or OPEN_SHORT_H or OPEN_SHORT_I ? 1 : 0

LONG_CORRECTED = OPEN_LONG_A or OPEN_LONG_B or OPEN_LONG_C or OPEN_LONG_D or OPEN_LONG_E or 
   OPEN_LONG_F or OPEN_LONG_G or OPEN_LONG_H or OPEN_LONG_I ? 1 : 0

CLOSE_LONGS = PYR_SHORTS_OPEN == 1 and SHORT and STRATEGY == "Margin" ? 1 : 0  //(( PHASE_LONG and OPEN_SHORT_A ) or ( PHASE_SHORT and OPEN_LONG_A ) ? 1 : 0 ) :  IGNORE == 1 ? (( PHASE_LONG and OPEN_SHORT_B )  or ( PHASE_SHORT and OPEN_LONG_B ) ? 1 : 0) : IGNORE == 2 ? (( PHASE_LONG and OPEN_SHORT_C )      or (PHASE_SHORT and OPEN_LONG_C) ? 1 : 0) : 0

CLOSE_SHORTS = PYR_LONGS_OPEN == 1 and LONG and STRATEGY == "Margin" ? 1 : 0

//                      FIX MULTIFIRE
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
SHORT_ALERT = multifirefix(SHORT_CORRECTED)

LONG_ALERT = multifirefix(LONG_CORRECTED)

CLOSE_ALERT := CLOSE_LONGS or CLOSE_SHORTS or SELL_LONGS ? 1 : 0


//                      CONSTANT TO PULSE
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
CLOSE_ONE_A = multifirefix(pulse(TAKEPROFIT_LONG_A) or pulse(STOPLOSS_LONG_A) or pulse(TAKEPROFIT_SHORT_A) or pulse(STOPLOSS_SHORT_A) ? 1 : 0)
CLOSE_ONE_B = multifirefix(pulse(TAKEPROFIT_LONG_B) or pulse(STOPLOSS_LONG_B) or pulse(TAKEPROFIT_SHORT_B) or pulse(STOPLOSS_SHORT_B) ? 1 : 0)
CLOSE_ONE_C = multifirefix(pulse(TAKEPROFIT_LONG_C) or pulse(STOPLOSS_LONG_C) or pulse(TAKEPROFIT_SHORT_C) or pulse(STOPLOSS_SHORT_C) ? 1 : 0)
CLOSE_ONE_D = multifirefix(pulse(TAKEPROFIT_LONG_D) or pulse(STOPLOSS_LONG_D) or pulse(TAKEPROFIT_SHORT_D) or pulse(STOPLOSS_SHORT_D) ? 1 : 0)
CLOSE_ONE_E = multifirefix(pulse(TAKEPROFIT_LONG_E) or pulse(STOPLOSS_LONG_E) or pulse(TAKEPROFIT_SHORT_E) or pulse(STOPLOSS_SHORT_E) ? 1 : 0)
CLOSE_ONE_F = multifirefix(pulse(TAKEPROFIT_LONG_F) or pulse(STOPLOSS_LONG_F) or pulse(TAKEPROFIT_SHORT_F) or pulse(STOPLOSS_SHORT_F) ? 1 : 0)
CLOSE_ONE_G = multifirefix(pulse(TAKEPROFIT_LONG_G) or pulse(STOPLOSS_LONG_G) or pulse(TAKEPROFIT_SHORT_G) or pulse(STOPLOSS_SHORT_G) ? 1 : 0)
CLOSE_ONE_H = multifirefix(pulse(TAKEPROFIT_LONG_H) or pulse(STOPLOSS_LONG_H) or pulse(TAKEPROFIT_SHORT_H) or pulse(STOPLOSS_SHORT_H) ? 1 : 0)
CLOSE_ONE_I = multifirefix(pulse(TAKEPROFIT_LONG_I) or pulse(STOPLOSS_LONG_I) or pulse(TAKEPROFIT_SHORT_I) or pulse(STOPLOSS_SHORT_I) ? 1 : 0)

//                      SPREAD MULTIPLE PULSES
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

CLOSE_AMOUNT = (CLOSE_ONE_A ? 1 : 0) + (CLOSE_ONE_B ? 1 : 0) + (CLOSE_ONE_C ? 1 : 0) + 
   (CLOSE_ONE_D ? 1 : 0) + (CLOSE_ONE_E ? 1 : 0) + (CLOSE_ONE_F ? 1 : 0) + 
   (CLOSE_ONE_G ? 1 : 0) + (CLOSE_ONE_H ? 1 : 0) + (CLOSE_ONE_I ? 1 : 0)
CLOSE_ONE = CLOSE_AMOUNT > 0 ? 1 : 
   CLOSE_AMOUNT[1] > 1 ? 1 : CLOSE_AMOUNT[2] > 2 ? 1 : CLOSE_AMOUNT[3] > 3 ? 1 : 
   CLOSE_AMOUNT[4] > 4 ? 1 : CLOSE_AMOUNT[5] > 5 ? 1 : CLOSE_AMOUNT[6] > 6 ? 1 : 
   CLOSE_AMOUNT[7] > 7 ? 1 : CLOSE_AMOUNT[8] > 8 ? 1 : CLOSE_AMOUNT[9] > 9 ? 1 : 0


//                                                              GENERAL
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
//                      WITHOUT SPECIFIC ORDER TAGS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
plot(VIEWPOSITIONS and LONG_ALERT[1] ? close * 1.2 : LONG_ALERT[1] ? 50 : 0, color=C5A, title="OPEN 1 LONG")

plot(VIEWPOSITIONS and SHORT_ALERT[1] ? close * 1.2 : SHORT_ALERT[1] ? 50 : 0, color=Y6A, title="OPEN 1 SHORT")

plot(VIEWPOSITIONS and CLOSE_ONE ? close * 1.2 : CLOSE_ONE ? 50 : 0, color=M4A, title="SL/TP 1 POSITION")

plot(VIEWPOSITIONS and CLOSE_ALERT ? close * 1.2 : CLOSE_ALERT ? 50 : 0, color=color.white, title="CLOSE POSITIONS")

//                                                                              BACKTESTING
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
        //                                                              LONGS
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
//                      OPEN LONG
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
if OPEN_LONG_A
    strategy.entry("LONG ENTRY :: A", strategy.long)
if OPEN_LONG_B
    strategy.entry("LONG ENTRY :: B", strategy.long)
if OPEN_LONG_C
    strategy.entry("LONG ENTRY :: C", strategy.long)
if OPEN_LONG_D
    strategy.entry("LONG ENTRY :: D", strategy.long)
if OPEN_LONG_E
    strategy.entry("LONG ENTRY :: E", strategy.long)
if OPEN_LONG_F
    strategy.entry("LONG ENTRY :: F", strategy.long)
if OPEN_LONG_G
    strategy.entry("LONG ENTRY :: G", strategy.long)
if OPEN_LONG_H
    strategy.entry("LONG ENTRY :: H", strategy.long)
if OPEN_LONG_I
    strategy.entry("LONG ENTRY :: I", strategy.long)

//                      CLOSE LONG
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
if STOPLOSS_LONG_A or TAKEPROFIT_LONG_A or SELL_LONGS
    strategy.close("LONG ENTRY :: A", strategy.long)
if STOPLOSS_LONG_B or TAKEPROFIT_LONG_B or SELL_LONGS
    strategy.close("LONG ENTRY :: B", strategy.long)
if STOPLOSS_LONG_C or TAKEPROFIT_LONG_C or SELL_LONGS
    strategy.close("LONG ENTRY :: C", strategy.long)
if STOPLOSS_LONG_D or TAKEPROFIT_LONG_D or SELL_LONGS
    strategy.close("LONG ENTRY :: D", strategy.long)
if STOPLOSS_LONG_E or TAKEPROFIT_LONG_E or SELL_LONGS
    strategy.close("LONG ENTRY :: E", strategy.long)
if STOPLOSS_LONG_F or TAKEPROFIT_LONG_F or SELL_LONGS
    strategy.close("LONG ENTRY :: F", strategy.long)
if STOPLOSS_LONG_G or TAKEPROFIT_LONG_G or SELL_LONGS
    strategy.close("LONG ENTRY :: G", strategy.long)
if STOPLOSS_LONG_H or TAKEPROFIT_LONG_H or SELL_LONGS
    strategy.close("LONG ENTRY :: H", strategy.long)
if STOPLOSS_LONG_I or TAKEPROFIT_LONG_I or SELL_LONGS
    strategy.close("LONG ENTRY :: I", strategy.long)

//                                                              SHORTS
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
//                      OPEN SHORT
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
if OPEN_SHORT_A
    strategy.entry("SHORT ENTRY :: A", strategy.short)
if OPEN_SHORT_B
    strategy.entry("SHORT ENTRY :: B", strategy.short)
if OPEN_SHORT_C
    strategy.entry("SHORT ENTRY :: C", strategy.short)
if OPEN_SHORT_D
    strategy.entry("SHORT ENTRY :: D", strategy.short)
if OPEN_SHORT_E
    strategy.entry("SHORT ENTRY :: E", strategy.short)
if OPEN_SHORT_F
    strategy.entry("SHORT ENTRY :: F", strategy.short)
if OPEN_SHORT_G
    strategy.entry("SHORT ENTRY :: G", strategy.short)
if OPEN_SHORT_H
    strategy.entry("SHORT ENTRY :: H", strategy.short)
if OPEN_SHORT_I
    strategy.entry("SHORT ENTRY :: I", strategy.short)

//                      CLOSE SHORT
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
if STOPLOSS_SHORT_A or TAKEPROFIT_SHORT_A
    strategy.close("SHORT ENTRY :: A", strategy.short)
if STOPLOSS_SHORT_B or TAKEPROFIT_SHORT_B
    strategy.close("SHORT ENTRY :: B", strategy.short)
if STOPLOSS_SHORT_C or TAKEPROFIT_SHORT_C
    strategy.close("SHORT ENTRY :: C", strategy.short)
if STOPLOSS_SHORT_D or TAKEPROFIT_SHORT_D
    strategy.close("SHORT ENTRY :: D", strategy.short)
if STOPLOSS_SHORT_E or TAKEPROFIT_SHORT_E
    strategy.close("SHORT ENTRY :: E", strategy.short)
if STOPLOSS_SHORT_F or TAKEPROFIT_SHORT_F
    strategy.close("SHORT ENTRY :: F", strategy.short)
if STOPLOSS_SHORT_G or TAKEPROFIT_SHORT_G
    strategy.close("SHORT ENTRY :: G", strategy.short)
if STOPLOSS_SHORT_H or TAKEPROFIT_SHORT_H
    strategy.close("SHORT ENTRY :: H", strategy.short)
if STOPLOSS_SHORT_I or TAKEPROFIT_SHORT_I
    strategy.close("SHORT ENTRY :: I", strategy.short)


//                                                                              GRAPHICAL
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
// //                                                              SMA
// //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// SMA_COLOR               = SMA_SHORT ? Y7B : SMA_SHORT_TRENDING ? Y5B : SMA_LONG ? C7B : SMA_LONG_TRENDING ? C5B : K7C 
// ALPHA_COLOR             = SMA_SOURCE_ALPHA  ? Y7A : C7A
// BETA_COLOR              = SMA_ALPHA_BETA    ? Y5A : C5A
// GAMMA_COLOR             = SMA_BETA_GAMMA    ? Y3A : C3A

// PLOT_ZERO               = plot  ( VIEWPOSITIONS ? 0             :na         , color=K6A   )

// plot                            ( VIEWPOSITIONS ? SMA_ALPHA_DIR * 100 :na         , color=K4A       , title='MOVING ALPHA ANGLE'    )
// plot                            ( VIEWPOSITIONS ? SMA_BETA_DIR  * 100 :na         , color=K2A       , title='MOVING BETA ANGLE'     )
// plot                            ( VIEWPOSITIONS ? SMA_GAMMA_DIR * 100 :na         , color=white     , title='MOVING GAMMA ANGLE'    )


// PLOT_CLOSE              = plot  ( VIEWPOSITIONS ? close         :na         , color=C1A   )
// PLOT_SMA_ALPHA          = plot  ( VIEWPOSITIONS ? SMA_ALPHA     :na         , color=C2A     )
// PLOT_SMA_BETA           = plot  ( VIEWPOSITIONS ? SMA_BETA      :na         , color=C5A     )
// PLOT_SMA_GAMMA          = plot  ( VIEWPOSITIONS ? SMA_GAMMA     :na         , color=C7A     )


// fill                            ( PLOT_ZERO         , PLOT_CLOSE                                                                , transp=70     , color= SMA_COLOR      , title='SMA COLOR'         , editable=false    )
// fill                            ( PLOT_CLOSE        , PLOT_SMA_ALPHA                                                            , transp=70     , color= ALPHA_COLOR    , title='MOVING ALPHA'      , editable=false    )
// fill                            ( PLOT_SMA_ALPHA    , PLOT_SMA_BETA                                                             , transp=80     , color= BETA_COLOR     , title='MOVING BETA'       , editable=false    )
// fill                            ( PLOT_SMA_BETA     , PLOT_SMA_GAMMA                                                            , transp=90     , color= GAMMA_COLOR    , title='MOVING GAMMA'      , editable=false    )

// //                                                              CLOSE
// //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// CLOSE                   = plot  ( SOURCE                                                        , transp=00     , color=white                             , title='RMI SOURCE'            , editable=true     )

//                                                              RMI
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
RMI_ALPHA_PLOT = plot(RMI_ALPHA_ON and not VIEWPOSITIONS ? RMI_ALPHA : na, transp=20, color=M4A, title='RMI Alpha', editable=true)

RMI_BETA_PLOT = plot(RMI_BETA_ON and not VIEWPOSITIONS ? RMI_BETA : na, transp=10, color=C4A, title='RMI Beta', editable=true)

RMI_GAMMA_PLOT = plot(RMI_GAMMA_ON and not VIEWPOSITIONS ? RMI_GAMMA : na, transp=00, color=Y5A, title='RMI Gamma', editable=true)

//                                                              LINES
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
plot(RMI_ALPHA_ON and not VIEWPOSITIONS ? RMI_ALPHA_HIGH : na, transp=20, color=M7B, title='Alpha   High', editable=false)
plot(RMI_BETA_ON and not VIEWPOSITIONS ? RMI_BETA_HIGH : na, transp=20, color=C7A, title='Beta    High', editable=false)
plot(RMI_GAMMA_ON and not VIEWPOSITIONS ? RMI_GAMMA_HIGH : na, transp=20, color=Y8A, title='Gamma   High', editable=false)

hline(RMI_NEUTRALITY_MAX, linestyle=hline.style_dotted, color=K1A, title='MAX NEUTRALITY', editable=false)
hline(00, linestyle=hline.style_dotted, color=color.white, title='Midline', editable=false)
hline(RMI_NEUTRALITY_MIN, linestyle=hline.style_dotted, color=K1A, title='MIN NEUTRALITY', editable=false)

plot(RMI_ALPHA_ON and not VIEWPOSITIONS ? RMI_ALPHA_LOW : na, transp=20, color=M7B, title='Alpha   Low', editable=false)
plot(RMI_BETA_ON and not VIEWPOSITIONS ? RMI_BETA_LOW : na, transp=20, color=C7A, title='Beta    Low', editable=false)
plot(RMI_GAMMA_ON and not VIEWPOSITIONS ? RMI_GAMMA_LOW : na, transp=20, color=Y8A, title='Gamma   Low', editable=false)

//                                                              RMI FILL LINES
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
RMI_ALPHA_PLOT_HIGH = plot(RMI_ALPHA >= RMI_ALPHA_HIGH and RMI_ALPHA_ON ? RMI_ALPHA_HIGH : RMI_ALPHA_ON ? RMI_ALPHA : na, color=M4A, title='Alpha   Over  High', editable=false, transp=100)
RMI_BETA_PLOT_HIGH = plot(RMI_BETA >= RMI_BETA_HIGH and RMI_BETA_ON ? RMI_BETA_HIGH : RMI_BETA_ON ? RMI_BETA : na, color=C5A, title='Beta    Over  High', editable=false, transp=100)
RMI_GAMMA_PLOT_HIGH = plot(RMI_GAMMA >= RMI_GAMMA_HIGH and RMI_GAMMA_ON ? RMI_GAMMA_HIGH : RMI_GAMMA_ON ? RMI_GAMMA : na, color=Y6A, title='Gamma   Over  High', editable=false, transp=100)

RMI_ALPHA_PLOT_LOW = plot(RMI_ALPHA <= RMI_ALPHA_LOW and RMI_ALPHA_ON ? RMI_ALPHA_LOW : RMI_ALPHA_ON ? RMI_ALPHA : na, color=M4A, title='Alpha   Under Low', editable=false, transp=100)
RMI_BETA_PLOT_LOW = plot(RMI_BETA <= RMI_BETA_LOW and RMI_BETA_ON ? RMI_BETA_LOW : RMI_BETA_ON ? RMI_BETA : na, color=C5A, title='Beta    Under Low', editable=false, transp=100)
RMI_GAMMA_PLOT_LOW = plot(RMI_GAMMA <= RMI_GAMMA_LOW and RMI_GAMMA_ON ? RMI_GAMMA_LOW : RMI_GAMMA_ON ? RMI_GAMMA : na, color=Y6A, title='SLow    Under Low', editable=false, transp=100)

//                                                              RMI FILL
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
fill(RMI_ALPHA_PLOT, RMI_ALPHA_PLOT_HIGH, transp=30, color=M7B, title='Alpha   High Fill', editable=false)
fill(RMI_ALPHA_PLOT, RMI_ALPHA_PLOT_LOW, transp=30, color=M7B, title='Alpha   Low  Fill', editable=false)

fill(RMI_BETA_PLOT, RMI_BETA_PLOT_HIGH, transp=50, color=C7B, title='Beta    High Fill', editable=false)
fill(RMI_BETA_PLOT, RMI_BETA_PLOT_LOW, transp=50, color=C7B, title='Beta    Low  Fill', editable=false)

fill(RMI_GAMMA_PLOT, RMI_GAMMA_PLOT_HIGH, transp=70, color=Y7B, title='Gamma   High Fill', editable=false)
fill(RMI_GAMMA_PLOT, RMI_GAMMA_PLOT_LOW, transp=70, color=Y7B, title='Gamma   Low  Fill', editable=false)

// //                                                             ARROWS
// //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// plotshape               ( RMI_TOP_3      , transp=50    , color=Y6A    , size=size.small    , style=shape.triangledown  , location=location.top        , title="RMI Top"           , editable=true     )
// plotshape               ( RMI_DIP_3      , transp=50    , color=C5A    , size=size.small    , style=shape.triangleup    , location=location.bottom     , title="RMI Dip"           , editable=true     )

// //                                                             ENTRY ARROWS
// //▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
// plotshape               ( SHORT             , transp=50     , color=Y6A    , size=size.normal  , style=shape.triangledown  , location=location.top     , title="RMI Top"           , editable=true     )
// plotshape               ( LONG              , transp=50     , color=C5A    , size=size.normal  , style=shape.triangleup    , location=location.bottom  , title="RMI Dip"           , editable=true     )

// plotshape               ( SHORT_ALT         , transp=50     , color=Y8A    , size=size.large   , style=shape.triangledown  , location=location.top     , title="RMI Top Extra"     , editable=true     )
// plotshape               ( LONG_ALT          , transp=50     , color=C7A   , size=size.large   , style=shape.triangleup    , location=location.bottom  , title="RMI Dip Extra"     , editable=true     )
//                                                              SPECIFIC ARROWS
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
//                      SHORTS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
plotshape(OPEN_SHORT_A[1], transp=30, color=Y7A, size=size.large, style=shape.triangledown, location=location.top, title="SHORT A", text="SHORT A", editable=false)
plotshape(OPEN_SHORT_B[1], transp=30, color=Y7A, size=size.large, style=shape.triangledown, location=location.top, title="SHORT B", text="SHORT B", editable=false)
plotshape(OPEN_SHORT_C[1], transp=30, color=Y7A, size=size.large, style=shape.triangledown, location=location.top, title="SHORT C", text="SHORT C", editable=false)
plotshape(OPEN_SHORT_D[1], transp=30, color=Y7A, size=size.large, style=shape.triangledown, location=location.top, title="SHORT D", text="SHORT D", editable=false)
plotshape(OPEN_SHORT_E[1], transp=30, color=Y7A, size=size.large, style=shape.triangledown, location=location.top, title="SHORT E", text="SHORT E", editable=false)
plotshape(OPEN_SHORT_F[1], transp=30, color=Y7A, size=size.large, style=shape.triangledown, location=location.top, title="SHORT F", text="SHORT F", editable=false)
plotshape(OPEN_SHORT_G[1], transp=30, color=Y7A, size=size.large, style=shape.triangledown, location=location.top, title="SHORT G", text="SHORT G", editable=false)
plotshape(OPEN_SHORT_H[1], transp=30, color=Y7A, size=size.large, style=shape.triangledown, location=location.top, title="SHORT H", text="SHORT H", editable=false)
plotshape(OPEN_SHORT_I[1], transp=30, color=Y7A, size=size.large, style=shape.triangledown, location=location.top, title="SHORT I", text="SHORT I", editable=false)

//                      TAKEPROFITS SHORTS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
plotshape(TAKEPROFIT_SHORT, transp=00, color=K3A, size=size.normal, style=shape.diamond, location=location.top, title="TAKEPROFIT SHORT", text="TAKEPROFIT SHORT", editable=false)

//                      STOPLOSSS SHORTS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
plotshape(STOPLOSS_SHORT, transp=00, color=K3A, size=size.normal, style=shape.square, location=location.bottom, title="STOPLOSS SHORT", text="STOPLOSS SHORT", editable=false)


//                      LONGS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
plotshape(OPEN_LONG_A[1], transp=30, color=C6A, size=size.large, style=shape.triangleup, location=location.bottom, title="LONG A", text="LONG A", editable=false)
plotshape(OPEN_LONG_B[1], transp=30, color=C6A, size=size.large, style=shape.triangleup, location=location.bottom, title="LONG B", text="LONG B", editable=false)
plotshape(OPEN_LONG_C[1], transp=30, color=C6A, size=size.large, style=shape.triangleup, location=location.bottom, title="LONG C", text="LONG C", editable=false)
plotshape(OPEN_LONG_D[1], transp=30, color=C6A, size=size.large, style=shape.triangleup, location=location.bottom, title="LONG D", text="LONG D", editable=false)
plotshape(OPEN_LONG_E[1], transp=30, color=C6A, size=size.large, style=shape.triangleup, location=location.bottom, title="LONG E", text="LONG E", editable=false)
plotshape(OPEN_LONG_F[1], transp=30, color=C6A, size=size.large, style=shape.triangleup, location=location.bottom, title="LONG F", text="LONG F", editable=false)
plotshape(OPEN_LONG_G[1], transp=30, color=C6A, size=size.large, style=shape.triangleup, location=location.bottom, title="LONG G", text="LONG G", editable=false)
plotshape(OPEN_LONG_H[1], transp=30, color=C6A, size=size.large, style=shape.triangleup, location=location.bottom, title="LONG H", text="LONG H", editable=false)
plotshape(OPEN_LONG_I[1], transp=30, color=C6A, size=size.large, style=shape.triangleup, location=location.bottom, title="LONG I", text="LONG I", editable=false)

//                      TAKEPROFITS LONGS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
plotshape(TAKEPROFIT_LONG, transp=00, color=K3A, size=size.normal, style=shape.diamond, location=location.top, title="TAKEPROFIT LONG", text="TAKEPROFIT LONG", editable=false)

//                      STOPLOSSS LONGS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
plotshape(STOPLOSS_LONG, transp=00, color=K3A, size=size.normal, style=shape.square, location=location.bottom, title="STOPLOSS LONG", text="STOPLOSS LONG", editable=false)


//                      CLOSE POSITIONS
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
plotshape(CLOSE_ALERT, transp=00, color=color.white, size=size.normal, style=shape.xcross, location=location.top, title="CLOSE POSITIONS", text="CLOSE POSITIONS", editable=false)

//                                                              PLOT POSITIONS
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
//                      PYRAMIDING COUNTER
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
// plot                       ( PYR_LONGS_OPEN*                                 , color=C5A    )
// plot                       ( PYR_LONGS_CLOSED                                , color=C7A   )

// plot                       ( PYR_SHORTS_OPEN                                 , color=Y6A    )
// plot                       ( PYR_SHORTS_CLOSED                               , color=Y8A    )

//                      LAST CLOSED VALUE
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
// plot                       ( PYR_SHORTS_LAST                                                 )
// plot                       ( PYR_LONGS_LAST                                                  )

// //                      SHORTS
// //░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
// plot                    ( VIEWPOSITIONS ? PYR_SHORTS_A           : na             , color=Y6A    , editable=false   )
// plot                    ( VIEWPOSITIONS ? PYR_SHORTS_B           : na             , color=Y6A    , editable=false   )
// plot                    ( VIEWPOSITIONS ? PYR_SHORTS_C           : na             , color=Y6A    , editable=false   )
// plot                    ( VIEWPOSITIONS ? PYR_SHORTS_D           : na             , color=Y6A    , editable=false   )
// plot                    ( VIEWPOSITIONS ? PYR_SHORTS_E           : na             , color=Y6A    , editable=false   )
// plot                    ( VIEWPOSITIONS ? PYR_SHORTS_F           : na             , color=Y6A    , editable=false   )
// plot                    ( VIEWPOSITIONS ? PYR_SHORTS_G           : na             , color=Y6A    , editable=false   )
// plot                    ( VIEWPOSITIONS ? PYR_SHORTS_H           : na             , color=Y6A    , editable=false   )
// plot                    ( VIEWPOSITIONS ? PYR_SHORTS_I           : na             , color=Y6A    , editable=false   )

// //                      LONGS
// //░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
// plot                    ( VIEWPOSITIONS ? PYR_LONGS_A            : na             , color=C5A    , editable=false   )
// plot                    ( VIEWPOSITIONS ? PYR_LONGS_B            : na             , color=C5A    , editable=false   )
// plot                    ( VIEWPOSITIONS ? PYR_LONGS_C            : na             , color=C5A    , editable=false   )
// plot                    ( VIEWPOSITIONS ? PYR_LONGS_D            : na             , color=C5A    , editable=false   )
// plot                    ( VIEWPOSITIONS ? PYR_LONGS_E            : na             , color=C5A    , editable=false   )
// plot                    ( VIEWPOSITIONS ? PYR_LONGS_F            : na             , color=C5A    , editable=false   )
// plot                    ( VIEWPOSITIONS ? PYR_LONGS_G            : na             , color=C5A    , editable=false   )
// plot                    ( VIEWPOSITIONS ? PYR_LONGS_H            : na             , color=C5A    , editable=false   )
// plot                    ( VIEWPOSITIONS ? PYR_LONGS_I            : na             , color=C5A    , editable=false   )

//                                                              BLACKGROUND
//▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
bgcolor(BLACKGROUND and not(SWEEP_LONG or SWEEP_SHORT or FIX_LONG or FIX_SHORT) ? K9A : (SWEEP_LONG or SWEEP_SHORT) and not(FIX_LONG or FIX_SHORT) ? K8A : FIX_LONG ? C8B : FIX_SHORT ? Y8C : na, transp=00, title='Blackground', editable=true)

Overview

CMYK RMI SMA evaluates short, medium, and long-term momentum alongside SMA trend phasing to determine entry conditions across high and low boundary thresholds. The indicator plots oscillator lines in a separate pane. A notable limitation is that the strategy operates without built-in stop loss or take profit parameters by default.

Details

Published by

myn on GitHub

Type

Strategy, Pine Script v4

Draws

Separate pane below the chart

Category

Oscillators & momentum, Trend, Moving averages

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • The strategy enters trades without a stop loss

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Resolutionint3RESOLUTION
SourcesourcecloseSOURCE
Alpha RMI On —————————————————————booltrueRMI_ALPHA_ON
Alpha Periodint29ALPHA_PERIOD
Alpha Momentumint7ALPHA_MOMENTUM
Beta RMI On ——————————————————————booltrueRMI_BETA_ON
Beta Periodint239BETA_PERIOD
Beta Momentumint23BETA_MOMENTUM
Gamma RMI On ————————————————————booltrueRMI_GAMMA_ON
Gamma Periodint2467GAMMA_PERIOD
Gamma Momentumint239GAMMA_MOMENTUM
Gamma Boundary Correction ————————————booltrueRMI_TREND_CORRECTION
Show all 33 settings
SettingTypeDefaultVariable
Correction Factorfloat0.2RMI_TREND_FACTOR
Correction Periodint1400RMI_TREND_PERIOD
SMA Phasing ——————————————————————booltrueSMA_TREND_CORRECTION
SMA Multiplierfloat3.3SMA_TREND_FACTOR
SMA Trend Entries ——————————————————booltrueSMA_TREND_ENTRIES
SMA Trend Periodint10080SMA_TREND_PERIOD
SMA Trend Threshold %float2.1SMA_TREND_THRESHOLD
——————————————————————————————boolfalseLINE_BREAK
Alpha HIGH Boundaryint19RMI_ALPHA_HIGH_BNDR
Beta HIGH Boundaryint18RMI_BETA_HIGH_BNDR
Gamma HIGH Boundaryint17RMI_GAMMA_HIGH_BNDR
RMI Nuetrality widthint27RMI_NEUTRALITY
Gamma LOW Boundaryint-17RMI_GAMMA_LOW_BNDR
Beta LOW Boundaryint-18RMI_BETA_LOW_BNDR
Alpha LOW Boundaryint-19RMI_ALPHA_LOW_BNDR
Strategy Type —————————————————————stringMarginSTRATEGY
Pyramidingint1PYRAMIDING
Delay Between Entriesint69DELAY_BETWEEN_ENTRIES
Comission value %float0.075COMISSION_VALUE
Take Profit %float6TAKEPROFIT
Stop Loss %float6STOPLOSS
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

330 stars

MACD + Divergences [Alorse] is a momentum strategy that calculates the MACD oscillator and identifies regular and hidden divergences using pivot high and low points. It filters trade signals with dual Exponential Moving Averages (EMA 150 and 600) to ensure tra

Strategy v4 Oscillators & momentumDivergence Repaints
Updated 2 months ago · None stated GitHub repository of MACD + Divergences [Alorse]

MACD Crossover (tutorial)

by pineforge-4pass
193 stars

This strategy calculates the Moving Average Convergence Divergence indicator and plots the MACD line, signal line, and histogram in a separate pane. It enters long positions when the MACD line crosses above the signal line and enters short positions when it cr

Strategy v6 Oscillators & momentumMoving averages
Updated today · Apache-2.0 GitHub repository of MACD Crossover (tutorial)

ADX Strategy

by hasnocool
106 stars

An ADX-based trend-following strategy that enters long positions when the positive directional indicator (DI+) crosses above the Average Directional Index (ADX) while ADX remains below a preset threshold and a fast EMA is above a slow EMA. It allows adding to

Strategy v4 TrendOscillators & momentum
Updated 2 years ago · GPL-3.0 GitHub repository of ADX Strategy

EMA Ribbon MACD

by iamrichardD
57 stars

This strategy combines an EMA ribbon with MACD confirmation and plots in a separate pane. It enters when ribbon alignment, optional MACD zero or signal crosses, volume, smart-money, and multi-timeframe filters agree, then exits with percentage stop loss and ta

Strategy v6 TrendMoving averages
Updated 1 year ago · Apache-2.0 GitHub repository of EMA Ribbon MACD

This strategy evaluates the dynamic relationship between price and a chosen moving average across six diagnostic dimensions: trend, respect, extension, compression, acceleration, and exhaustion. It plots a normalized relationship oscillator bounded between -10

Strategy v6 TrendMoving averages
Updated yesterday · MIT GitHub repository of Market–Average Relationship Engine [WavesUnchained]
7 stars

This strategy combines the Moving Average Convergence Divergence oscillator with an Exponential Moving Average trend filter. It enters long positions when the MACD histogram crosses above zero while price stays above the long-term EMA filter. Long trades close

Strategy v5 TrendOscillators & momentum
Updated 1 year ago · MPL-2.0 GitHub repository of MACD EMA Filter Strategy

More strategys

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers