//@version=4 strategy("My Script") option_1_len = input(10, 'Bars Back for Option 1') second_crosses_decay = input(3, '"2nd Crosses" Option Decay in Bars') rsi_len = input(14, 'RSI Length') rsi_src = input(close, 'RSI Source') rsi_os = input(30, 'RSI Oversold') rsi_ob = input(70, 'RSI Overbought') // STO Input sto_k = input(14, 'Stoch K') sto_d = input(3, 'Stoch D') sto_smo = input(3, 'Stoch Smooth') sto_os = input(20, 'Stoch Oversold') sto_ob = input(80, 'Stoch Overbought') // Variables and Calculations // RSI rsi = rsi(rsi_src, rsi_len) rsi_crossover_os = crossover(rsi, rsi_os) rsi_crossover_ob = crossover(rsi, rsi_ob) rsi_crossunder_os = crossunder(rsi, rsi_os) rsi_crossunder_ob = crossunder(rsi, rsi_ob) rsi_oversold = rsi < rsi_os rsi_overbought = rsi > rsi_ob rsi_crossover_os_decay = barssince(rsi_crossover_os) >= second_crosses_decay rsi_crossunder_ob_decay = barssince(rsi_crossunder_ob) >= second_crosses_decay rsi_back_from_below = false rsi_back_from_above = false rsi_back_from_below := rsi_crossover_os? true: rsi_crossunder_os or rsi_crossover_ob or rsi_crossover_os_decay? false: rsi_back_from_below[1] rsi_back_from_above := rsi_crossunder_ob? true: rsi_crossunder_os or rsi_crossover_ob or rsi_crossunder_ob_decay? false: rsi_back_from_above[1] // Stochastic stoch_k = sma(stoch(close, high, low, sto_k), sto_smo) sto_crossover_os = crossover(stoch_k, sto_os) sto_crossover_ob = crossover(stoch_k, sto_ob) sto_crossunder_os = crossunder(stoch_k, sto_os) sto_crossunder_ob = crossunder(stoch_k, sto_ob) sto_oversold = stoch_k < sto_os sto_overbought = stoch_k > sto_ob sto_crossover_os_decay = barssince(sto_crossover_os) >= second_crosses_decay sto_crossunder_ob_decay = barssince(sto_crossunder_ob) >= second_crosses_decay sto_back_from_below = false sto_back_from_above = false sto_back_from_below := sto_crossover_os? true: sto_crossunder_os or sto_crossover_ob or sto_crossover_os_decay? false: sto_back_from_below[1] sto_back_from_above := sto_crossunder_ob? true: sto_crossunder_os or sto_crossover_ob or sto_crossunder_ob_decay? false: sto_back_from_above[1] mode_2_bull_cond = (rsi_crossover_os and sto_back_from_below[1]) or (sto_crossover_os and rsi_back_from_below[1]) mode_2_bear_cond = (rsi_crossunder_ob and sto_back_from_above[1]) or (sto_crossunder_ob and rsi_back_from_above[1]) opt1_pre_cond = 0 opt1_pre_cond := mode_2_bull_cond? 1: mode_2_bear_cond? -1: 0 last_opt1_pre_cond = 0 last_opt1_pre_cond := opt1_pre_cond == 1? 1: opt1_pre_cond == -1? -1: last_opt1_pre_cond[1] barssince_opt1_pre_cond_bull = barssince(opt1_pre_cond == 1) barssince_opt1_pre_cond_bear = barssince(opt1_pre_cond == -1) opt1_cond = barssince_opt1_pre_cond_bull <= option_1_len and last_opt1_pre_cond == 1? 1: barssince_opt1_pre_cond_bear <= option_1_len and last_opt1_pre_cond == -1? -1: 0 use_gatekeeper = input(true, '-------------------- Use Signal GateKeeper ----------------------------------------- (Confirmation that price is moving in the direction on the candle after the signal.)') gatekeeper_method = input('On Close', 'Method used for Gatekeeping', options=['On Close', 'If at any time']) gatekeeper_x = input(3, 'If X Out of the last') gatekeeper_y = input(3, 'Y bars are in the same Direction') // Gatekeeping Conditions bull_value_to_use = gatekeeper_method == 'On Close'? close: high bear_value_to_use = gatekeeper_method == 'On Close'? close: low bull_move = bull_value_to_use > close[1]? 1: 0 bear_move = bear_value_to_use < close[1]? 1: 0 // Counter bull_move_sum = sum(bull_move, gatekeeper_y) bear_move_sum = sum(bear_move, gatekeeper_y) bull_move_allow = bull_move_sum >= gatekeeper_x bear_move_allow = bear_move_sum >= gatekeeper_x use_gatekeeper_opt1 = input(true, 'Use GateKeeper on Option 1') opt1_gated_bull_cond = use_gatekeeper and use_gatekeeper_opt1? opt1_cond[gatekeeper_y] == 1 and bull_move_allow: opt1_cond == 1 opt1_gated_bear_cond = use_gatekeeper and use_gatekeeper_opt1? opt1_cond[gatekeeper_y] == -1 and bear_move_allow: opt1_cond == -1 plot(close) strategy.entry("LONG", strategy.long, when=opt1_gated_bull_cond) strategy.entry("SHORT", strategy.short, when=opt1_gated_bear_cond)