// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © fasamkoh //@version=5 indicator(title='Chop Filter', shorttitle="CF", overlay=true) //========================================================================================================================================== // Take Profit Inputs. This is build base on the crossover of 2 T3 Moving Averages. // Define inputs length_1 = input(title='Fast Length', defval=2, group = "Take Profit Inputs") length_2 = input(title='SlowLength', defval=4, group = "Take Profit Inputs") factor = input.float(title='Factor', minval=0, maxval=1.5, step=0.01, defval=0.90, group = "Take Profit Inputs") src = input(defval=close, title='Source', group = "Indicator Inputs") // calculate fast T3 gd1(src, length_1) => ta.ema(src, length_1) * (1 + factor) - ta.ema(ta.ema(src, length_1), length_1) * factor fast_t3 = gd1(gd1(gd1(src, length_1), length_1), length_1) // calculate Slow T3 gd2(src, length_2) => ta.ema(src, length_2) * (1 + factor) - ta.ema(ta.ema(src, length_2), length_2) * factor slow_t3 = gd2(gd2(gd2(src, length_2), length_2), length_2) //========================================================================================================================================== //Trading inputs Trtrade_amt = input.int(20000, title='Lot Size (Units)', minval=1, step=1, group = "Trade Inputs") acc_name = input.string(defval="Primary", title='Webhook Name', options=["Primary", "Account-2", "Account-3", "Account-4", "Account-5", "Account-6", "Account-7", "Account-8", "Account-9", "Account-10", "Account-11", "Account-12", "Account-13", "Account-14", "Account-15", "Account-16", "Account-17", "Account-18"], group = "Trade Inputs") // Sampling Period per = input.int(defval=2, minval=1, step=1, title='Period') // Range Multiplier mult = input.float(defval=0.97, minval=0, title='Multiplier', step=0.01) // Trade re entering (if the trade is stopped out and returns to the winning direction, this permits you to re enter the trade) re_enter = input.string(defval="No", title='Re Enter Trade', options=["No", "Yes"]) include_take_profit = input.bool(false, title="Use Take Profit?") // Smooth Average Range smoothrng(x, t, m) => wper = t * 2 - 1 avrng = ta.ema(math.abs(x - x[1]), t) smoothrng = ta.ema(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]) // Building Trading Strategy longCond = bool(na) shortCond = bool(na) longCond := src > filt and src > src[1] and upward > 0 or src > filt and src < src[1] and upward > 0 shortCond := src < filt and src < src[1] and downward > 0 or src < filt and src > src[1] and downward > 0 var co = 0 CondIni = 0 CondIni := longCond ? 1 : shortCond ? -1 : CondIni[1] Long = longCond and CondIni[1] == -1 and co!=1 Short = shortCond and CondIni[1] == 1 and co!=-1 Long_r = ((longCond and CondIni[1] == -1) or (open < close and upward > 0 )) and co!=1 Short_r = ((shortCond and CondIni[1] == 1) or (open > close and downward > 0)) and co!=-1 //========================================================================================================================================== // entries and exits bool buy_long = na bool sell_short = na if re_enter == "No" buy_long := Long sell_short := Short else buy_long := Long_r or Long sell_short := Short_r or Short if buy_long co:=1 if sell_short co:=-1 // Take Profit Point bool long_out = na bool short_out = na if include_take_profit == true long_out := ta.crossunder(fast_t3,slow_t3) and open > close and upward > 0 and co==1 short_out := ta.crossover(fast_t3,slow_t3) and open < close and downward > 0 and co==-1 // Stop Loss entry_long = ta.valuewhen(buy_long,open,0) entry_short =ta.valuewhen(sell_short,open,0) clong = closeopen and co==-1 and close>entry_short if clong or cshort or short_out or long_out co:=0 // Trading conditions bool exit_long = na bool exit_short = na if include_take_profit == false exit_long := clong exit_short := cshort else exit_long := clong or long_out exit_short := cshort or short_out plotshape(exit_long,location=location.abovebar,text="out", textcolor=color.new(color.yellow, 0), style=shape.arrowdown,color=color.yellow,size=size.tiny,title="Stop Loss") plotshape(exit_short,location=location.belowbar,text="out", textcolor=color.new(color.blue, 0), style=shape.arrowup,color=color.blue,size=size.tiny,title="Stop Loss") // Trading conditions Exit_Trade = exit_long or exit_short or buy_long or sell_short //Buy Signals plotshape(buy_long, title="Buy", text="B", textcolor=color.blue, style=shape.arrowup, size=size.small, location=location.belowbar, color=color.new(color.blue, 0)) plotshape(sell_short, title="Sell", text="S", textcolor=color.yellow, style=shape.arrowdown, size=size.small, location=location.abovebar, color=color.new(color.yellow, 0)) // getting the currency pairs fx_pair_base = syminfo.basecurrency + "/" + syminfo.currency msg_out = acc_name + " s=" + fx_pair_base + " c=position t=market" msg_up = acc_name + " s=" + fx_pair_base + " c=position b=short t=market | " + acc_name + " s=" + fx_pair_base + " b=long q=" + str.tostring(Trtrade_amt) + " t=market" msg_dwn = acc_name + " s=" + fx_pair_base + " c=position b=long t=market | " + acc_name + " s=" + fx_pair_base + " b=short q=" + str.tostring(Trtrade_amt) + " t=market" // ALERTS if buy_long alert(msg_up, alert.freq_once_per_bar) if sell_short alert(msg_dwn, alert.freq_once_per_bar) if Exit_Trade alert(msg_out, alert.freq_once_per_bar)