Can you write a pine script strategy to test the Twin Range Filter indicator with the…
- User
Can you write a pine script strategy to test the Twin Range Filter indicator with the settings below: Fast period=12 Fast range=1 Slow period=4 Slow range=2
- Assistant
Sure! Below is an example of how you could write a Pine Script strategy to test the Twin Range Filter indicator with the specified settings. Please note that the Twin Range Filter is not a standard indicator in TradingView, so I’ll assume it's a conceptual indicator that you're asking about—one that likely uses two different SMA (Simple Moving Average) ranges to generate signals. Given the lack of a standard definition, I'll improvise an interpretation based on the provided parameters.
- User
What do you mean by adjusting the logic for calculating fastSmaFilter and slowSmaFilter?
- Assistant
- User
If I paste a pine script here, can you create a pine script that turns this into a strategy
- Assistant
- User
Ok I want you to create a strategy pine script for: study(title="Twin Range Filter", overlay=true)
source = input(defval=close, title="Source")
// Smooth Average Range
per1 = input(defval=27, minval=1, title="Fast period") mult1 = input(defval=1.6, minval=0.1, title="Fast range")
per2 = input(defval=55, minval=1, title="Slow period") mult2 = input(defval=2, minval=0.1, title="Slow range")
smoothrng(x, t, m) => wper = t * 2 - 1 avrng = ema(abs(x - x[1]), t) smoothrng = ema(avrng, wper) * m smoothrng smrng1 = smoothrng(source, per1, mult1) smrng2 = smoothrng(source, per2, mult2) smrng = (smrng1 + smrng2) / 2
// 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(source, smrng)
- Assistant
- User
I got an error like: "The arguments 'maxval', 'minval', and 'step' cannot be used with the input() function. You can use the input.int() or input.float() functions to specify a range of input data values" on the line "per1 = input(defval=27, minval=1, title=“Fast period”)"
- Assistant
- User
Can you edit this to enter only long trades? and also can you replace the left double quotation marks and right double quotation marks with regular quotation marks?
- Assistant
Certainly! Here is the modified script that focuses only on entering long trades. Additionally, I've replaced all the fancy quotation marks with regular ones.
//@version=5 strategy(title="Twin Range Filter Strategy - Long Only", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10) source = input(defval=close, title="Source") // Smooth Average Range per1 = input.int(defval=27, minval=1, title="Fast period") mult1 = input.float(defval=1.6, minval=0.1, title="Fast range") per2 = input.int(defval=55, minval=1, title="Slow period") mult2 = input.float(defval=2, minval=0.1, title="Slow 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 smrng1 = smoothrng(source, per1, mult1) smrng2 = smoothrng(source, per2, mult2) smrng = (smrng1 + smrng2) / 2 // 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(source, smrng) upward = 0.0 upward := filt > filt[1] ? nz(upward[1]) + 1 : filt < filt[1] ? 0 : nz(upward[1]) longCond = source > filt and source > source[1] and upward > 0 or source > filt and source < source[1] and upward > 0 // Strategy Execution if (longCond) strategy.entry("Long", strategy.long) // Visualization (optional for the strategy but included for reference) plotshape(series=longCond, title="Long", location=location.belowbar, color=color.new(color.green, 0), style=shape.labelup, size=size.small, text="L", textcolor=color.white) - User
But you haven't replaced the left and right quotation marks. I just want to use regular quote like "
- Assistant
My apologies for overlooking that. Let's correct the script with regular quotation marks (") for all strings:
//@version=5 strategy(title="Twin Range Filter Strategy - Long Only", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10) source = input(defval=close, title="Source") // Smooth Average Range per1 = input.int(defval=27, minval=1, title="Fast period") mult1 = input.float(defval=1.6, minval=0.1, title="Fast range") per2 = input.int(defval=55, minval=1, title="Slow period") mult2 = input.float(defval=2, minval=0.1, title="Slow 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 smrng1 = smoothrng(source, per1, mult1) smrng2 = smoothrng(source, per2, mult2) smrng = (smrng1 + smrng2) / 2 // 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(source, smrng) upward = 0.0 upward := filt > filt[1] ? nz(upward[1]) + 1 : filt < filt[1] ? 0 : nz(upward[1]) longCond = source > filt and source > source[1] and upward > 0 or source > filt and source < source[1] and upward > 0 // Strategy Execution if (longCond) strategy.entry("Long", strategy.long) // Visualization (optional for the strategy but included for reference) plotshape(series=longCond, title="Long", location=location.belowbar, color=color.new(color.green, 0), style=shape.labelup, size=size.small, text="L", textcolor=color.white) - User
Dude I want to replace the characters '“' and '”' with the character '"'
- Assistant
My apologies for the confusion. Here's the corrected version with appropriate quotation marks ("):
//@version=5 strategy(title="Twin Range Filter Strategy - Long Only", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10) source = input(defval=close, title="Source") // Smooth Average Range per1 = input.int(defval=27, minval=1, title="Fast period") mult1 = input.float(defval=1.6, minval=0.1, title="Fast range") per2 = input.int(defval=55, minval=1, title="Slow period") mult2 = input.float(defval=2, minval=0.1, title="Slow 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 smrng1 = smoothrng(source, per1, mult1) smrng2 = smoothrng(source, per2, mult2) smrng = (smrng1 + smrng2) / 2 // 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(source, smrng) upward = 0.0 upward := filt > filt[1] ? nz(upward[1]) + 1 : filt < filt[1] ? 0 : nz(upward[1]) longCond = source > filt and source > source[1] and upward > 0 or source > filt and source < source[1] and upward > 0 // Strategy Execution if (longCond) strategy.entry("Long", strategy.long) // Visualization (optional for the strategy but included for reference) plotshape(series=longCond, title="Long", location=location.belowbar, color=color.new(color.green, 0), style=shape.labelup, size=size.small, text="L", textcolor=color.white) - User
Ok I don't want to remove short trades. Keep the short trades as well. Add also SALMA indicator with the settings of Length=45 and Extra Smooth=1 and enter trades when only this SALMA indicator is green and the Twin Range indicator that you wrote gives buy signal.
- Assistant
- User
Change this to sell when SALMA turns red instead.
- Assistant