Sure, here is the improved TradingView strategy code based on your instructions:
//@version=5
strategy(title="Demo GPT - Gaussian Channel Strategy v3.0", overlay=true, initial_capital=1000, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.1, slippage=3)
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
// Date condition inputs
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
startDate = input.time(timestamp("1 Jan 2018 00:00 +0000"), "Start Date", group="Main Algo Settings")
endDate = input.time(timestamp("31 Dec 2069 23:59 +0000"), "End Date", group="Main Algo Settings")
timeCondition = time >= startDate and time <= endDate
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
// Gaussian Channel Indicator - courtesy of @DonovanWall
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
// Functions - courtesy of @e2e4mfck
// Filter function
f_filt9x (_a, _s, _i) =>
int _m2 = 0, int _m3 = 0, int _m4 = 0, int _m5 = 0, int _m6 = 0,
int _m7 = 0, int _m8 = 0, int _m9 = 0, float _f = .0, _x = (1 - _a)
// Weights.
// Initial weight _m1 is a pole number and equal to _i
_m2 := _i == 9 ? 36 : _i == 8 ? 28 : _i == 7 ? 21 : _i == 6 ? 15 : _i == 5 ? 10 : _i == 4 ? 6 : _i == 3 ? 3 : _i == 2 ? 1 : 0
_m3 := _i == 9 ? 84 : _i == 8 ? 56 : _i == 7 ? 35 : _i == 6 ? 20 : _i == 5 ? 10 : _i == 4 ? 4 : _i == 3 ? 1 : 0
_m4 := _i == 9 ? 126 : _i == 8 ? 70 : _i == 7 ? 35 : _i == 6 ? 15 : _i == 5 ? 5 : _i == 4 ? 1 : 0
_m5 := _i == 9 ? 126 : _i == 8 ? 56 : _i == 7 ? 21 : _i == 6 ? 6 : _i == 5 ? 1 : 0
_m6 := _i == 9 ? 84 : _i == 8 ? 28 : _i == 7 ? 7 : _i == 6 ? 1 : 0
_m7 := _i == 9 ? 36 : _i == 8 ? 8 : _i == 7 ? 1 : 0
_m8 := _i == 9 ? 9 : _i == 8 ? 1 : 0
_m9 := _i == 9 ? 1 : 0
// filter
_f := math.pow(_a, _i) * nz(_s) +
_i * _x * nz(_f[1]) - (_i >= 2 ?
_m2 * math.pow(_x, 2) * nz(_f[2]) : 0) + (_i >= 3 ?
_m3 * math.pow(_x, 3) * nz(_f[3]) : 0) - (_i >= 4 ?
_m4 * math.pow(_x, 4) * nz(_f[4]) : 0) + (_i >= 5 ?
_m5 * math.pow(_x, 5) * nz(_f[5]) : 0) - (_i >= 6 ?
_m6 * math.pow(_x, 6) * nz(_f[6]) : 0) + (_i >= 7 ?
_m7 * math.pow(_x, 7) * nz(_f[7]) : 0) - (_i >= 8 ?
_m8 * math.pow(_x, 8) * nz(_f[8]) : 0) + (_i == 9 ?
_m9 * math.pow(_x, 9) * nz(_f[9]) : 0)
// 9 var declaration fun
f_pole (_a, _s, _i) =>
_f1 = f_filt9x(_a, _s, 1), _f2 = (_i >= 2 ? f_filt9x(_a, _s, 2) : 0), _f3 = (_i >= 3 ? f_filt9x(_a, _s, 3) : 0)
_f4 = (_i >= 4 ? f_filt9x(_a, _s, 4) : 0), _f5 = (_i >= 5 ? f_filt9x(_a, _s, 5) : 0), _f6 = (_i >= 6 ? f_filt9x(_a, _s, 6) : 0)
_f7 = (_i >= 2 ? f_filt9x(_a, _s, 7) : 0), _f8 = (_i >= 8 ? f_filt9x(_a, _s, 8) : 0), _f9 = (_i == 9 ? f_filt9x(_a, _s, 9) : 0)
_fn = _i == 1 ? _f1 : _i == 2 ? _f2 : _i == 3 ? _f3 :
_i == 4 ? _f4 : _i == 5 ? _f5 : _i == 6 ? _f6 :
_i == 7 ? _f7 : _i == 8 ? _f8 : _i == 9 ? _f9 : na
[_fn, _f1]
// Inputs for Gaussian Channel
src = input(defval=hlc3, title="Source")
int N = input.int(defval=4, title="Poles", minval=1, maxval=9)
int per = input.int(defval=144, title="Sampling Period", minval=2)
float mult = input.float(defval=1.414, title="Filtered True Range Multiplier", minval=0)
bool modeLag = input.bool(defval=false, title="Reduced Lag Mode")
bool modeFast = input.bool(defval=false, title="Fast Response Mode")
// Calculations for Gaussian Channel
beta = (1 - math.cos(4*math.asin(1)/per)) / (math.pow(1.414, 2/N) - 1)
alpha = - beta + math.sqrt(math.pow(beta, 2) + 2*beta)
lag = (per - 1)/(2*N)
srcdata = modeLag ? src + (src - src[lag]) : src
trdata = modeLag ? ta.tr(true) + (ta.tr(true) - ta.tr(true)[lag]) : ta.tr(true)
[filtn, filt1] = f_pole(alpha, srcdata, N)
[filtntr, filt1tr] = f_pole(alpha, trdata, N)
filt = modeFast ? (filtn + filt1)/2 : filtn
filttr = modeFast ? (filtntr + filt1tr)/2 : filtntr
hband = filt + filttr*mult
lband = filt - filttr*mult
// Colors
color1 = #0aff68
color2 = #00752d
color3 = #ff0a5a
color4 = #990032
fcolor = filt > filt[1] ? #0aff68 : filt < filt[1] ? #ff0a5a : #cccccc
barcolor_ = (src > src[1]) and (src > filt) and (src < hband) ? #0aff68 : (src > src[1]) and (src >= hband) ? #0aff1b : (src <= src[1]) and (src > filt) ? #00752d :
(src < src[1]) and (src < filt) and (src > lband) ? #ff0a5a : (src < src[1]) and (src <= lband) ? #ff0a11 : (src >= src[1]) and (src < filt) ? #990032 : #cccccc
// Outputs for Gaussian Channel
// Keep all existing plots & visuals
filtplot = plot(filt, title="Filter", color=fcolor, linewidth=3)
hbandplot = plot(hband, title="Filtered True Range High Band", color=fcolor)
lbandplot = plot(lband, title="Filtered True Range Low Band", color=fcolor)
fill(hbandplot, lbandplot, title="Channel Fill", color=color.new(fcolor, 80))
barcolor(barcolor_)
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
// Stochastic RSI Indicator (calculations only, plots and fills removed)
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
// Inputs for Stochastic RSI
smoothK = input.int(3, "K", minval=1)
smoothD = input.int(3, "D", minval=1)
lengthRSI = input.int(14, "RSI Length", minval=1)
lengthStoch = input.int(14, "Stochastic Length", minval=1)
srcRSI = input(close, title="RSI Source")
// Calculations for Stochastic RSI
rsi1 = ta.rsi(srcRSI, lengthRSI)
k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = ta.sma(k, smoothD)
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
// Trading Logic
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
// Conditions
gaussianIsGreen = filt > filt[1]
stochRSICondition = (k > 80) or (k < 20)
longCondition = gaussianIsGreen and (close > hband) and stochRSICondition and timeCondition
closeAllCondition = ta.crossunder(close, hband) and timeCondition
// Entry and Exit Signals
if longCondition
strategy.entry("long", strategy.long)
if closeAllCondition
strategy.close("long")
Explanation of the Modifications:
-
Added Stochastic RSI Calculations: Integrated the Stochastic RSI calculations into the strategy without the plots and fills as per your instructions. The variables have been renamed where necessary to avoid conflicts.
-
Modified Trading Logic:
-
Preserved Timeframe Logic: Since the Stochastic RSI indicator had timeframe="", which means it uses the chart's timeframe, we didn't need to make additional adjustments. The calculations will fill gaps whenever possible due to default behavior.
-
Kept All Existing Plots & Visuals: All plots and visuals from the original Gaussian Channel code have been kept intact as per your request.
-
Strategy Settings Adjustments:
- Commission and Slippage: Set
commission_type=strategy.commission.percent, commission_value=0.1, and slippage=3 directly in the strategy() function.
- Using 100% of Capital: Ensured by
default_qty_type=strategy.percent_of_equity and default_qty_value=100.
- Title Update: Changed the strategy title to include "Demo GPT - " at the beginning.
- Date Inputs: Added start and end date inputs allowing users to specify the trading period.
-
No Short Positions: The strategy only enters long positions and exits them; it does not enter short positions.
Note:
- Variable Names: Adjusted variable names where necessary to prevent any naming conflicts, especially with the
src variable used in both the Gaussian Channel and Stochastic RSI calculations.
- No Line Breaks in
strategy() Function: Ensured that the strategy() function call does not contain any line breaks to prevent compilation errors.
- Default Strategy Settings: Left all other strategy settings to their default values, as per your instructions.
This code should meet all your specified requirements and improve your strategy as requested.