Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=5
- strategy(title="Indicator Test Strategy", shorttitle="ITS", format=format.price, precision=4)
- useDateFilter = input.bool(true, title="Filter Date Range of Backtest",
- group="Backtest Time Period")
- backtestStartDate = input.time(timestamp("1 Jan 2018"),
- title="Start Date", group="Backtest Time Period",
- tooltip="This start date is in the time zone of the exchange " +
- "where the chart's instrument trades. It doesn't use the time " +
- "zone of the chart or of your computer.")
- backtestEndDate = input.time(timestamp("1 Jan 2092"),
- title="End Date", group="Backtest Time Period",
- tooltip="This end date is in the time zone of the exchange " +
- "where the chart's instrument trades. It doesn't use the time " +
- "zone of the chart or of your computer.")
- //Range Conditions
- inDateRange = not useDateFilter or (time >= backtestStartDate and
- time < backtestEndDate)
- //_________________________________________________________________________________________________________________
- //_________________________________________________________________________________________________________________
- //_________________________________________________________________________________________________________________
- //_________________________________________________________________________________________________________________
- timeframej = input.string("3D", "Days", ["1D", "2D", "3D", "4D", "5D", "6D", "7D"],group = "Indicator timeframe")
- // Inputs
- length = input.int(14, title="Length")
- volatilityPeriod = input.int(14, title="Volatility Period")
- adaptive = input.bool(true, title="Adaptive")
- // Initialize variables
- var float gma = na
- var float sumOfWeights = na
- // Get the standard deviation or use the input value
- sigma = adaptive ? ta.stdev(close, volatilityPeriod) : input.float(1.0, title="Fixed StdDev")
- // Pre-calculate the highest and lowest values
- var float[] highestValues = array.new_float(length, na)
- var float[] lowestValues = array.new_float(length, na)
- for i = 0 to length - 1
- array.set(highestValues, i, ta.highest(close, i + 1))
- array.set(lowestValues, i, ta.lowest(close, i + 1))
- // Calculate GMA
- gma := 0.0
- sumOfWeights := 0.0
- for i = 0 to length - 1
- weight = math.exp(-math.pow((i - (length - 1)) / (2 * sigma), 2) / 2)
- value = array.get(highestValues, i) + array.get(lowestValues, i)
- gma := gma + (value * weight)
- sumOfWeights := sumOfWeights + weight
- gma := (gma / sumOfWeights) / 2
- // Define long and short conditions
- Long = close > gma
- Short = close < gma
- long1 = request.security(syminfo.tickerid,timeframej,Long)
- short1 = request.security(syminfo.tickerid,timeframej,Short)
- var color barColor = na
- // Determine the bar color based on the conditions
- if long1
- barColor := color.green
- else if short1
- barColor := color.red
- // Apply the bar color
- barcolor(barColor)
- // Strategy Execution
- if (long1) and inDateRange
- strategy.entry("Long", strategy.long)
- if (short1) and inDateRange
- strategy.entry("Short", strategy.short)
Advertisement
Comments
-
- download all types of premium tradingview indicators codes available on telegram - https://t.me/tradingview_premium_indicator
Add Comment
Please, Sign In to add comment
Advertisement