//@version=6 strategy("CCI Lab: thresholds, continuation and recovery", overlay=false, initial_capital=10000, default_qty_type=strategy.fixed, default_qty_value=1, process_orders_on_close=false) cci_length = input.int(20, "CCI lookback", minval=2, maxval=100) threshold = input.int(100, "Absolute entry threshold", minval=25, maxval=325) rule_mode = input.int(0, "Rule: 0 strength, 1 fade, 2 recovery", minval=0, maxval=2) cci_value = ta.cci(hlc3, cci_length) recovery = ta.crossover(cci_value, -threshold) and cci_value[1] < -threshold // The newest observation belongs to the same mean-deviation window. // A strict excursion cannot exceed the bound N / 0.03. feasible = threshold < cci_length / 0.03 - 0.000001 ready = bar_index >= 500 and not na(cci_value) and feasible enter_long = ready and (rule_mode == 0 ? cci_value > threshold : rule_mode == 1 ? cci_value < -threshold : recovery) exit_long = ready and (rule_mode == 0 ? cci_value < 0 : cci_value >= 0) plot(cci_value, "CCI", color=color.blue) plot(threshold, "Upper threshold", color=color.orange) plot(-threshold, "Lower threshold", color=color.green) plot(0, "Mean", color=color.gray) if exit_long and strategy.position_size > 0 strategy.close("Long") if enter_long and not exit_long and strategy.position_size == 0 strategy.entry("Long", strategy.long, qty=100.0 / close)