```js // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © hunkOfTuna //@version=5 strategy("Instantaneous Trendline Filter", initial_capital = 1000, commission_type = strategy.commission.percent, commission_value = 0.25, pyramiding = 0) pct_of_equity = input.float(1, "Percentage of Equity (%)", minval=0, maxval=100) / 100 date_from = input.time(defval = timestamp("01 Jan 2022 00:00 +0000"), title = "From") date_thru = input.time(defval = timestamp("01 Jan 2024 00:00 +0000"), title = "To") date_show = input (defval = true, title = "Show Date Range") date() => time >= date_from and time <= date_thru alpha = input.float(0.07, "alpha", minval = 0, maxval = 1, step = 0.01) rngfrac = input.float(35, "Percentage of price range +/- to close for short/long limit orders", minval=0, maxval=100) / 100 revpct = input.float(1.5, "Percentage of price that goes against your position to automatically reverse position", minval=0, maxval=100, step = 0.1) / 100 + 1 price = (high + low) / 2 // Calculating Alpha (not used yet) length = input.int(26, title="Length") radians = 0.707 * math.pi / length alpha_calculated = (math.cos(radians) + math.sin(radians) - 1) / math.cos(radians) plotchar(alpha_calculated, "alpha", display = display.data_window) a = alpha var itrend = 0.0 var smooth = 0.0 var trigger = 0.0 itrend := ((a - a * a / 4) * price + 0.5 * a * a * nz(price[1]) - (a - 0.75 * a * a) * nz(price[2]) + 2 * (1 - a) * nz(itrend[1]) - (1 - a) * (1 - a) * nz(itrend[2])) trigger := 2 * itrend - nz(itrend[2]) plot(itrend, "Trend", color.orange) plot(trigger, "Trigger", color.gray) qty = strategy.equity * pct_of_equity / close if ta.crossover(trigger, itrend) and date() // strategy.entry("long", strategy.long, qty, close - rngfrac * (high - low)) strategy.entry("long", strategy.long, qty) if ta.crossunder(trigger, itrend) and date() // strategy.entry("short", strategy.short, qty, close + rngfrac * (high - low)) strategy.entry("short", strategy.short, qty) hasOpenLong = strategy.position_entry_name == 'long' hasOpenShort = strategy.position_entry_name == 'short' openPositionSize = math.abs(strategy.position_size) // If price goes against position by more than revpct, // the strategy will correct itself and reverse the position if hasOpenLong and close < strategy.position_avg_price/revpct and date() strategy.entry("short", strategy.short, openPositionSize, comment = "reverse") // strategy.entry("short", strategy.short, qty, close + rngfrac * (high - low), comment = "reverse") if hasOpenShort and close > revpct*strategy.position_avg_price and date() strategy.entry("long", strategy.long, openPositionSize, comment = "reverse") // strategy.entry("long", strategy.long, qty, close - rngfrac * (high - low), comment = "reverse") ```