Indicators & Conditions Test Framework [DTU]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © dturkuler
//v1.06
//ADD:  Custom CRONEX T DEMARKER indicator added to custom indicator 4
//ADD:  Plotting Type constants added, fn_plotFunction and Inputs options updated
//UPD:  Increased number of spare operators to be used in future
//UPD:  Condition tooltip updated

//v1.05
//ADD:  Number of custom indicators increased from 3 to 5 (see the cutom indicator inputs and functions)
//ADD:  Added double triple, Quatr factorizer for all indicators (like convert EMA to DEMA, TEMA, QEMA...)
//ADD:  fn_factor function added 
//UPD:  fn_get_indicator updated
//UPD:  Some tooltips updated

//v1.04
//ADD:  Number condition increased from 3 to 4 for aech combination (longentry, shortentry, longclose, shortclose)
//ADD:  Stochastic, Percent value added for each indicator
//ADD:  Source data added for each indicator
//UPD:  Calculation bug on src_data removed
//UPD:  Indicators default values updated (Arranged to  BTCUSDT for testing purpose)

//v1.03
//UPD:  Longlose and shortclose codes updated. Now works as expected

//v1.02
//ADD:  Tooltips addec to the settings screen. (Tooltip constants added)
//ADD:  Falling and rising operators added to combinations. USAGE:(1st Indicator---Falling---IS(1st Indicator,VALUE)----int VALUE)
//      Calculates: ta.falling(1st indicator, VALUE)... VALUE Should be integer (1,2...n) 
//      Desc: if the 1st indicator is falling since the previous # of bars (VALUE). 
//ADD:  use Simple Log addded for testing purpose but not available for usage (to be corrected)
//UPD:  Strategy part improved. Long close and short close now works as expected (without opposite close) 
//UPD:  Constants Updated: CONDITION INDICATOR SOURCE CONSTANTS, CONDITION OPERATOR CONSTANTS
//UPD:  functions updated: fn_get_source() , fn_plotFunction(), fn_get_indicator(), fn_get_Condition_Indicator(), fn_get_Condition_Result()

//v1.01
//UPD:  Document violations removed

//v1.00
//UPD:  Condition indicator value input will be previous value of the selected indicator if "VALUE" is not selected
//ADD:  Added Profit gfx (should be improved!!!) 
//UPD:  Updated condition result & join conditions functions  with constants variables
//ADD:  Added area for Custom indicators on input panel
//ADD:  Added External indicator import on settings panel
//ADD:  Prepared documentation

//v_x.xx
//TODO: Add factorized Fibo avg range indicator (good for trend definition and entry exit points)
//TODO: Add bands to the indicator and conditions
//TODO: Add debug window for exporting indicator's parameters
//TODO: Add Alerts, Condiional alerts for indicator (study) part
//TODO: Create export function v3 for Pinecoders Indicator framework

//@version=5
SystemVersion     =     'v1.06 STRATEGY'
SystemName =            'Indicators & Conditions Test Framework '+SystemVersion+' [DTU]'
TradeId =               'IND COND TEST FRMWRK STRT'+SystemVersion
InitCapital =           1000
InitPosition =          100.0       // %10 of capital with x10 leverage
InitCommission =        0.04
InitPyramidMax =        5           // Arrange it regarding to no margin call in strategy/performance tab 
CalcOnorderFills =      false
ProcessOrdersOnClose =  false
CalcOnEveryTick =       false
marginlong=             1./10*50    // (1/10x leverage) * 50 (Margin ratio in general)  
marginshort=            1./10*50    // (1/10x leverage) * 50 (Margin ratio in general)
precision_=             4           // Keep it >=4 (I use it for data export )
float indicator1=       0.0
float indicator2=       0.0
float indicator3=       0.0
float indicator4=       0.0
float indicator5=       0.0

//indicator(title=SystemName,shorttitle=TradeId,overlay=true, precision=precision_)
strategy( title=SystemName, shorttitle=SystemName, overlay=true,
          margin_short=marginshort, margin_long=marginlong,
          pyramiding=InitPyramidMax, initial_capital=InitCapital, 
          default_qty_type=strategy.percent_of_equity, default_qty_value=InitPosition, 
          commission_type=strategy.commission.percent, commission_value=InitCommission, 
          calc_on_order_fills=CalcOnorderFills, precision=precision_, 
          process_orders_on_close=ProcessOrdersOnClose, calc_on_every_tick=CalcOnEveryTick, 
          scale=scale.left,  currency=currency.USD)                 

import dturkuler/lib_Indicators_DT/1 as ind //CREDITS: library includes indicators, snippets from tradingview , @03.freeman ("All MAs displayed") public script.. Thanks to all!!!

//************CONSTANTS{
//_______________GROUP NAME CONSTANTS
s_grp_settings=     "A) ═════════════ SETTINGS ══════════"
s_grp_plottype=     "B)══════════ PLOT TYPE OPS ═════════"
s_grp_indicators=   "C)════════════ INDICATORS ══════════"
s_grp_LEC=          "D1)══════ LONG ENTRY CONDITION ═════"
s_grp_SEC=          "D2)══════ SHORT ENTRY CONDITION ════"
s_grp_LCC=          "D3)══════ LONG CLOSE CONDITION ═════"
s_grp_SCC=          "D4)══════ SHORT CLOSE CONDITION ════"
s_grp_custom_ind=   "E) ═══════ CUSTOM INDICATORS  ══════"
s_grp_custom_ind1=  "E1)═══════ CUSTOM INDICATOR 1 ══════"
s_grp_custom_ind2=  "E2)═══════ CUSTOM INDICATOR 2 ══════"
s_grp_custom_ind3=  "E3)═══════ CUSTOM INDICATOR 3 ══════"
s_grp_custom_ind4=  "E4)═══════ CUSTOM INDICATOR 4 ══════"
s_grp_custom_ind5=  "E5)═══════ CUSTOM INDICATOR 5 ══════"

//_______________INDICATOR CONSTANTS
//i29-i33 (*ma1-*ma5) reserved for future moving average indicators that will be installed into library
h01='Hide',
i00='▼▼▼ MOV AVGs ▼▼▼'
i01='alma', 	i02='cma', 		i03='dema', 	i04='ema', 		i05='gmma', 	i06='hl2ma', 	i07='hull', 	i08='lagAdapt', i09='lagAdaptV',i10='laguerre', i11='lesrcp'
i12='linreg', 	i13='lexp', 	i14='percntl', 	i15='percntli', i16='rema', 	i17='rma', 		i18='sma', 		i19='smma', 	i20='ssma', 	i21='super2', 	i22='super3' 
i23='swma', 	i24='tema', 	i25='tma', 		i26='vida', 	i27='vwma', 	i28='wma', 		i29='*ma1', 	i30='*ma2', 	i31='*ma3', 	i32='*ma4', 	i33='*ma5'

//o25-i29 (*ot1-*ot5) reserved for future other indicators that will be installed into library
o00='▼▼▼ OTHER INDICATORS ▼▼▼'
o01='bbr', 		o02='bbw', 		o03='cci', 		o04='cctbbo', 	o05='change', 	o06='cmo', 		o07='cog', 		o08='copcurve', o09='correl', 	o10='count', 	o11='dev' 
o12='kcr', 		o13='kcw', 		o14='macd', 	o15='mfi', 		o16='roc', 		o17='rsi', 		o18='smi_Osc',	o19='smi_sig', 	o20='stdev', 	o21='trix' , 	o22='tsi'
o23='variance', o24='willprc', 	o25='*ot1', 	o26='*ot2', 	o27='*ot3', 	o28='*ot4', 	o29='*ot5'

// Currently 5 custom indicators inserted into the current Indicator code 
//c06-c08 (*ci6-*ci8) reserved for future usage. custom indicators can be added/updated in the indicator code area
c00='▼▼▼ CUSTOM INDICATORS ▼▼▼'
c01='cust ind1',c02='Custom Squeeze MOM',c03='Custom SuperTrend',c04='cust CRONEX', c05='cust ind5', 	c06='*ci6', 	c07='*ci7', 	c08='*ci8'


//_______________INDICATOR PLOTTING TYPE CONSTANTS
//Currently there are 4 plotting type exist in the system
p01='Original' , p02='Stochastic' , p03='PercentRank' , p04='Org. Range (-1,1)'

//_______________CONDITION INDICATOR SOURCE CONSTANTS
//s16-s19 (*s01-*s04) reserved for future indicator sources
s00= "NONE", s01="IND1", s02="IND2", s03="IND3", s04="IND4", s05="IND5", s06="VALUE", s07="close", s08="open", s09="high", s10="low", s11="hl2", s12="hlc3", s13="ohlc4", s14="heikin" 
s15="EXT", s16="IS(1st Indicator,VALUE)", s17="*s02", s18="*s03", s19="*s04"

//_______________CONDITION OPERATOR CONSTANTS
//op09-op11 (*op01-*op03) reserved for future operators
op01="crossover", op02="crossunder", op03="cross", op04=">", op05="<", op06=">=", op07="<=", op08="=", op09="!=", op10="rising", op11="falling", op12="%", op13="*op01", op14="*op02", op15="*op03"


//_______________TOOLTIPS CONSTANTS

s_tt_settings=      "Select the Source, timeframe and Secure type that your indicators will use.\n
                     SOURCE: Used to feed indicators source  \n
                     TIMEFRAME: indicators timeframe  \n
                     SECURE: option is defined as reducing repaint in tradingview calculations as much as possible. The following function is used.\n
                     Here, the Secure entry consists of 3 parts and the f_security function is used to determine it.\n
                     a) SECURE: This option is defined as reducing repaint in tradingview calculations as much as possible\n
                     b) SEMI SECURE : While this option can reduce repaint in tradingview calculations as much as possible, it is less secure. \n
                     c) REPAINT: This option turns on the repaint feature."

s_tt_data_ext=      "EXT SOURCE: You can import external Indicator sources from here . It appears on condition/combination area as EXT. \n To impor it you should  export your indicator value as PLOT  with a title.\n Then It will be visible in Ext data source dropdown input "
s_tt_testPeriod=    "TEST PERIOD: Determine your strategy testing period range by selecting start and end date/time"

s_tt_settings1=     "PLOT ALERTS: Plot condition result as alerts arrows on the chart's bottom for LONG and the top for SHORT entries, exits\n 
                     CLOSE ON OPPOSITE: When selected, a long entry gets closed when a short entry opens and vice versa"

s_tt_Profit=        "SHOW PROFIT:It appears if the script is in strategy mode (not in study) this can display current or open profit for better reanalyzing your strategy entry exit points. (Currently under development)"


s_tt_PlotType=      "MULT:Sets the multiplier for the selected Plot Type EXAMPLE: When 1000 is selected, the indicator in the range of (-1,1) will appear in the range of (-1000, 1000) on the screen other than Original\n
                     SHIFT:It determines the shift that will appear on the screen for the selected Plot Type ( stochastic , Percentrank, Org Range (-1,1) ) in the range (-1,1) other than Original.\n
                     SMOOTH:This option (only for Stochastic & PercentRank) allows to smooth the indicator to be displayed.\n
                     HLINE:Adjusts the horizontal lines to appear on the screen according to the mult factor for the range (-1,1). The lines represent the values (-1, -05, 0, 05 , 1)"

s_tt_ind=           "INDICATOR INPUTS:\n"+ 
                     "A) INDICATOR:\n" +  
                     " 1) MOVING AVERAGES :  These are indicators such as EMA , SMA that you can show on the stock. \n " +
                     " 2) OTHER INDICATORS : These are different indicators from the stock value such as RSI , COG. \n" +
                     " 3) CUSTOM INDICATORS: These indicators are the ones you can create by programming yourself in the source code..\n" +
                     "B) INDICATOR SOURCE: \n" +
                     " indicator source such as close, open... (Not: it does not work for custom indicators since they have their parameter on cust. Ind. input screen ) \n" +
                     "C) INDICATOR LENGTH: \n" +
                     " indicator length value . (Not: it does not work for custom indicators since they have their parameter on cust. Ind. input screen ) \n" +
                     "D) INDICATOR PLOTTING TYPE: This is an input selection field about how indicaor will be displayed on the screen. \n" +
                     "  1) ORIGINAL: The indicator is displayed on the screen with its current values. Can be used to display moving average indicators such as ( EMA , SMA ) \n" +
                     "  2) STOCHASTIC, PERCENTRANK: The indicator is displayed on the screen with stochastic calculation in the range of -1.1.It uses the stochastic (50) calculation method to spread indicators such as ( RSI , COB) over the range (-1,1). Indicators in. You can see the original values of the relevant indicator on the Data Window screen.\n" +
                     "  3) ORG RANGE (-1,1): If your indicator is in the range of -1.1, your indicator will be displayed on the screen with its original calculation in the range of -1.1.\n" +
                     "E) STOCHASTIC/PERCENTAGE VALUE:\n" +
                     "  Stoch, Perc plot type value. (Not: it does not impact plot type ORIGINAL )\n" +
                     "F) FACTORIZER VALUE:\n" +
                     "  double triple, Quatr factorizer value (like convert EMA to DEMA, TEMA, QEMA...) 1=Original \n" +
                     "G) INDICATOR COLOR:\n" +
                     "   Define indicator color on the chart "
                     

s_tt_combination=   "Each combination are build from 4 parts\n"+
                     "1)1ST INDICATOR: If set to NONE this combination will not be used on calculations. You can select IND1-5: from indicators (See above), EXT: value from externally imported indicator Stock built-in values: close, open...\n"+
                     "2)OPERATOR : Selected Operator compares 1st Indicator with the 2nd one. You can select different operators such as crossover, crossunder, cross,>,<,=....\n"+
                     "             Standart operators USAGE 1: [ 1)1st Indicator  2)cross 3)2nd Indicator 4)2nd indicator[VALUE] ] \n"+
                     "             Standart operators USAGE 2: [ 1)1st Indicator  2)cross 3)VALUE 4)Value ]  \n"+
                     "             FALLING/RISING operators USAGE:[ 1)1st Indicator---2)Falling---3)IS(1st Indicator,VALUE)----4)int VALUE ] \n"+
                     "3)2ND INDICATOR : This indicator will be compared with the 1st one via selected Operator. You can select\n" + 
                     "                  IND1-5: from indicators (See above),\n"+ 
                     "                  VALUE: a float value defined in the combinations value parameter\n  "+ 
                     "                  EXT: value from externally imported indicator.\n"+ 
                     "                  Stock builtin values: close,open...\n"+
                     "4)VALUE: When the 2nd indicator field selected as VALUE, value area compares the entered flaot value with indicator 1\n"+ 
                     "         When the 2nd indicator field selected other than VALUE, Value area define 2nd indicator's previous value ex: close[2] \n"+
                     "         When the 2nd indicator and operator fields are rising, falling , Value area define rising/falling bars ex: rising(IND1, 2)\n"

s_tt_Comb_op=       "Each combination in Condition is compared with the next one via JOIN operator. The join operator can be selected as AND or OR."

s_tt_custind=       "There is an area in the code for designing Custom Indicators.\n
                     Here you can design your own indicators and use them in the framework.\n
                     You can also create unlimited parameters for your indicators in the SETTINGS custom indicator field.\n
                     Examples are entered in the code for custom indicators."


//************CONSTANTS}

//************INPUTS{
//_______________F_SECURITY***NOREPAINT
data =            input.string(   defval="close", title='Ind Source', options=[s07, s08, s09, s10, s11, s12, s13, s14],    group=s_grp_settings, inline="settings",tooltip=s_tt_settings) 
Timeframe =       input.timeframe(defval='',    title='',                                                   group=s_grp_settings, inline="settings")
secure =          input.string(   defval='Secure',options=['Secure', 'Semi Secure', 'Repaint'], title='',   group=s_grp_settings, inline="settings")

data_ext =        input.source(   defval=close, title='Ext Source',                                         group=s_grp_settings, inline="data_ext", tooltip=s_tt_data_ext) //The ext source Accept extrenal Indicator sources also. To export the External indicator plot it with a title. It will be visible in source dropdown input

//_______________END F_SECURITY***NOREPAINT

//_______________SETTINGS 
t_testPeriodStart=   input.time(     defval=timestamp('01 Apr 2021 00:00'),title='Start Time:',             group=s_grp_settings, inline='test period', tooltip=s_tt_testPeriod)
t_testPeriodStop =   input.time(     defval=timestamp('30 Dec 2021 23:30'),title='End Time  :',             group=s_grp_settings, inline='test period')
b_plotalert =        input.bool(     defval=true,        title='Plot Alerts',                               group=s_grp_settings, inline="settings1", tooltip=s_tt_settings1)
b_isopposite =       input.bool(      defval=true,       title="Close on opposite",                         group=s_grp_settings, inline="settings1")

//_______________PLOT TYPE INPUTS 
i_ind_mult=          input.int(      defval=2000,        title="mult",                                      group= s_grp_plottype,inline="plot type",   step=100, tooltip=s_tt_PlotType)
i_ind_shift=         input.int(      defval=35000,       title="shift",                                     group= s_grp_plottype,inline="plot type",   step=1000)
//b_ind_log=           input.bool(     defval=true,        title="Use log",                                   group= s_grp_plottype,inline="plot type")
b_ind_pSWMA=         input.bool(     defval=true,        title="Smooth",                                    group= s_grp_plottype,inline="plot type")
b_ind_hline=         input.bool(     defval=true,        title="hline",                                     group= s_grp_plottype,inline="plot type")

//_______________INDICATORS DEFINITIONS 
//_______________INDICATOR 1    
s_ind1_src=          input.string(   defval='alma',      title='IND1', options=[h01, i00, i01, i02, i03, i04, i05, i06, i07, i08, i09, i10, i11, i12, i13, i14, i15, i16, i17, i18,
														    				  i19, i20, i21, i22, i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33, o00, o01, o02, o03, o04,
														    				  o05, o06, o07, o08, o09, o10, o11, o12, o13, o14, o15, o16, o17, o18, o19, o20, o21, o22, o23, o24,
														    				  o25, o26, o27, o28, o29, c00, c01, c02, c03, c04, c05, c06, c07, c08]
                                                                 ,   group= s_grp_indicators, inline="ind1")
s_ind1_data =        input.string(   defval="high",      title='',   options=[s07, s08, s09, s10, s11, s12, s13, s14],   group= s_grp_indicators, inline="ind1") 
i_ind1_len   =       input.int(      defval=300,         title='',   group= s_grp_indicators, inline="ind1")
s_ind1_pType =       input.string(   defval="Original",  title="",   group= s_grp_indicators, inline="ind1", options=[p01, p02, p03, p04])
i_ind1_stoch =       input.int(      defval=50,          title='',   group= s_grp_indicators, inline="ind1")
i_ind1_fact_ =       input.int(      defval=1,           title='',   group= s_grp_indicators, inline="ind1", minval=1,maxval=4)
c_ind1_color =       input.color(    defval=color.green, title="",   group= s_grp_indicators, inline="ind1", tooltip=s_tt_ind)


    
//_______________INDICATOR 2    
s_ind2_src=          input.string(defval='rma',          title='IND2', options= [h01, i00, i01, i02, i03, i04, i05, i06, i07, i08, i09, i10, i11, i12, i13, i14, i15, i16, i17, i18,
														  			           i19, i20, i21, i22, i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33, o00, o01, o02, o03, o04,
														  			           o05, o06, o07, o08, o09, o10, o11, o12, o13, o14, o15, o16, o17, o18, o19, o20, o21, o22, o23, o24,
														  			           o25, o26, o27, o28, o29, c00, c01, c02, c03, c04, c05, c06, c07, c08]  
                                                                 ,   group= s_grp_indicators, inline="ind2")
s_ind2_data =        input.string(   defval="close",     title='',   options=[s07, s08, s09, s10, s11, s12, s13, s14],   group= s_grp_indicators, inline="ind2") 
i_ind2_len   =       input.int(      defval=220,         title='',   group= s_grp_indicators, inline="ind2")
s_ind2_pType =       input.string(   defval="Stochastic",title="",   group= s_grp_indicators, inline="ind2", options=[p01, p02, p03, p04])
i_ind2_stoch =       input.int(      defval=50,          title='',   group= s_grp_indicators, inline="ind2")
i_ind2_fact_ =       input.int(      defval=1,           title='',   group= s_grp_indicators, inline="ind2", minval=1,maxval=4)
c_ind2_color =       input.color(    defval=color.red,   title="",   group= s_grp_indicators, inline="ind2", tooltip=s_tt_ind)
        
//_______________INDICATOR 3    
s_ind3_src=          input.string(defval='super3',       title='IND3', options= [h01, i00, i01, i02, i03, i04, i05, i06, i07, i08, i09, i10, i11, i12, i13, i14, i15, i16, i17, i18,
														  					   i19, i20, i21, i22, i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33, o00, o01, o02, o03, o04,
														  					   o05, o06, o07, o08, o09, o10, o11, o12, o13, o14, o15, o16, o17, o18, o19, o20, o21, o22, o23, o24,
														  					   o25, o26, o27, o28, o29, c00, c01, c02, c03, c04, c05, c06, c07, c08]
                                                                 ,   group= s_grp_indicators, inline="ind3")
s_ind3_data =        input.string(   defval="close",     title='',   options=[s07, s08, s09, s10, s11, s12, s13, s14],   group= s_grp_indicators, inline="ind3") 
i_ind3_len   =       input.int(      defval=210,         title='',   group= s_grp_indicators, inline="ind3")
s_ind3_pType =       input.string(   defval="Stochastic",title="",   group= s_grp_indicators, inline="ind3", options=[p01, p02, p03, p04])
i_ind3_stoch =       input.int(      defval=50,          title='',   group= s_grp_indicators, inline="ind3")
i_ind3_fact_ =       input.int(      defval=1,           title='',   group= s_grp_indicators, inline="ind3", minval=1,maxval=4)
c_ind3_color =       input.color(    defval=color.blue,  title="",   group= s_grp_indicators, inline="ind3", tooltip=s_tt_ind)
    
//_______________INDICATOR 4    
s_ind4_src=          input.string(defval='Hide',         title='IND4', options= [h01, i00, i01, i02, i03, i04, i05, i06, i07, i08, i09, i10, i11, i12, i13, i14, i15, i16, i17, i18,
														                       i19, i20, i21, i22, i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33, o00, o01, o02, o03, o04,
														                       o05, o06, o07, o08, o09, o10, o11, o12, o13, o14, o15, o16, o17, o18, o19, o20, o21, o22, o23, o24,
														                       o25, o26, o27, o28, o29, c00, c01, c02, c03, c04, c05, c06, c07, c08]
                                                                 ,   group= s_grp_indicators, inline="ind4")
s_ind4_data =        input.string(   defval="close",     title='',   options=[s07, s08, s09, s10, s11, s12, s13, s14],   group= s_grp_indicators, inline="ind4") 
i_ind4_len   =       input.int(      defval=25,          title='',   group= s_grp_indicators, inline="ind4")
s_ind4_pType =       input.string(   defval="Stochastic",title="",   group= s_grp_indicators, inline="ind4", options=[p01, p02, p03, p04])
i_ind4_stoch =       input.int(      defval=50,          title='',   group= s_grp_indicators, inline="ind4")
i_ind4_fact_ =       input.int(      defval=1,           title='',   group= s_grp_indicators, inline="ind4", minval=1,maxval=4)
c_ind4_color =       input.color(    defval=color.black, title="",   group= s_grp_indicators, inline="ind4", tooltip=s_tt_ind)
    
//_______________INDICATOR 5    
s_ind5_src=          input.string(defval='Hide',         title='IND5', options= [h01, i00, i01, i02, i03, i04, i05, i06, i07, i08, i09, i10, i11, i12, i13, i14, i15, i16, i17, i18,
														   				       i19, i20, i21, i22, i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33, o00, o01, o02, o03, o04,
														   				       o05, o06, o07, o08, o09, o10, o11, o12, o13, o14, o15, o16, o17, o18, o19, o20, o21, o22, o23, o24,
														   				       o25, o26, o27, o28, o29, c00, c01, c02, c03, c04, c05, c06, c07, c08]
                                                                 ,   group= s_grp_indicators, inline="ind5")
s_ind5_data =        input.string(   defval="close",     title='',   options=[s07, s08, s09, s10, s11, s12, s13, s14],   group= s_grp_indicators, inline="ind5") 
i_ind5_len   =       input.int(      defval=51,          title='',   group= s_grp_indicators, inline="ind5")
s_ind5_pType =       input.string(   defval="Original",  title="",   group= s_grp_indicators, inline="ind5", options=[p01, p02, p03, p04])
i_ind5_stoch =       input.int(      defval=50,          title='',   group= s_grp_indicators, inline="ind5")
i_ind5_fact_ =       input.int(      defval=1,           title='',   group= s_grp_indicators, inline="ind5", minval=1,maxval=4)
c_ind5_color =       input.color(    defval=color.purple,title="",   group= s_grp_indicators, inline="ind5", tooltip=s_tt_ind)

//************LONG ENTRY CONDITIONS 
//_______________LONG ENTRY 1 CONDITION 
s_Cond_LE_1_ind1=    input.string(defval="IND2",         title="",   options=[s00, s01, s02, s03, s04, s05, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_LEC,inline="LE_cond1")
s_Cond_LE_1_op=      input.string(defval="crossover",    title="",   options=[op01, op02, op03, op04, op05, op06, op07, op08, op09, op10, op11, op12, op13, op14, op15],       group= s_grp_LEC,inline="LE_cond1")
s_Cond_LE_1_ind2=    input.string(defval="IND3",         title="",   options=[s01, s02, s03, s04, s05, s06, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_LEC,inline="LE_cond1")
f_Cond_LE_1_ind2_val=input.float( defval=0,              title="",                             group= s_grp_LEC,   inline="LE_cond1",step=0.1, tooltip=s_tt_combination)
  
//_______________LONG ENTRY 1-2 JOIN   
s_Cond_LE_1_join_2=  input.string(defval="AND",          title="",   options=["AND","OR"] ,    group= s_grp_LEC, tooltip=s_tt_Comb_op)
      
//_______________LONG ENTRY 2 CONDITION       
s_Cond_LE_2_ind1=    input.string(defval="IND2",         title="",   options=[s00, s01, s02, s03, s04, s05, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_LEC,inline="LE_cond2")
s_Cond_LE_2_op=      input.string(defval="<",            title="",   options=[op01, op02, op03, op04, op05, op06, op07, op08, op09, op10, op11, op12, op13, op14, op15],       group= s_grp_LEC,inline="LE_cond2")
s_Cond_LE_2_ind2=    input.string(defval="VALUE",        title="",   options=[s01, s02, s03, s04, s05, s06, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_LEC,inline="LE_cond2")
f_Cond_LE_2_ind2_val=input.float( defval=-0.9,           title="",                             group= s_grp_LEC,   inline="LE_cond2",step=0.1, tooltip=s_tt_combination)
    
//_______________LONG ENTRY 2-3 JOIN    
s_Cond_LE_2_join_3=  input.string(defval="AND",          title="",   options=["AND","OR"] ,    group= s_grp_LEC, tooltip=s_tt_Comb_op)


//_______________LONG ENTRY 3 CONDITION     
s_Cond_LE_3_ind1=    input.string(defval="IND1",         title="",   options=[s00, s01, s02, s03, s04, s05, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_LEC,inline="LE_cond3")
s_Cond_LE_3_op=      input.string(defval="<",            title="",   options=[op01, op02, op03, op04, op05, op06, op07, op08, op09, op10, op11, op12, op13, op14, op15],       group= s_grp_LEC,inline="LE_cond3")
s_Cond_LE_3_ind2=    input.string(defval="low",          title="",   options=[s01, s02, s03, s04, s05, s06, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_LEC,inline="LE_cond3")
f_Cond_LE_3_ind2_val=input.float( defval=0,              title="",                             group= s_grp_LEC,   inline="LE_cond3",step=0.1, tooltip=s_tt_combination)
        
//_______________LONG ENTRY 3-4 JOIN    
s_Cond_LE_3_join_4=  input.string(defval="AND",          title="",   options=["AND","OR"] ,    group= s_grp_LEC, tooltip=s_tt_Comb_op)

//_______________LONG ENTRY 4 CONDITION     
s_Cond_LE_4_ind1=    input.string(defval="NONE",         title="",   options=[s00, s01, s02, s03, s04, s05, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_LEC,inline="LE_cond4")
s_Cond_LE_4_op=      input.string(defval="<",            title="",   options=[op01, op02, op03, op04, op05, op06, op07, op08, op09, op10, op11, op12, op13, op14, op15],       group= s_grp_LEC,inline="LE_cond4")
s_Cond_LE_4_ind2=    input.string(defval="low",          title="",   options=[s01, s02, s03, s04, s05, s06, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_LEC,inline="LE_cond4")
f_Cond_LE_4_ind2_val=input.float( defval=0,              title="",                             group= s_grp_LEC,   inline="LE_cond4",step=0.1, tooltip=s_tt_combination)



//************SHORT ENTRY CONDITIONS        
//_______________SHORT ENTRY 1 CONDITION        
s_Cond_SE_1_ind1=    input.string(defval="IND2",         title="",   options=[s00, s01, s02, s03, s04, s05, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_SEC,inline="SE_cond1")
s_Cond_SE_1_op=      input.string(defval="crossunder",   title="",   options=[op01, op02, op03, op04, op05, op06, op07, op08, op09, op10, op11, op12, op13, op14, op15],       group= s_grp_SEC,inline="SE_cond1")
s_Cond_SE_1_ind2=    input.string(defval="IND3",         title="",   options=[s01, s02, s03, s04, s05, s06, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_SEC,inline="SE_cond1")
f_Cond_SE_1_ind2_val=input.float( defval=0,              title="",                             group= s_grp_SEC,   inline="SE_cond1",step=0.1, tooltip=s_tt_combination)
         
//_______________SHORT ENTRY 1-2 JOIN        
s_Cond_SE_1_join_2=  input.string(defval="AND",          title="",   options=["AND","OR"] ,    group= s_grp_SEC, tooltip=s_tt_Comb_op)
         
//_______________SHORT ENTRY 2 CONDITION     
s_Cond_SE_2_ind1=    input.string(defval="IND2",         title="",   options=[s00, s01, s02, s03, s04, s05, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_SEC,inline="SE_cond2")
s_Cond_SE_2_op=      input.string(defval=">",            title="",   options=[op01, op02, op03, op04, op05, op06, op07, op08, op09, op10, op11, op12, op13, op14, op15],       group= s_grp_SEC,inline="SE_cond2")
s_Cond_SE_2_ind2=    input.string(defval="VALUE",        title="",   options=[s01, s02, s03, s04, s05, s06, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_SEC,inline="SE_cond2")
f_Cond_SE_2_ind2_val=input.float( defval=0.9,            title="",                             group= s_grp_SEC,   inline="SE_cond2",step=0.1, tooltip=s_tt_combination)
         
//_______________SHORT ENTRY 2-3 JOIN        
s_Cond_SE_2_join_3=  input.string(defval="AND",          title="",   options=["AND","OR"] ,    group= s_grp_SEC, tooltip=s_tt_Comb_op)
         
//_______________SHORT ENTRY 3 CONDITION         
s_Cond_SE_3_ind1=    input.string(defval="IND1",         title="",   options=[s00, s01, s02, s03, s04, s05, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_SEC,inline="SE_cond3")
s_Cond_SE_3_op=      input.string(defval=">",            title="",   options=[op01, op02, op03, op04, op05, op06, op07, op08, op09, op10, op11, op12, op13, op14, op15],       group= s_grp_SEC,inline="SE_cond3")
s_Cond_SE_3_ind2=    input.string(defval="high",         title="",   options=[s01, s02, s03, s04, s05, s06, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_SEC,inline="SE_cond3")
f_Cond_SE_3_ind2_val=input.float( defval=0,              title="",                             group= s_grp_SEC,   inline="SE_cond3",step=0.1, tooltip=s_tt_combination)
         
//_______________SHORT ENTRY 3-4 JOIN        
s_Cond_SE_3_join_4=  input.string(defval="AND",          title="",   options=["AND","OR"] ,    group= s_grp_SEC, tooltip=s_tt_Comb_op)
         
//_______________SHORT ENTRY 4 CONDITION         
s_Cond_SE_4_ind1=    input.string(defval="NONE",         title="",   options=[s00, s01, s02, s03, s04, s05, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_SEC,inline="SE_cond4")
s_Cond_SE_4_op=      input.string(defval=">",            title="",   options=[op01, op02, op03, op04, op05, op06, op07, op08, op09, op10, op11, op12, op13, op14, op15],       group= s_grp_SEC,inline="SE_cond4")
s_Cond_SE_4_ind2=    input.string(defval="high",         title="",   options=[s01, s02, s03, s04, s05, s06, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_SEC,inline="SE_cond4")
f_Cond_SE_4_ind2_val=input.float( defval=0,              title="",                             group= s_grp_SEC,   inline="SE_cond4",step=0.1, tooltip=s_tt_combination)




//************LONG CLOSE CONDITIONS          
//_______________LONG CLOSE 1 CONDITION          
s_Cond_LC_1_ind1=    input.string(defval="NONE",         title="",   options=[s00, s01, s02, s03, s04, s05, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_LCC,inline="LC_cond1")
s_Cond_LC_1_op=      input.string(defval="crossover",    title="",   options=[op01, op02, op03, op04, op05, op06, op07, op08, op09, op10, op11, op12, op13, op14, op15],       group= s_grp_LCC,inline="LC_cond1")
s_Cond_LC_1_ind2=    input.string(defval="IND1",         title="",   options=[s01, s02, s03, s04, s05, s06, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_LCC,inline="LC_cond1")
f_Cond_LC_1_ind2_val=input.float( defval=0,              title="",                             group= s_grp_LCC,   inline="LC_cond1",step=0.1, tooltip=s_tt_combination)
         
//_______________LONG CLOSE 1-2 JOIN         
s_Cond_LC_1_join_2=  input.string(defval="AND",          title="",   options=["AND","OR"] ,    group= s_grp_LCC, tooltip=s_tt_Comb_op)
         
//_______________LONG CLOSE 2 CONDITION          
s_Cond_LC_2_ind1=    input.string(defval="NONE",         title="",   options=[s00, s01, s02, s03, s04, s05, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_LCC,inline="LC_cond2")
s_Cond_LC_2_op=      input.string(defval="crossover",    title="",   options=[op01, op02, op03, op04, op05, op06, op07, op08, op09, op10, op11, op12, op13, op14, op15],       group= s_grp_LCC,inline="LC_cond2")
s_Cond_LC_2_ind2=    input.string(defval="IND2",         title="",   options=[s01, s02, s03, s04, s05, s06, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_LCC,inline="LC_cond2")
f_Cond_LC_2_ind2_val=input.float( defval=0,              title="",                             group= s_grp_LCC,   inline="LC_cond2",step=0.1, tooltip=s_tt_combination)
         
//_______________LONG CLOSE 2-3 JOIN         
s_Cond_LC_2_join_3=  input.string(defval="AND",          title="",   options=["AND","OR"] ,    group= s_grp_LCC, tooltip=s_tt_Comb_op)
         
//_______________LONG CLOSE 3 CONDITION          
s_Cond_LC_3_ind1=    input.string(defval="NONE",         title="",   options=[s00, s01, s02, s03, s04, s05, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_LCC,inline="LC_cond3")
s_Cond_LC_3_op=      input.string(defval="crossover",    title="",   options=[op01, op02, op03, op04, op05, op06, op07, op08, op09, op10, op11, op12, op13, op14, op15],       group= s_grp_LCC,inline="LC_cond3")
s_Cond_LC_3_ind2=    input.string(defval="IND3",         title="",   options=[s01, s02, s03, s04, s05, s06, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_LCC,inline="LC_cond3")
f_Cond_LC_3_ind2_val=input.float( defval=0,              title="",                             group= s_grp_LCC,   inline="LC_cond3",step=0.1, tooltip=s_tt_combination)
         
//_______________LONG CLOSE 3-4 JOIN         
s_Cond_LC_3_join_4=  input.string(defval="AND",          title="",   options=["AND","OR"] ,    group= s_grp_LCC, tooltip=s_tt_Comb_op)
         
//_______________LONG CLOSE 4 CONDITION          
s_Cond_LC_4_ind1=    input.string(defval="NONE",         title="",   options=[s00, s01, s02, s03, s04, s05, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_LCC,inline="LC_cond4")
s_Cond_LC_4_op=      input.string(defval="crossover",    title="",   options=[op01, op02, op03, op04, op05, op06, op07, op08, op09, op10, op11, op12, op13, op14, op15],       group= s_grp_LCC,inline="LC_cond4")
s_Cond_LC_4_ind2=    input.string(defval="IND3",         title="",   options=[s01, s02, s03, s04, s05, s06, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_LCC,inline="LC_cond4")
f_Cond_LC_4_ind2_val=input.float( defval=0,              title="",                             group= s_grp_LCC,   inline="LC_cond4",step=0.1, tooltip=s_tt_combination)



//************SHORT CLOSE CONDITIONS         
//_______________SHORT CLOSE 1 CONDITION         
s_Cond_SC_1_ind1=    input.string(defval="NONE",         title="",  options=[s00, s01, s02, s03, s04, s05, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_SCC,inline="SC_cond1")
s_Cond_SC_1_op=      input.string(defval="crossover",    title="",  options=[op01, op02, op03, op04, op05, op06, op07, op08, op09, op10, op11, op12, op13, op14, op15],       group= s_grp_SCC,inline="SC_cond1")
s_Cond_SC_1_ind2=    input.string(defval="IND1",         title="",  options=[s01, s02, s03, s04, s05, s06, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_SCC,inline="SC_cond1")
f_Cond_SC_1_ind2_val=input.float( defval=0,              title="",                            group= s_grp_SCC,   inline="SC_cond1",step=0.1, tooltip=s_tt_combination)
        
//_______________SHORT CLOSE 1-2 JOIN       
s_Cond_SC_1_join_2=  input.string(defval="AND",          title="",  options=["AND","OR"] ,    group= s_grp_SCC, tooltip=s_tt_Comb_op)
        
//_______________SHORT CLOSE 2 CONDITION        
s_Cond_SC_2_ind1=    input.string(defval="NONE",         title="",  options=[s00, s01, s02, s03, s04, s05, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_SCC,inline="SC_cond2")
s_Cond_SC_2_op=      input.string(defval="crossover",    title="",  options=[op01, op02, op03, op04, op05, op06, op07, op08, op09, op10, op11, op12, op13, op14, op15],       group= s_grp_SCC,inline="SC_cond2")
s_Cond_SC_2_ind2=    input.string(defval="IND2",         title="",  options=[s01, s02, s03, s04, s05, s06, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_SCC,inline="SC_cond2")
f_Cond_SC_2_ind2_val=input.float( defval=0,              title="",                            group= s_grp_SCC,   inline="SC_cond2",step=0.1, tooltip=s_tt_combination)
        
//_______________SHORT CLOSE 2-3 JOIN       
s_Cond_SC_2_join_3=  input.string(defval="AND",          title="",  options=["AND","OR"] ,    group= s_grp_SCC, tooltip=s_tt_Comb_op)
        
//_______________SHORT CLOSE 3 CONDITION        
s_Cond_SC_3_ind1=    input.string(defval="NONE",         title="",  options=[s00, s01, s02, s03, s04, s05, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_SCC,inline="SC_cond3")
s_Cond_SC_3_op=      input.string(defval="crossover",    title="",  options=[op01, op02, op03, op04, op05, op06, op07, op08, op09, op10, op11, op12, op13, op14, op15],       group= s_grp_SCC,inline="SC_cond3")
s_Cond_SC_3_ind2=    input.string(defval="IND3",         title="",  options=[s01, s02, s03, s04, s05, s06, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_SCC,inline="SC_cond3")
f_Cond_SC_3_ind2_val=input.float( defval=0,              title="",                            group= s_grp_SCC,   inline="SC_cond3",step=0.1, tooltip=s_tt_combination)

//_______________SHORT CLOSE 3-4 JOIN       
s_Cond_SC_3_join_4=  input.string(defval="AND",          title="",  options=["AND","OR"] ,    group= s_grp_SCC, tooltip=s_tt_Comb_op)
        
//_______________SHORT CLOSE 4 CONDITION        
s_Cond_SC_4_ind1=    input.string(defval="NONE",         title="",  options=[s00, s01, s02, s03, s04, s05, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_SCC,inline="SC_cond4")
s_Cond_SC_4_op=      input.string(defval="crossover",    title="",  options=[op01, op02, op03, op04, op05, op06, op07, op08, op09, op10, op11, op12, op13, op14, op15],       group= s_grp_SCC,inline="SC_cond4")
s_Cond_SC_4_ind2=    input.string(defval="IND3",         title="",  options=[s01, s02, s03, s04, s05, s06, s07, s08, s09, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19],  group= s_grp_SCC,inline="SC_cond4")
f_Cond_SC_4_ind2_val=input.float( defval=0,              title="",                            group= s_grp_SCC,   inline="SC_cond4",step=0.1, tooltip=s_tt_combination)



//************CUSTOM INDICATORS //This area can be by personal usage         
b_cut_ind      =    input.bool(   defval=true,      title=s_grp_custom_ind,                                                                         tooltip=s_tt_custind)

//_______________CUSTOM INDICATOR 1    
//EMA indicator paramaeters (@ tradingview)
s_cind1_src    =    input.string( defval='close',   title='Source',     options=['close', 'open', 'high', 'low', 'hlc3', 'hl2','ohlc4', 'heikin'],  group= s_grp_custom_ind1, inline="cind1")
i_cind1_len    =    input.int(    defval=520,       title='Len',                                                                                    group= s_grp_custom_ind1, inline="cind1")  
//_______________CUSTOM INDICATOR 2    
//Squeeze Momentum indicator parameters (@LazyBear)
s_cind2_src    =    input.string( defval='close',   title='Source',     options=['close', 'open', 'high', 'low', 'hlc3', 'hl2','ohlc4', 'heikin'],  group= s_grp_custom_ind2, inline="cind2_1")
b_cind2_TR     =    input.bool(   defval=true,      title="True Range",                                                                             group= s_grp_custom_ind2, inline="cind2_1")
i_cind2_lenBB  =    input.int(    defval=20,        title='BB Len',                                                                                 group= s_grp_custom_ind2, inline="cind2_2")
f_cind2_multBB =    input.float(  defval=2.0,       title='Fact',       step=0.1,                                                                   group= s_grp_custom_ind2, inline="cind2_2")
i_cind2_lenKC  =    input.int(    defval=20,        title='KC Len',                                                                                 group= s_grp_custom_ind2, inline="cind2_3")
f_cind2_multKC =    input.float(  defval=1.5,       title='Fact',       step=0.1,                                                                   group= s_grp_custom_ind2, inline="cind2_3")
//_______________CUSTOM INDICATOR 3    
//Supertrend indicator parameters (@ tradingview)
s_cind3_src    =    input.string( defval='close',   title='Source',     options=['close', 'open', 'high', 'low', 'hlc3', 'hl2','ohlc4', 'heikin'],  group= s_grp_custom_ind3, inline="cind3_1")
b_cind3_chgATR =    input.bool(   defval=true,      title="Change ATR Method",                                                                      group= s_grp_custom_ind3, inline="cind3_1")
i_cind3_atrP   =    input.int(    defval=10,        title='ATR Len',                                                                                group= s_grp_custom_ind3, inline="cind3_2")  
f_cind3_factor =    input.float(  defval=3.0,       title='Fact',       step=0.1,                                                                   group= s_grp_custom_ind3, inline="cind3_2")


//_______________CUSTOM INDICATOR 4    
//EMA indicator paramaeters (@ tradingview)
s_cind4_src=        input.string( defval='vwma',    title='CIND4', options=[h01, i00, i01, i02, i03, i04, i05, i06, i07, i08, i09, i10, i11, i12, i13, i14, i15, i16, i17, i18,
														    				  i19, i20, i21, i22, i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33, o00, o01, o02, o03, o04,
														    				  o05, o06, o07, o08, o09, o10, o11, o12, o13, o14, o15, o16, o17, o18, o19, o20, o21, o22, o23, o24,
														    				  o25, o26, o27, o28, o29, c00, c01, c02, c03, c04, c05, c06, c07, c08]
                                                                 ,                                                  group= s_grp_custom_ind4, inline="cind4_1")
s_cind4_data =      input.string( defval="close",   title='',   options=[s07, s08, s09, s10, s11, s12, s13, s14],   group= s_grp_custom_ind4, inline="cind4_1") 
i_cind4_demlen =    input.int(    defval=24,        title='Len',                                                    group= s_grp_custom_ind4, inline="cind4_2")  
i_cind4_step   =    input.int(    defval=4,         title='Step',                                                   group= s_grp_custom_ind4, inline="cind4_2")  
f_cind4_curv   =    input.float(  defval=0.618,     title='Curv',                                                   group= s_grp_custom_ind4, inline="cind4_2")  
b_cind4_cross =     input.bool(   defval=false,     title="Cross",                                                  group= s_grp_custom_ind4, inline="cind4_3")
 

//_______________CUSTOM INDICATOR 5    
//EMA indicator paramaeters (@ tradingview)
s_cind5_src    =    input.string( defval='close',   title='Source',     options=['close', 'open', 'high', 'low', 'hlc3', 'hl2','ohlc4', 'heikin'],  group= s_grp_custom_ind5, inline="cind5")
i_cind5_len    =    input.int(    defval=100,       title='Len',                                                                                    group= s_grp_custom_ind5, inline="cind5")  


//************END INPUTS}       



//************FUNCTIONS{
//_____________PERIOD DEFINITION
fn_testPeriod() =>         
    time >= t_testPeriodStart and time <= t_testPeriodStop ? true : false
//___________________#GET SECURE SOURCE

//_____________F_SECURITY***NOREPAINT (or Minimize it) 
f_security(_symbol, _res, _src, _secure) =>
    _secure ==    'Secure' ? request.security(_symbol, _res, _src[1], lookahead=barmerge.lookahead_on) : _secure == 'Semi Secure' ? request.security(_symbol, _res, _src[barstate.isrealtime ? 1 : 0])[barstate.isrealtime ? 0 : 1] : _secure == 'Repaint' ? request.security(_symbol, _res, _src[0])[0] : na


src_open =        f_security(syminfo.tickerid, Timeframe, open,  secure)
src_close =       f_security(syminfo.tickerid, Timeframe, close, secure)
src_high =        f_security(syminfo.tickerid, Timeframe, high,  secure)
src_low =         f_security(syminfo.tickerid, Timeframe, low,   secure)
src_hl2 =         f_security(syminfo.tickerid, Timeframe, hl2,   secure)
src_hlc3 =        f_security(syminfo.tickerid, Timeframe, hlc3,  secure)
src_ohlc4 =       f_security(syminfo.tickerid, Timeframe, ohlc4, secure)
src_heikin =      f_security(ticker.heikinashi(syminfo.tickerid), Timeframe, close, secure)



fn_get_source(string source_) => 
//     source_ == 'Avg Price'? close : 
    result  =   switch source_
        s07 =>  src_close 
        s08 =>  src_open 
        s09 =>  src_high 
        s10 =>  src_low 
        s11 =>  src_hl2 
        s12 =>  src_hlc3 
        s13 =>  src_ohlc4 
        s14 =>  src_heikin 
        =>na
    result

//src_data =        f_security(syminfo.tickerid, Timeframe, fn_get_source(data), secure)
src_external =    f_security(syminfo.tickerid, Timeframe, data_ext, secure)



//___________________#PLOTTING FUNCTIONS
fn_plotFunction(float src_, simple string plotingType_="Original", simple int stochlen_=50, bool plotSWMA_ = false, bool useSimpleLog_=false) =>
    f_plot_= switch plotingType_ 
        p02 => ta.stoch(src_, src_, src_, stochlen_) / 50 - 1 
        p03 => ta.percentrank(src_, stochlen_) / 50 - 1 
        p01 => src_
        p04 => src_
        => src_
    f_plot_:=plotSWMA_ ? ta.swma(f_plot_) : f_plot_
    f_plot_:=useSimpleLog_ ? math.log(f_plot_) : f_plot_


//**********************************************************    
//_____________CUSTOM INDICATORS DEFINITIONS
//**********************************************************    
//_____________CUSTOM INDICATOR 1 DESIGN 
// define custom indicator 1 below. Define and Get input parameters from INPUTS/CUSTOM INDICATOR 1
// you can change the name of the custom indicator by updating CONSTANTS/CUSTOM INDICATORS c01="Cust Ind1" to your preffered name Ex: c01="My Custom EMA". (name should be unique and be different from other indicators names) 
// NOTE:!!! Custom indicators dont use the value on the SETTINGS / INDICATOR selection screen.. Just accepts the parameters on the current custom indicator(n) setting area

//Builtin tradingview EMA indicator for customization example
fn_custom_ind1()=>
    source_ =   fn_get_source(s_cind1_src)  //secure selected source
    len_    =   i_cind1_len             //ex: get len for custom indicator 1 from INPUTS/CUSTOM INDICATOR 1
    result  =   ta.ema(source_,len_)             //indicator calculation return float value to result variable as output
    result

//_____________CUSTOM INDICATOR 2 DESIGN 
// Squeeze Momentum Indicator [LazyBear]  a little bit Modified and only squeeze part with -1,1 range added to show Indicator "Org. Range (-1,1)"  PlotType
// Additionally For showing indicator name change, Custom indicator name of constant "C02" updated from "Cust Ind2" to "Custom Squeeze MOM" 
fn_custom_ind2()=>
    source_ =   fn_get_source(s_cind2_src)  //secure selected source
    high_   =   fn_get_source("high")       //secure "high"
    low_    =   fn_get_source("low")        //secure "low"
    multBB_ =   f_cind2_multBB              //ex: get "BB multiplier" parameter for custom indicator 2 from INPUTS/CUSTOM INDICATOR 2
    lenBB_  =   i_cind2_lenBB               //ex: get "BB Length" parameter for custom indicator 2 from INPUTS/CUSTOM INDICATOR 2
    multKC_ =   f_cind2_multKC              //ex: get "KC multiplier" parameter for custom indicator 2 from INPUTS/CUSTOM INDICATOR 2
    lenKC_  =   i_cind2_lenKC               //ex: get "KC Length" parameter for custom indicator 2 from INPUTS/CUSTOM INDICATOR 2
    useTR_  =   b_cind2_TR                  //ex: get "Use True Range" parameter for custom indicator 2 from INPUTS/CUSTOM INDICATOR 2
   
    // Calculate BB
    basis_      =   ta.sma(source_, lenBB_)
    dev_        =   multBB_ * ta.stdev(source_, lenBB_)
    upperBB_    =   basis_ + dev_
    lowerBB_    =   basis_ - dev_
    // Calculate KC
    ma_         =   ta.sma(source_, lenKC_)
    range_      =   useTR_ ? ta.tr : (high_ - low_)
    rangema_    =   ta.sma(range_, lenKC_)
    upperKC_    =   ma_ + rangema_ * multKC_
    lowerKC_    =   ma_ - rangema_ * multKC_
    // Calculate Squeeze
    sqzOn_      =   (lowerBB_ > lowerKC_) and (upperBB_ < upperKC_)
    sqzOff_     =   (lowerBB_ < lowerKC_) and (upperBB_ > upperKC_)
    noSqz_      =   (sqzOn_ == false) and (sqzOff_ == false)
    result      =   sqzOn_? 1.0: sqzOff_? -1.0: noSqz_? 0.0: na   //indicator calculation return float value to result variable as indicator output
    result

//_____________CUSTOM INDICATOR 3 DESIGN 
// SuperTrend Indicator [@KivancOzbilgic]  a little bit Modified 
// Additionally For showing indicator name change, Custom indicator name of constant "C03" updated from "Cust Ind3" to "Custom SuperTrend" 
fn_custom_ind3()=>
    source_ =   fn_get_source(s_cind3_src)  //secure selected source
	close_  =   fn_get_source("close")      //secure "close"
    chg_ATR_=   b_cind3_chgATR              //ex: get ATR Method parameter for custom indicator 3 from INPUTS/CUSTOM INDICATOR 3
    atr_per_=   i_cind3_atrP                //ex: get ATR period parameter for custom indicator 3 from INPUTS/CUSTOM INDICATOR 3
    atr_fct_=   f_cind3_factor              //ex: get ATR Factor parameter for custom indicator 3 from INPUTS/CUSTOM INDICATOR 3
    
	atr2_   =   ta.sma(ta.tr, atr_per_)
	atr_    =   chg_ATR_ ? ta.atr(atr_per_) : atr2_
	up_     =   source_-(atr_fct_*atr_)
	up1_    =   nz(up_[1], up_)
	up_     :=  close_[1] > up1_ ? math.max(up_,up1_) : up_
	dn_     =   source_ + (atr_fct_ * atr_)
	dn1_    =   nz(dn_[1], dn_)
	dn_     :=  close_[1] < dn1_ ? math.min(dn_, dn1_) : dn_
	trend_  =   1
	trend_  :=  nz(trend_[1], trend_)
	trend_  :=  trend_ == -1 and close_ > dn1_ ? 1 : trend_ == 1 and close_ < up1_ ? -1 : trend_
	result  =   trend_ == 1 ? up_ : dn_  //indicator calculation return float value to result variable as indicator output
    result




//_____________CUSTOM INDICATOR 4 DESIGN 
//Cronex T Demarker indicator for customization example
fn_demarker(simple int dem_length) =>
    deMax = src_high - src_high[1] > 0 ? src_high - src_high[1] : 0
    deMin = src_low[1] - src_low > 0 ? src_low[1] - src_low : 0
    sma_deMax = ind.f_funcPlot(s_cind4_src,deMax, length_=dem_length)
    sma_deMin = ind.f_funcPlot(s_cind4_src,deMin, length_=dem_length)
    sma_deMax / (sma_deMax + sma_deMin)

fn_cronex() =>
    bool crossing           = b_cind4_cross
    int demarker_length     = i_cind4_demlen
    int dem_step            = i_cind4_step
    float curvature         = f_cind4_curv

    e1 = 0.0, e2 = 0.0,e3 = 0.0, e4 = 0.0, e5 = 0.0, e6 = 0.0
    n  = 1 + 0.5 * (demarker_length - 1)
    w1 = 2 / (demarker_length + 1)
    w2 = 1 - w1
    b2 = curvature * curvature
    b3 = b2 * curvature
    c1 = -b3
    c2 = (3 * (b2 + b3))
    c3 = -3 * (2 * b2 + curvature + b3)
    c4 = (1 + 3 * curvature + b3 + 3 * b2)
    
    demarker_v = (fn_demarker(demarker_length) + fn_demarker(demarker_length + dem_step) + fn_demarker(demarker_length + dem_step * 2) + fn_demarker(demarker_length + dem_step * 3)) * 100 / 4 - 50
    e1 := not na(demarker_v) ? w1 * demarker_v + w2 * e1[1] : 0
    e2 := not na(demarker_v) ? w1 * e1 + w2 * e2[1] : 0
    e3 := not na(demarker_v) ? w1 * e2 + w2 * e3[1] : 0
    e4 := not na(demarker_v) ? w1 * e3 + w2 * e4[1] : 0
    e5 := not na(demarker_v) ? w1 * e4 + w2 * e5[1] : 0
    e6 := not na(demarker_v) ? w1 * e5 + w2 * e6[1] : 0
    
    demarker_t = c1*e6 + c2*e5 + c3*e4 + c4*e3
    signal = 0
    if crossing
        signal := ta.crossover(demarker_v,demarker_t)?1:ta.crossunder(demarker_v,demarker_t)?-1:0
    else
        signal := (demarker_v > demarker_t)? 1:-1
    [signal,demarker_v,demarker_t]

fn_custom_ind4()=>
    [x,y,z]=fn_cronex()
    result=x


//_____________CUSTOM INDICATOR 5 DESIGN 
//Builtin tradingview EMA indicator for customization example
fn_custom_ind5()=>
    source_ =   fn_get_source(s_cind5_src)  //secure selected source
    len_    =   i_cind5_len             //ex: get len for custom indicator 1 from INPUTS/CUSTOM INDICATOR 1
    result  =   ta.ema(source_,len_)             //indicator calculation return float value to result variable as output
    result



//**********************************************************    
//_____________END CUSTOM INDICATORS DEFINITIONS
//********************************************************** 


//_____________FACTORIZE INDICATOR
//Add double triple, Quatr factor to selected  indicator (like convert EMA to DEMA, TEMA, QEMA...)
fn_factor(simple string ind_src_,float source_,simple int length_,simple int fact_=1)=>
    float xIND1 = ind.f_funcPlot(ind_src_,source_, length_=length_)
    float xIND2 = ind.f_funcPlot(ind_src_,xIND1, length_=length_)
    float xIND3 = ind.f_funcPlot(ind_src_,xIND2, length_=length_)
    float xIND4 = ind.f_funcPlot(ind_src_,xIND3, length_=length_)
    float xIND5 = ind.f_funcPlot(ind_src_,xIND4, length_=length_)
    //float indfact_=0
    indfact_ = switch fact_
        1      =>  xIND1
        2      =>  (2 * xIND1) - xIND2
        3      =>  3 * (xIND1 - xIND2) + xIND3
        4      =>  5 * xIND1 - 10 * xIND2 + 10 * xIND3 - 5 * xIND4 + xIND5  
    float result=indfact_ 
    result

//_____________GET SELECTED INDICATOR VALUE
fn_get_indicator(simple string ind_src_, simple string ind_src_data_="close",simple int length_, simple string plotingType_,simple int stoch_length_=50,simple int ind_factor_=1)=>
    float indicator_=0.0
    bool b_ind_pSWMA_= switch plotingType_
        "Original"          => false
        "Org. Range (-1,1)" => false
        => b_ind_pSWMA

//    bool b_ind_log_= switch plotingType_
//        "Original"          => false
//        "Org. Range (-1,1)" => false
//        => b_ind_log

    indicator_ := switch ind_src_ 
        c01 => fn_custom_ind1()
        c02 => fn_custom_ind2()
        c03 => fn_custom_ind3()
        c04 => fn_custom_ind4()
        c05 => fn_custom_ind5()
        => fn_factor(ind_src_=ind_src_,source_=fn_get_source(ind_src_data_), length_=length_, fact_=ind_factor_)
//        => ind.f_funcPlot(ind_src_,fn_get_source(ind_src_data_), length_=length_)
        
        
    indicator_:=fn_plotFunction(src_=indicator_, plotingType_=plotingType_, stochlen_=stoch_length_, plotSWMA_ = b_ind_pSWMA_)
    indicator_



//_____________CONDITION RESULT
fn_get_Condition_Indicator(string cond_ind_,float cond_ind_value_,bool first_ind=true)=>
// if selection other than "VALUE"
// and if first_ind=false then use condition's value field for second  indicator's #previous value
    float cond_ind_val_ = switch cond_ind_
        s00	=> 0.
    	s01	=> first_ind?indicator1 : indicator1[cond_ind_value_]
    	s02	=> first_ind?indicator2 : indicator2[cond_ind_value_]
    	s03	=> first_ind?indicator3 : indicator3[cond_ind_value_]
    	s04	=> first_ind?indicator4 : indicator4[cond_ind_value_]
    	s05	=> first_ind?indicator5 : indicator5[cond_ind_value_]
    	s06	=> cond_ind_value_
    	s07	=> first_ind? src_close : src_close[cond_ind_value_] 
    	s08	=> first_ind? src_open  : src_open[cond_ind_value_]  
    	s09	=> first_ind? src_high  : src_high[cond_ind_value_]  
    	s10	=> first_ind? src_low   : src_low[cond_ind_value_]   
    	s11	=> first_ind? src_hl2   : src_hl2[cond_ind_value_]   
    	s12	=> first_ind? src_hlc3  : src_hlc3[cond_ind_value_]  
    	s13	=> first_ind? src_ohlc4 : src_ohlc4[cond_ind_value_] 
    	s14	=> first_ind? src_heikin: src_heikin[cond_ind_value_]
    	s15	=> first_ind? src_external: src_external[cond_ind_value_]
        s16	=> cond_ind_value_
        s17	=> 0.
        s18	=> 0.
        s19	=> 0.
        => 0.
    cond_ind_val_
    
fn_get_Condition_Result(string cond_ind1_,string cond_op_,string cond_ind2_,float cond_ind2_value_)=>
    result=0
    if fn_testPeriod()
        cond_ind1_val_ = fn_get_Condition_Indicator(cond_ind1_, cond_ind2_value_, true)
        cond_ind2_val_ = fn_get_Condition_Indicator(cond_ind2_, cond_ind2_value_, false)

        int cond_result_=   switch cond_op_     
        	op01	=> ta.crossover(cond_ind1_val_,cond_ind2_val_)?  1 : -1
        	op02	=> ta.crossunder(cond_ind1_val_,cond_ind2_val_)? 1 : -1
        	op03	=> ta.cross(cond_ind1_val_,cond_ind2_val_)? 1 : -1
        	op04	=> cond_ind1_val_ >  cond_ind2_val_ ? 1 : -1
        	op05	=> cond_ind1_val_ <  cond_ind2_val_ ? 1 : -1
        	op06	=> cond_ind1_val_ >= cond_ind2_val_ ? 1 : -1
        	op07	=> cond_ind1_val_ <= cond_ind2_val_ ? 1 : -1
        	op08	=> cond_ind1_val_ == cond_ind2_val_ ? 1 : -1
        	op09	=> cond_ind1_val_ != cond_ind2_val_ ? 1 : -1
        	op10	=> ta.rising( cond_ind1_val_,int(cond_ind2_val_))?1:-1
            op11	=> ta.falling(cond_ind1_val_,int(cond_ind2_val_))?1:-1
            op12	=> 0
			=> 0
        result:=cond_ind1_=="NONE"?0:cond_result_
    result

//_____________JOIN CONDITIONS
fn_join_Condition_Result(int cond1_=0,int cond2_=0,int cond3_=0,int cond4_=0, string cond_1_join_2_="AND", string cond_2_join_3_="AND", string cond_3_join_4_="AND")=>
    bool cond_=false
    if cond1_!=0
        cond_:=cond1_==1
        if cond1_!=0 and cond2_ !=0
            cond_ := switch cond_1_join_2_
            	"AND"   => cond1_==1 and cond2_==1
            	"OR"    => cond1_==1 or cond2_ ==1
            if cond3_ !=0
                cond_:= switch cond_2_join_3_
                	"AND"   => cond_==1 and cond3_==1
                	"OR"    => cond_==1 or  cond3_==1
                if cond4_ !=0
                    cond_:= switch cond_3_join_4_
                    	"AND"   => cond_==1 and cond4_==1
                    	"OR"    => cond_==1 or  cond4_==1


    result=cond_



//_____________GET CONDITIONS
fn_get_conditions(string type_)=>
    result=false
    if type_=="LONG" and (s_Cond_LE_1_ind1!="NONE" or s_Cond_LE_2_ind1!="NONE" or s_Cond_LE_3_ind1!="NONE") 
        int long_entry1=fn_get_Condition_Result(s_Cond_LE_1_ind1,s_Cond_LE_1_op,s_Cond_LE_1_ind2,f_Cond_LE_1_ind2_val)
        int long_entry2=fn_get_Condition_Result(s_Cond_LE_2_ind1,s_Cond_LE_2_op,s_Cond_LE_2_ind2,f_Cond_LE_2_ind2_val)
        int long_entry3=fn_get_Condition_Result(s_Cond_LE_3_ind1,s_Cond_LE_3_op,s_Cond_LE_3_ind2,f_Cond_LE_3_ind2_val)
        int long_entry4=fn_get_Condition_Result(s_Cond_LE_4_ind1,s_Cond_LE_4_op,s_Cond_LE_4_ind2,f_Cond_LE_4_ind2_val)
        result := fn_join_Condition_Result(cond1_=long_entry1,cond2_=long_entry2,cond3_=long_entry3,cond4_=long_entry4, cond_1_join_2_=s_Cond_LE_1_join_2, cond_2_join_3_=s_Cond_LE_2_join_3, cond_3_join_4_=s_Cond_LE_3_join_4)
    
    if type_=="SHORT" and (s_Cond_SE_1_ind1!="NONE" or s_Cond_SE_2_ind1!="NONE" or s_Cond_SE_3_ind1!="NONE")
        int short_entry1=fn_get_Condition_Result(s_Cond_SE_1_ind1,s_Cond_SE_1_op,s_Cond_SE_1_ind2,f_Cond_SE_1_ind2_val)
        int short_entry2=fn_get_Condition_Result(s_Cond_SE_2_ind1,s_Cond_SE_2_op,s_Cond_SE_2_ind2,f_Cond_SE_2_ind2_val)
        int short_entry3=fn_get_Condition_Result(s_Cond_SE_3_ind1,s_Cond_SE_3_op,s_Cond_SE_3_ind2,f_Cond_SE_3_ind2_val)
        int short_entry4=fn_get_Condition_Result(s_Cond_SE_4_ind1,s_Cond_SE_4_op,s_Cond_SE_4_ind2,f_Cond_SE_4_ind2_val)
//        result := fn_join_Condition_Result(short_entry1,short_entry2,short_entry3, s_Cond_SE_1_join_2, s_Cond_SE_2_join_3)
        result := fn_join_Condition_Result(cond1_=short_entry1,cond2_=short_entry2,cond3_=short_entry3,cond4_=short_entry4, cond_1_join_2_=s_Cond_SE_1_join_2, cond_2_join_3_=s_Cond_SE_2_join_3, cond_3_join_4_=s_Cond_SE_3_join_4)
    
    if type_=="LONGCLOSE" and (s_Cond_LC_1_ind1!="NONE" or s_Cond_LC_2_ind1!="NONE" or s_Cond_LC_3_ind1!="NONE")
        int long_close1=fn_get_Condition_Result(s_Cond_LC_1_ind1,s_Cond_LC_1_op,s_Cond_LC_1_ind2,f_Cond_LC_1_ind2_val)
        int long_close2=fn_get_Condition_Result(s_Cond_LC_2_ind1,s_Cond_LC_2_op,s_Cond_LC_2_ind2,f_Cond_LC_2_ind2_val)
        int long_close3=fn_get_Condition_Result(s_Cond_LC_3_ind1,s_Cond_LC_3_op,s_Cond_LC_3_ind2,f_Cond_LC_3_ind2_val)
        int long_close4=fn_get_Condition_Result(s_Cond_LC_4_ind1,s_Cond_LC_4_op,s_Cond_LC_4_ind2,f_Cond_LC_4_ind2_val)
//        result := fn_join_Condition_Result(long_close1,long_close2,long_close3, s_Cond_LC_1_join_2, s_Cond_LC_2_join_3)
        result := fn_join_Condition_Result(cond1_=long_close1,cond2_=long_close2,cond3_=long_close3,cond4_=long_close4, cond_1_join_2_=s_Cond_LC_1_join_2, cond_2_join_3_=s_Cond_LC_2_join_3, cond_3_join_4_=s_Cond_LC_3_join_4)
    
    if type_=="SHORTCLOSE" and (s_Cond_SC_1_ind1!="NONE" or s_Cond_SC_2_ind1!="NONE" or s_Cond_SC_3_ind1!="NONE")
        int short_close1=fn_get_Condition_Result(s_Cond_SC_1_ind1,s_Cond_SC_1_op,s_Cond_SC_1_ind2,f_Cond_SC_1_ind2_val)
        int short_close2=fn_get_Condition_Result(s_Cond_SC_2_ind1,s_Cond_SC_2_op,s_Cond_SC_2_ind2,f_Cond_SC_2_ind2_val)
        int short_close3=fn_get_Condition_Result(s_Cond_SC_3_ind1,s_Cond_SC_3_op,s_Cond_SC_3_ind2,f_Cond_SC_3_ind2_val)
        int short_close4=fn_get_Condition_Result(s_Cond_SC_4_ind1,s_Cond_SC_4_op,s_Cond_SC_4_ind2,f_Cond_SC_4_ind2_val)
//        result := fn_join_Condition_Result(short_close1,short_close2,short_close3, s_Cond_SC_1_join_2, s_Cond_SC_2_join_3)
        result := fn_join_Condition_Result(cond1_=short_close1,cond2_=short_close2,cond3_=short_close3,cond4_=short_close4, cond_1_join_2_=s_Cond_SC_1_join_2, cond_2_join_3_=s_Cond_SC_2_join_3, cond_3_join_4_=s_Cond_SC_3_join_4)
    result
//************END FUNCTIONS}

//************CUSTOM INDICATORS CALCULATIONS{   
float custom_ind1=fn_custom_ind1()      //dont change "float custom_ind1". this is result of the indicator used by the system 
float custom_ind2=fn_custom_ind2()      //dont change "float custom_ind2". this is result of the indicator used by the system 
float custom_ind3=fn_custom_ind3()      //dont change "float custom_ind3". this is result of the indicator used by the system 
float custom_ind4=fn_custom_ind4()      //dont change "float custom_ind4". this is result of the indicator used by the system 
float custom_ind5=fn_custom_ind5()      //dont change "float custom_ind5". this is result of the indicator used by the system 
//******END CUSTOM INDICATORS CALCULATIONS}


//************SIGNAL CALCULATIONS{
indicator1:=    fn_get_indicator(s_ind1_src,ind_src_data_=s_ind1_data,length_=i_ind1_len, plotingType_=s_ind1_pType,stoch_length_=i_ind1_stoch, ind_factor_=i_ind1_fact_)
indicator2:=    fn_get_indicator(s_ind2_src,ind_src_data_=s_ind2_data,length_=i_ind2_len, plotingType_=s_ind2_pType,stoch_length_=i_ind2_stoch, ind_factor_=i_ind2_fact_)
indicator3:=    fn_get_indicator(s_ind3_src,ind_src_data_=s_ind3_data,length_=i_ind3_len, plotingType_=s_ind3_pType,stoch_length_=i_ind3_stoch, ind_factor_=i_ind3_fact_)
indicator4:=    fn_get_indicator(s_ind4_src,ind_src_data_=s_ind4_data,length_=i_ind4_len, plotingType_=s_ind4_pType,stoch_length_=i_ind4_stoch, ind_factor_=i_ind4_fact_)
indicator5:=    fn_get_indicator(s_ind5_src,ind_src_data_=s_ind5_data,length_=i_ind5_len, plotingType_=s_ind5_pType,stoch_length_=i_ind5_stoch, ind_factor_=i_ind5_fact_)
    
bool long=      fn_get_conditions("LONG")
bool short=     fn_get_conditions("SHORT")
bool longclose= fn_get_conditions("LONGCLOSE")     
bool shortclose=fn_get_conditions("SHORTCLOSE")    
if b_isopposite
    longclose:= longclose or short
    shortclose:=shortclose or long
//************END SIGNAL CALCULATIONS}

//************PLOTs{
//_________PLOT INDICATORS  (if Indicator PlotType is "Original" plot over chart otherwise plot between HLines) 
plot(s_ind1_src!="Hide" ? (s_ind1_pType=="Original"?indicator1: i_ind_shift + (indicator1 * i_ind_mult)):na, "",c_ind1_color) 
plot(s_ind2_src!="Hide" ? (s_ind2_pType=="Original"?indicator2: i_ind_shift + (indicator2 * i_ind_mult)):na, "",c_ind2_color) 
plot(s_ind3_src!="Hide" ? (s_ind3_pType=="Original"?indicator3: i_ind_shift + (indicator3 * i_ind_mult)):na, "",c_ind3_color) 
plot(s_ind4_src!="Hide" ? (s_ind4_pType=="Original"?indicator4: i_ind_shift + (indicator4 * i_ind_mult)):na, "",c_ind4_color) 
plot(s_ind5_src!="Hide" ? (s_ind5_pType=="Original"?indicator5: i_ind_shift + (indicator5 * i_ind_mult)):na, "",c_ind5_color) 

//_________SHOW REAL INDICATOR VALUES ON DATA WINDOW
plotshape(s_ind1_src!="Hide" ? indicator1:na, "INDICATOR1", text="",color=color.new(c_ind1_color,100)) 
plotshape(s_ind1_src!="Hide" ? indicator2:na, "INDICATOR2", text="",color=color.new(c_ind2_color,100)) 
plotshape(s_ind1_src!="Hide" ? indicator3:na, "INDICATOR3", text="",color=color.new(c_ind3_color,100)) 
plotshape(s_ind1_src!="Hide" ? indicator4:na, "INDICATOR4", text="",color=color.new(c_ind4_color,100)) 
plotshape(s_ind1_src!="Hide" ? indicator5:na, "INDICATOR5", text="",color=color.new(c_ind5_color,100)) 

//_________PLOT HLINE for Stochastic/Precentage/Org Range(-1,1) between range -1 and 1
hline(b_ind_hline?i_ind_shift + (1 *  i_ind_mult):na, title="LINE 1",   linestyle=hline.style_dashed ,color=color.new(color.black, 0)) 
hline(b_ind_hline?i_ind_shift + (0.5 *i_ind_mult):na, title="LINE 0.5", linestyle=hline.style_dashed ,color=color.new(color.black, 75)) 
hline(b_ind_hline?i_ind_shift + (0 *  i_ind_mult):na, title="LINE 0",   linestyle=hline.style_dashed ,color=color.new(color.black, 0)) 
hline(b_ind_hline?i_ind_shift + (-0.5*i_ind_mult):na, title="LINE -0.5",linestyle=hline.style_dashed ,color=color.new(color.black, 75)) 
hline(b_ind_hline?i_ind_shift + (-1 * i_ind_mult):na, title="LINE -1",  linestyle=hline.style_dashed ,color=color.new(color.black, 0)) 

//_________PLOT ALERTS for LONG at Bottom and for SHORT at TOP of the screen 
plotshape(b_plotalert ? long : na,        title='LONG',      style=shape.triangleup,    location=location.bottom,   color=color.new(color.green, 0),size=size.tiny)
plotshape(b_plotalert ? short : na,       title='SHORT',     style=shape.triangledown,  location=location.top,      color=color.new(color.blue, 0), size=size.tiny)
plotshape(b_plotalert ? longclose : na,   title='EXIT LONG', style=shape.triangledown,  location=location.bottom,   color=color.new(color.red, 0),  size=size.tiny)
plotshape(b_plotalert ? shortclose : na,  title='EXIT SHORT',style=shape.triangleup,    location=location.top,      color=color.new(color.black, 0),size=size.tiny)

//************END PLOTs}

//************STRATEGY DEFINITIONS   (For indicator usage: Mark following lines &  Change strategy(....) to indicator(....) at start of the code {
//________________STRATEGY ENTRIES & EXITS
if fn_testPeriod() and long       
//    strategy.entry("buy",strategy.long,alert_message=web_hook("Buy",close,bot_json))
    strategy.entry("buy", strategy.long, alert_message="L",comment="L")

if fn_testPeriod() and short       
//    strategy.entry("sell",strategy.short,alert_message=web_hook("Sell",close,bot_json))
    strategy.entry("sell",strategy.short,alert_message="S",comment="S")

if fn_testPeriod() and longclose 
//    strategy.close_all(alert_message=web_hook("Close",close,bot_json))
    strategy.close("buy", alert_message="CL",comment="CL")

if fn_testPeriod() and shortclose
//    strategy.close_all(alert_message=web_hook("Close",close,bot_json))
    strategy.close("sell",alert_message="CS",comment="CS")
//-------------------------------------------------------------

//_____________SHOW PROFIT    // Should be improved I am stucked !!!
s_showProfit=       input.string(   defval="None",  title="Show Profit", options=["None", "Open Profit", "Current Profit" ],  group=s_grp_settings,inline="show profit")
i_showProfit_trans= input.int(      defval=80,      title="Transparency",                                                     group=s_grp_settings,inline="show profit", tooltip=s_tt_Profit)

fn_get_profit_bytype(string s_profitType_)=>
    float f_profit_=switch s_profitType_
        "None"          =>  na
        "Current Profit"=>  (strategy.netprofit+strategy.openprofit)
        "Open Profit"   =>  strategy.openprofit
    f_profit_
var openposprof= array.new_float(0)

float cur=0.
float max=1.
float min=0.
if s_showProfit!="None"
    array.push(openposprof,fn_get_profit_bytype(s_showProfit)/strategy.initial_capital)
    cur:=array.get(openposprof,bar_index)
    max:=array.max(openposprof)
    min:=array.min(openposprof)
float f_profit=cur/ (max - min)
f_profitper=f_profit

fill(plot(s_showProfit!="None"?i_ind_shift + (f_profit * i_ind_mult):na,   title="",color=f_profit<0?color.new(color.red,i_showProfit_trans):color.new(color.green,i_showProfit_trans)),
     plot(s_showProfit!="None"?i_ind_shift:na,                                title="",color=color.new(color.white,100)),
     f_profit<0?color.new(color.red,i_showProfit_trans+5):color.new(color.green,i_showProfit_trans+5))  //Fill area on Profit value on between hlines
plotshape(s_showProfit!="None"?f_profit*100:na,  title="PROFIT%",text="",color=color.new(color.white,100))  //Show Real Profit value on Data Window
//************END STRATEGY DEFINITIONS}

////************INDICATOR's SIGNALS EXPORT{
//// Followings are for my personal usage on my own strategy framework. 
//// Created Parallel data transfer algorithm (@DTurkuler)
//// With this algo all builded signals can be transferred to another strategy/indicator without interferring with another signal

//longCondition =long
//shortCondition = short
//ExitLongCondition = longclose
//ExitshortCondition = shortclose
//
//Signal1 = 0.
////Long Entry Signals 
////(10:Long Entry 20:Long Exit 30:No Operation)
//Signal1 := longCondition ? 10 : ExitLongCondition ? 20 : 30

////Short Entry Signals 
////(10:Long Entry 20:Long Exit 30:No Operation)
//Signal1 := Signal1 + (shortCondition ? 1 : ExitshortCondition ? 2 : 3)

////TODO: Entry/Exit Filter Signals (1:Entry 2:No Entry 3:No Operation)
////Signal1 := Signal1 + (entryFilter ? 100 : exitFilter ? 200 : 300)

////TODO: Trend Filter Signals (1:Up 2:down 3:Sideways)
////Signal1 := Signal1 + (trend==1 ? 1000 : trend==-1 ? 2000 : 3000)
//

////_______________DATA ENCODER V2 (Set precision to 4)
//// V2 is used to plot the indicator values  over the stock. 
//// Signal data shifted to digit area and close value added as carrier to the fractional part   
//ext_version= input.int(      title='External Data Version',      defval=1,       group='═════════════ SETTINGS ═════════════' )
//encoder(float data_, source_=close)=>
//    float Signal_=0.
//    float f_carrier=int(source_)
//    Signal_:=0.0001*data_
//    Signal_+=f_carrier
//    result=Signal_
//Signal1:=ext_version==2?encoder(data_=Signal1):Signal1
////_______________DATA ENCODER
//
//plot(Signal1, title='🔌Connector🔌', color=color.new(color.blue,100))
//plotshape(b_plotalert ? longCondition : na,title='LONG', style=shape.triangleup, location=location.bottom, color=color.new(color.green, 0), size=size.tiny)
//plotshape(b_plotalert ? shortCondition : na, title='SHORT', style=shape.triangledown, location=location.top, color=color.new(color.blue, 0), size=size.tiny)
//plotshape(b_plotalert ? ExitLongCondition : na, title='EXIT LONG', style=shape.triangledown, location=location.bottom, color=color.new(color.red, 0), size=size.tiny)
//plotshape(b_plotalert ? ExitshortCondition : na, title='EXIT SHORT', style=shape.triangleup, location=location.top, color=color.new(color.black, 0), size=size.tiny)
////************END INDICATOR's SIGNALS EXPORT}




