The Strategy - Ichimoku Kinko Hyo and more
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=5

strategy(title='The Strategy - Ichimoku Kinko Hyo and more', shorttitle='Strategy', overlay=true, initial_capital=10000, pyramiding=1, default_qty_type=strategy.fixed, default_qty_value=0.05, currency=currency.USD, 
             commission_type=strategy.commission.percent, commission_value=0.075, margin_long=0, margin_short=0)


//-------------------------------------------------------------------- inputs ---------------------------------------------------------------

general_info        = input.bool(title='General Description', defval=false, 
                     tooltip='This script offers you the opportunity to backtest many signals and filters to compare the results.  Make yourself familiar with the signals and the options and become creative ...  
                     At least you will be able to see what and on what circumstances works or not. Note the \'KISS\' principle and that \'context\' matters.  
                     The key feature of technical analysis is that every market participant is an interface to the world or market-environment and all market relevant information is visible in the chart.  
                     The default values are suitable for Bitcoin on ByBit (one tick = 0.5). Deselect \'Signal Labels\' at strategy properties -> Style to avoid signal label strings.')

entry_type          = input.string('Both', title='Position Entry-Direction', options=['Both', 'Long', 'Short'], 
                     tooltip='Long= long entries only, Short= short entries only, Both= long and short entries are possible.')

repaint             = input.string('Allowed', title='Repainting', options=['Not allowed', 'Allowed'], 
                     tooltip='If repainting "Not allowed" is selected, then signals from higher time frames have a lag of an additional bar from the higher time frame but you trade what you have backtested.  
                     Otherwise singals can occure multiple times as long as the higher time frame bar is not closed but the first signal of these possible multiple signals is much earlier.')

panel               = input.bool(true, title='Plot Info Panel', tooltip='Info panel for current unrealized profit or loss for the open position')

plot_avg_price      = input.bool(true, title='Plot Position Avarage Price Line.', 
                     tooltip='This plots the position avarage price line.')

one_side_pyr        = input.bool(title='Pyramiding Mode', defval=false, 
                     tooltip='If disabled: as long as the signal is valid a new trade can be opened on each bar (more filter sensitive). If not \'Both\' as direction is selected,  
                     the opposite signal closes the current position. If enabled: a new trade is opened with every signal. Use Properties -> Pyramiding to increase the maximum amount of orders.')



htf_entr_opt_1      = input.string('Current', title='Time Frame - 1st ENTRY SIGNAL', options=['Current', '5m', '10m', '15m', '30m', '1H', '2H', '3H', '4H', '6H', '12H', 'D', '3D', 'W', 'M'], 
                     tooltip='Choose always a higher time frame than the current. Lower time frames than the current may result in false values.', group='entry signals:')

htf_entr_opt_2      = input.string('Current', title='Time Frame - 2nd ENTRY SIGNAL', options=['Current', '5m', '10m', '15m', '30m', '1H', '2H', '3H', '4H', '6H', '12H', 'D', '3D', 'W', 'M'], 
                     tooltip='Choose always a higher time frame than the current. Lower time frames than the current may result in false values.', group='entry signals:')

X_opt               = input.string('Price X Kumo sig', title='--- 1st ENTRY SIGNAL ---', options=['---', 'Fractals trend lines sig (no tf filter)', 'Segments sig (no tf filter)', 
                     'DMI ADX classic sig', 'DMI ADX mod sig', 'DMI ADX-slope sig', 'EMA1 x EMA2 sig', 'TRIX slope sig', 'ALMA slope sig', 'MACD(fast) slope sig', 'HMA slope sig', 
                     'Grid - reentry sig (no tf filter)', 'Grid - counter trend sig (no tf filter)', 'Pin bar sig', 'Inside bar sig', 'Outside bar sig', 'Sandwich bar sig', 'Bar breakout 1 sig', 'Bar breakout 2 sig', 
                     'Higher-low/lower-high bar sig', 'Entry on every bar-open sig', 'Fractals sig', 'Reverse fractal sig', 'SMA sig', 
                     'RSI50 sig', 'Parabolic SAR sig', 'SuperTrend sig', 'Price X Kijun sig', 'Price X Kumo sig', 'Kumo flip sig', 
                     'Chikou X price sig', 'Chikou X Kumo sig', 'Price X Tenkan sig', 'Tenkan X Kumo sig', 'Tenkan X Kijun sig', 'CB/CS sig (no tf filter)', 'IB/IS sig (no tf filter)', 'B1/S1 sig', 'B2/S2 sig'], 
                     tooltip='Various entry signals. 1st ENTRY SIGNAL and 2nd ENTRY SIGNAL are OR connected.', group='entry signals:')

X_opt_2             = input.string('---', title='--- 2nd ENTRY SIGNAL --- (Enable Pyramiding Mode)', options=['---', 'Fractals trend lines sig (no tf filter)', 'Segments sig (no tf filter)', 
                     'DMI ADX classic sig', 'DMI ADX mod sig', 'DMI ADX-slope sig', 'EMA1 x EMA2 sig', 'TRIX slope sig', 'ALMA slope sig', 'MACD(fast) slope sig', 'HMA slope sig',  
                     'Grid - reentry sig (no tf filter)', 'Grid - counter trend sig (no tf filter)', 'Pin bar sig', 'Inside bar sig', 'Outside bar sig', 'Sandwich bar sig', 'Bar breakout 1 sig', 'Bar breakout 2 sig', 
                     'Higher-low/lower-high bar sig', 'Entry on every bar-open sig', 'Fractals sig', 'Reverse fractal sig', 'SMA sig', 
                     'RSI50 sig', 'Parabolic SAR sig', 'SuperTrend sig', 'Price X Kijun sig', 'Price X Kumo sig', 'Kumo flip sig', 
                     'Chikou X price sig', 'Chikou X Kumo sig', 'Price X Tenkan sig', 'Tenkan X Kumo sig', 'Tenkan X Kijun sig', 'CB/CS sig (no tf filter)', 'IB/IS sig (no tf filter)', 'B1/S1 sig', 'B2/S2 sig'], 
                     tooltip='Various entry signals. Signal 1 and signal 2 are OR connected. Works with pyramiding mode only', group='entry signals:')


htf_filt_opt_1      = input.string('Current', title='Time Frame - Entry Filter 1', options=['Current', '5m', '10m', '15m', '30m', '1H', '2H', '3H', '4H', '6H', '12H', 'D', '3D', 'W', 'M'], 
                     tooltip='The time frame for the 1st ENTRY SIGNAL filter. Choose always a higher time frame than the current. Lower time frames than the current may result in false values.', group='Entry filters:')

htf_filt_opt_2      = input.string('Current', title='Time Frame - Entry Filter 2', options=['Current', '5m', '10m', '15m', '30m', '1H', '2H', '3H', '4H', '6H', '12H', 'D', '3D', 'W', 'M'], 
                     tooltip='The time frame for the 2st ENTRY SIGNAL filter. Choose always a higher time frame than the current. Lower time frames than the current may result in false values.', group='Entry filters:')

entry_f_1           = input.string('---', title='Entry Filter 1', options=['---', 'Fractals trend lines filter (no tf filter)', 'Bar breakout 1 filter', 'Bar breakout 2 filter', 'SMA filter', 'MACD filter', 
                     'EMA1 x EMA2 filter', 'RSI Stochastic filter', 'TRIX slope filter', 'ALMA slope filter','HMA slope filter',  
                     'MACD(fast) slope filter', 'RSI50 filter', 'Segments filter (no tf filter)', 'Reverse fractal filter', 'Fractals filter', 
                     'SuperTrend filter', 'Parabolic SAR filter', 'ADX Threshold filter', 'DMI filter', 'Price X Kumo filter', 'Price X Kijun filter', 'Kumo flip filter', 'Price filtered Kumo flip filter (no tf filter)', 
                     'Chikou X price filter', 'Chikou X Kumo filter', 'Price X Tenkan filter', 'Tenkan X Kumo filter', 'Tenkan X Kijun filter', 'B1/S1 sig', 'B2/S2 sig', 'IB/IS sig (no tf filter)'], group='Entry filters:', 
                     tooltip='Various entry filter signals. Entry filter 1 and Entry filter 2 are AND connected.')

entry_f_2           = input.string('---', title='Entry Filter 2', options=['---', 'Fractals trend lines filter (no tf filter)', 'Bar breakout 1 filter', 'Bar breakout 2 filter', 'SMA filter', 'MACD filter', 
                     'EMA1 x EMA2 filter', 'RSI Stochastic filter', 'TRIX slope filter', 'ALMA slope filter','HMA slope filter',  
                     'MACD(fast) slope filter', 'RSI50 filter', 'Segments filter (no tf filter)', 'Reverse fractal filter', 'Fractals filter',
                     'SuperTrend filter', 'Parabolic SAR filter', 'ADX Threshold filter', 'DMI filter', 'Price X Kumo filter', 'Price X Kijun filter', 'Kumo flip filter', 'Price filtered Kumo flip filter (no tf filter)', 
                     'Chikou X price filter', 'Chikou X Kumo filter', 'Price X Tenkan filter', 'Tenkan X Kumo filter', 'Tenkan X Kijun filter', 'B1/S1 sig', 'B2/S2 sig', 'IB/IS sig (no tf filter)'], group='Entry filters:', 
                     tooltip='Various entry filter signals. Filter 1 and filter 2 are AND connected.')


htf_exit_opt_1      = input.string('Current', title='Time Frame - Exit Filter 1', options=['Current', '5m', '10m', '15m', '30m', '1H', '2H', '3H', '4H', '6H', '12H', 'D', '3D', 'W', 'M'], 
                     tooltip='Choose always a higher time frame than the current. Lower time frames than the current may result in false values.', group='exit filters:')

htf_exit_opt_2      = input.string('Current', title='Time Frame - Exit Filter 2', options=['Current', '5m', '10m', '15m', '30m', '1H', '2H', '3H', '4H', '6H', '12H', 'D', '3D', 'W', 'M'], 
                     tooltip='Choose always a higher time frame than the current. Lower time frames than the current may result in false values.', group='exit filters:')

exit_f_1            = input.string('---', title='Exit Filter 1', options=['---', 'Reverse bar exit', 'Reverse fractal exit', 'Bar breakout 2 exit', 'SMA exit', 'MACD exit', 'MACD(fast) slope exit',  'RSI50 exit',
                      'RSI Stochastic exit', 'TRIX slope exit',
                      'HMA slope exit', 'EMA1 x EMA2 exit', 'Fractals exit', 'SuperTrend exit', 'Parabolic SAR exit', 'ADX Threshold exit', 'Cloud exit', 'Kijun exit', 'IB/IS exit (no tf filter)'], group='exit filters:', 
                     tooltip='Some exit filter signals. Exit filter 1 and Exit filter 2 are OR connected.')

exit_f_2            = input.string('---', title='Exit Filter 2', options=['---', 'Reverse bar exit', 'Reverse fractal exit', 'Bar breakout 2 exit', 'SMA exit', 'MACD exit', 'MACD(fast) slope exit', 'RSI50 exit',
                      'RSI Stochastic exit', 'TRIX slope exit',
                      'HMA slope exit', 'EMA1 x EMA2 exit', 'Fractals exit', 'SuperTrend exit', 'Parabolic SAR exit', 'ADX Threshold exit', 'Cloud exit', 'Kijun exit', 'IB/IS exit (no tf filter)'], group='exit filters:', 
                     tooltip='Some exit filter signals. Exit filter 1 and Exit filter 2 are OR connected.')


//------------------------ lot size for live trading --------------------------

//Alertatron lot size on Bybit
lot_size            = input.int(300, title='Lot Size', minval=1, step=100, 
                     tooltip='Alertatron lot size string for Bybit exchange. Affects live trading only.', group='Live trading Parameter (does not affect backtesting):')

grid_gap            = input.float(500, title='Grid Gap - Base Currency', minval=1, step=50, group='Grid parameter:', tooltip='The minimum trigger-gap between two trades in case of a selected grid signal.')


//------------------------ Backtest periode inputs ----------------------------

period_start        = input.time(title='', inline='start_timestamp', defval=timestamp('13 Mar 2020 00:00 +0000'), group='Define the backtest period or start of trend:', tooltip='Backtest period start.')

period_stop         = input.time(title='', inline='end_timestamp', defval=timestamp('31 Dec 2120 00:00 +0000'), group='Define the backtest period or start of trend:', tooltip='Backtest period end.')


//---------------------- take profit inputs -----------------------------

av_tp_en            = input.bool(title='Enable take Profit - Average Position Price Profit', defval=true, group='Take profit based on average position price:', tooltip='Profit taking condition: 
                     current price >= average position price of positions + ATR')
av_tp_qty           = input.float(10, title='Take Average Position Price Profit - Quantity Of Position (Percent)', minval=1, step=5, maxval=100, group='Take profit based on average position price:', 
                     tooltip='Reduction of the current position in percent.')

atr_l               = input.int(50, title='ATR Length', minval=0, step=5, group='Take profit based on average position price:', 
                     tooltip='ATR length for profit and step calculation')

atr_tf              = input.string('W', title='ATR Time Frame', options=['60', '120', '240', '480', '960', 'D', '3D', 'W', 'M'], group='Take profit based on average position price:', 
                     tooltip='ATR time frame for profit and step calculation')

atr_fact            = input.float(1, title='ATR Factor', minval=0, step=0.1, group='Take profit based on average position price:', 
                     tooltip='ATR factor to increase/decrease ATR for profit and step calculation')


//---------------------- stop loss inputs --------------------------

sl_en               = input.bool(title='Enable Stop Loss - Average Position Price Loss', defval=true, group='Stop loss based on average position price:', 
                     tooltip='Stop loss condition: current price <= average position price of positions - \'Stop average-entry loss...\' AND current price <= average position price of positions - 
                     \'Stop loss step...\'')

av_sl_qty           = input.float(50, title='Stop Average Position Price Loss - Quantity Of Position (Percent)', minval=0, step=5, maxval=100, group='Stop loss based on average position price:', 
                     tooltip='Reduction of the current position in percent.')

atr_l_l             = input.int(50, title='ATR Length', minval=0, step=5, group='Stop loss based on average position price:', tooltip='ATR length for loss and step calculation')

atr_tf_l            = input.string('W', title='ATR Time Frame', options=['60', '120', '240', '480', '960', 'D', '3D', 'W', 'M'], group='Stop loss based on average position price:', 
                     tooltip='ATR time frame for loss and step calculation')

atr_fact_l          = input.float(1, title='ATR Factor', minval=0, step=0.1, group='Stop loss based on average position price:', tooltip='ATR factor to increase/decrease ATR for loss and step calculation')


//------------------------ Signal inputs CB/CS and IB/IS ---------------------------

trig_gap_cbcs       = input.float(2, title='CB/CS Signal Offset', minval=-6, maxval=9, step=1, group='Confluence signal offsets:', tooltip='Decreasing this parameter increases the sensitivity of the signal.')
trig_gap_ibis       = input.float(0, title='IB/IS Signal Offset', minval=-5, maxval=4, step=1, group='Confluence signal offsets:', tooltip='Decreasing this parameter increases the sensitivity of the signal.')


//----------------------- entry signals and filters -----------------------

sb                  = input.int(10, title='Segment Max Bars', minval=0, step=1, group='Shared filter and entry signal parameters:')

p_bar_sens_1        = input.float(0.6, title='Pin Bar Sensitivity 1', step=0.02, tooltip='Condition: candle wick > candle body * \'Pin bar sensitivity\' .  
                     The smaller the factor, the more wicks are detected as part of a pin bar.', group='Shared filter and entry signal parameters:')

p_bar_sens_2        = input.int(1, title='Pin Bar Sensitivity 2', step=1, minval=1, tooltip='Condition: high/low >< last two high/low\'.', group='Shared filter and entry signal parameters:')

fr_period           = input.int(2, title='Fractals Period', minval=1, group='Shared filter and entry signal parameters:')

rsi_period          = input.int(14, title='RSI Period', minval=1, group='Shared filter and entry signal parameters:')

ma_period           = input.int(50, title='SMA Period', minval=1, step=5, group='Shared filter and entry signal parameters:')

mult                = input.float(3, title='SuperTrend Multiplier', minval=1, step=0.2, group='Shared filter and entry signal parameters:')
len                 = input.int(6, title='SuperTrend Length', minval=1, group='Shared filter and entry signal parameters:')

start               = 0.02  //input(0.02, title= "PSAR Start (Filter/Entry)", minval= 0)
inc                 = 0.02  //input(0.02, title= "PSAR Increment (Filter/Entry)", minval= 0)
max                 = 0.2  //input(.2, title= "PSAR Maximum (Filter/Entry)", minval= 0)

di_length_s         = input.int(10, title='ADX DI Length', minval=1, group='Shared filter and entry signal parameters:')
adx_smooth_s        = input.int(10, title='ADX Smooth', minval=1, group='Shared filter and entry signal parameters:')
adx_thres_s         = input.int(25, title='ADX Threshold', step=2, minval=1, group='Shared filter and entry signal parameters:')

windowsize          = input.int(9, title="ALMA Window Size", minval=1, step=1, group='Shared filter and entry signal parameters:', tooltip='')
offset              = input.float(0.85, title="ALMA Offset", minval=0, step=0.05, group='Shared filter and entry signal parameters:', tooltip='')
sigma               = input.float(6, title="ALMA Sigma", minval=1, step=1, group='Shared filter and entry signal parameters:', tooltip='')

slope_len           = input.int(1, minval=1, title='MACD MacdlLine Slope Lenth', tooltip='MACD\'s fast line', group='Shared filter and entry signal parameters:')

hma_len_f           = input.int(100, minval=1, step=5, title='HMA Length', group='Shared filter and entry signal parameters:')

ema1_len_f          = input.int(10, minval=1, step=2, title='EMA1 Length', group='Shared filter and entry signal parameters:')
ema2_len_f          = input.int(20, minval=1, step=2, title='EMA2 Length', group='Shared filter and entry signal parameters:')

k_smoo_f            = input.int(3, title='RSI-Stoch K-line', minval=1, tooltip='RSI Stochastic\'s fast line', group='Shared filter and entry signal parameters:')
d_smoo_f            = input.int(3, title='RSI-Stoch D-line', minval=1, tooltip='RSI Stochastic\'s slow line', group='Shared filter and entry signal parameters:')
stoch_length_f      = input.int(14, title='RSI-Stoch Stochastic Length', minval=1, group='Shared filter and entry signal parameters:')
rsi_length_sto_f    = input.int(14, title='RSI-Stoch RSI Length', minval=1, group='Shared filter and entry signal parameters:')

trix_len_f          = input.int(10, title="TRIX Length", minval=1, group='Shared filter and entry signal parameters:')


// exit filters
fr_period_x         = input.int(2, title='Exit Fractals - Period', minval=1, group='Exit filter Parameters:')
fr_past_x           = input.int(0, title='Exit Fractals - Past Fractal', minval=0, group='Exit filter Parameters:')

rsi_period_x        = input.int(14, title='Exit RSI Period', minval=1, group='Exit filter Parameters:')

ma_period_x         = input.int(50, title='Exit SMA Period', step=5, minval=1, group='Exit filter Parameters:')

mult_x              = input.float(2, title='Exit SuperTrend Multiplier', minval=1, group='Exit filter Parameters:')
len_x               = input.int(5, title='Exit SuperTrend Length', minval=1, group='Exit filter Parameters:')

di_length_x         = input.int(10, title='Exit ADX Period', minval=1, group='Exit filter Parameters:')
adx_smooth_x        = input.int(10, title='Exit ADX Smooth', minval=1, group='Exit filter Parameters:')
adx_thres_x         = input.int(25, title='Exit ADX Threshold', step=2, minval=1, group='Exit filter Parameters:')

slope_len_x         = input.int(1, title='Exit MACD MacdlLine Slope Lenth', minval=1, tooltip='MACD\'s fast line', group='Exit filter Parameters:')

hma_len_x           = input.int(100, minval=1, step=5, title='Exit HMA Length', group='Exit filter Parameters:')

ema1_len_x          = input.int(10, title='Exit EMA1 Length', minval=1, step=2, group='Exit filter Parameters:')
ema2_len_x          = input.int(20, title='Exit EMA2 Length', minval=1, step=2, group='Exit filter Parameters:')

k_smoo_x            = input.int(3, title='Exit RSI-Stoch K-line', minval=1, tooltip='RSI Stochastic\'s fast line', group='Exit filter Parameters:')
d_smoo_x            = input.int(3, title='Exit RSI-Stoch D-line', minval=1, tooltip='RSI Stochastic\'s slow line', group='Exit filter Parameters:')
stoch_length_x      = input.int(14, title='Exit RSI-Stoch Stochastic Length', minval=1, group='Exit filter Parameters:')
rsi_length_sto_x    = input.int(14, title='Exit RSI-Stoch RSI Length', minval=1, group='Exit filter Parameters:')

trix_len_x          = input.int(10, title="Exit TRIX Length", minval=1, group='Exit filter Parameters:')


//--------------- Current unrealized profit or loss for the open position -------------------

if panel
    var info_panel = table.new(position = position.bottom_left, columns = 1, rows = 1, bgcolor=color.new(color.blue, 92), frame_width=1, border_width=1)

    text1   = "Unrealized Profit/Loss:\n" + str.tostring(strategy.openprofit, "#.00")

    table.cell(table_id=info_panel, column=0, row=0, text=text1, text_size= size.normal, text_color=color.new(color.silver, 0))


//----------------------- Backtest periode --------------------------------

backtest_period() =>
    time >= period_start and time <= period_stop ? true : false


//-------------------- Ichimoku --------------------

TKlength            = 9  //input(9, "Tenkan-sen length", minval= 1)
KJlength            = 26  //input(26, "Kijun-sen length", minval= 1)
CSHSlength          = 26  //input(26, "Chikouspan length/horizontal shift", minval= 1)
SBlength            = 52  //input(52, "SenkouspanB length", minval= 1)
SAlength            = 26  //input(26, "SenkouspanA length", minval= 1)

// calculation
TK                  = math.avg(ta.lowest(TKlength), ta.highest(TKlength))
KJ                  = math.avg(ta.lowest(KJlength), ta.highest(KJlength))
CS                  = close
SB                  = math.avg(ta.lowest(SBlength), ta.highest(SBlength))
SA                  = math.avg(TK, KJ)

kumo_high           = math.max(SA[CSHSlength - 1], SB[CSHSlength - 1])
kumo_low            = math.min(SA[CSHSlength - 1], SB[CSHSlength - 1])


//--------------------------------------------------------------------------------- Filters and entry signals -----------------------------------------------------------------------------------

//---------------------- Ichimoku filter ------------------------

var bool tkkj_x = true
if ta.crossover(TK, KJ) and TK > kumo_high and KJ > kumo_high
    tkkj_x := true
    tkkj_x

if ta.crossunder(TK, KJ) and TK < kumo_low and KJ < kumo_low
    tkkj_x := false
    tkkj_x

//Ichimoku entry signals
kijun_buy                           = one_side_pyr ? ta.crossover(close, KJ) : close > KJ
kumo_buy                            = one_side_pyr ? ta.crossover(close, kumo_high) : close > kumo_high
kumo_flip_buy                       = one_side_pyr ? ta.crossover(SA, SB) : SA > SB
chikou_X_price_buy                  = one_side_pyr ? ta.crossover(CS, high[26 - 1]) : CS > high[26 - 1]
chikou_X_kumo_buy                   = one_side_pyr ? ta.crossover(CS, kumo_high[26 - 1]) : CS > kumo_high[26 - 1]
price_X_tenkan_buy                  = one_side_pyr ? ta.crossover(close, TK) : close > TK
tenkan_X_kumo_buy                   = one_side_pyr ? ta.crossover(TK, kumo_high) : TK > kumo_high
tenkan_X_kijun_buy                  = one_side_pyr ? ta.crossover(TK, KJ) : TK > KJ


kijun_sell                          = one_side_pyr ? ta.crossunder(close, KJ) : close < KJ
kumo_sell                           = one_side_pyr ? ta.crossunder(close, kumo_low) : close < kumo_low
kumo_flip_sell                      = one_side_pyr ? ta.crossunder(SA, SB) : SA < SB
chikou_X_price_sell                 = one_side_pyr ? ta.crossunder(CS, low[26 - 1]) : CS < low[26 - 1]
chikou_X_kumo_sell                  = one_side_pyr ? ta.crossunder(CS, kumo_low[26 - 1]) : CS < kumo_low[26 - 1]
price_X_tenkan_sell                 = one_side_pyr ? ta.crossunder(close, TK) : close < TK
tenkan_X_kumo_sell                  = one_side_pyr ? ta.crossunder(TK, kumo_low) : TK < kumo_low
tenkan_X_kijun_sell                 = one_side_pyr ? ta.crossunder(TK, KJ) : TK < KJ


// Ichimoku filters
kijun_buy_f                         = close > KJ
kumo_buy_f                          = close > kumo_high
kumo_flip_buy_f                     = SA > SB
chikou_X_price_buy_f                = CS > high[26 - 1]
chikou_X_kumo_buy_f                 = CS > kumo_high[26 - 1]
price_X_tenkan_buy_f                = close > TK
tenkan_X_kumo_buy_f                 = TK > kumo_high
tenkan_X_kijun_buy_f                = TK > KJ
kumo_filtered_tenkan_X_kijun_buy_f  = tkkj_x and TK > kumo_high and KJ > kumo_high and TK > KJ


kijun_sell_f                        = close < KJ
kumo_sell_f                         = close < kumo_low
kumo_flip_sell_f                    = SA < SB
chikou_X_price_sell_f               = CS < low[26 - 1]
chikou_X_kumo_sell_f                = CS < kumo_low[26 - 1]
price_X_tenkan_sell_f               = close < TK
tenkan_X_kumo_sell_f                = TK < kumo_low
tenkan_X_kijun_sell_f               = TK < KJ
kumo_filtered_tenkan_X_kijun_sell_f = not tkkj_x and TK < kumo_low and KJ < kumo_low and TK < KJ

// Ichimoku exits
kijun_buy_x                         = ta.crossover(high, KJ)
kijun_sell_x                        = ta.crossunder(low, KJ)

kumo_buy_x                          = ta.crossover(high, kumo_high)
kumo_sell_x                         = ta.crossunder(low, kumo_low)


//----------------------- bar signals -----------------------
//entry signal
bar_sig_1_buy                       = close > high[1]                           // bar breakout signal 1
bar_sig_1_sell                      = close < low[1]
//filter
bar_sig_1_buy_f                     = close > high[1]                           // bar breakout signal 1 - filter
bar_sig_1_sell_f                    = close < low[1]
//entry
bar_sig_2_buy                       = high > high[1]                            // bar breakout signal 2
bar_sig_2_sell                      = low < low[1]
//filter
bar_sig_2_buy_f                     = high > high[1]                            // bar breakout signal 2 - filter
bar_sig_2_sell_f                    = low < low[1]

rev_bar_sig_buy                     = close > open                              // reverse bar exit
rev_bar_sig_sell                    = close < open

hllh_buy                            = low > low[1]                              // higher-low/lower-high bar sig
hllh_sell                           = high < high[1]

open_buy                            = close != 0                                // entry on every open bar sig
open_sell                           = close != 0


//-------------------------- trix --------------------------

f_trix(_trix_len)                   => 10000 * ta.change(ta.ema(ta.ema(ta.ema(math.log(close), _trix_len), _trix_len), _trix_len))

trix_f                              = f_trix(trix_len_f)
trix_x                              = f_trix(trix_len_x)

trix_buy_s                          = ta.rising(trix_f, 1) and not ta.rising(trix_f, 1)[1] //one_side_pyr ? ta.rising(trix_f, 1) and not ta.rising(trix_f, 1)[1] : ta.rising(trix_f, 1) 
trix_sell_s                         = ta.falling(trix_f, 1) and not ta.falling(trix_f, 1)[1] //one_side_pyr ? ta.falling(trix_f, 1) and not ta.falling(trix_f, 1)[1] : ta.falling(trix_f, 1) 

trix_slo_up_f                       = ta.rising(trix_f, 1) 

trix_slo_up_x                       = ta.rising(trix_x, 1) and not ta.rising(trix_x, 1)[1]
trix_slo_dn_x                       = ta.falling(trix_x, 1) and not ta.falling(trix_x, 1)[1]


//----------------------- rsi stochastic ----------------------------

//filter
rsi_rs_f                            = ta.rsi(close, rsi_length_sto_f)

k_f                                 = ta.sma(ta.stoch(rsi_rs_f, rsi_rs_f, rsi_rs_f, stoch_length_f), k_smoo_f)
d_f                                 = ta.sma(k_f, d_smoo_f)

//signal
kd_mom_up_f                         = k_f > 20 and d_f > 20 and not(k_f < d_f and k_f < 80 and d_f < 80)
kd_mom_dn_f                         = k_f < 80 and d_f < 80 and not(k_f > d_f and k_f > 80 and d_f > 80)


//exit
rsi_rs_x                            = ta.rsi(close, rsi_length_sto_x)

k_x                                 = ta.sma(ta.stoch(rsi_rs_x, rsi_rs_x, rsi_rs_x, stoch_length_x), k_smoo_x)
d_x                                 = ta.sma(k_x, d_smoo_x)

//signal
kd_mom_up_x                         = k_x > 20 and d_x > 20 and not(k_x < d_x and k_x < 80 and d_x < 80)
kd_mom_dn_x                         = k_x < 80 and d_x < 80 and not(k_x > d_x and k_x > 80 and d_x > 80)




//------------------------- hma ----------------------------

f_hma(_hma_len)                     => ta.hma(close, _hma_len)  


hma_f                               = f_hma(hma_len_f)                                                                                      // entry filter
hma_x                               = f_hma(hma_len_x)                                                                                      // exit 


hma_up_e                            = ta.rising(hma_f, 1) and not ta.rising(hma_f, 1)[1] //one_side_pyr ? ta.rising(hma_f, 1) and not ta.rising(hma_f, 1)[1] : ta.rising(hma_f, 1)          // entry signal
hma_dn_e                            = ta.falling(hma_f, 1) and not ta.falling(hma_f, 1)[1] //one_side_pyr ? ta.falling(hma_f, 1) and not ta.falling(hma_f, 1)[1] : ta.falling(hma_f, 1) 

hma_up_f                            = ta.rising(hma_f, 1)              // entry filter

hma_up_x                            = ta.rising(hma_x, 1) and not ta.rising(hma_x, 1)[1]                                                    // exit 
hma_dn_x                            = ta.falling(hma_x, 1) and not ta.falling(hma_x, 1)[1] 


//------------------------ grid --------------------------

re_grid = 0.
re_grid := nz(high > re_grid[1] + grid_gap or low < re_grid[1] - grid_gap ? close : re_grid[1])


grid_ct_buy         = re_grid < re_grid[1]
grid_ct_sell        = re_grid > re_grid[1]

grid_re_buy         = re_grid > re_grid[1]
grid_re_sell        = re_grid < re_grid[1]

//plot(re_grid,"Plot", color= color.yellow, linewidth= 2)


//---------------------- reverse fractal signal and filter --------------------------

up_bar      = close[0] > open[0]
dn_bar      = close[0] < open[0]
hl          = low[0] > low[1]
lh          = high[0] < high[1]

rev_up_fr_sell = ta.pivothigh(high, 3, 0) and dn_bar and up_bar[1] or ta.pivothigh(high, 4, 1) and dn_bar and up_bar[1] or ta.pivothigh(high, 4, 1) and lh and up_bar and up_bar[1]

rev_dn_fr_buy = ta.pivotlow(low, 3, 0) and up_bar and dn_bar[1] or ta.pivotlow(low, 4, 1) and up_bar and dn_bar[1] or ta.pivotlow(low, 4, 1) and hl and dn_bar and dn_bar[1]


//------------------------- ema1 x ema2 ------------------------

f_ema(_ema_len) =>  ta.ema(close, _ema_len)                     // ma function definition

f_sma(_sma_len) =>  ta.sma(close, _sma_len)                     // ma function definition


ema1_f                              = f_ema(ema1_len_f)
ema2_f                              = f_ema(ema2_len_f)

ema1_x                              = f_ema(ema1_len_x)
ema2_x                              = f_ema(ema2_len_x)

ema_1x2_buy_s                       = ta.crossover(ema1_f, ema2_f)
ema_1x2_sell_s                      = ta.crossunder(ema1_f, ema2_f)

ema_1x2_buy_f                       = ema1_f > ema2_f

ema_1x2_buy_x                       = ta.crossover(ema1_x, ema2_x)
ema_1x2_sell_x                      = ta.crossunder(ema1_x, ema2_x)


//--------------------- ALMA ------------------------

alma                                = ta.alma(close, windowsize, offset, sigma)

//entry signals
alma_slo_up_e                       = ta.rising(alma, 1) and not ta.rising(alma, 1)[1] //one_side_pyr ? ta.rising(alma, 1) and not ta.rising(alma, 1)[1] : ta.rising(alma, 1) 
alma_slo_dn_e                       = ta.falling(alma, 1) and not ta.falling(alma, 1)[1] //one_side_pyr ? ta.falling(alma, 1) and not ta.falling(alma, 1)[1] : ta.falling(alma, 1) 

//filters
alma_slo_up_f                       = ta.rising(alma, 1)  


//----------------------- macd filter and macd slope-----------------------

[macdLine_f, signalLine_f, histLine_f]  = ta.macd(close, 12, 26, 9)
//filters
macd_buy                                = macdLine_f > signalLine_f
macd_sell                               = macdLine_f < signalLine_f

//exit
macd_buy_x                              = ta.crossover(macdLine_f, signalLine_f)
macd_sell_x                             = ta.crossunder(macdLine_f, signalLine_f)

//macd fast line slope
macd_slope_up_e                         = ta.rising(macdLine_f, slope_len) and not ta.rising(macdLine_f, slope_len)[1] 
                                        //one_side_pyr ? ta.rising(macdLine_f, slope_len) and not ta.rising(macdLine_f, slope_len)[1] : ta.rising(macdLine_f, slope_len)                    // entry
macd_slope_dn_e                         = ta.falling(macdLine_f, slope_len) and not ta.falling(macdLine_f, slope_len)[1] 
                                        //one_side_pyr ? ta.falling(macdLine_f, slope_len) and not ta.falling(macdLine_f, slope_len)[1] : ta.falling(macdLine_f, slope_len)

macd_slope_up_f                         = ta.rising(macdLine_f, slope_len)                                                                                                                  // filter

macd_slope_up_x                         = ta.rising(macdLine_f, slope_len_x) and not ta.rising(macdLine_f, slope_len_x)[1]                                                                  // exit
macd_slope_dn_x                         = ta.falling(macdLine_f, slope_len_x) and not ta.falling(macdLine_f, slope_len_x)[1]


//---------------------- rsi filter and entry signal------------------------

f_rsi(_rsi_period)           => ta.rsi(close, _rsi_period)

//entry
rsi_f           = f_rsi(rsi_period)
rsi_f_buy       = one_side_pyr ? ta.crossover(rsi_f, 50) : rsi_f > 50
rsi_f_sell      = one_side_pyr ? ta.crossunder(rsi_f, 50) : rsi_f < 50

//filters
rsi_f_buy_f     = rsi_f > 50
rsi_f_sell_f    = rsi_f < 50

//exit
rsi_f_x         = f_rsi(rsi_period_x)
rsi_f_buy_x     = ta.crossover(rsi_f_x, 50)
rsi_f_sell_x    = ta.crossunder(rsi_f_x, 50)


//---------------- Bill Williams Fractals (filter and entry signal) -----------------

up_fr           = ta.pivothigh(fr_period, fr_period)
dn_fr           = ta.pivotlow(fr_period, fr_period)

fractal_up_v    = ta.valuewhen(up_fr, high[fr_period], 0)
fractal_dn_v    = ta.valuewhen(dn_fr, low[fr_period], 0)

//entry signal
fr_upx          = one_side_pyr ? ta.crossover(high, fractal_up_v) : high > fractal_up_v 
fr_dnx          = one_side_pyr ? ta.crossunder(low, fractal_dn_v) : low < fractal_dn_v

//filters
fr_upx_f        = high > fractal_up_v
fr_dnx_f        = low < fractal_dn_v

//exit
up_fr_x         = ta.pivothigh(fr_period_x, fr_period_x)
dn_fr_x         = ta.pivotlow(fr_period_x, fr_period_x)

fractal_up_v_x  = ta.valuewhen(up_fr_x, high[fr_period_x], fr_past_x)
fractal_dn_v_x  = ta.valuewhen(dn_fr_x, low[fr_period_x], fr_past_x)

fr_upx_x        = ta.crossover(high, fractal_up_v_x)
fr_dnx_x        = ta.crossunder(low, fractal_dn_v_x)



//-------------------- SuperTrend filter and entry signal ---------------------
//entry
[SuperTrend, Dir]       = ta.supertrend(mult, len)
sup_buy                 = one_side_pyr ? ta.crossover(high, SuperTrend) : high > SuperTrend
sup_sell                = one_side_pyr ? ta.crossunder(low, SuperTrend) : low < SuperTrend

//filters
sup_buy_f               = high > SuperTrend
sup_sell_f              = low < SuperTrend

//exit
[SuperTrend_x, Dir_x]   = ta.supertrend(mult_x, len_x)
sup_buy_x               = ta.crossover(high, SuperTrend_x)
sup_sell_x              = ta.crossunder(low, SuperTrend_x)


//----------------- Parabolic SAR Signal (pb/ps) and filter -------------------

psar_buy                = one_side_pyr ? ta.crossover(high, ta.sar(start, inc, max)[0]) : high > ta.sar(start, inc, max)[0]
psar_sell               = one_side_pyr ? ta.crossunder(low, ta.sar(start, inc, max)[0]) : low < ta.sar(start, inc, max)[0]

//filters
psar_buy_f              = high > ta.sar(start, inc, max)[0]
psar_sell_f             = low < ta.sar(start, inc, max)[0]

psar_buy_x              = ta.crossover(high, ta.sar(start, inc, max)[0])
psar_sell_x             = ta.crossunder(low, ta.sar(start, inc, max)[0])


//-------------------------- DMI ADX entry sig and filter ---------------------------

//exit
[diplus_f_x, diminus_f_X, adx_f_x]      = ta.dmi(di_length_x, adx_smooth_x)
adx_thres_f_x                           = ta.crossunder(adx_f_x, adx_thres_x)


//dmi and adx signal 1/2/3 and filters
[diplus_s, diminus_s, adx_s]            = ta.dmi(di_length_s, adx_smooth_s)
adx_above_thres                         = adx_s > adx_thres_s
adx_slope_up                            = ta.rising(adx_s, 1)

//entry filter
long_e      = diplus_s > diminus_s

short_e     = diplus_s < diminus_s

//entry
long_1      = one_side_pyr ? ta.crossover(diplus_s, diminus_s) and adx_s < diplus_s and adx_s > diminus_s : diplus_s > diminus_s and adx_s < diplus_s and adx_s > diminus_s

short_1     = one_side_pyr ? ta.crossunder(diplus_s, diminus_s) and adx_s > diplus_s and adx_s < diminus_s : diplus_s < diminus_s and adx_s > diplus_s and adx_s < diminus_s


long_2      = one_side_pyr ? ta.crossover(diplus_s, diminus_s) and adx_s > adx_thres_s : diplus_s > diminus_s and adx_above_thres

short_2     = one_side_pyr ? ta.crossunder(diplus_s, diminus_s) and adx_s > adx_thres_s : diplus_s < diminus_s and adx_above_thres

//dmi adx-slope signal
adx_up_c    = adx_slope_up and diplus_s > diminus_s and not (adx_slope_up and diplus_s > diminus_s)[1]                      

adx_dn_c    = adx_slope_up and diplus_s < diminus_s and not (adx_slope_up and diplus_s < diminus_s)[1]


//-------------------------- SMA50 filter and entry---------------------------
//entry
sma_buy     = one_side_pyr ? ta.crossover(close[1], f_sma(ma_period)) : close[2] > f_sma(ma_period)
sma_sell    = one_side_pyr ? ta.crossunder(close[1], f_sma(ma_period)) : close[2] < f_sma(ma_period)

//filters
sma_buy_f   = close[2] > f_sma(ma_period)
sma_sell_f  = close[2] < f_sma(ma_period)

//exit
sma_buy_x   = ta.crossover(close[1], f_sma(ma_period_x))
sma_sell_x  = ta.crossunder(close[1], f_sma(ma_period_x))


//---------------- williams fractals trend lines -----------------

up_w_fr         = ta.pivothigh(2, 2)
dn_w_fr         = ta.pivotlow(2, 2)

y1_frup_1       = ta.valuewhen(up_w_fr, high[2], 1)
y0_frup_0       = ta.valuewhen(up_w_fr, high[2], 0)

y1_frdn_1       = ta.valuewhen(dn_w_fr, low[2], 1)
y0_frdn_0       = ta.valuewhen(dn_w_fr, low[2], 0)

// bar-id loops to get x1 and x2 for line.new()
xup0 = 2
for i = 1 to 35 by 1
    if high[i + 2] >= high[i + 3] and high[i + 2] > high[i + 4] and high[i + 2] > high[i + 1] and high[i + 2] >= high[i + 0]
        break
    xup0 := xup0 + 1
    xup0

xup1 = xup0
for i = xup1 to 50 by 1
    if high[i + 2] >= high[i + 3] and high[i + 2] > high[i + 4] and high[i + 2] > high[i + 1] and high[i + 2] >= high[i + 0]
        break
    xup1 := xup1 + 1
    xup1


xdn0 = 2
for i = 1 to 35 by 1
    if low[i + 2] <= low[i + 3] and low[i + 2] < low[i + 4] and low[i + 2] < low[i + 1] and low[i + 2] <= low[i + 0]
        break
    xdn0 := xdn0 + 1
    xdn0

xdn1 = xdn0
for i = xdn1 to 50 by 1
    if low[i + 2] <= low[i + 3] and low[i + 2] < low[i + 4] and low[i + 2] < low[i + 1] and low[i + 2] <= low[i + 0]
        break
    xdn1 := xdn1 + 1
    xdn1


// y-linebreak values for upper_line and lower_line

y_up_lvl        = (y0_frup_0 - y1_frup_1) / (xup1 + 2 - xup0) * xup0 + y0_frup_0  // y = slope * x0 + y0
y_dn_lvl        = (y0_frdn_0 - y1_frdn_1) / (xdn1 + 2 - xdn0) * xdn0 + y0_frdn_0
// entry
//frup_buy        = ta.crossover(high, y0_frup_0)
//frdn_sell       = ta.crossunder(low, y0_frdn_0)

buy_up_line     = one_side_pyr ? ta.crossover(high, y_up_lvl) : high > y_up_lvl// or frup_buy
sell_dn_line    = one_side_pyr ? ta.crossunder(low, y_dn_lvl) : low < y_dn_lvl// or frdn_sell


//--------------------------- Segments signal ----------------------------

count1_l                    = 0
count2_l                    = 0
segment_1_stat_l            = false
segment_2_stat_l            = false
segment_3_stat_l            = false

higher_low                  = low > low[1]

var line segment_low_1_l    = na
var line segment_low_2_l    = na
var line segment_low_3_l    = na

// long segments
for i = 0 to sb by 1
    count1_l := count1_l + 1
    if low[1] > low[i + 2] and higher_low
        segment_1_stat_l := true
        break

for i = count1_l to sb + count1_l by 1
    count2_l := count2_l + 1
    if low[1 + count1_l] > low[i + 2] and segment_1_stat_l
        segment_2_stat_l := true
        break

for i = count2_l to sb + count2_l by 1
    if low[1 + count1_l + count2_l] > low[i + 2 + count1_l] and segment_2_stat_l
        segment_3_stat_l := true
        break


// short segments
count1_s                    = 0
count2_s                    = 0
segment_1_stat_s            = false
segment_2_stat_s            = false
segment_3_stat_s            = false

lower_high                  = high < high[1]

var line segment_high_1     = na
var line segment_high_2     = na
var line segment_high_3     = na

for i = 0 to sb by 1
    count1_s := count1_s + 1
    if high[1] < high[i + 2] and lower_high
        segment_1_stat_s := true
        break

for i = count1_s to sb + count1_s by 1
    count2_s := count2_s + 1
    if high[1 + count1_s] < high[i + 2] and segment_1_stat_s
        segment_2_stat_s := true
        break

for i = count2_s to sb + count2_s by 1
    if high[1 + count1_s + count2_s] < high[i + 2 + count1_s] and segment_2_stat_s
        segment_3_stat_s := true
        break

// segments signals
seg_stat_l                  = segment_1_stat_l and segment_2_stat_l and segment_3_stat_l
seg_stat_s                  = segment_1_stat_s and segment_2_stat_s and segment_3_stat_s

//entry
segments_buy                = high > high[1] and seg_stat_l[1]
segments_sell               = low < low[1] and seg_stat_s[1]

//filters
segments_buy_f              = high > high[1] and seg_stat_l[1]
segments_sell_f             = low < low[1] and seg_stat_s[1]


//----------------------- i-o-s-p signals ------------------------

i_bar_buy           = high[1] < high[2] and low[1] > low[2] and close > high[1]
i_bar_sell          = high[1] < high[2] and low[1] > low[2] and close < low[1]

o_bar_buy           = high[1] > high[2] and low[1] < low[2] and high > high[1]
o_bar_sell          = high[1] > high[2] and low[1] < low[2] and low < low[1]

s_bar_buy           = high[2] < high[3] and low[2] > low[3] and high[1] > high[2] and low[1] < low[2] and high > high[1]
s_bar_sell          = high[2] < high[3] and low[2] > low[3] and high[1] > high[2] and low[1] < low[2] and low < low[1]

//pinbar
candle_body         = math.abs(open - close)

pivot_up            = ta.pivothigh(high, p_bar_sens_2, 0)
pivot_dn            = ta.pivotlow(low, p_bar_sens_2, 0)

up_wick             = close > open ? high - close : high - open
dn_wick             = close > open ? open - low : close - low

pin_up_def          = high - open > p_bar_sens_1 * candle_body and close < open or high - close > p_bar_sens_1 * candle_body and close > open

pin_dn_def          = open - low > p_bar_sens_1 * candle_body and close > open or close - low > p_bar_sens_1 * candle_body and close < open

p_bar_sell          = pin_up_def and pivot_up and up_wick > dn_wick
p_bar_buy           = pin_dn_def and pivot_dn and up_wick < dn_wick


//----------------- Ichimoku Signal B1/S1 -----------------

buy_strong_B1       = TK >= KJ and high > kumo_high and CS > high[26 - 1] and CS > kumo_high[26 - 1] and SA > SB
sell_strong_S1      = TK <= KJ and low < kumo_low and CS < low[26 - 1] and CS < kumo_low[26 - 1] and SA < SB


//----------------- Ichimoku Signal B2/S2 -----------------

buy_strong_B2       = TK >= KJ and high > kumo_high and CS > high[26 - 1]
sell_strong_S2      = TK <= KJ and low < kumo_low and CS < low[26 - 1]


//---------------------------- Confluence Signal CB/CS ----------------------------

long_short_trig     = 10  //input(9, type= input.float, title= "Confluence signal trigger Level", step= 0.2)

//Indicators
// ma
sma1                                = f_sma(50)
sma2                                = f_sma(200)
ema1                                = f_ema(50)
ema2                                = f_ema(200)
ema_c                               = f_ema(21)
sma_c                               = f_sma(20)
[macdLine, signalLine, histLine]    = ta.macd(close, 12, 26, 9)
rsi                                 = f_rsi(14)
[diplus, diminus, adx]              = ta.dmi(7, 7)
[superTrend, dir]                   = ta.supertrend(2, 5)
//Klinger Oszillator
sv                                  = ta.change(hlc3) >= 0 ? volume : -volume
kvo                                 = ta.ema(sv, 34) - ta.ema(sv, 55)
sig                                 = ta.ema(kvo, 13)
//Vortex Indicator
VMP                                 = math.sum(math.abs(high - low[1]), 14)
VMM                                 = math.sum(math.abs(low - high[1]), 14)
STR                                 = math.sum(ta.atr(1), 14)
VIP                                 = VMP / STR
VIM                                 = VMM / STR
//HMA slope
hma_slo_up                          = ta.rising(f_hma(15),1)
//ALMA slope
alma_slo_up                         = ta.rising(ta.alma(close, 9, 0.85, 6),1)
//MACD slope
macd_slo_up                         = ta.rising(macdLine, 1)


//Signals
var float sem_sig_w                 = na
var float sma_sig_w                 = na
var float ema_sig_w                 = na
var float p_kj_sig_w                = na
var float tk_kj_sig_w               = na
var float B1_S1_sig_w               = na
var float B2_S2_sig_w               = na
var float psar_sig_w                = na
var float frac_sig_w                = na
var float macd_sig_w                = na
var float rsi_sig_w                 = na
var float p_tk_sig_w                = na
var float dmi_sig_w                 = na
var float klin_sig_w                = na
var float vort_sig_w                = na
var float sup_sig_w                 = na
var float hma_sig_w                 = na
var float alma_sig_w                = na
var float macd_s_sig_w              = na


if ema_c > sma_c
    sem_sig_w := 1
else if ema_c < sma_c
    sem_sig_w := 0


if sma1 > sma2
    sma_sig_w := 1
else if sma1 < sma2
    sma_sig_w := 0


if ema1 > ema2
    ema_sig_w := 1
else if ema1 < ema2
    ema_sig_w := 0


if high > KJ
    p_kj_sig_w := 1
else if low < KJ
    p_kj_sig_w := 0


if TK > KJ
    tk_kj_sig_w := 1
else if TK < KJ
    tk_kj_sig_w := 0


if buy_strong_B1
    B1_S1_sig_w := 1
else if sell_strong_S1
    B1_S1_sig_w := 0


if buy_strong_B2
    B2_S2_sig_w := 1
else if sell_strong_S2
    B2_S2_sig_w := 0


if high >= ta.sar(start, inc, max)[0]
    psar_sig_w := 1
else if low <= ta.sar(start, inc, max)[0]
    psar_sig_w := 0


if high > fractal_up_v
    frac_sig_w := 1
else if low < fractal_dn_v
    frac_sig_w := 0


if macdLine > signalLine
    macd_sig_w := 1
else if macdLine < signalLine
    macd_sig_w := 0


if rsi > 50
    rsi_sig_w := 1
else if rsi < 50
    rsi_sig_w := 0


if high > TK
    p_tk_sig_w := 1
else if low < TK
    p_tk_sig_w := 0


if diplus > diminus
    dmi_sig_w := 1
else if diplus < diminus
    dmi_sig_w := 0


if sig > 0
    klin_sig_w := 1
else if sig < 0
    klin_sig_w := 0


if VIP > VIM
    vort_sig_w := 1
else if VIP < VIM
    vort_sig_w := 0


if high > superTrend
    sup_sig_w := 1
else if low < superTrend
    sup_sig_w := 0


if hma_slo_up
    hma_sig_w := 2
else if not hma_slo_up
    hma_sig_w := -1


if alma_slo_up
    alma_sig_w := 2
else if not alma_slo_up
    alma_sig_w := -1


if macd_slo_up
    macd_s_sig_w := 1
else if not macd_slo_up
    macd_s_sig_w := 0



bs_conf_sig             = sma_sig_w + ema_sig_w + p_kj_sig_w + tk_kj_sig_w + B1_S1_sig_w + B2_S2_sig_w + psar_sig_w + frac_sig_w + macd_sig_w + rsi_sig_w + dmi_sig_w + klin_sig_w + vort_sig_w + sup_sig_w + 
                         p_tk_sig_w + sem_sig_w + hma_sig_w + alma_sig_w + macd_s_sig_w

long_c                  = one_side_pyr ? ta.crossover(bs_conf_sig, (long_short_trig + trig_gap_cbcs)) : bs_conf_sig > (long_short_trig + trig_gap_cbcs)                         //with +- signal is more stable
short_c                 = one_side_pyr ? ta.crossunder(bs_conf_sig, (long_short_trig - trig_gap_cbcs)) : bs_conf_sig < (long_short_trig - trig_gap_cbcs)


//---------------------------- Pure Ichimoku Confluence Signal IB/IS ----------------------------

pic_l_s_trig            = 5  //input(4, type= input.float, title= "Ichimoku confluence signal trigger Level", step= 0.1)

//Signals
var float tkkh_sig_w    = na
var float csh_sig_w     = na
var float cskh_sig_w    = na
var float pkj_sig_w     = na
var float ptk_sig_w     = na
var float tkkj_sig_w    = na
var float sasb_sig_w    = na
var float ckh_sig_w     = na

var float cskj_sig_w    = na
var float cstk_sig_w    = na
var float kjcl_sig_w    = na


if TK > kumo_high
    tkkh_sig_w := 1
else if TK < kumo_low
    tkkh_sig_w := 0


if CS > high[26 - 1]
    csh_sig_w := 1
else if CS < low[26 - 1]
    csh_sig_w := 0


if CS > kumo_high[26 - 1]
    cskh_sig_w := 1
else if CS < kumo_low[26 - 1]
    cskh_sig_w := 0


if high > TK
    ptk_sig_w := 1
else if low < TK
    ptk_sig_w := 0


if high > KJ
    pkj_sig_w := 1
else if low < KJ
    pkj_sig_w := 0


if TK > KJ
    tkkj_sig_w := 1
else if TK < KJ
    tkkj_sig_w := 0


if SA > SB
    sasb_sig_w := 1
else if SA < SB
    sasb_sig_w := 0


if high > kumo_high
    ckh_sig_w := 1
else if low < kumo_low
    ckh_sig_w := 0




if CS > KJ[26 - 1]
    cskj_sig_w := 1
else if CS < KJ[26 - 1]
    cskj_sig_w := 0


if CS > TK[26 - 1]
    cstk_sig_w := 1
else if CS < TK[26 - 1]
    cstk_sig_w := 0


if KJ > kumo_high
    kjcl_sig_w := 1
else if KJ < kumo_high
    kjcl_sig_w := 0



bs_pic_sig          = tkkh_sig_w + csh_sig_w + cskh_sig_w + ptk_sig_w + pkj_sig_w + tkkj_sig_w + sasb_sig_w + ckh_sig_w   +   cskj_sig_w + cstk_sig_w + kjcl_sig_w

long_pic            = one_side_pyr ? ta.crossover(bs_pic_sig, pic_l_s_trig + trig_gap_ibis) : bs_pic_sig > pic_l_s_trig + trig_gap_ibis
short_pic           = one_side_pyr ? ta.crossunder(bs_pic_sig, pic_l_s_trig - trig_gap_ibis) : bs_pic_sig < pic_l_s_trig - trig_gap_ibis

long_pic_x          = long_pic and not long_pic[1]
short_pic_x         = short_pic and not short_pic[1]


//-------------------------------------------------------------------- entry filters -------------------------------------------------------------------

f_secureSecurity(_symbol, _res, _src) => request.security(_symbol, _res, _src[repaint == 'Allowed' ? 0 : 1])                 // no repainting - taken from PineCoders 
    

entry_filter_sig_buy_1      = entry_f_1 == '---' ? true : 
                             entry_f_1 == 'TRIX slope filter' ? trix_slo_up_f :
                             entry_f_1 == 'RSI Stochastic filter' ? kd_mom_up_f : 
                             entry_f_1 == 'EMA1 x EMA2 filter' ? ema_1x2_buy_f : 
                             entry_f_1 == 'HMA slope filter' ? hma_up_f : 
                             entry_f_1 == 'MACD(fast) slope filter' ? macd_slope_up_f : 
                             entry_f_1 == 'MACD filter' ? macd_buy : 
                             entry_f_1 == 'RSI50 filter' ? rsi_f_buy_f : 
                             entry_f_1 == 'Fractals filter' ? fr_upx_f : 
                             entry_f_1 == 'SuperTrend filter' ? sup_buy_f : 
                             entry_f_1 == 'Parabolic SAR filter' ? psar_buy_f : 
                             entry_f_1 == 'SMA filter' ? sma_buy_f : 
                             entry_f_1 == 'ADX Threshold filter' ? adx_above_thres : 
                             entry_f_1 == 'DMI filter' ? long_e : 
                             entry_f_1 == 'Bar breakout 1 filter' ? bar_sig_1_buy_f : 
                             entry_f_1 == 'Bar breakout 2 filter' ? bar_sig_2_buy_f : 
                             entry_f_1 == 'Reverse fractal filter' ? rev_dn_fr_buy : 
                             entry_f_1 == 'ALMA slope filter' ? alma_slo_up_f : 
                             entry_f_1 == 'Price X Kumo filter' ? kumo_buy_f : 
                             entry_f_1 == 'Price X Kijun filter' ? kijun_buy_f : 
                             entry_f_1 == 'Kumo flip filter' ? kumo_flip_buy_f : 
                             entry_f_1 == 'Chikou X price filter' ? chikou_X_price_buy_f : 
                             entry_f_1 == 'Chikou X Kumo filter' ? chikou_X_kumo_buy_f : 
                             entry_f_1 == 'Price X Tenkan filter' ? price_X_tenkan_buy_f : 
                             entry_f_1 == 'Tenkan X Kumo filter' ? tenkan_X_kumo_buy_f : 
                             entry_f_1 == 'Tenkan X Kijun filter' ? tenkan_X_kijun_buy_f : 
                             entry_f_1 == 'B1/S1 sig' ? buy_strong_B1 : 
                             entry_f_1 == 'B2/S2 sig' ? buy_strong_B2 : true

entry_filter_sig_sell_1     = entry_f_1 == '---' ? true : 
                             entry_f_1 == 'TRIX slope filter' ? not trix_slo_up_f :
                             entry_f_1 == 'RSI Stochastic filter' ? kd_mom_dn_f : 
                             entry_f_1 == 'EMA1 x EMA2 filter' ? not ema_1x2_buy_f : 
                             entry_f_1 == 'HMA slope filter' ? not hma_up_f : 
                             entry_f_1 == 'MACD(fast) slope filter' ? not macd_slope_up_f : 
                             entry_f_1 == 'MACD filter' ? macd_sell : 
                             entry_f_1 == 'RSI50 filter' ? rsi_f_sell_f : 
                             entry_f_1 == 'Fractals filter' ? fr_dnx_f : 
                             entry_f_1 == 'SuperTrend filter' ? sup_sell_f : 
                             entry_f_1 == 'Parabolic SAR filter' ? psar_sell_f : 
                             entry_f_1 == 'SMA filter' ? sma_sell_f : 
                             entry_f_1 == 'ADX Threshold filter' ? adx_above_thres : 
                             entry_f_1 == 'DMI filter' ? short_e : 
                             entry_f_1 == 'Bar breakout 1 filter' ? bar_sig_1_sell_f : 
                             entry_f_1 == 'Bar breakout 2 filter' ? bar_sig_2_sell_f : 
                             entry_f_1 == 'Reverse fractal filter' ? rev_up_fr_sell : 
                             entry_f_1 == 'ALMA slope filter' ? not alma_slo_up_f : 
                             entry_f_1 == 'Price X Kumo filter' ? kumo_sell_f : 
                             entry_f_1 == 'Price X Kijun filter' ? kijun_sell_f : 
                             entry_f_1 == 'Kumo flip filter' ? kumo_flip_sell_f : 
                             entry_f_1 == 'Chikou X price filter' ? chikou_X_price_sell_f : 
                             entry_f_1 == 'Chikou X Kumo filter' ? chikou_X_kumo_sell_f : 
                             entry_f_1 == 'Price X Tenkan filter' ? price_X_tenkan_sell_f : 
                             entry_f_1 == 'Tenkan X Kumo filter' ? tenkan_X_kumo_sell_f : 
                             entry_f_1 == 'Tenkan X Kijun filter' ? tenkan_X_kijun_sell_f : 
                             entry_f_1 == 'B1/S1 sig' ? sell_strong_S1 : 
                             entry_f_1 == 'B2/S2 sig' ? sell_strong_S2 : true


entry_filter_sig_buy_2      = entry_f_2 == '---' ? true : 
                             entry_f_2 == 'TRIX slope filter' ? trix_slo_up_f :
                             entry_f_2 == 'RSI Stochastic filter' ? kd_mom_up_f : 
                             entry_f_2 == 'EMA1 x EMA2 filter' ? ema_1x2_buy_f : 
                             entry_f_2 == 'HMA slope filter' ? hma_up_f : 
                             entry_f_2 == 'MACD(fast) slope filter' ? macd_slope_up_f : 
                             entry_f_2 == 'MACD filter' ? macd_buy : 
                             entry_f_2 == 'RSI50 filter' ? rsi_f_buy_f :
                             entry_f_2 == 'Fractals filter' ? fr_upx_f : 
                             entry_f_2 == 'SuperTrend filter' ? sup_buy_f : 
                             entry_f_2 == 'Parabolic SAR filter' ? psar_buy_f : 
                             entry_f_2 == 'SMA filter' ? sma_buy_f : 
                             entry_f_2 == 'ADX Threshold filter' ? adx_above_thres : 
                             entry_f_2 == 'DMI filter' ? long_e : 
                             entry_f_2 == 'Bar breakout 1 filter' ? bar_sig_1_buy_f : 
                             entry_f_2 == 'Bar breakout 2 filter' ? bar_sig_2_buy_f : 
                             entry_f_2 == 'Reverse fractal filter' ? rev_dn_fr_buy : 
                             entry_f_2 == 'ALMA slope filter' ? alma_slo_up_f : 
                             entry_f_2 == 'Price X Kumo filter' ? kumo_buy_f : 
                             entry_f_2 == 'Price X Kijun filter' ? kijun_buy_f : 
                             entry_f_2 == 'Kumo flip filter' ? kumo_flip_buy_f : 
                             entry_f_2 == 'Chikou X price filter' ? chikou_X_price_buy_f : 
                             entry_f_2 == 'Chikou X Kumo filter' ? chikou_X_kumo_buy_f : 
                             entry_f_2 == 'Price X Tenkan filter' ? price_X_tenkan_buy_f : 
                             entry_f_2 == 'Tenkan X Kumo filter' ? tenkan_X_kumo_buy_f : 
                             entry_f_2 == 'Tenkan X Kijun filter' ? tenkan_X_kijun_buy_f : 
                             entry_f_2 == 'B1/S1 sig' ? buy_strong_B1 : 
                             entry_f_2 == 'B2/S2 sig' ? buy_strong_B2 : true

entry_filter_sig_sell_2     = entry_f_2 == '---' ? true : 
                             entry_f_2 == 'TRIX slope filter' ? not trix_slo_up_f :
                             entry_f_2 == 'RSI Stochastic filter' ? kd_mom_dn_f : 
                             entry_f_2 == 'EMA1 x EMA2 filter' ? not ema_1x2_buy_f : 
                             entry_f_2 == 'HMA slope filter' ? not hma_up_f : 
                             entry_f_2 == 'MACD(fast) slope filter' ? not macd_slope_up_f : 
                             entry_f_2 == 'MACD filter' ? macd_sell : 
                             entry_f_2 == 'RSI50 filter' ? rsi_f_sell_f : 
                             entry_f_2 == 'Fractals filter' ? fr_dnx_f : 
                             entry_f_2 == 'SuperTrend filter' ? sup_sell_f : 
                             entry_f_2 == 'Parabolic SAR filter' ? psar_sell_f : 
                             entry_f_2 == 'SMA filter' ? sma_sell_f : 
                             entry_f_2 == 'ADX Threshold filter' ? adx_above_thres : 
                             entry_f_2 == 'DMI filter' ? long_e : 
                             entry_f_2 == 'Bar breakout 1 filter' ? bar_sig_1_sell_f : 
                             entry_f_2 == 'Bar breakout 2 filter' ? bar_sig_2_sell_f : 
                             entry_f_2 == 'Reverse fractal filter' ? rev_up_fr_sell : 
                             entry_f_2 == 'ALMA slope filter' ? not alma_slo_up_f : 
                             entry_f_2 == 'Price X Kumo filter' ? kumo_sell_f : 
                             entry_f_2 == 'Price X Kijun filter' ? kijun_sell_f : 
                             entry_f_2 == 'Kumo flip filter' ? kumo_flip_sell_f : 
                             entry_f_2 == 'Chikou X price filter' ? chikou_X_price_sell_f : 
                             entry_f_2 == 'Chikou X Kumo filter' ? chikou_X_kumo_sell_f : 
                             entry_f_2 == 'Price X Tenkan filter' ? price_X_tenkan_sell_f : 
                             entry_f_2 == 'Tenkan X Kumo filter' ? tenkan_X_kumo_sell_f : 
                             entry_f_2 == 'Tenkan X Kijun filter' ? tenkan_X_kijun_sell_f : 
                             entry_f_2 == 'B1/S1 sig' ? sell_strong_S1 : 
                             entry_f_2 == 'B2/S2 sig' ? sell_strong_S2 : true


//entry buy filter 1 options
entry_filter_buy_1          = htf_filt_opt_1 == 'Current' ? entry_filter_sig_buy_1[repaint == 'Allowed' ? 0 : 1] : 
                             f_secureSecurity(syminfo.tickerid, 
                             htf_filt_opt_1 == '5m' ? '5' : htf_filt_opt_1 == '10m' ? '10' : htf_filt_opt_1 == '15m' ? '15' : 
                             htf_filt_opt_1 == '30m' ? '30' : htf_filt_opt_1 == '1H' ? '60' : htf_filt_opt_1 == '2H' ? '120' : htf_filt_opt_1 == '3H' ? '180' : htf_filt_opt_1 == '4H' ? '240' : 
                             htf_filt_opt_1 == '6H' ? '360' : htf_filt_opt_1 == '12H' ? '720' : htf_filt_opt_1 == 'D' ? 'D' : htf_filt_opt_1 == '3D' ? '3D' : htf_filt_opt_1 == 'W' ? 'W' : htf_filt_opt_1 == 'M' ? 'M' : na, 
                             entry_filter_sig_buy_1)

entry_filter_buy_11         = entry_f_1 == 'Segments filter (no tf filter)' ? segments_buy : 
                             entry_f_1 == 'IB/IS sig (no tf filter)' ? long_pic[repaint == 'Allowed' ? 0 : 1] :
                             entry_f_1 == 'Fractals trend lines filter (no tf filter)' ? buy_up_line  : true


//entry sell filter 1 options
entry_filter_sell_1         = htf_filt_opt_1 == 'Current' ? entry_filter_sig_sell_1[repaint == 'Allowed' ? 0 : 1] : 
                             f_secureSecurity(syminfo.tickerid, 
                             htf_filt_opt_1 == '5m' ? '5' : htf_filt_opt_1 == '10m' ? '10' : htf_filt_opt_1 == '15m' ? '15' : 
                             htf_filt_opt_1 == '30m' ? '30' : htf_filt_opt_1 == '1H' ? '60' : htf_filt_opt_1 == '2H' ? '120' : htf_filt_opt_1 == '3H' ? '180' : htf_filt_opt_1 == '4H' ? '240' : 
                             htf_filt_opt_1 == '6H' ? '360' : htf_filt_opt_1 == '12H' ? '720' : htf_filt_opt_1 == 'D' ? 'D' : htf_filt_opt_1 == '3D' ? '3D' : htf_filt_opt_1 == 'W' ? 'W' : htf_filt_opt_1 == 'M' ? 'M' : na, 
                             entry_filter_sig_sell_1)

entry_filter_sell_11        = entry_f_1 == 'Segments filter (no tf filter)' ? segments_sell : 
                             entry_f_1 == 'IB/IS sig (no tf filter)' ? short_pic[repaint == 'Allowed' ? 0 : 1] :
                             entry_f_1 == 'Fractals trend lines filter (no tf filter)' ? sell_dn_line : true


//entry buy filter 2 options
entry_filter_buy_2          = htf_filt_opt_2 == 'Current' ? entry_filter_sig_buy_2[repaint == 'Allowed' ? 0 : 1] : 
                             f_secureSecurity(syminfo.tickerid, 
                             htf_filt_opt_2 == '5m' ? '5' : htf_filt_opt_2 == '10m' ? '10' : htf_filt_opt_2 == '15m' ? '15' : 
                             htf_filt_opt_2 == '30m' ? '30' : htf_filt_opt_2 == '1H' ? '60' : htf_filt_opt_2 == '2H' ? '120' : htf_filt_opt_2 == '3H' ? '180' : htf_filt_opt_2 == '4H' ? '240' : 
                             htf_filt_opt_2 == '6H' ? '360' : htf_filt_opt_2 == '12H' ? '720' : htf_filt_opt_2 == 'D' ? 'D' : htf_filt_opt_2 == '3D' ? '3D' : htf_filt_opt_2 == 'W' ? 'W' : htf_filt_opt_2 == 'M' ? 'M' : na, 
                             entry_filter_sig_buy_2)

entry_filter_buy_22         = entry_f_2 == 'Fractals trend lines filter (no tf filter)' ? buy_up_line : 
                             entry_f_2 == 'Segments filter (no tf filter)' ? segments_buy : 
                             entry_f_2 == 'IB/IS sig (no tf filter)' ? long_pic[repaint == 'Allowed' ? 0 : 1] : true


//entry sell filter 2 options
entry_filter_sell_2         = htf_filt_opt_2 == 'Current' ? entry_filter_sig_sell_2[repaint == 'Allowed' ? 0 : 1] : 
                             f_secureSecurity(syminfo.tickerid, 
                             htf_filt_opt_2 == '5m' ? '5' : htf_filt_opt_2 == '10m' ? '10' : htf_filt_opt_2 == '15m' ? '15' : 
                             htf_filt_opt_2 == '30m' ? '30' : htf_filt_opt_2 == '1H' ? '60' : htf_filt_opt_2 == '2H' ? '120' : htf_filt_opt_2 == '3H' ? '180' : htf_filt_opt_2 == '4H' ? '240' : 
                             htf_filt_opt_2 == '6H' ? '360' : htf_filt_opt_2 == '12H' ? '720' : htf_filt_opt_2 == 'D' ? 'D' : htf_filt_opt_2 == '3D' ? '3D' : htf_filt_opt_2 == 'W' ? 'W' : htf_filt_opt_2 == 'M' ? 'M' : na, 
                             entry_filter_sig_sell_2)

entry_filter_sell_22        = entry_f_2 == 'Fractals trend lines filter (no tf filter)' ? sell_dn_line : 
                             entry_f_2 == 'Segments filter (no tf filter)' ? segments_sell : 
                             entry_f_2 == 'IB/IS sig (no tf filter)' ? short_pic[repaint == 'Allowed' ? 0 : 1] : true


//-------------------------------------------------------------------- exit filters -----------------------------------------------------------------------

exit_filter_sig_buy_1       = exit_f_1 == '---' ? false : 
                             exit_f_1 == 'TRIX slope exit' ? trix_slo_up_x : 
                             exit_f_1 == 'RSI Stochastic exit' ? kd_mom_up_x : 
                             exit_f_1 == 'EMA1 x EMA2 exit' ? ema_1x2_buy_x : 
                             exit_f_1 == 'HMA slope exit' ? hma_up_x : 
                             exit_f_1 == 'Reverse bar exit' ? rev_bar_sig_buy : 
                             exit_f_1 == 'Reverse fractal exit' ? rev_dn_fr_buy : 
                             exit_f_1 == 'Bar breakout 2 exit' ? bar_sig_2_buy : 
                             exit_f_1 == 'MACD(fast) slope exit' ? macd_slope_up_x : 
                             exit_f_1 == 'MACD exit' ? macd_buy_x : 
                             exit_f_1 == 'RSI50 exit' ? rsi_f_buy_x : 
                             exit_f_1 == 'Fractals exit' ? fr_upx_x : 
                             exit_f_1 == 'SuperTrend exit' ? sup_buy_x : 
                             exit_f_1 == 'Parabolic SAR exit' ? psar_buy_x : 
                             exit_f_1 == 'SMA exit' ? sma_buy_x : 
                             exit_f_1 == 'ADX Threshold exit' ? adx_thres_f_x : 
                             exit_f_1 == 'Cloud exit' ? kumo_buy_x : 
                             exit_f_1 == 'Kijun exit' ? kijun_buy_x : false

exit_filter_sig_sell_1      = exit_f_1 == '---' ? false : 
                             exit_f_1 == 'TRIX slope exit' ? trix_slo_dn_x :
                             exit_f_1 == 'RSI Stochastic exit' ? kd_mom_dn_x : 
                             exit_f_1 == 'EMA1 x EMA2 exit' ? ema_1x2_sell_x : 
                             exit_f_1 == 'HMA slope exit' ? hma_dn_x : 
                             exit_f_1 == 'Reverse bar exit' ? rev_bar_sig_sell : 
                             exit_f_1 == 'Reverse fractal exit' ? rev_up_fr_sell : 
                             exit_f_1 == 'Bar breakout 2 exit' ? bar_sig_2_sell : 
                             exit_f_1 == 'MACD(fast) slope exit' ? macd_slope_dn_x : 
                             exit_f_1 == 'MACD exit' ? macd_sell_x : 
                             exit_f_1 == 'RSI50 exit' ? rsi_f_sell_x : 
                             exit_f_1 == 'Fractals exit' ? fr_dnx_x : 
                             exit_f_1 == 'SuperTrend exit' ? sup_sell_x : 
                             exit_f_1 == 'Parabolic SAR exit' ? psar_sell_x :
                             exit_f_1 == 'SMA exit' ? sma_sell_x : 
                             exit_f_1 == 'ADX Threshold exit' ? adx_thres_f_x : 
                             exit_f_1 == 'Cloud exit' ? kumo_sell_x : 
                             exit_f_1 == 'Kijun exit' ? kijun_sell_x : false


exit_filter_sig_buy_2       = exit_f_2 == '---' ? false : 
                             exit_f_2 == 'TRIX slope exit' ? trix_slo_up_x :
                             exit_f_2 == 'RSI Stochastic exit' ? kd_mom_up_x : 
                             exit_f_2 == 'EMA1 x EMA2 exit' ? ema_1x2_buy_x : 
                             exit_f_2 == 'HMA slope exit' ? hma_up_x : 
                             exit_f_2 == 'Reverse bar exit' ? rev_bar_sig_buy : 
                             exit_f_2 == 'Reverse fractal exit' ? rev_dn_fr_buy : 
                             exit_f_2 == 'Bar breakout 2 exit' ? bar_sig_2_buy : 
                             exit_f_2 == 'MACD(fast) slope exit' ? macd_slope_up_x : 
                             exit_f_2 == 'MACD exit' ? macd_buy_x : 
                             exit_f_2 == 'RSI50 exit' ? rsi_f_buy_x : 
                             exit_f_2 == 'Fractals exit' ? fr_upx_x : 
                             exit_f_2 == 'SuperTrend exit' ? sup_buy_x : 
                             exit_f_2 == 'Parabolic SAR exit' ? psar_buy_x : 
                             exit_f_2 == 'SMA exit' ? sma_buy_x : 
                             exit_f_2 == 'ADX Threshold exit' ? adx_thres_f_x : 
                             exit_f_2 == 'Cloud exit' ? kumo_buy_x : 
                             exit_f_2 == 'Kijun exit' ? kijun_buy_x : false

exit_filter_sig_sell_2      = exit_f_2 == '---' ? false : 
                             exit_f_2 == 'TRIX slope exit' ? trix_slo_dn_x :
                             exit_f_2 == 'RSI Stochastic exit' ? kd_mom_dn_x : 
                             exit_f_2 == 'EMA1 x EMA2 exit' ? ema_1x2_sell_x : 
                             exit_f_2 == 'HMA slope exit' ? hma_dn_x : 
                             exit_f_2 == 'Reverse bar exit' ? rev_bar_sig_sell : 
                             exit_f_2 == 'Reverse fractal exit' ? rev_up_fr_sell : 
                             exit_f_2 == 'Bar breakout 2 exit' ? bar_sig_2_sell : 
                             exit_f_2 == 'MACD(fast) slope exit' ? macd_slope_dn_x : 
                             exit_f_2 == 'MACD exit' ? macd_sell_x : 
                             exit_f_2 == 'RSI50 exit' ? rsi_f_sell_x : 
                             exit_f_2 == 'Fractals exit' ? fr_dnx_x : 
                             exit_f_2 == 'SuperTrend exit' ? sup_sell_x : 
                             exit_f_2 == 'Parabolic SAR exit' ? psar_sell_x : 
                             exit_f_2 == 'SMA exit' ? sma_sell_x : 
                             exit_f_2 == 'ADX Threshold exit' ? adx_thres_f_x : 
                             exit_f_2 == 'Cloud exit' ? kumo_sell_x : 
                             exit_f_2 == 'Kijun exit' ? kijun_sell_x : false


//short exit buy filter 1 options
exit_filter_buy_1           = htf_exit_opt_1 == 'Current' ? exit_filter_sig_buy_1[repaint == 'Allowed' ? 0 : 1] : 
                             f_secureSecurity(syminfo.tickerid, 
                             htf_exit_opt_1 == '5m' ? '5' : htf_exit_opt_1 == '10m' ? '10' : htf_exit_opt_1 == '15m' ? '15' : 
                             htf_exit_opt_1 == '30m' ? '30' : htf_exit_opt_1 == '1H' ? '60' : htf_exit_opt_1 == '2H' ? '120' : htf_exit_opt_1 == '3H' ? '180' : htf_exit_opt_1 == '4H' ? '240' : 
                             htf_exit_opt_1 == '6H' ? '360' : htf_exit_opt_1 == '12H' ? '720' : htf_exit_opt_1 == 'D' ? 'D' : htf_exit_opt_1 == '3D' ? '3D' : htf_exit_opt_1 == 'W' ? 'W' : 
                             htf_exit_opt_1 == 'M' ? 'M' : na, exit_filter_sig_buy_1)

exit_filter_sig_buy_11      = exit_f_1 == 'IB/IS exit (no tf filter)' ? long_pic_x[repaint == 'Allowed' ? 0 : 1] : false


//long exit sell filter 1 options
exit_filter_sell_1          = htf_exit_opt_1 == 'Current' ? exit_filter_sig_sell_1[repaint == 'Allowed' ? 0 : 1] : 
                             f_secureSecurity(syminfo.tickerid, 
                             htf_exit_opt_1 == '5m' ? '5' : htf_exit_opt_1 == '10m' ? '10' : htf_exit_opt_1 == '15m' ? '15' : 
                             htf_exit_opt_1 == '30m' ? '30' : htf_exit_opt_1 == '1H' ? '60' : htf_exit_opt_1 == '2H' ? '120' : htf_exit_opt_1 == '3H' ? '180' : htf_exit_opt_1 == '4H' ? '240' : 
                             htf_exit_opt_1 == '6H' ? '360' : htf_exit_opt_1 == '12H' ? '720' : htf_exit_opt_1 == 'D' ? 'D' : htf_exit_opt_1 == '3D' ? '3D' : htf_exit_opt_1 == 'W' ? 'W' : 
                             htf_exit_opt_1 == 'M' ? 'M' : na, exit_filter_sig_sell_1)

exit_filter_sig_sell_11     = exit_f_1 == 'IB/IS exit (no tf filter)' ? short_pic_x[repaint == 'Allowed' ? 0 : 1] : false


//short exit buy filter 2 options
exit_filter_buy_2           = htf_exit_opt_2 == 'Current' ? exit_filter_sig_buy_2[repaint == 'Allowed' ? 0 : 1] : 
                             f_secureSecurity(syminfo.tickerid, 
                             htf_exit_opt_2 == '5m' ? '5' : htf_exit_opt_2 == '10m' ? '10' : htf_exit_opt_2 == '15m' ? '15' :
                             htf_exit_opt_2 == '30m' ? '30' : htf_exit_opt_2 == '1H' ? '60' : htf_exit_opt_2 == '2H' ? '120' : htf_exit_opt_2 == '3H' ? '180' : htf_exit_opt_2 == '4H' ? '240' : 
                             htf_exit_opt_2 == '6H' ? '360' : htf_exit_opt_2 == '12H' ? '720' : htf_exit_opt_2 == 'D' ? 'D' : htf_exit_opt_2 == '3D' ? '3D' : htf_exit_opt_2 == 'W' ? 'W' : 
                             htf_exit_opt_2 == 'M' ? 'M' : na, exit_filter_sig_buy_2)

exit_filter_sig_buy_22      = exit_f_2 == 'IB/IS exit (no tf filter)' ? long_pic_x[repaint == 'Allowed' ? 0 : 1] : false


//long exit sell filter 2 options
exit_filter_sell_2          = htf_exit_opt_2 == 'Current' ? exit_filter_sig_sell_2[repaint == 'Allowed' ? 0 : 1] : 
                             f_secureSecurity(syminfo.tickerid, 
                             htf_exit_opt_2 == '5m' ? '5' : htf_exit_opt_2 == '10m' ? '10' : htf_exit_opt_2 == '15m' ? '15' : 
                             htf_exit_opt_2 == '30m' ? '30' : htf_exit_opt_2 == '1H' ? '60' : htf_exit_opt_2 == '2H' ? '120' : htf_exit_opt_2 == '3H' ? '180' : htf_exit_opt_2 == '4H' ? '240' : 
                             htf_exit_opt_2 == '6H' ? '360' : htf_exit_opt_2 == '12H' ? '720' : htf_exit_opt_2 == 'D' ? 'D' : htf_exit_opt_2 == '3D' ? '3D' : htf_exit_opt_2 == 'W' ? 'W' : 
                             htf_exit_opt_2 == 'M' ? 'M' : na, exit_filter_sig_sell_2)

exit_filter_sig_sell_22     = exit_f_2 == 'IB/IS exit (no tf filter)' ? short_pic_x[repaint == 'Allowed' ? 0 : 1] : false


//---------------------------------------------------------------------- entry signals -------------------------------------------------------------------

entry_opt_sig_buy_1         = X_opt == '---' ? na : 
                             X_opt == 'TRIX slope sig' ? trix_buy_s : 
                             X_opt == 'EMA1 x EMA2 sig' ? ema_1x2_buy_s : 
                             X_opt == 'HMA slope sig' ? hma_up_e : 
                             X_opt == 'DMI ADX-slope sig' ? adx_up_c : 
                             X_opt == 'DMI ADX mod sig' ? long_1 : 
                             X_opt == 'DMI ADX classic sig' ? long_2 : 
                             X_opt == 'Pin bar sig' ? p_bar_buy : 
                             X_opt == 'Inside bar sig' ? i_bar_buy : 
                             X_opt == 'Outside bar sig' ? o_bar_buy : 
                             X_opt == 'Sandwich bar sig' ? s_bar_buy : 
                             X_opt == 'Bar breakout 1 sig' ? bar_sig_1_buy : 
                             X_opt == 'Bar breakout 2 sig' ? bar_sig_2_buy : 
                             X_opt == 'Higher-low/lower-high bar sig' ? hllh_buy : 
                             X_opt == 'Entry on every bar-open sig' ? open_buy : 
                             X_opt == 'SMA sig' ? sma_buy : 
                             X_opt == 'Fractals sig' ? fr_upx : 
                             X_opt == 'Reverse fractal sig' ? rev_dn_fr_buy : 
                             X_opt == 'MACD(fast) slope sig' ? macd_slope_up_e : 
                             X_opt == 'RSI50 sig' ? rsi_f_buy : 
                             X_opt == 'Parabolic SAR sig' ? psar_buy : 
                             X_opt == 'ALMA slope sig' ? alma_slo_up_e : 
                             X_opt == 'SuperTrend sig' ? sup_buy : 
                             X_opt == 'Price X Kijun sig' ? kijun_buy : 
                             X_opt == 'Price X Kumo sig' ? kumo_buy : 
                             X_opt == 'Kumo flip sig' ? kumo_flip_buy : 
                             X_opt == 'Chikou X price sig' ? chikou_X_price_buy : 
                             X_opt == 'Chikou X Kumo sig' ? chikou_X_kumo_buy : 
                             X_opt == 'Price X Tenkan sig' ? price_X_tenkan_buy : 
                             X_opt == 'Tenkan X Kumo sig' ? tenkan_X_kumo_buy : 
                             X_opt == 'Tenkan X Kijun sig' ? tenkan_X_kijun_buy : 
                             X_opt == 'B1/S1 sig' ? buy_strong_B1 : 
                             X_opt == 'B2/S2 sig' ? buy_strong_B2 : na

entry_opt_sig_sell_1        = X_opt == '---' ? na : 
                             X_opt == 'TRIX slope sig' ? trix_sell_s :
                             X_opt == 'EMA1 x EMA2 sig' ? ema_1x2_sell_s : 
                             X_opt == 'HMA slope sig' ? hma_dn_e : 
                             X_opt == 'DMI ADX-slope sig' ? adx_dn_c : 
                             X_opt == 'DMI ADX mod sig' ? short_1 : 
                             X_opt == 'DMI ADX classic sig' ? short_2 : 
                             X_opt == 'Pin bar sig' ? p_bar_sell : 
                             X_opt == 'Inside bar sig' ? i_bar_sell : 
                             X_opt == 'Outside bar sig' ? o_bar_sell : 
                             X_opt == 'Sandwich bar sig' ? s_bar_sell : 
                             X_opt == 'Bar breakout 1 sig' ? bar_sig_1_sell : 
                             X_opt == 'Bar breakout 2 sig' ? bar_sig_2_sell : 
                             X_opt == 'Higher-low/lower-high bar sig' ? hllh_sell : 
                             X_opt == 'Entry on every bar-open sig' ? open_sell : 
                             X_opt == 'SMA sig' ? sma_sell : 
                             X_opt == 'Fractals sig' ? fr_dnx : 
                             X_opt == 'Reverse fractal sig' ? rev_up_fr_sell : 
                             X_opt == 'MACD(fast) slope sig' ? macd_slope_dn_e : 
                             X_opt == 'RSI50 sig' ? rsi_f_sell : 
                             X_opt == 'Parabolic SAR sig' ? psar_sell : 
                             X_opt == 'ALMA slope sig' ? alma_slo_dn_e : 
                             X_opt == 'SuperTrend sig' ? sup_sell : 
                             X_opt == 'Price X Kijun sig' ? kijun_sell : 
                             X_opt == 'Price X Kumo sig' ? kumo_sell : 
                             X_opt == 'Kumo flip sig' ? kumo_flip_sell : 
                             X_opt == 'Chikou X price sig' ? chikou_X_price_sell : 
                             X_opt == 'Chikou X Kumo sig' ? chikou_X_kumo_sell : 
                             X_opt == 'Price X Tenkan sig' ? price_X_tenkan_sell : 
                             X_opt == 'Tenkan X Kumo sig' ? tenkan_X_kumo_sell : 
                             X_opt == 'Tenkan X Kijun sig' ? tenkan_X_kijun_sell : 
                             X_opt == 'B1/S1 sig' ? sell_strong_S1 : 
                             X_opt == 'B2/S2 sig' ? sell_strong_S2 : na


entry_opt_sig_buy_2         = X_opt_2 == '---' ? na : 
                             X_opt_2 == 'TRIX slope sig' ? trix_buy_s :
                             X_opt_2 == 'EMA1 x EMA2 sig' ? ema_1x2_buy_s : 
                             X_opt_2 == 'HMA slope sig' ? hma_up_e : 
                             X_opt_2 == 'DMI ADX-slope sig' ? adx_up_c : 
                             X_opt_2 == 'DMI ADX mod sig' ? long_1 : 
                             X_opt_2 == 'DMI ADX classic sig' ? long_2 : 
                             X_opt_2 == 'Pin bar sig' ? p_bar_buy : 
                             X_opt_2 == 'Inside bar sig' ? i_bar_buy : 
                             X_opt_2 == 'Outside bar sig' ? o_bar_buy : 
                             X_opt_2 == 'Sandwich bar sig' ? s_bar_buy : 
                             X_opt_2 == 'Bar breakout 1 sig' ? bar_sig_1_buy : 
                             X_opt_2 == 'Bar breakout 2 sig' ? bar_sig_2_buy : 
                             X_opt_2 == 'Higher-low/lower-high bar sig' ? hllh_buy : 
                             X_opt_2 == 'Entry on every bar-open sig' ? open_buy : 
                             X_opt_2 == 'SMA sig' ? sma_buy : 
                             X_opt_2 == 'Fractals sig' ? fr_upx : 
                             X_opt_2 == 'Reverse fractal sig' ? rev_dn_fr_buy : 
                             X_opt_2 == 'MACD(fast) slope sig' ? macd_slope_up_e : 
                             X_opt_2 == 'RSI50 sig' ? rsi_f_buy : 
                             X_opt_2 == 'Parabolic SAR sig' ? psar_buy : 
                             X_opt_2 == 'ALMA slope sig' ? alma_slo_up_e : 
                             X_opt_2 == 'SuperTrend sig' ? sup_buy : 
                             X_opt_2 == 'Price X Kijun sig' ? kijun_buy : 
                             X_opt_2 == 'Price X Kumo sig' ? kumo_buy : 
                             X_opt_2 == 'Kumo flip sig' ? kumo_flip_buy : 
                             X_opt_2 == 'Chikou X price sig' ? chikou_X_price_buy : 
                             X_opt_2 == 'Chikou X Kumo sig' ? chikou_X_kumo_buy : 
                             X_opt_2 == 'Price X Tenkan sig' ? price_X_tenkan_buy : 
                             X_opt_2 == 'Tenkan X Kumo sig' ? tenkan_X_kumo_buy : 
                             X_opt_2 == 'Tenkan X Kijun sig' ? tenkan_X_kijun_buy : 
                             X_opt_2 == 'B1/S1 sig' ? buy_strong_B1 : 
                             X_opt_2 == 'B2/S2 sig' ? buy_strong_B2 : na

entry_opt_sig_sell_2        = X_opt_2 == '---' ? na : 
                             X_opt_2 == 'TRIX slope sig' ? trix_sell_s :
                             X_opt_2 == 'EMA1 x EMA2 sig' ? ema_1x2_sell_s : 
                             X_opt_2 == 'HMA slope sig' ? hma_dn_e : 
                             X_opt_2 == 'DMI ADX-slope sig' ? adx_dn_c : 
                             X_opt_2 == 'DMI ADX mod sig' ? short_1 : 
                             X_opt_2 == 'DMI ADX classic sig' ? short_2 : 
                             X_opt_2 == 'Pin bar sig' ? p_bar_sell : 
                             X_opt_2 == 'Inside bar sig' ? i_bar_sell : 
                             X_opt_2 == 'Outside bar sig' ? o_bar_sell : 
                             X_opt_2 == 'Sandwich bar sig' ? s_bar_sell : 
                             X_opt_2 == 'Bar breakout 1 sig' ? bar_sig_1_sell : 
                             X_opt_2 == 'Bar breakout 2 sig' ? bar_sig_2_sell :
                             X_opt_2 == 'Higher-low/lower-high bar sig' ? hllh_sell : 
                             X_opt_2 == 'Entry on every bar-open sig' ? open_sell : 
                             X_opt_2 == 'SMA sig' ? sma_sell : 
                             X_opt_2 == 'Fractals sig' ? fr_dnx : 
                             X_opt_2 == 'Reverse fractal sig' ? rev_up_fr_sell : 
                             X_opt_2 == 'MACD(fast) slope sig' ? macd_slope_dn_e : 
                             X_opt_2 == 'RSI50 sig' ? rsi_f_sell : 
                             X_opt_2 == 'Parabolic SAR sig' ? psar_sell : 
                             X_opt_2 == 'ALMA slope sig' ? alma_slo_dn_e : 
                             X_opt_2 == 'SuperTrend sig' ? sup_sell : 
                             X_opt_2 == 'Price X Kijun sig' ? kijun_sell : 
                             X_opt_2 == 'Price X Kumo sig' ? kumo_sell : 
                             X_opt_2 == 'Kumo flip sig' ? kumo_flip_sell : 
                             X_opt_2 == 'Chikou X price sig' ? chikou_X_price_sell : 
                             X_opt_2 == 'Chikou X Kumo sig' ? chikou_X_kumo_sell : 
                             X_opt_2 == 'Price X Tenkan sig' ? price_X_tenkan_sell : 
                             X_opt_2 == 'Tenkan X Kumo sig' ? tenkan_X_kumo_sell : 
                             X_opt_2 == 'Tenkan X Kijun sig' ? tenkan_X_kijun_sell : 
                             X_opt_2 == 'B1/S1 sig' ? sell_strong_S1 : 
                             X_opt_2 == 'B2/S2 sig' ? sell_strong_S2 : na


// buy signal options 1
opt_sig_buy_1               = htf_entr_opt_1 == 'Current' ? entry_opt_sig_buy_1[repaint == 'Allowed' ? 0 : 1] : 
                             f_secureSecurity(syminfo.tickerid, 
                             htf_entr_opt_1 == '5m' ? '5' : htf_entr_opt_1 == '10m' ? '10' : htf_entr_opt_1 == '15m' ? '15' : 
                             htf_entr_opt_1 == '30m' ? '30' : htf_entr_opt_1 == '1H' ? '60' : htf_entr_opt_1 == '2H' ? '120' : htf_entr_opt_1 == '3H' ? '180' : htf_entr_opt_1 == '4H' ? '240' : 
                             htf_entr_opt_1 == '6H' ? '360' : htf_entr_opt_1 == '12H' ? '720' : htf_entr_opt_1 == 'D' ? 'D' : htf_entr_opt_1 == '3D' ? '3D' : htf_entr_opt_1 == 'W' ? 'W' : 
                             htf_entr_opt_1 == 'M' ? 'M' : na, entry_opt_sig_buy_1)

opt_sig_buy_11              = X_opt == 'Grid - counter trend sig (no tf filter)' ? grid_ct_buy : 
                             X_opt == 'Grid - reentry sig (no tf filter)' ? grid_re_buy : 
                             X_opt == 'Fractals trend lines sig (no tf filter)' ? buy_up_line : 
                             X_opt == 'Segments sig (no tf filter)' ? segments_buy : 
                             X_opt == 'CB/CS sig (no tf filter)' ? long_c[repaint == 'Allowed' ? 0 : 1] : 
                             X_opt == 'IB/IS sig (no tf filter)' ? long_pic[repaint == 'Allowed' ? 0 : 1] : na


// sell signal options 1
opt_sig_sell_1              = htf_entr_opt_1 == 'Current' ? entry_opt_sig_sell_1[repaint == 'Allowed' ? 0 : 1] : 
                             f_secureSecurity(syminfo.tickerid, 
                             htf_entr_opt_1 == '5m' ? '5' : htf_entr_opt_1 == '10m' ? '10' : htf_entr_opt_1 == '15m' ? '15' : 
                             htf_entr_opt_1 == '30m' ? '30' : htf_entr_opt_1 == '1H' ? '60' : htf_entr_opt_1 == '2H' ? '120' : htf_entr_opt_1 == '3H' ? '180' : htf_entr_opt_1 == '4H' ? '240' : 
                             htf_entr_opt_1 == '6H' ? '360' : htf_entr_opt_1 == '12H' ? '720' : htf_entr_opt_1 == 'D' ? 'D' : htf_entr_opt_1 == '3D' ? '3D' : htf_entr_opt_1 == 'W' ? 'W' : 
                             htf_entr_opt_1 == 'M' ? 'M' : na, entry_opt_sig_sell_1)

opt_sig_sell_11             = X_opt == 'Grid - counter trend sig (no tf filter)' ? grid_ct_sell : 
                             X_opt == 'Grid - reentry sig (no tf filter)' ? grid_re_sell : 
                             X_opt == 'Fractals trend lines sig (no tf filter)' ? sell_dn_line : 
                             X_opt == 'Segments sig (no tf filter)' ? segments_sell : 
                             X_opt == 'CB/CS sig (no tf filter)' ? short_c[repaint == 'Allowed' ? 0 : 1] : 
                             X_opt == 'IB/IS sig (no tf filter)' ? short_pic[repaint == 'Allowed' ? 0 : 1] : na


// buy signal options 2
opt_sig_buy_2               = htf_entr_opt_2 == 'Current' ? entry_opt_sig_buy_2[repaint == 'Allowed' ? 0 : 1] : 
                             f_secureSecurity(syminfo.tickerid, 
                             htf_entr_opt_2 == '5m' ? '5' : htf_entr_opt_2 == '10m' ? '10' : htf_entr_opt_2 == '15m' ? '15' : 
                             htf_entr_opt_2 == '30m' ? '30' : htf_entr_opt_2 == '1H' ? '60' : htf_entr_opt_2 == '2H' ? '120' : htf_entr_opt_2 == '3H' ? '180' : htf_entr_opt_2 == '4H' ? '240' : 
                             htf_entr_opt_2 == '6H' ? '360' : htf_entr_opt_2 == '12H' ? '720' : htf_entr_opt_2 == 'D' ? 'D' : htf_entr_opt_2 == '3D' ? '3D' : htf_entr_opt_2 == 'W' ? 'W' : 
                             htf_entr_opt_2 == 'M' ? 'M' : na, entry_opt_sig_buy_2)

opt_sig_buy_22              = X_opt_2 == 'Grid - counter trend sig (no tf filter)' ? grid_ct_buy : 
                             X_opt_2 == 'Grid - reentry sig (no tf filter)' ? grid_re_buy : 
                             X_opt_2 == 'Fractals trend lines sig (no tf filter)' ? buy_up_line : 
                             X_opt_2 == 'Segments sig (no tf filter)' ? segments_buy : 
                             X_opt_2 == 'CB/CS sig (no pyr, no tf filter)' ? long_c[repaint == 'Allowed' ? 0 : 1] : 
                             X_opt_2 == 'IB/IS sig (no pyr, no tf filter)' ? long_pic[repaint == 'Allowed' ? 0 : 1] : na


// sell signal options 2
opt_sig_sell_2              = htf_entr_opt_2 == 'Current' ? entry_opt_sig_sell_2[repaint == 'Allowed' ? 0 : 1] : 
                             f_secureSecurity(syminfo.tickerid, 
                             htf_entr_opt_2 == '5m' ? '5' : htf_entr_opt_2 == '10m' ? '10' : htf_entr_opt_2 == '15m' ? '15' : 
                             htf_entr_opt_2 == '30m' ? '30' : htf_entr_opt_2 == '1H' ? '60' : htf_entr_opt_2 == '2H' ? '120' : htf_entr_opt_2 == '3H' ? '180' : htf_entr_opt_2 == '4H' ? '240' : 
                             htf_entr_opt_2 == '6H' ? '360' : htf_entr_opt_2 == '12H' ? '720' : htf_entr_opt_2 == 'D' ? 'D' : htf_entr_opt_2 == '3D' ? '3D' : htf_entr_opt_2 == 'W' ? 'W' : 
                             htf_entr_opt_2 == 'M' ? 'M' : na, entry_opt_sig_sell_2)

opt_sig_sell_22             = X_opt_2 == 'Grid - counter trend sig (no tf filter)' ? grid_ct_sell : 
                             X_opt_2 == 'Grid - reentry sig (no tf filter)' ? grid_re_sell : 
                             X_opt_2 == 'Fractals trend lines sig (no tf filter)' ? sell_dn_line : 
                             X_opt_2 == 'Segments sig (no tf filter)' ? segments_sell : 
                             X_opt_2 == 'CB/CS sig (no tf filter)' ? short_c[repaint == 'Allowed' ? 0 : 1] : 
                             X_opt_2 == 'IB/IS sig (no tf filter)' ? short_pic[repaint == 'Allowed' ? 0 : 1] : na


//---------------------------------------------------- Take profit, stop loss and trailing price --------------------------------------------------------

//take profit of average position price

atr_sec                     = request.security(syminfo.tickerid, atr_tf, ta.atr(atr_l))
atr_calc                    = atr_sec * atr_fact

tp_step = 0.
tp_step := nz(high > tp_step[1] + atr_calc or low < tp_step[1] - atr_calc ? close : tp_step[1])

tp_step_l                   = tp_step > tp_step[1]
tp_step_s                   = tp_step < tp_step[1]

av_profit_l                 = close - strategy.position_avg_price > atr_calc and tp_step_l
av_profit_s                 = strategy.position_avg_price - close > atr_calc and tp_step_s


//stop loss of average position price

atr_sec_l                   = request.security(syminfo.tickerid, atr_tf_l, ta.atr(atr_l_l))
atr_calc_l                  = atr_sec_l * atr_fact_l

sl_step = 0.
sl_step := nz(high > sl_step[1] + atr_calc_l or low < sl_step[1] - atr_calc_l ? close : sl_step[1])

sl_step_l                   = sl_step < sl_step[1]
sl_step_s                   = sl_step > sl_step[1]

sl_t_l                      = strategy.position_avg_price - close > atr_calc_l and sl_step_l
sl_t_s                      = close - strategy.position_avg_price > atr_calc_l and sl_step_s


//average position price line
plot(plot_avg_price ? strategy.position_avg_price : na, linewidth=1, color=color.new(color.blue, 30), title='position_avg_price')


//---------------------------------------------------------------- strategy entry / exit -----------------------------------------------------------------

long                        = entry_type != 'Short'                 // long or both
short                       = entry_type != 'Long'                  // short or both
both                        = entry_type == 'Both'                  // both

//exit filter
exit_long_1                 = exit_filter_sell_1 or exit_filter_sell_2 or exit_filter_sig_sell_11 or exit_filter_sig_sell_22
exit_short_1                = exit_filter_buy_1 or exit_filter_buy_2 or exit_filter_sig_buy_11 or exit_filter_sig_buy_22

//opposite signal as exit signal
exit_long_2                 = opt_sig_sell_1 or opt_sig_sell_2 or opt_sig_sell_11 or opt_sig_sell_22
exit_short_2                = opt_sig_buy_1 or opt_sig_buy_2 or opt_sig_buy_11 or opt_sig_buy_22

//long entry conditions for the 1st and the 2nd entry signal
entry_long_1                = (opt_sig_buy_11 or opt_sig_buy_1) and entry_filter_buy_1 and entry_filter_buy_2 and entry_filter_buy_11 and entry_filter_buy_22 and not exit_long_1
entry_long_2                = (opt_sig_buy_22 or opt_sig_buy_2) and entry_filter_buy_1 and entry_filter_buy_2 and entry_filter_buy_11 and entry_filter_buy_22 and not exit_long_1

//short entry conditions for the 1st and the 2nd entry signal
entry_short_1               = (opt_sig_sell_11 or opt_sig_sell_1) and entry_filter_sell_1 and entry_filter_sell_2 and entry_filter_sell_11 and entry_filter_sell_22 and not exit_short_1
entry_short_2               = (opt_sig_sell_22 or opt_sig_sell_2) and entry_filter_sell_1 and entry_filter_sell_2 and entry_filter_sell_11 and entry_filter_sell_22 and not exit_short_1



if backtest_period()
    if long
        if entry_long_1
            strategy.entry('os_b', strategy.long)                                                                               // 1st entry signal 

        if one_side_pyr and entry_long_2
            strategy.entry('os_b', strategy.long)                                                                               // 2nd entry signal when in pyramiding mode
        
        
        if not both and not one_side_pyr ? exit_long_2 : exit_long_1
            strategy.close('os_b')                                                                                              // opposite signal or exit-filter signal exit 
        
        if not both and one_side_pyr ? exit_long_1 and not exit_long_1[1] : na
            strategy.close('os_b')                                                                                              // exit-filter exit 

        if av_tp_en and av_profit_l
            strategy.close('os_b', qty_percent= av_tp_qty)                                                                      // average take profit
        
        if sl_en and sl_t_l
            strategy.close('os_b', qty_percent= av_sl_qty)                                                                      // average stop loss



    if short
        if entry_short_1
            strategy.entry('os_s', strategy.short) 

        if one_side_pyr and entry_short_2
            strategy.entry('os_s', strategy.short)


        if not both and not one_side_pyr ? exit_short_2 : exit_short_1
            strategy.close('os_s')

        if not both and one_side_pyr ? exit_short_1 and not exit_short_1[1] : na
            strategy.close('os_s')

        if av_tp_en and av_profit_s
            strategy.close('os_s', qty_percent= av_tp_qty)

        if sl_en and sl_t_s
            strategy.close('os_s', qty_percent= av_sl_qty) 



//alert messages - same conditions like strategy....() - live trading
if  long and entry_long_1 or 
     long and (one_side_pyr ? entry_long_2 : na)

    alert(message= 'BybitAPI(BTCUSD) { continue(if=positionShort); market(position= 0); } BybitAPI(BTCUSD) { wait(0.5s); market(side=buy, amount=' + str.tostring(lot_size) + '); } \n #bot', 
     freq= alert.freq_once_per_bar)


if  short and entry_short_1 or 
     short and (one_side_pyr ? entry_short_2 : na)

    alert(message= 'BybitAPI(BTCUSD) { continue(if=positionLong); market(position= 0); } BybitAPI(BTCUSD) { wait(0.5s); market(side=sell, amount=' + str.tostring(lot_size) + '); } \n #bot', 
     freq= alert.freq_once_per_bar)


if  long and (not both and not one_side_pyr ? exit_long_2 : exit_long_1) or   
     long and (not both and one_side_pyr ? exit_long_1 and not exit_long_1[1] : na) or
    
     short and (not both and not one_side_pyr ? exit_short_2 : exit_short_1) or    
     short and (not both and one_side_pyr ? exit_short_1 and not exit_short_1[1] : na)
    
    alert(message= 'BybitAPI(BTCUSD) { market(position= 0 %p); } \n #bot', freq= alert.freq_once_per_bar)


var float av_tp_qty_var     = 100 - av_tp_qty                                                                                                                           // calc because of alertatron syntax string
var float av_sl_qty_var     = 100 - av_sl_qty


if  long and (av_tp_en ? av_profit_l : na) or
     short and (av_tp_en ? av_profit_s : na)
    
    alert(message= 'BybitAPI(BTCUSD) { market(position=' + str.tostring(av_tp_qty_var) + '%p); } \n #bot', freq= alert.freq_once_per_bar)


if  long and (sl_en ? sl_t_l : na) or
     short and (sl_en ? sl_t_s : na)
    
    alert(message= 'BybitAPI(BTCUSD) { market(position=' + str.tostring(av_sl_qty_var) + '%p); } \n #bot', freq= alert.freq_once_per_bar)




