//@version=5 strategy('Range filter v2 Strategy', overlay=true) ///////////////// // Check_long = input.bool(title='Long', defval=true, group='Trade') Check_short = input.bool(title='Short', defval=true, group='Trade') // i_startTime = input.time(defval=timestamp('1 Feb 2021 00:00 +0000'), title='Start Time', group='Backtest Date Range') i_endTime = input.time(defval=timestamp('30 Sep 2022 00:00 +0000'), title='End Time', group='Backtest Date Range') inDateRange = time >= i_startTime and time <= i_endTime //Session Time Frames allow_alert_bool = input.bool(true, 'Allowed session', inline='1', group='Allowed session') allow_alert_ = input.session(defval='0900-1500:1234567', inline='1', title='', group='Allowed session') plot_session_ = time('', allow_alert_) plot_session_time = na(plot_session_) ? 0 : 1 allowed = allow_alert_bool?plot_session_time :true //Source src = close //Sampling Period per = input.int(defval=25, minval=1, title='Sampling Period',group="Range filter") //Range Multiplier mult = input.int(defval=2, minval=0, title='Range Multiplier',group="Range filter") //----------------------------------------------------------------------------------------------------------------------------------------------------------------- //Definitions //----------------------------------------------------------------------------------------------------------------------------------------------------------------- //Smooth Average Range smoothrng(x, t, m) => wper = t / 3 - 1 avrng = ta.sma(math.abs(x - x[1]), t) smoothrng = ta.sma(avrng, wper) * m smoothrng smrng = smoothrng(src, per, mult) //Range Filter rngfilt(x, r) => rngfilt = x rngfilt := x > nz(rngfilt[1]) ? x - r < nz(rngfilt[1]) ? nz(rngfilt[1]) : x - r : x + r > nz(rngfilt[1]) ? nz(rngfilt[1]) : x + r rngfilt filt = rngfilt(src, smrng) //Filter Direction upward = 0.0 upward := filt > filt[1] ? nz(upward[1]) + 1 : filt < filt[1] ? 0 : nz(upward[1]) downward = 0.0 downward := filt < filt[1] ? nz(downward[1]) + 1 : filt > filt[1] ? 0 : nz(downward[1]) //Target Bands hband = filt + smrng lband = filt - smrng //Colors filtcolor = upward > 0 ? color.lime : downward > 0 ? color.red : color.orange //barcolor = (src > filt) and (src > src[1]) and (upward > 0) ? lime : (src > filt) and (src < src[1]) and (upward > 0) ? green : (src < filt) and (src < src[1]) and (downward > 0) ? red : (src < filt) and (src > src[1]) and (downward > 0) ? maroon : orange //----------------------------------------------------------------------------------------------------------------------------------------------------------------- //Plots //----------------------------------------------------------------------------------------------------------------------------------------------------------------- //Range Filter //filtplot = plot(filt, color=filtcolor, linewidth=4, title="Range Filter") // Factor = input.int(3, minval=1, maxval=100,group="Range filter") Pd = input.int(3, minval=1, maxval=100,group="Range filter") Up = hl2 - Factor * ta.atr(Pd) Dn = hl2 + Factor * ta.atr(Pd) TrendUp = 0.0 TrendDown = 0.0 linecolor = color(na) up = float(na) down = float(na) Tsl = 0.0 Trend = 0.0 TrendUp := close[1] > TrendUp[1] ? math.max(Up, TrendUp[1]) : Up TrendDown := close[1] < TrendDown[1] ? math.min(Dn, TrendDown[1]) : Dn Trend := close > TrendDown[1] ? 1 : close < TrendUp[1] ? -1 : nz(Trend[1], 1) Tsl := Trend == 1 ? TrendUp : TrendDown linecolor := Trend == 1 ? color.green : color.red //plot(Tsl, color = linecolor , style = line , linewidth = 3,title = "SuperTrend") // Strategy longCond = bool(na) shortCond = bool(na) longCond := ta.crossunder(Tsl, filt) shortCond := ta.crossover(Tsl, filt) // Count your long short conditions for more control with Pyramiding sectionLongs = 0 sectionLongs := nz(sectionLongs[1]) sectionShorts = 0 sectionShorts := nz(sectionShorts[1]) if longCond sectionLongs += 1 sectionShorts := 0 sectionShorts if shortCond sectionLongs := 0 sectionShorts += 1 sectionShorts // Pyramiding pyrl = 1 // These check to see your signal and cross references it against the pyramiding settings above longCondition = longCond and sectionLongs <= pyrl shortCondition = shortCond and sectionShorts <= pyrl // Get the price of the last opened long or short last_open_longCondition = float(na) last_open_shortCondition = float(na) last_open_longCondition := longCondition ? open : nz(last_open_longCondition[1]) last_open_shortCondition := shortCondition ? open : nz(last_open_shortCondition[1]) // Check if your last postion was a long or a short last_longCondition = float(na) last_shortCondition = float(na) last_longCondition := longCondition ? time : nz(last_longCondition[1]) last_shortCondition := shortCondition ? time : nz(last_shortCondition[1]) in_longCondition = last_longCondition > last_shortCondition in_shortCondition = last_shortCondition > last_longCondition // Take profit // isTPl = input(true, 'Take Profit Long') // isTPs = input(true, 'Take Profit Short') // tp = input.float(5, 'Take Profit ') // long_tp = isTPl and ta.crossover(high, (1 + tp / 100) * last_open_longCondition) and longCondition == 0 and in_longCondition == 1 // short_tp = isTPs and ta.crossunder(low, (1 - tp / 100) * last_open_shortCondition) and shortCondition == 0 and in_shortCondition == 1 // // Create a single close for all the different closing conditions. // long_close = long_tp ? 1 : 0 // short_close = short_tp ? 1 : 0 // Get the time of the last close last_long_close = float(na) last_short_close = float(na) // last_long_close := long_close ? time : nz(last_long_close[1]) // last_short_close := short_close ? time : nz(last_short_close[1]) // Alerts & Signals bton(b) => b ? 1 : 0 ///////////////////////////////////// length = input.int(14, minval=1,group="Choppiness index setting") ci = 100 * math.log10(math.sum(ta.atr(1), length) / (ta.highest(length) - ta.lowest(length))) / math.log10(length) offset = input.int(0, "Offset", minval = -500, maxval = 500,group="Choppiness index setting") //plot(ci, "CHOP", color=#2962FF, offset = offset) band1 = input.float(40.0, "Long Trade Threshold value",group="Choppiness index setting")//, color=#787B86, linestyle=hline.style_dashed) band0 = input.float(40.0, "Short Trade Threshold value",group="Choppiness index setting")//, color=#787B86, linestyle=hline.style_dashed) //fill(band1, band0, color = color.rgb(33, 150, 243, 90), title = "Background") ///////////////////////////////////// long_final=longCondition and ci 0 strategy.exit(id='Close Long', stop=longStop, limit=longExitPrice) if strategy.position_size < 0 strategy.exit(id='Close Short', stop=shortStop, limit=shortExitPrice) // Plot stop loss values for confirmation sl_ = plot(series=strategy.position_size > 0 ? longStop : na, color=color.new(#ef5350, 0), style=plot.style_linebr, linewidth=1, title='Long Stop') // Plot take profit values for confirmation tp_ = plot(series=strategy.position_size > 0 ? longExitPrice : na, color=color.new(#26a69a, 0), style=plot.style_linebr, linewidth=1, title='Long Take Profit') // Plot stop loss values for confirmation sl_s = plot(series=strategy.position_size < 0 ? shortStop : na, color=color.new(#ef5350, 0), style=plot.style_linebr, linewidth=1, title='Short Stop') // Plot take profit values for confirmation tp_s = plot(series=strategy.position_size < 0 ? shortExitPrice : na, color=color.new(#26a69a, 0), style=plot.style_linebr, linewidth=1, title='Short Take Profit') entry_plot = plot(series=strategy.position_size > 0 or strategy.position_size < 0 ? strategy.position_avg_price : na, color=color.new(color.blue, 0), style=plot.style_linebr, linewidth=1, title='Entry')