Strategy · Pine Script v4 · Own pane

Scaled Normalized Vector Strategy

by capissimo

Calculates normalized price vector changes across custom higher timeframes using a minimax scaling algorithm and configurable activation functions like Swish or LReLU.

View on GitHub

Repaints

GitHub stars114on the repository
Pine versionv4older, still runs
Lines of code84
Settings12inputs
LicenceNone statedfile: MPL-2.0
Last update1 year ago7 Nov 2024

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

84 lines of Pine Script v4 from scripts/040-Scaled-Normalized-Vector-Strategy-(v4-1).pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

040-Scaled-Normalized-Vector-Strategy-(v4-1).pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © capissimo

//@version=4
strategy("Scaled Normalized Vector Strategy (ver.4.1)", '', false, precision=2)

// Description:

// This modification of the Scaled Normalized Vector Strategy uses trailing stops and is optimized for lower TFs

price    = input(close,  "Price Data")
tf       = input(6,      "Timeframe", minval=1, maxval=1440)
thresh   = input(14.,    "Threshold", minval=.1, step=.1) 
div      = input(1000000,"Divisor", options=[1,10,100,1000,10000,100000,1000000,10000000,100000000])
mmx      = input(233,    "Minimax Lookback", options=[1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584])
showVol  = input(false,  "Volume")
useold   = input(true,   "Use Old System")
method   = input("Swish","Activation", options=["Step", "LReLU", "Swish", "None"])
useStop  = input(true,   "Use Trailing Stop?")
slPoints = input(200,    "Stop Loss Trail Points", minval = 1)
slOffset = input(400,    "Stop Loss Trail Offset", minval = 1)

scaleMinimax(X, p, min, max) => 
    hi = highest(X, p), lo = lowest(X, p)
    (max - min) * (X - lo)/(hi - lo) + min

getdiff(prc, tf) =>
    prev  = scaleMinimax((useold ? security(syminfo.tickerid, tostring(tf), prc[1], barmerge.gaps_off, barmerge.lookahead_on) 
                                 : security(syminfo.tickerid, tostring(tf), prc[1])), tf, 0, 1)
    curr  = scaleMinimax((useold ? security(syminfo.tickerid, tostring(tf), hlc3, barmerge.gaps_off, barmerge.lookahead_on)  
                                 : security(syminfo.tickerid, tostring(tf), hlc3)), tf, 0, 1)
    (curr/prev) - 1

relu(x) => max(x, 0)
lrelu(x, alpha) => relu(x) - alpha * relu(-x)
step(x) => x >= 0 ? 1 : -1
sigmoid(x) => 1 / (1 + exp(-x))
swish(x) => x * sigmoid(x)

f(m) => method==m

vol  = useold ? security(syminfo.tickerid, tostring(tf), volume, barmerge.gaps_off, barmerge.lookahead_on) 
              : security(syminfo.tickerid, tostring(tf), volume)
changed = change(price)
obvvar  = cum(changed > 0 ? vol : changed < 0 ? -vol : 0*vol)
prix = showVol ? obvvar : price
x    = getdiff(prix, tf)
p    = f("Swish") ? swish(x) : f("Step") ? step(x) : f("LReLU") ? lrelu(x, .8) : x
th   = thresh/div
long = crossover(p, th)
short= crossunder(p, -th)

lime  = color.new(color.lime, 10), fuchsia = color.new(color.fuchsia, 10), 
black = color.new(color.black, 100), gray = color.new(color.gray, 50)
bg    = long ? lime : short ? fuchsia : black
cl    = p > th ? color.green : p < -th ? color.red : color.silver

bgcolor(bg, editable=false)
plot(scaleMinimax(th, mmx, -1, 1), color=lime, editable=false, transp=0)
hline(0, linestyle=hline.style_dotted, title="base line", color=gray, editable=false)
plot(scaleMinimax(-th, mmx, -1, 1), color=fuchsia, editable=false, transp=0)
plot(scaleMinimax(p, mmx, -1, 1), color=cl, style=plot.style_histogram, transp=70, editable=false)
plot(scaleMinimax(p, mmx, -1, 1), color=cl, style=plot.style_linebr, title="prediction", transp=0, editable=false)

strategy.entry("L", true, 1, when=long)
strategy.entry("S", false, 1, when=short)

if (useStop) // in case of using the trailing stop
    strategy.exit("L", from_entry="L", trail_points=slPoints, trail_offset=slOffset)
    strategy.exit("S", from_entry="S", trail_points=slPoints, trail_offset=slOffset)
    
alertcondition(long, title='Long', message='Long Signal!')
alertcondition(short, title='Short', message='Short Signal!')

//*** Karobein Oscillator
per  = input(8, "Karobein Osc Lookback")

prix2 = ema(price, per)
a = ema(prix2 < prix2[1] ? prix2/prix2[1] : 0, per)
b = ema(prix2 > prix2[1] ? prix2/prix2[1] : 0, per)
c = (prix2/prix2[1])/(prix2/prix2[1] + b)
d = 2*((prix2/prix2[1])/(prix2/prix2[1] + c*a)) - 1

plot(scaleMinimax(d, mmx, -1, 1), color=color.orange, transp=0)

Overview

Scaled Normalized Vector Strategy can optionally incorporate volume data through cumulative volume shifts. The strategy plots prediction values and threshold bounds in a separate pane, triggering long entries on upper threshold crossovers and short entries on lower threshold crossunders. Exits are managed with a trailing stop loss. Historical signals repaint when using the default setting due to higher-timeframe lookahead.

Details

Published by

capissimo on GitHub

Type

Strategy, Pine Script v4

Draws

Separate pane below the chart

Category

Oscillators & momentum

Markets

Any symbol

Timeframes

M1-M5, M15-H1

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)
  • Reads higher-timeframe data with lookahead on, so the history can show signals that were not there in real time

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Price Datasourcecloseprice
Timeframeint6tf
Thresholdinput14.thresh
Divisorint1000000div
Minimax Lookbackint233mmx
VolumeboolfalseshowVol
Use Old Systembooltrueuseold
ActivationstringSwishmethod
Use Trailing Stop?booltrueuseStop
Stop Loss Trail Pointsint200slPoints
Stop Loss Trail Offsetint400slOffset
Karobein Osc Lookbackint8per
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

330 stars

MACD + Divergences [Alorse] is a momentum strategy that calculates the MACD oscillator and identifies regular and hidden divergences using pivot high and low points. It filters trade signals with dual Exponential Moving Averages (EMA 150 and 600) to ensure tra

Strategy v4 Oscillators & momentumDivergence Repaints
Updated 2 months ago · None stated GitHub repository of MACD + Divergences [Alorse]

Q-Trend + KVO Strategy

by TradersPost
218 stars

This strategy combines the ATR-banded Q-Trend indicator with the Klinger Volume Oscillator (KVO) to execute confluence trades. It enters long when a Q-Trend buy trigger aligns with a positive KVO value and a bullish candle confirmation, taking short trades und

Strategy v6 TrendVolume
Updated 3 months ago · None stated GitHub repository of Q-Trend + KVO Strategy

MACD Crossover (tutorial)

by pineforge-4pass
193 stars

This strategy calculates the Moving Average Convergence Divergence indicator and plots the MACD line, signal line, and histogram in a separate pane. It enters long positions when the MACD line crosses above the signal line and enters short positions when it cr

Strategy v6 Oscillators & momentumMoving averages
Updated today · Apache-2.0 GitHub repository of MACD Crossover (tutorial)

2xTP, SL to Entry

by hasnocool
106 stars

This strategy executes mean-reversion trades based on Bollinger Bands and the Stochastic Oscillator. Long orders trigger when price dips below the lower band alongside a bullish Stochastic crossover, with an optional oversold threshold. Short orders execute on

Strategy v5 Mean reversionOscillators & momentum
Updated 2 years ago · GPL-3.0 GitHub repository of 2xTP, SL to Entry
65 stars

This strategy calculates a 20-period Relative Strength Index (RSI) and generates entry orders based on crossovers and crossunders of the 50 level. When RSI crosses above 50, it places a long stop entry at the current high price with a custom alert message. Whe

Strategy v4 Oscillators & momentum
Updated 4 days ago · GPL-3.0 GitHub repository of Strategy Using Alert Message

BTC Futures MACD

by iamrichardD
57 stars

This strategy plots a MACD oscillator in a separate pane and trades Bitcoin futures from zero-line and signal-line crosses. Optional early momentum signals, a volume filter, and a trend moving-average filter can confirm entries. It sizes positions as fixed con

Strategy v6 Oscillators & momentumMoving averages Repaints
Updated 1 year ago · Apache-2.0 GitHub repository of BTC Futures MACD

More strategys

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers