Price Memory Trend for Thinkorswim

chewie76

Well-known member
VIP
VIP Enthusiast
The Price Memory Trend conversion was taken from this original code.
https://www.tradingview.com/script/1CDVWIFi-Uptrick-Price-Memory-Trend/
Bubbles and candle color can be turned on/off in the settings.

Price Memory Trend is a custom indicator designed to detect directional shifts and volatility changes using a non-traditional price memory approach. Unlike moving average systems, it builds a dynamic memory of price that adapts gradually over time, allowing it to detect significant deviations and trend transitions with reduced noise.

1769318790745.png


Code:
#Price_Memory_Trend
#Converted by Chewie 1/24/2026
#Original Code: https://www.tradingview.com/script/1CDVWIFi-Uptrick-Price-Memory-Trend/

declare upper;

# =========================
# INPUTS
# =========================
input colorbar = yes;
input bubble = yes;
input memoryStrength = 0.12;
input memoryDecay    = 0.96;
input devLen         = 50;
input bandMult       = 1.6;
input atrLength      = 50;
input atrMult        = 1.3;

# =========================
# COLORS
# =========================
DefineGlobalColor("Bull", CreateColor(0, 255, 170));
DefineGlobalColor("Bear", CreateColor(255, 0, 255));
DefineGlobalColor("Neutral", Color.GRAY);

# =========================
# PRICE MEMORY
# =========================
def price = close;

rec memory =
    if BarNumber() == 1 then price
    else memory[1] + (price - memory[1]) * memoryStrength;

def base = memory * memoryDecay + price * (1 - memoryDecay);

# =========================
# ADAPTIVE DEVIATION
# =========================
def dev    = AbsValue(price - base);
def avgDev = ExpAverage(dev, devLen);

def rawUpper = base + avgDev * bandMult;
def rawLower = base - avgDev * bandMult;

# =========================
# TREND STATE
# =========================
def bullCond = price crosses above rawUpper;
def bearCond = price crosses below rawLower;

rec trendState =
    if BarNumber() == 1 then 0
    else if bullCond then 1
    else if bearCond then -1
    else trendState[1];

def newBull = bullCond and trendState[1] != 1;
def newBear = bearCond and trendState[1] != -1;

# =========================
# TRAILING CORE BANDS
# =========================
rec trailLower =
    if BarNumber() == 1 then rawLower
    else if trendState == 1 then Max(rawLower, trailLower[1])
    else rawLower;

rec trailUpper =
    if BarNumber() == 1 then rawUpper
    else if trendState == -1 then Min(rawUpper, trailUpper[1])
    else rawUpper;

# =========================
# ACTIVE CORE BANDS (MASKED)
# =========================
def bullCore = if trendState == 1 then trailLower else Double.NaN;
def bearCore = if trendState == -1 then trailUpper else Double.NaN;

# =========================
# ATR OUTER BANDS
# =========================
def atr = Average(TrueRange(high, close, low), atrLength);

def bullOuter = if trendState == 1 then bullCore - atr * atrMult else Double.NaN;
def bearOuter = if trendState == -1 then bearCore + atr * atrMult else Double.NaN;

# =========================
# CLOUD SERIES (NO RECS HERE)
# =========================
def bullCloudTop = bullCore;
def bullCloudBot = bullOuter;

def bearCloudTop = bearOuter;
def bearCloudBot = bearCore;

# =========================
# PLOTS
# =========================
plot BullCorePlot = bullCore;
BullCorePlot.SetDefaultColor(GlobalColor("Bull"));
BullCorePlot.SetLineWeight(3);

plot BearCorePlot = bearCore;
BearCorePlot.SetDefaultColor(GlobalColor("Bear"));
BearCorePlot.SetLineWeight(3);

plot BullATRPlot = bullOuter;
BullATRPlot.SetDefaultColor(GlobalColor("Bull"));
#BullATRPlot.SetStyle(Curve.SHORT_DASH);

plot BearATRPlot = bearOuter;
BearATRPlot.SetDefaultColor(GlobalColor("Bear"));
#BearATRPlot.SetStyle(Curve.SHORT_DASH);

# =========================
# CLOUDS (FINALLY LEGAL)
# =========================
AddCloud(bullCloudTop, bullCloudBot,
         GlobalColor("Bull"), GlobalColor("Bull"));

AddCloud(bearCloudTop, bearCloudBot,
         GlobalColor("Bear"), GlobalColor("Bear"));

# =========================
# UP / DOWN MARKERS
# =========================
AddChartBubble(bubble and newBull, low, "Up", GlobalColor("Bull"), no);
AddChartBubble(bubble and newBear, high, "Down", GlobalColor("Bear"), yes);

# =========================
# STATUS LABEL
# =========================
AddLabel(
    yes,
    if trendState == 1 then "PMT: BULL"
    else if trendState == -1 then "PMT: BEAR"
    else "PMT: NEUTRAL",
    if trendState == 1 then GlobalColor("Bull")
    else if trendState == -1 then GlobalColor("Bear")
    else GlobalColor("Neutral")
);


def bull = trendState == 1;
def bear = trendState == -1;


AssignPriceColor(if !colorbar then Color.CURRENT else if bull then GlobalColor("Bull") else if bear then GlobalColor("Bear")  else Color.GRAY);
 
Last edited:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

The Price Memory Trend conversion was taken from this original code.
https://www.tradingview.com/script/1CDVWIFi-Uptrick-Price-Memory-Trend/
Bubbles and candle color can be turned on/off in the settings.

Price Memory Trend is a custom indicator designed to detect directional shifts and volatility changes using a non-traditional price memory approach. Unlike moving average systems, it builds a dynamic memory of price that adapts gradually over time, allowing it to detect significant deviations and trend transitions with reduced noise.

View attachment 26871

Code:
#Price_Memory_Trend
#Converted by Chewie 1/24/2026
#Original Code: https://www.tradingview.com/script/1CDVWIFi-Uptrick-Price-Memory-Trend/

declare upper;

# =========================
# INPUTS
# =========================
input colorbar = yes;
input bubble = yes;
input memoryStrength = 0.12;
input memoryDecay    = 0.96;
input devLen         = 50;
input bandMult       = 1.6;
input atrLength      = 50;
input atrMult        = 1.3;

# =========================
# COLORS
# =========================
DefineGlobalColor("Bull", CreateColor(0, 255, 170));
DefineGlobalColor("Bear", CreateColor(255, 0, 255));
DefineGlobalColor("Neutral", Color.GRAY);

# =========================
# PRICE MEMORY
# =========================
def price = close;

rec memory =
    if BarNumber() == 1 then price
    else memory[1] + (price - memory[1]) * memoryStrength;

def base = memory * memoryDecay + price * (1 - memoryDecay);

# =========================
# ADAPTIVE DEVIATION
# =========================
def dev    = AbsValue(price - base);
def avgDev = ExpAverage(dev, devLen);

def rawUpper = base + avgDev * bandMult;
def rawLower = base - avgDev * bandMult;

# =========================
# TREND STATE
# =========================
def bullCond = price crosses above rawUpper;
def bearCond = price crosses below rawLower;

rec trendState =
    if BarNumber() == 1 then 0
    else if bullCond then 1
    else if bearCond then -1
    else trendState[1];

def newBull = bullCond and trendState[1] != 1;
def newBear = bearCond and trendState[1] != -1;

# =========================
# TRAILING CORE BANDS
# =========================
rec trailLower =
    if BarNumber() == 1 then rawLower
    else if trendState == 1 then Max(rawLower, trailLower[1])
    else rawLower;

rec trailUpper =
    if BarNumber() == 1 then rawUpper
    else if trendState == -1 then Min(rawUpper, trailUpper[1])
    else rawUpper;

# =========================
# ACTIVE CORE BANDS (MASKED)
# =========================
def bullCore = if trendState == 1 then trailLower else Double.NaN;
def bearCore = if trendState == -1 then trailUpper else Double.NaN;

# =========================
# ATR OUTER BANDS
# =========================
def atr = Average(TrueRange(high, close, low), atrLength);

def bullOuter = if trendState == 1 then bullCore - atr * atrMult else Double.NaN;
def bearOuter = if trendState == -1 then bearCore + atr * atrMult else Double.NaN;

# =========================
# CLOUD SERIES (NO RECS HERE)
# =========================
def bullCloudTop = bullCore;
def bullCloudBot = bullOuter;

def bearCloudTop = bearOuter;
def bearCloudBot = bearCore;

# =========================
# PLOTS
# =========================
plot BullCorePlot = bullCore;
BullCorePlot.SetDefaultColor(GlobalColor("Bull"));
BullCorePlot.SetLineWeight(3);

plot BearCorePlot = bearCore;
BearCorePlot.SetDefaultColor(GlobalColor("Bear"));
BearCorePlot.SetLineWeight(3);

plot BullATRPlot = bullOuter;
BullATRPlot.SetDefaultColor(GlobalColor("Bull"));
#BullATRPlot.SetStyle(Curve.SHORT_DASH);

plot BearATRPlot = bearOuter;
BearATRPlot.SetDefaultColor(GlobalColor("Bear"));
#BearATRPlot.SetStyle(Curve.SHORT_DASH);

# =========================
# CLOUDS (FINALLY LEGAL)
# =========================
AddCloud(bullCloudTop, bullCloudBot,
         GlobalColor("Bull"), GlobalColor("Bull"));

AddCloud(bearCloudTop, bearCloudBot,
         GlobalColor("Bear"), GlobalColor("Bear"));

# =========================
# UP / DOWN MARKERS
# =========================
AddChartBubble(bubble and newBull, low, "Up", GlobalColor("Bull"), no);
AddChartBubble(bubble and newBear, high, "Down", GlobalColor("Bear"), yes);

# =========================
# STATUS LABEL
# =========================
AddLabel(
    yes,
    if trendState == 1 then "PMT: BULL"
    else if trendState == -1 then "PMT: BEAR"
    else "PMT: NEUTRAL",
    if trendState == 1 then GlobalColor("Bull")
    else if trendState == -1 then GlobalColor("Bear")
    else GlobalColor("Neutral")
);


def bull = trendState == 1;
def bear = trendState == -1;


AssignPriceColor(if !colorbar then Color.CURRENT else if bull then GlobalColor("Bull") else if bear then GlobalColor("Bear")  else Color.GRAY);
Compare the PMT vs Double Pass Kalman or a Triple Pass Kalman, the Kalman is more responsive.
1769356041765.png
 
Compare the PMT vs Double Pass Kalman or a Triple Pass Kalman, the Kalman is more responsive.
View attachment 26877
Kalman is more responsive, but what strategy are you trading? If you want to buy and hold over longer timeframes, PMT keeps you in the trade because it is showing you trend. Kalman is good if you are taking shorter term trades. So it depends on your trading strategy to determine which is more effective.
 
Kalman is more responsive, but what strategy are you trading? If you want to buy and hold over longer timeframes, PMT keeps you in the trade because it is showing you trend. Kalman is good if you are taking shorter term trades. So it depends on your trading strategy to determine which is more effective.
Take Long trades with the Bullish trend, Take Short trades with a Bearish Trend. I would not use the Price Memory Trend as a trigger signal. I would rather take the DP Kalman as a trigger signal!
 
What is interesting is when I plotted the bands at all times and added a midline. Now the bright clouds show the original half band with the benefit of seeing all the bands display.

1770215096779.png



Code:
#Price_Memory_Trend_BANDS
#Converted by Chewie 1/24/2026
#Added dots, single cloud color
#Original Code: https://www.tradingview.com/script/1CDVWIFi-Uptrick-Price-Memory-Trend/

declare upper;

# =========================
# INPUTS
# =========================
input colorbar = no;
input bubble = no;
input dots = yes;
input memoryStrength = 0.12;
input memoryDecay    = 0.96;
input devLen         = 50;
input bandMult       = 1.6;

input atrLength      = 50;
input atrMult        = 1.3;

# =========================
# COLORS
# =========================
DefineGlobalColor("Bull", CreateColor(0, 255, 170));
DefineGlobalColor("Bear", CreateColor(255, 0, 255));
DefineGlobalColor("Neutral", Color.GRAY);

# =========================
# PRICE MEMORY
# =========================
def price = close;

rec memory =
    if BarNumber() == 1 then price
    else memory[1] + (price - memory[1]) * memoryStrength;

def base = memory * memoryDecay + price * (1 - memoryDecay);

# =========================
# ADAPTIVE DEVIATION
# =========================
def dev    = AbsValue(price - base);
def avgDev = ExpAverage(dev, devLen);

def rawUpper = base + avgDev * bandMult;
def rawLower = base - avgDev * bandMult;

# =========================
# TREND STATE
# =========================
def bullCond = price crosses above rawUpper;
def bearCond = price crosses below rawLower;

rec trendState =
    if BarNumber() == 1 then 0
    else if bullCond then 1
    else if bearCond then -1
    else trendState[1];

def newBull = bullCond and trendState[1] != 1;
def newBear = bearCond and trendState[1] != -1;

# =========================
# TRAILING CORE BANDS
# =========================
rec trailLower =
    if BarNumber() == 1 then rawLower
    else if trendState == 1 then Max(rawLower, trailLower[1])
    else rawLower;

rec trailUpper =
    if BarNumber() == 1 then rawUpper
    else if trendState == -1 then Min(rawUpper, trailUpper[1])
    else rawUpper;

# =========================
# ACTIVE CORE BANDS (MASKED)
# =========================
def bullCore = if trendState == 1 or -1 then trailLower else Double.NaN;
def bearCore = if trendState == -1 or 1 then trailUpper else Double.NaN;

# =========================
# ATR OUTER BANDS
# =========================
def atr = Average(TrueRange(high, close, low), atrLength);

def bullOuter = if trendState == 1 or -1 then bullCore - atr * atrMult else Double.NaN;
def bearOuter = if trendState == -1 or 1 then bearCore + atr * atrMult else Double.NaN;

# =========================
# CLOUD SERIES (NO RECS HERE)
# =========================
def bullCloudTop = bullCore;
def bullCloudBot = bullOuter;

def bearCloudTop = bearOuter;
def bearCloudBot = bearCore;

# =========================
# PLOTS
# =========================
plot BullCorePlot = bullCore;
#BullCorePlot.SetDefaultColor(GlobalColor("Bull"));
Bullcoreplot.AssignValueColor(color = if trendstate == 1 then 
    color.green else GlobalColor("Bear"));
BullCorePlot.SetLineWeight(3);

plot BearCorePlot = bearCore;
#BearCorePlot.SetDefaultColor(GlobalColor("Bear"));
BearCorePlot.AssignValueColor(color = if trendstate == 1 then 
    GlobalColor("Bull") else color.red);
BearCorePlot.SetLineWeight(3);

plot BullATRPlot = bullOuter;
#BullATRPlot.SetDefaultColor(GlobalColor("Bull"));
BullATRPlot.AssignValueColor(color = if trendstate == 1 then 
    GlobalColor("Bull") else GlobalColor("Bear"));
BullATRPlot.SetLineWeight(2);

plot BearATRPlot = bearOuter;
#BearATRPlot.SetDefaultColor(GlobalColor("Bear"));
BearATRPlot.AssignValueColor(color = if trendstate == 1 then 
    GlobalColor("Bull") else GlobalColor("Bear"));
BearATRPlot.SetLineWeight(2);

plot Mid = (BearCorePlot - Bullcoreplot)/2 + bullcoreplot;
Mid.setdefaultColor(GlobalColor("Bull"));
Mid.SetLineWeight(2);
def bullCore1 = if trendState == 1 then trailLower else Double.NaN;
def bearCore1 = if trendState == -1 then trailUpper else Double.NaN;

# =========================
# ATR OUTER BANDS
# =========================

def bullOuter1 = if trendState == 1 then bullCore - atr * atrMult else Double.NaN;
def bearOuter1 = if trendState == -1 then bearCore + atr * atrMult else Double.NaN;

# =========================
# CLOUDS (FINALLY LEGAL)
# =========================
AddCloud(bullCloudTop, bullCloudBot,
         GlobalColor("Bull"), GlobalColor("Bull"));

AddCloud(bearCloudTop, bearCloudBot,
         GlobalColor("Bear"), GlobalColor("Bear"));
AddCloud(bullCore1, bullouter1,
         color.green, GlobalColor("Bull"));

AddCloud(bearCore1, bearouter1,
         GlobalColor("Bear"), color.red);




# =========================
# UP / DOWN MARKERS
# =========================

AddChartBubble(bubble and newBull, low, "Up", GlobalColor("Bull"), no);
AddChartBubble(bubble and newBear, high, "Down", GlobalColor("Bear"), yes);

# =========================
# STATUS LABEL
# =========================
AddLabel(
    yes,
    if trendState == 1 then "PMT: BULL"
    else if trendState == -1 then "PMT: BEAR"
    else "PMT: NEUTRAL",
    if trendState == 1 then GlobalColor("Bull")
    else if trendState == -1 then GlobalColor("Bear")
    else GlobalColor("Neutral")
);


def bull = trendState == 1;
def bear = trendState == -1;


AssignPriceColor(if !colorbar then Color.CURRENT else if bull then GlobalColor("Bull") else if bear then GlobalColor("Bear")  else Color.GRAY);

#Dots
def length = 14;
def price2 = close + low + high;
def linDev = lindev(price2, length);
def CCI = if linDev == 0 then 0 else (price2 - Average(price2, length)) / linDev / 0.015;

def sell = high > bearcore and cci > 100;
def buy = low < bullcore and cci < -100;

plot Bdot = if dots and buy then low - ATR(60) / 2 else Double.NaN;
Bdot.SetDefaultColor(Color.cyan);
Bdot.SetPaintingStrategy(PaintingStrategy.POINTS);
Bdot.SetLineWeight(5);

plot Sdot = if dots and sell then high + ATR(60) / 2 else Double.NaN;
Sdot.SetDefaultColor(Color.magenta);
Sdot.SetPaintingStrategy(PaintingStrategy.POINTS);
Sdot.SetLineWeight(5);
 
Last edited:
just wondering how to scan with this?? I used the scan with Bullcoreplot to be true for 1 day which really dosn't work. How can I get it to scan 1 or 2 day after green buy bubble?? thank you
 
I'm a huge fan of this indicator. I made a version that has a single cloud color and changed the band colors to change for uptrend green or downtrend magenta instead of the cloud change so it's easier to see the candles when they are inside the cloud. Also added DOTS where price is in the cloud and CCI is +/- 100.

1769892595826.png

Code:
#Price_Memory_Trend_BANDS
#Converted by Chewie 1/24/2026
#Added dots, single cloud color
#Original Code: https://www.tradingview.com/script/1CDVWIFi-Uptrick-Price-Memory-Trend/

declare upper;

# =========================
# INPUTS
# =========================
input colorbar = yes;
input bubble = yes;
input dots = yes;
input memoryStrength = 0.12;
input memoryDecay    = 0.96;
input devLen         = 50;
input bandMult       = 1.6;

input atrLength      = 50;
input atrMult        = 1.3;

# =========================
# COLORS
# =========================
DefineGlobalColor("Bull", CreateColor(0, 255, 170));
DefineGlobalColor("Bear", CreateColor(255, 0, 255));
DefineGlobalColor("Neutral", Color.GRAY);

# =========================
# PRICE MEMORY
# =========================
def price = close;

rec memory =
    if BarNumber() == 1 then price
    else memory[1] + (price - memory[1]) * memoryStrength;

def base = memory * memoryDecay + price * (1 - memoryDecay);

# =========================
# ADAPTIVE DEVIATION
# =========================
def dev    = AbsValue(price - base);
def avgDev = ExpAverage(dev, devLen);

def rawUpper = base + avgDev * bandMult;
def rawLower = base - avgDev * bandMult;

# =========================
# TREND STATE
# =========================
def bullCond = price crosses above rawUpper;
def bearCond = price crosses below rawLower;

rec trendState =
    if BarNumber() == 1 then 0
    else if bullCond then 1
    else if bearCond then -1
    else trendState[1];

def newBull = bullCond and trendState[1] != 1;
def newBear = bearCond and trendState[1] != -1;

# =========================
# TRAILING CORE BANDS
# =========================
rec trailLower =
    if BarNumber() == 1 then rawLower
    else if trendState == 1 then Max(rawLower, trailLower[1])
    else rawLower;

rec trailUpper =
    if BarNumber() == 1 then rawUpper
    else if trendState == -1 then Min(rawUpper, trailUpper[1])
    else rawUpper;

# =========================
# ACTIVE CORE BANDS (MASKED)
# =========================
def bullCore = if trendState == 1 or -1 then trailLower else Double.NaN;
def bearCore = if trendState == -1 or 1 then trailUpper else Double.NaN;

# =========================
# ATR OUTER BANDS
# =========================
def atr = Average(TrueRange(high, close, low), atrLength);

def bullOuter = if trendState == 1 or -1 then bullCore - atr * atrMult else Double.NaN;
def bearOuter = if trendState == -1 or 1 then bearCore + atr * atrMult else Double.NaN;

# =========================
# CLOUD SERIES (NO RECS HERE)
# =========================
def bullCloudTop = bullCore;
def bullCloudBot = bullOuter;

def bearCloudTop = bearOuter;
def bearCloudBot = bearCore;

# =========================
# PLOTS
# =========================
plot BullCorePlot = bullCore;
#BullCorePlot.SetDefaultColor(GlobalColor("Bull"));
Bullcoreplot.AssignValueColor(color = if trendstate == 1 then
    GlobalColor("Bull") else GlobalColor("Bear"));
BullCorePlot.SetLineWeight(3);

plot BearCorePlot = bearCore;
#BearCorePlot.SetDefaultColor(GlobalColor("Bear"));
BearCorePlot.AssignValueColor(color = if trendstate == 1 then
    GlobalColor("Bull") else GlobalColor("Bear"));
BearCorePlot.SetLineWeight(3);

plot BullATRPlot = bullOuter;
#BullATRPlot.SetDefaultColor(GlobalColor("Bull"));
BullATRPlot.AssignValueColor(color = if trendstate == 1 then
    GlobalColor("Bull") else GlobalColor("Bear"));
BullATRPlot.SetLineWeight(2);

plot BearATRPlot = bearOuter;
#BearATRPlot.SetDefaultColor(GlobalColor("Bear"));
BearATRPlot.AssignValueColor(color = if trendstate == 1 then
    GlobalColor("Bull") else GlobalColor("Bear"));
BearATRPlot.SetLineWeight(2);

plot Mid = (BearCorePlot - Bullcoreplot)/2 + bullcoreplot;
Mid.setdefaultColor(color.yellow);
Mid.SetLineWeight(1);
def bullCore1 = if trendState == 1 then trailLower else Double.NaN;
def bearCore1 = if trendState == -1 then trailUpper else Double.NaN;

# =========================
# ATR OUTER BANDS
# =========================

def bullOuter1 = if trendState == 1 then bullCore - atr * atrMult else Double.NaN;
def bearOuter1 = if trendState == -1 then bearCore + atr * atrMult else Double.NaN;

# =========================
# CLOUDS (FINALLY LEGAL)
# =========================
AddCloud(bullCloudTop, bullCloudBot,
         GlobalColor("Bull"), GlobalColor("Bull"));

AddCloud(bearCloudTop, bearCloudBot,
         GlobalColor("Bear"), GlobalColor("Bear"));
#AddCloud(bullCore1, bullouter1,
#         GlobalColor("Bull"), GlobalColor("Bull"));

#AddCloud(bearCore1, bearouter1,
#         GlobalColor("Bear"), GlobalColor("Bear"));


# =========================
# UP / DOWN MARKERS
# =========================

AddChartBubble(bubble and newBull, low, "Up", GlobalColor("Bull"), no);
AddChartBubble(bubble and newBear, high, "Down", GlobalColor("Bear"), yes);

# =========================
# STATUS LABEL
# =========================
AddLabel(
    yes,
    if trendState == 1 then "PMT: BULL"
    else if trendState == -1 then "PMT: BEAR"
    else "PMT: NEUTRAL",
    if trendState == 1 then GlobalColor("Bull")
    else if trendState == -1 then GlobalColor("Bear")
    else GlobalColor("Neutral")
);


def bull = trendState == 1;
def bear = trendState == -1;


AssignPriceColor(if !colorbar then Color.CURRENT else if bull then GlobalColor("Bull") else if bear then GlobalColor("Bear")  else Color.GRAY);

#Dots
def length = 14;
def price2 = close + low + high;
def linDev = lindev(price2, length);
def CCI = if linDev == 0 then 0 else (price2 - Average(price2, length)) / linDev / 0.015;

def sell = high > bearcore and cci > 100;
def buy = low < bullcore and cci < -100;

plot Bdot = if dots and buy then low - ATR(60) / 2 else Double.NaN;
Bdot.SetDefaultColor(Color.cyan);
Bdot.SetPaintingStrategy(PaintingStrategy.POINTS);
Bdot.SetLineWeight(5);

plot Sdot = if dots and sell then high + ATR(60) / 2 else Double.NaN;
Sdot.SetDefaultColor(Color.magenta);
Sdot.SetPaintingStrategy(PaintingStrategy.POINTS);
Sdot.SetLineWeight(5);
 
just wondering how to scan with this?? I used the scan with Bullcoreplot to be true for 1 day which really dosn't work. How can I get it to scan 1 or 2 day after green buy bubble?? thank you
You can search for NewBear or NewBull is true, OR if price is above or below the other plots which are the lines.
 
If you want to take a look at a lower indicator that converts it into an oscillator, take a look at this. It includes divergence too.

1770216649405.png


Code:
# Price Memory Trend – LOWER Oscillator
# Assembled by Chewie 2/4/2026
# Lower companion to PMT Bands

declare lower;

# =========================
# INPUTS
# =========================
input memoryStrength = 0.12;
input memoryDecay    = 0.96;
input devLen         = 50;
input bandMult       = 1.6;

input atrLength      = 50;
input atrMult        = 1.3;


# =========================
# COLORS
# =========================
DefineGlobalColor("Bull", CreateColor(0, 255, 170));
DefineGlobalColor("Bear", CreateColor(255, 0, 255));
DefineGlobalColor("Neutral", Color.GRAY);

# =========================
# PRICE MEMORY ENGINE
# =========================
def price = close;

rec memory =
    if BarNumber() == 1 then price
    else memory[1] + (price - memory[1]) * memoryStrength;

def base = memory * memoryDecay + price * (1 - memoryDecay);

# =========================
# ADAPTIVE DEVIATION
# =========================
def dev    = AbsValue(price - base);
def avgDev = ExpAverage(dev, devLen);

def rawUpper = base + avgDev * bandMult;
def rawLower = base - avgDev * bandMult;

# =========================
# TREND STATE
# =========================
def bullCond1 = price crosses above rawUpper;
def bearCond1 = price crosses below rawLower;

rec trendState =
    if BarNumber() == 1 then 0
    else if bullCond1 then 1
    else if bearCond1 then -1
    else trendState[1];

# =========================
# TRAILING CORE
# =========================
rec trailLower =
    if BarNumber() == 1 then rawLower
    else if trendState == 1 then Max(rawLower, trailLower[1])
    else rawLower;

rec trailUpper =
    if BarNumber() == 1 then rawUpper
    else if trendState == -1 then Min(rawUpper, trailUpper[1])
    else rawUpper;

def core =
    if trendState == 1 then trailLower
    else if trendState == -1 then trailUpper
    else base;

# =========================
# NORMALIZED OSCILLATOR
# =========================
def atr = Average(TrueRange(high, close, low), atrLength);

# Distance from PMT core, normalized by ATR
def osc =
    if atr != 0 then
        (price - core) / (atr * atrMult)
    else 0;

# =========================
# PLOTS
# =========================
plot PMT = osc;
PMT.AssignValueColor(
    if trendState == 1 then GlobalColor("Bull")
    else if trendState == -1 then GlobalColor("Bear")
    else GlobalColor("Neutral")
);
PMT.SetLineWeight(3);

plot ZeroLine = 0;
plot one = 1.0;
plot none = -1.0;
plot two = 2.0;
plot ntwo = -2.0;
plot three = 3.0;
plot nthree = -3.0;
ZeroLine.SetDefaultColor(Color.yellow);
one.SetDefaultColor(Color.dark_red);
none.SetDefaultColor(Color.dark_green);
two.SetDefaultColor(Color.red);
ntwo.SetDefaultColor(Color.green);
three.SetDefaultColor(Color.magenta);
nthree.SetDefaultColor(Color.cyan);


# =========================
# BULL / BEAR ZONES
# =========================
AddCloud(PMT, ZeroLine,
         GlobalColor("Bull"), Color.CURRENT);

AddCloud(ZeroLine, PMT,
         GlobalColor("Bear"), Color.CURRENT);

AddCloud(2, 3,  color.current, GlobalColor("Bear"));
AddCloud(-3, -2, color.current, GlobalColor("Bull"));

def bullcore1 = if trendState == 1 then -2 else double.nan;
def bullouter1 = if trendState == 1 then -3 else double.nan;
def bearcore1 = if trendState == -1 then 2 else double.nan;
def bearouter1 = if trendState == -1 then 3 else double.nan;

AddCloud(bullCore1, bullouter1,
         color.green, GlobalColor("Bull"));

AddCloud(bearCore1, bearouter1,
         GlobalColor("Bear"), color.red);



# DIVERGENCE

input divergenceLength = 20; #hint divergenceLength: The number of bars used to calculate divergences.

input divergenceType = {default regular, reverse}; #hint divergenceType: The type of divergence. A regular divergence is when price is making higher highs (or lower lows), while the indicator is making lower highs (or higher lows). A reverse divergence (also called a hidden divergence) is when the indicator is making higher highs (or lower lows), while price is making lower highs (or higher lows).
#Hint: The output of this indicator is for informational and educational use only, is not an investment recommendation or advice, and should not be relied upon in making the decision to buy or sell a security or pursue a particular investment strategy.

def xDownBars;
def xUpBars;
def xDowns;
def xUps;
def hiBars;
def loBars;
def pivotTop;
def pivotBottom;
def hiInd;
def loInd;
def hiPrice;
def loPrice;
plot bearishd;
plot bullishd;

def K = PMT;
def Over_Boughta = 1.5;
def Over_Solda = -1.5;



def ind;
ind = k;

# Bearish
pivotTop =
if
    divergenceType == divergenceType.regular
then
    ind[1] > over_Boughta and ind[1] == Highest(ind, divergenceLength + 1)
else
    ind[1] >= 50 and
    ind[1] == highest(ind, divergenceLength + 1) and
    ind[1] == highest(ind, divergenceLength+1)[-divergenceLength+1];

if pivotTop
then {
    hiBars = 1;
    hiInd = ind[1];
    hiPrice = max(high[2], max(high[1], high[0]));
}
else {
    hiBars = hiBars[1] + 1;
    hiInd = hiInd[1];
    hiPrice = hiPrice[1];
}

if ind[1] crosses below  Over_Boughta
then {
    xDownBars = 1;
    xDowns = xDowns[1] + 1;
}
else {
    xDownBars = xDownBars[1] + 1;
    xDowns = if pivotTop[1] then 0 else xDowns[1];
}

def bearCond;
switch (divergenceType) {
case regular:
bearCond =
    ind[1] >= ZeroLine and
    ind < ind[1] and
    high[1] == Highest(high, divergenceLength + 1) and
    hiBars[1] > xDownBars[1] and
    xDowns == 1 and
    close < close[1] and
    hiPrice[1] < high[1] and
    hiInd[1] > ind[1];
case reverse:
bearCond =
    ind[1] >= ZeroLine and
    ind < ind[1] and
#    high[1] == Highest(high, divergenceLength) and
#    hiBars[1] > xDownBars[1] and
#    xDowns == 1 and
    close < close[1] and
    hiPrice[1] > high[1] and hiPrice[1] > high and
    hiInd[1] < ind[1];}

bearishd =
    if
        bearCond
    then
        ind[1]
    else
        Double.NaN;;

bearishd.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
bearishd.SetDefaultColor(Color.CYAN);
bearishd.setLineWeight(2);
bearishd.hideTitle();
bearishd.hideBubble();

def countBear = if bearCond[-1] then countBear[1] + 1 else countBear[1];
def recentBear = countBear == HighestAll(countBear);
def secHigh = highestAll(if bearCond[-1] and recentBear then ind else Double.NaN);
#def firstHigh = highestAll(if bearCond and recentBear and ind[1] == secHigh then hiInd[1] else double.NaN);
def FH_bar = highestAll(if recentBear and bearCond[-1] and ind == secHigh then getvalue(barNumber(), hibars) else double.NaN);

plot bearTrendline =
if
    recentBear and bearCond[-1] and ind == secHigh
then
    max(ind[1], ind[0])
else
#    if pivotTop and hiInd == firstHigh
    if
        FH_bar == barNumber()
    then
        ind
    else
        double.NaN;
bearTrendline.EnableApproximation();
bearTrendline.setDefaultColor(color.RED);
bearTrendline.setLineWeight(4);
bearTrendline.hideBubble();
bearTrendline.hideTitle();

#Bullish
pivotBottom =
if
    divergenceType == divergenceType.regular
then
    ind[1] < over_Solda and ind[1] == lowest(ind, divergenceLength + 1)
else
    ind[1] <= 50 and
    ind[1] == lowest(ind, divergenceLength+1) and
    ind[1] == lowest(ind, divergenceLength+1)[-divergenceLength+1];

if pivotBottom
then {
    loBars = 1;
    loInd = ind[1];
    loPrice = min(low[2], min(low[1], low[0]));
}
else {
    loBars = loBars[1] + 1;
    loInd = loInd[1];
    loPrice = loPrice[1];
}

if ind[1] crosses above over_Solda
then {
    xUpBars = 1;
    xUps = xUps[1] + 1;
}
else {
    xUpBars = xUpBars[1] + 1;
    xUps = if pivotBottom[1] then 0 else xUps[1];
}

def bullCond;
switch (divergenceType){
case regular:
bullCond =
    ind[1] <= ZeroLine and
    ind > ind[1] and     
    low[1] == Lowest(low, divergenceLength + 1) and
    loBars[1] > xUpBars[1] and
    xUps == 1 and
    close > close[1] and
    loPrice[1] > low[1] and
    loInd[1] < ind[1];
case reverse:
bullCond =
    ind[1] <= ZeroLine and
    ind > ind[1] and     
#    low[1] == Lowest(low, divergenceLength) and
#    loBars[1] > xUpBars[1] and
#    xUps == 1 and
    close > close[1] and
    loPrice[1] < low[1] and loPrice[1] < low and
    loInd[1] > ind[1];}

bullishd =
    if
        bullCond
    then
        ind[1]
    else
        Double.NaN;

bullishd.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
bullishd.SetDefaultColor(Color.CYAN);
bullishd.setLineWeight(2);
bullishd.HideTitle();
bullishd.HideBubble();

def countBull = if bullCond[-1] then countBull[1] + 1 else countBull[1];
def recentBull = countBull == HighestAll(countBull);
def secLow = highestAll(if bullCond[-1] and recentBull then ind else Double.NaN);
#def firstLow = highestAll(if bullCond and recentBull and ind[1] == secLow then loInd[1] else double.NaN);
def FL_bar = highestAll(if recentBull and bullCond[-1] and ind == secLow then getvalue(barNumber(), lobars) else double.NaN);

plot bullTrendline =
    if
        recentBull and bullCond[-1] and ind == secLow
    then
        min(ind[1], ind[0])
    else
        if
#            pivotBottom and loInd == firstLow
            FL_bar == barNumber()
        then
            ind[0]
        else
            double.NaN;
bullTrendline.EnableApproximation();
bullTrendline.setDefaultColor(color.GREEN);
bullTrendline.setLineWeight(4);
bullTrendline.hideBubble();
bullTrendline.hideTitle();
 
Do you like to trade with a MTF, Multi Time Frame version? Then this is for you. This picture shows a 3M chart with the 4 hour PMT plotted.

MTF PMT

1770225110429.png



Code:
# ==================================================
# MTF_PRICE_MEMORY_TREND_BANDS
# Assembled by Chewie 2/4/26
# ==================================================

declare upper;

# =========================
# INPUTS
# =========================
input higherTF = AggregationPeriod.FOUR_HOURS;

input colorbar = no;
input bubble   = no;
input dots     = yes;

input memoryStrength = 0.12;
input memoryDecay    = 0.96;
input devLen         = 50;
input bandMult       = 1.6;
input atrLength      = 50;
input atrMult        = 1.3;

# =========================
# COLORS
# =========================
DefineGlobalColor("Bull", CreateColor(0, 255, 170));
DefineGlobalColor("Bear", CreateColor(255, 0, 255));
DefineGlobalColor("Neutral", color.gray);

# =========================
# HIGHER-TIMEFRAME SERIES
# =========================
def price  = close(period = higherTF);
def highTF = high(period = higherTF);
def lowTF  = low(period = higherTF);

# Detect NEW HTF candle
def newHTF = price != price[1];

# =========================
# PRICE MEMORY (HTF-GATED)
# =========================
rec memory =
    if IsNaN(memory[1]) then price
    else if newHTF then
        memory[1] + (price - memory[1]) * memoryStrength
    else
        memory[1];

def base =
    memory * memoryDecay + price * (1 - memoryDecay);

# =========================
# ADAPTIVE DEVIATION (HTF)
# =========================
def dev    = AbsValue(price - base);
def avgDev = ExpAverage(dev, devLen);

def rawUpper = base + avgDev * bandMult;
def rawLower = base - avgDev * bandMult;

# =========================
# TREND STATE (HTF-GATED)
# =========================
def bullCond = price crosses above rawUpper;
def bearCond = price crosses below rawLower;

rec trendState =
    if IsNaN(trendState[1]) then 0
    else if newHTF then
        (if bullCond then 1
         else if bearCond then -1
         else trendState[1])
    else
        trendState[1];

def newBull = newHTF and bullCond and trendState[1] != 1;
def newBear = newHTF and bearCond and trendState[1] != -1;

# =========================
# TRAILING CORE BANDS (HTF-GATED)
# =========================
rec trailLower =
    if IsNaN(trailLower[1]) then rawLower
    else if newHTF then
        (if trendState == 1 then
            Max(rawLower, trailLower[1])
         else
            rawLower)
    else
        trailLower[1];

rec trailUpper =
    if IsNaN(trailUpper[1]) then rawUpper
    else if newHTF then
        (if trendState == -1 then
            Min(rawUpper, trailUpper[1])
         else
            rawUpper)
    else
        trailUpper[1];

def bullCore = if trendState == 1 or -1 then trailLower else Double.NaN;
def bearCore = if trendState == -1 or 1 then trailUpper else Double.NaN;

# =========================
# ATR OUTER BANDS (HTF)
# =========================
def atr = Average(
    TrueRange(highTF, price, lowTF),
    atrLength
);

def bullOuter = if trendState == 1 or -1 then bullCore - atr * atrMult else Double.NaN;
def bearOuter = if trendState == -1 or 1 then bearCore + atr * atrMult else Double.NaN;


def bullCore1 = if trendState == 1 then trailLower else Double.NaN;
def bearCore1 = if trendState == -1  then trailUpper else Double.NaN;


def bullOuter1 = if trendState == 1 then bullCore1 - atr * atrMult else Double.NaN;
def bearOuter1 = if trendState == -1 then bearCore1 + atr * atrMult else Double.NaN;

# =========================
# CLOUDS
# =========================
AddCloud(bullCore, bullOuter,
         GlobalColor("Bull"), GlobalColor("Bull"));

AddCloud(bearOuter, bearCore,
         GlobalColor("Bear"), GlobalColor("Bear"));

# =========================
# PLOTS
# =========================
plot BullCorePlot = bullCore;
BullCorePlot.AssignValueColor(color = if trendstate == 1 then
    color.green else GlobalColor("Bull"));
BullCorePlot.SetLineWeight(3);

plot BearCorePlot = bearCore;
BearCorePlot.AssignValueColor(color = if trendstate == 1 then
    GlobalColor("Bull") else color.red);
BearCorePlot.SetLineWeight(3);

plot BullATRPlot = bullOuter;
BullATRPlot.SetDefaultColor(GlobalColor("Bull"));
BullATRPlot.SetLineWeight(2);

plot BearATRPlot = bearOuter;
BearATRPlot.SetDefaultColor(GlobalColor("Bear"));
BearATRPlot.SetLineWeight(2);

plot BullCorePlot1 = bullCore1;
BullCorePlot1.SetDefaultColor(GlobalColor("Bull"));
BullCorePlot1.SetLineWeight(3);

plot BearCorePlot1 = bearCore1;
BearCorePlot1.SetDefaultColor(GlobalColor("Bear"));
BearCorePlot1.SetLineWeight(3);

plot BullATRPlot1 = bullOuter1;
BullATRPlot1.SetDefaultColor(GlobalColor("Bull"));
BullATRPlot1.SetLineWeight(2);

plot BearATRPlot1 = bearOuter1;
BearATRPlot1.SetDefaultColor(GlobalColor("Bear"));
BearATRPlot1.SetLineWeight(2);

plot Mid = (BullCorePlot - Bearcoreplot)/2 + bearcoreplot;
Mid.SetDefaultColor(GlobalColor("Bull"));
Mid.SetLineWeight(2);


AddCloud(bullCore1, bullouter1 , color.green, GlobalColor("Bull"));

AddCloud(bearCore1, bearouter1,
         GlobalColor("Bear"), color.red);

# =========================
# BUBBLES (TRUE HTF)
# =========================
AddChartBubble(bubble and newBull, low, "4H UP", GlobalColor("Bull"), no);
AddChartBubble(bubble and newBear, high, "4H DOWN", GlobalColor("Bear"), yes);

# =========================
# STATUS LABEL
# =========================
AddLabel(
    yes,
    "PMT 4H: " +
    (if trendState == 1 then "BULL"
     else if trendState == -1 then "BEAR"
     else "NEUTRAL"),
    if trendState == 1 then GlobalColor("Bull")
    else if trendState == -1 then GlobalColor("Bear")
    else GlobalColor("Neutral")
);

# =========================
# PRICE COLORING
# =========================
AssignPriceColor(
    if !colorbar then Color.CURRENT
    else if trendState == 1 then GlobalColor("Bull")
    else if trendState == -1 then GlobalColor("Bear")
    else Color.GRAY
);

# =========================
# DOTS (HTF CONFIRMATION)
# =========================
def length = 14;
def price2 = price + highTF + lowTF;
def linDev = LinDev(price2, length);
def CCI =
    if linDev == 0 then 0
    else (price2 - Average(price2, length)) / linDev / 0.015;

def buy  = newHTF and lowTF < bullCore and CCI < -100;
def sell = newHTF and highTF > bearCore and CCI > 100;

plot Bdot = if dots and buy then low - atr / 2 else Double.NaN;
Bdot.SetDefaultColor(Color.CYAN);
Bdot.SetPaintingStrategy(PaintingStrategy.POINTS);
Bdot.SetLineWeight(5);

plot Sdot = if dots and sell then high + atr / 2 else Double.NaN;
Sdot.SetDefaultColor(Color.MAGENTA);
Sdot.SetPaintingStrategy(PaintingStrategy.POINTS);
Sdot.SetLineWeight(5);
 
What is interesting is when I plotted the bands at all times and added a midline. Now the bright clouds show the original half band with the benefit of seeing all the bands display.

View attachment 26893


Code:
#Price_Memory_Trend_Bands
#Converted by Chewie 1/24/2026
#Original Code: https://www.tradingview.com/script/1CDVWIFi-Uptrick-Price-Memory-Trend/

declare upper;

# =========================
# INPUTS
# =========================
input colorbar = no;
input bubble = no;
input memoryStrength = 0.12;
input memoryDecay    = 0.96;
input devLen         = 50;
input bandMult       = 1.6;

input atrLength      = 50;
input atrMult        = 1.3;

# =========================
# COLORS
# =========================
DefineGlobalColor("Bull", CreateColor(0, 255, 170));
DefineGlobalColor("Bear", CreateColor(255, 0, 255));
DefineGlobalColor("Neutral", Color.GRAY);

# =========================
# PRICE MEMORY
# =========================
def price = close;

rec memory =
    if BarNumber() == 1 then price
    else memory[1] + (price - memory[1]) * memoryStrength;

def base = memory * memoryDecay + price * (1 - memoryDecay);

# =========================
# ADAPTIVE DEVIATION
# =========================
def dev    = AbsValue(price - base);
def avgDev = ExpAverage(dev, devLen);

def rawUpper = base + avgDev * bandMult;
def rawLower = base - avgDev * bandMult;

# =========================
# TREND STATE
# =========================
def bullCond = price crosses above rawUpper;
def bearCond = price crosses below rawLower;

rec trendState =
    if BarNumber() == 1 then 0
    else if bullCond then 1
    else if bearCond then -1
    else trendState[1];

def newBull = bullCond and trendState[1] != 1;
def newBear = bearCond and trendState[1] != -1;

# =========================
# TRAILING CORE BANDS
# =========================
rec trailLower =
    if BarNumber() == 1 then rawLower
    else if trendState == 1 then Max(rawLower, trailLower[1])
    else rawLower;

rec trailUpper =
    if BarNumber() == 1 then rawUpper
    else if trendState == -1 then Min(rawUpper, trailUpper[1])
    else rawUpper;

# =========================
# ACTIVE CORE BANDS (MASKED)
# =========================
def bullCore = if trendState == 1 or -1 then trailLower else Double.NaN;
def bearCore = if trendState == -1 or 1 then trailUpper else Double.NaN;

# =========================
# ATR OUTER BANDS
# =========================
def atr = Average(TrueRange(high, close, low), atrLength);

def bullOuter = if trendState == 1 or -1 then bullCore - atr * atrMult else Double.NaN;
def bearOuter = if trendState == -1 or 1 then bearCore + atr * atrMult else Double.NaN;

# =========================
# CLOUD SERIES (NO RECS HERE)
# =========================
def bullCloudTop = bullCore;
def bullCloudBot = bullOuter;

def bearCloudTop = bearOuter;
def bearCloudBot = bearCore;

# =========================
# PLOTS
# =========================
plot BullCorePlot = bullCore;
BullCorePlot.SetDefaultColor(GlobalColor("Bull"));
BullCorePlot.SetLineWeight(3);

plot BearCorePlot = bearCore;
BearCorePlot.SetDefaultColor(GlobalColor("Bear"));
BearCorePlot.SetLineWeight(3);

plot BullATRPlot = bullOuter;
BullATRPlot.SetDefaultColor(GlobalColor("Bull"));
#BullATRPlot.SetStyle(Curve.SHORT_DASH);

plot BearATRPlot = bearOuter;
BearATRPlot.SetDefaultColor(GlobalColor("Bear"));
#BearATRPlot.SetStyle(Curve.SHORT_DASH);

plot Mid = (BearCorePlot - Bullcoreplot)/2 + bullcoreplot;
Mid.setdefaultColor(color.yellow);
Mid.SetLineWeight(2);
def bullCore1 = if trendState == 1 then trailLower else Double.NaN;
def bearCore1 = if trendState == -1 then trailUpper else Double.NaN;

# =========================
# ATR OUTER BANDS
# =========================

def bullOuter1 = if trendState == 1 then bullCore - atr * atrMult else Double.NaN;
def bearOuter1 = if trendState == -1 then bearCore + atr * atrMult else Double.NaN;

# =========================
# CLOUDS (FINALLY LEGAL)
# =========================
AddCloud(bullCloudTop, bullCloudBot,
         GlobalColor("Bull"), GlobalColor("Bull"));

AddCloud(bearCloudTop, bearCloudBot,
         GlobalColor("Bear"), GlobalColor("Bear"));
AddCloud(bullCore1, bullouter1,
         GlobalColor("Bull"), GlobalColor("Bull"));

AddCloud(bearCore1, bearouter1,
         GlobalColor("Bear"), GlobalColor("Bear"));


# =========================
# UP / DOWN MARKERS
# =========================

AddChartBubble(bubble and newBull, low, "Up", GlobalColor("Bull"), no);
AddChartBubble(bubble and newBear, high, "Down", GlobalColor("Bear"), yes);

# =========================
# STATUS LABEL
# =========================
AddLabel(
    yes,
    if trendState == 1 then "PMT: BULL"
    else if trendState == -1 then "PMT: BEAR"
    else "PMT: NEUTRAL",
    if trendState == 1 then GlobalColor("Bull")
    else if trendState == -1 then GlobalColor("Bear")
    else GlobalColor("Neutral")
);


def bull = trendState == 1;
def bear = trendState == -1;


AssignPriceColor(if !colorbar then Color.CURRENT else if bull then GlobalColor("Bull") else if bear then GlobalColor("Bear")  else Color.GRAY);
Thanks for the code. Can someone add alarm for up or down signal? Thanks in advance.
 
Care to share your script? Only found the single and double script here. Thanks
This is my version of a Triple Pass Kalman Filter w ATR Bands
Code:
input price = close;
# The first pass inputs
input length1 = 5;
input smoothness1 = 2.0;
#the second pass inputs 
input length2 = 10;
input smoothness2 = 1.75;
input length3 = 20;
input smoothness3 = 1.5;

# FIRST PASS. Calculate Dynamic Volatility
def noise1 = (stdev(price, length1) * smoothness1) + 0.0001;

# FIRST PASS. Kalman Recursive Logic
rec errorEst1;
rec estimate1;

# FIRST PASS. The Kalman Gain: K = (Error in Estimate) / (Error in Estimate + Error in Measurement)
def K1 = if (errorEst1[1] + noise1) != 0
        then errorEst1[1] / (errorEst1[1] + noise1)
        else 0.5; # Default to 0.5 if math fails to prevent flat lines

# FIRST PASS. Update State
estimate1 = CompoundValue(1, estimate1[1] + K1 * (price - estimate1[1]), price);

# FIRST PASS. Update the error estimate for the next calculation
errorEst1 = CompoundValue(1, (1 - K1) * errorEst1[1] + (AbsValue(estimate1 - estimate1[1])), noise1);

# FIRST PASS. Plots
plot Kalman1 = estimate1;
Kalman1.SetPaintingStrategy(PaintingStrategy.Line_vs_Points);
Kalman1.SetLineWeight(1);
Kalman1.AssignValueColor(if Kalman1 >= Kalman1[1] then Color.Lime else Color.Dark_Orange);
Kalman1.HideBubble();


#######################################

# SECOND PASS. Calculate Dynamic Volatility
def noise2 = (stdev(Kalman1, length2) * smoothness2) + 0.0001;

# SECOND PASS. Kalman Recursive Logic
rec errorEst2;
rec estimate2;

# SECOND PASS. The Kalman Gain: K = (Error in Estimate) / (Error in Estimate + Error in Measurement)
def K2 = if (errorEst2[1] + noise2) != 0
        then errorEst2[1] / (errorEst2[1] + noise2)
        else 0.5; # Default to 0.5 if math fails to prevent flat lines

# SECOND PASS. Update State
estimate2 = CompoundValue(1, estimate2[1] + K2 * (Kalman1 - estimate2[1]), Kalman1);

# SECOND PASS. Update the error estimate for the next calculation
errorEst2 = CompoundValue(1, (1 - K2) * errorEst2[1] + (AbsValue(estimate2 - estimate2[1])), noise2);

# SECOND PASS. Plots
plot Kalman2 = estimate2;
Kalman2.SetPaintingStrategy(PaintingStrategy.Line_vs_Points);
Kalman2.SetLineWeight(2);
Kalman2.AssignValueColor(if Kalman2 >= Kalman2[1] then Color.Light_Red else Color.Dark_Red);
Kalman2.HideBubble();

# Third PASS. Calculate Dynamic Volatility
def noise3 = (stdev(Kalman2, length3) * smoothness3) + 0.0001;

# Third PASS. Kalman Recursive Logic
rec errorEst3;
rec estimate3;

# Third PASS. The Kalman Gain: K = (Error in Estimate) / (Error in Estimate + Error in Measurement)
def K3 = if (errorEst3[1] + noise3) != 0
        then errorEst3[1] / (errorEst3[1] + noise3)
        else 0.5; # Default to 0.5 if math fails to prevent flat lines

# Third PASS. Update State
Estimate3 = CompoundValue(1, estimate3[1] + K3 * (Kalman2 – estimate3[1]), Kalman2);

# Third PASS. Update the error estimate for the next calculation
errorEst3 = CompoundValue(1, (1 – K3) * errorEst3[1] + (AbsValue(estimate3 – estimate3[1])), noise3);

# Third PASS. Plots
plot Kalman3 = estimate3;
Kalman3.SetPaintingStrategy(PaintingStrategy.Line_vs_Points);
Kalman3.SetLineWeight(2);
Kalman3.AssignValueColor(if Kalman3 >= Kalman3[1] then Color.Cyan else Color.Blue);
Kalman3.HideBubble();
Input AverageType = averagetype.Wilders;
Def A = MovingAverage(AverageType, TrueRange(high, close, low), 30);
Def TR = Average(A,60);
#Plot UB4 = Kalman3 + (6.85 * TR);
#UB4.SetDefaultColor(Color.Yellow);
#UB4.HideBubble();
Plot UB3 = Kalman3 + (4.25 * TR);
UB3.SetDefaultColor(Color.Gray);
UB3.HideBubble();
Plot UB2 = Kalman3 + (2.618 * TR);
UB2.SetDefaultColor(Color.Yellow);
UB2.HideBubble();
Plot UB1 = Kalman3 + (1.618 * TR);
UB1.SetDefaultColor(Color.Gray);
UB1.HideBubble();
Plot Cen = Kalman3;
Cen.SetPaintingStrategy(PaintingStrategy.Line);
Cen.SetLineWeight(1);
Cen.AssignValueColor(if Kalman3 >= Kalman3 [1] then Color.Cyan else Color.Blue);
Cen.HideBubble();
Plot LB1 = Kalman3 – (1.618* TR);
LB1.SetDefaultColor(Color.Gray);
LB1.HideBubble();
Plot LB2 = Kalman3  – (2.618* TR);
LB2.SetDefaultColor(Color.Yellow);
LB2.HideBubble();
Plot LB3 = Kalman3 – (4.25* TR);
LB3.SetDefaultColor(Color.Gray);
LB3.HideBubble();
#Plot LB4 = Kalman3 – (6.85* TR);
#LB4.SetDefaultColor(Color.Yellow);
#LB4.HideBubble();
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
750 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top