HMA-Kahlman Trend Module For ThinkOrSwim

bvaikunth

New member
Ruby:
#===HMA-Kahlman Trend Module / Trendlines Module===
#study("HMA-Kahlman Trend & Trendlines", precision=2, overlay=true)
#// compilation: capissimo
#
#// This script utilizes two modules, Trendlines Module (by Joris Duyck) and HMA-Kahlman Trend Module. 
#// Trendlines module produces crossovers predictive of the next local trend.

#//*** HMA-Kahlman Trend Module 
# ported by @bvaikunth 6/2022
#requested by @itrades42 
input price = hl2;
input hkmod = Yes;
input length = 14;
input k = Yes;
input gain = 0.7;
input O = Yes;
input DisplayLabel = yes;

script nz
{
input data1 = 0;
input data2 = close;
def ret_val = if IsNaN(data1) then data1 else data2;
plot return = ret_val;
}

script hma
{
input x = hl2;
input p = 22;
def ret_val = WMA((2 * WMA(x, p / 2)) - WMA(x, p), Round(Sqrt(p)));
plot return = ret_val;
}
script hma3
{
input p = 11;
def ret_val = WMA(WMA(close, p / 3) * 3 - WMA(close, p / 2) - WMA(close, p), p);
plot return = ret_val;
}

script kahlman
{
input x = hl2;
input g = 0.7;
def kf;
def dk;
def smooth;
def velo;
dk = x - nz(kf[1], x);
smooth = nz(kf[1], x) + dk * Sqrt(g * 2);
velo = nz(velo[1], 0) + (g * dk);
kf = smooth + velo;
plot retun = kf;
}

def a = if k then kahlman(hma(price, length), gain) else hma(price, length);
def b = if k then kahlman(hma3(p = length / 2), gain) else hma3(p = length / 2);
def c = if b > a then 1 else 0;

def crossdn = a > b and a[1] < b[1];
def crossup = b > a and b[1] < a[1];
def ofs = if O then -1 else 0;

plot aa = a;
aa.AssignValueColor(if c then Color.CYAN else Color.RED);
AddCloud(if b > a then a else Double.NaN, b, Color.GREEN, Color.GREEN);
AddCloud(if b < a then b else Double.NaN, a, Color.RED, Color.RED);

AddChartBubble(displayLabel and crossdn, a, "S", Color.WHITE);
AddChartBubble(displayLabel and crossup, a, "B", Color.WHITE);

declare Once_Per_Bar;

## Inputs
input showLines = yes;
input showValues = no;
input showBarNumbers = no;
input ExtensionLengthBars = 20; # added to control length of Entension
input UpperExtensionPercentLimit = 5;
input LowerExtensionPercentLimit = 5;

def vHigh = high; # creates the variable vHigh. Use of the variable reduce data calls to tos iData server

def vLow = low;
# def initLow = CompoundValue(1, low, low);
def vOpen = open;
# def initOpen = CompoundValue(1, open, open);
def vClose = close;
# def initClose = CompoundValue(1, close, close);
def vVolume = volume;
# def initVolume = CompoundValue(1, volume, volume);
def nan = Double.NaN;
# Bar Time & Date
def bn = BarNumber();
def currentBar = HighestAll(if !IsNaN(vHigh) then bn else nan);


# Bubble Locations
def x_AxisLastExpansionBar = BarNumber() == HighestAll(BarNumber());

def PH;
def PL;
def hh = fold i = 1 to length + 1
with p = 1
while p
do vHigh > getValue(vHigh, -i);
PH = if (bn > length and
vHigh == highest(vHigh, length) and
hh)
then vHigh
else double.NaN;
def ll = fold j = 1 to length + 1
with q = 1
while q
do vLow < getValue(low, -j);
PL = if (bn > length and
vLow == lowest(vLow, length) and
ll)
then vLow
else double.NaN;
def PHBar = if !isNaN(PH)
then bn
else PHBar[1];
def PLBar = if !isNaN(PL)
then bn
else PLBar[1];
def PHL = if !isNaN(PH)
then PH
else PHL[1];
def priorPHBar = if PHL != PHL[1]
then PHBar[1]
else priorPHBar[1];
def PLL = if !isNaN(PL)
then PL
else PLL[1];
def priorPLBar = if PLL != PLL[1]
then PLBar[1]
else priorPLBar[1];
def HighPivots = bn >= highestAll(priorPHBar);
def LowPivots = bn >= highestAll(priorPLBar);
def FirstRpoint = if HighPivots
then bn - PHBar
else 0;
def PriorRpoint = if HighPivots
then bn - PriorPHBar
else 0;
def RSlope = (getvalue(PH, FirstRpoint) - getvalue(PH, PriorRpoint))
/ (PHBar - PriorPHBar);
def FirstSpoint = if LowPivots
then bn - PLBar
else 0;
def PriorSpoint = if LowPivots
then bn - PriorPLBar
else 0;
def SSlope = (getvalue(PL, FirstSpoint) - getvalue(PL, PriorSpoint))
/ (PLBar - PriorPLBar);
def RExtend = if bn == highestall(PHBar)
then 1
else RExtend[1];
def SExtend = if bn == highestall(PLBar)
then 1
else SExtend[1];

plot pivotHigh = if HighPivots
then PH
else double.NaN;
pivotHigh.SetDefaultColor(GetColor(1));
pivotHigh.setPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
pivotHigh.setHiding(!showValues);

plot pivotHighLine = if PHL > 0 and
HighPivots
then PHL
else double.NaN;
pivotHighLine.SetPaintingStrategy(PaintingStrategy.DASHES);
pivotHighLine.setDefaultColor(color.uptick);
pivotHighLine.setHiding(!showLines);

plot RLine = pivotHigh;
RLine.enableApproximation();
RLine.SetDefaultColor(Color.LIGHT_GRAY);
RLine.SetStyle(Curve.Short_DASH);

def calc_ResistanceExtension = if RExtend
then (bn - PHBar) * RSlope + PHL
else double.NaN;
plot line_ResistanceExtension = if bn <= (Currentbar + ExtensionLengthBars)
and calc_ResistanceExtension[1] >= (lowestall(vLow) * (1-(lowerExtensionPercentLimit/100)))
and calc_ResistanceExtension[1] <= (Highestall(vHigh) * (1 + (upperExtensionPercentLimit/100)))
then calc_ResistanceExtension else double.nan;
line_ResistanceExtension.SetStyle(Curve.Short_DASH);
line_ResistanceExtension.SetDefaultColor(color.LIGHT_GRAY); #was 7
line_ResistanceExtension.setLineWeight(1);

# Low Plots
plot pivotLow = if LowPivots
then PL
else double.NaN;
pivotLow.setDefaultColor(GetColor(4));
pivotLow.setPaintingStrategy(PaintingStrategy.VALUES_BELOW);
pivotLow.setHiding(!showValues);

plot pivotLowLine = if PLL > 0 and
LowPivots
then PLL
else double.NaN;
pivotLowLine.SetPaintingStrategy(PaintingStrategy.DASHES);
pivotLowLine.setDefaultColor(color.DOWNTICK);
pivotLowLine.setHiding(!showLines);

plot SupportLine = pivotLow;
SupportLine.enableApproximation();
SupportLine.SetDefaultColor(color.LIGHT_GRAY);
SUpportLine.SetStyle(Curve.Short_DASH);

def calc_SupportExtension = if SExtend
then (bn - PLBar) * SSlope + PLL
else double.NaN;
plot line_SupportExtension = if bn <= (Currentbar + ExtensionLengthBars)
and calc_SupportExtension[1] >= (lowestall(vLow) * (1-(lowerExtensionPercentLimit/100)))
and calc_SupportExtension[1] <= (Highestall(vHigh) * (1 + (upperExtensionPercentLimit/100)))
then calc_supportExtension else double.nan;
line_SupportExtension.SetDefaultColor(color.LIGHT_GRAY); #was 7
line_SupportExtension.SetStyle(Curve.Short_DASH);
line_SupportExtension.setLineWeight(1);

plot BarNumbersBelow = bn;
BarNumbersBelow.SetDefaultColor(GetColor(0));
BarNumbersBelow.setHiding(!showBarNumbers);
BarNumbersBelow.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);

plot PivotDot = if !isNaN(pivotHigh)
then pivotHigh
else if !isNaN(pivotLow)
then pivotLow
else double.NaN;
pivotDot.SetDefaultColor(GetColor(7));
pivotDot.SetPaintingStrategy(PaintingStrategy.POINTS);
pivotDot.SetLineWeight(3);
 
Ruby:
#===HMA-Kahlman Trend Module / Trendlines Module===
#study("HMA-Kahlman Trend & Trendlines", precision=2, overlay=true)
#// compilation: capissimo
#
#// This script utilizes two modules, Trendlines Module (by Joris Duyck) and HMA-Kahlman Trend Module.
#// Trendlines module produces crossovers predictive of the next local trend.

#//*** HMA-Kahlman Trend Module
# ported by @bvaikunth 6/2022
#requested by @itrades42
input price = hl2;
input hkmod = Yes;
input length = 14;
input k = Yes;
input gain = 0.7;
input O = Yes;
input DisplayLabel = yes;

script nz
{
input data1 = 0;
input data2 = close;
def ret_val = if IsNaN(data1) then data1 else data2;
plot return = ret_val;
}

script hma
{
input x = hl2;
input p = 22;
def ret_val = WMA((2 * WMA(x, p / 2)) - WMA(x, p), Round(Sqrt(p)));
plot return = ret_val;
}
script hma3
{
input p = 11;
def ret_val = WMA(WMA(close, p / 3) * 3 - WMA(close, p / 2) - WMA(close, p), p);
plot return = ret_val;
}

script kahlman
{
input x = hl2;
input g = 0.7;
def kf;
def dk;
def smooth;
def velo;
dk = x - nz(kf[1], x);
smooth = nz(kf[1], x) + dk * Sqrt(g * 2);
velo = nz(velo[1], 0) + (g * dk);
kf = smooth + velo;
plot retun = kf;
}

def a = if k then kahlman(hma(price, length), gain) else hma(price, length);
def b = if k then kahlman(hma3(p = length / 2), gain) else hma3(p = length / 2);
def c = if b > a then 1 else 0;

def crossdn = a > b and a[1] < b[1];
def crossup = b > a and b[1] < a[1];
def ofs = if O then -1 else 0;

plot aa = a;
aa.AssignValueColor(if c then Color.CYAN else Color.RED);
AddCloud(if b > a then a else Double.NaN, b, Color.GREEN, Color.GREEN);
AddCloud(if b < a then b else Double.NaN, a, Color.RED, Color.RED);

AddChartBubble(displayLabel and crossdn, a, "S", Color.WHITE);
AddChartBubble(displayLabel and crossup, a, "B", Color.WHITE);

declare Once_Per_Bar;

## Inputs
input showLines = yes;
input showValues = no;
input showBarNumbers = no;
input ExtensionLengthBars = 20; # added to control length of Entension
input UpperExtensionPercentLimit = 5;
input LowerExtensionPercentLimit = 5;

def vHigh = high; # creates the variable vHigh. Use of the variable reduce data calls to tos iData server

def vLow = low;
# def initLow = CompoundValue(1, low, low);
def vOpen = open;
# def initOpen = CompoundValue(1, open, open);
def vClose = close;
# def initClose = CompoundValue(1, close, close);
def vVolume = volume;
# def initVolume = CompoundValue(1, volume, volume);
def nan = Double.NaN;
# Bar Time & Date
def bn = BarNumber();
def currentBar = HighestAll(if !IsNaN(vHigh) then bn else nan);


# Bubble Locations
def x_AxisLastExpansionBar = BarNumber() == HighestAll(BarNumber());

def PH;
def PL;
def hh = fold i = 1 to length + 1
with p = 1
while p
do vHigh > getValue(vHigh, -i);
PH = if (bn > length and
vHigh == highest(vHigh, length) and
hh)
then vHigh
else double.NaN;
def ll = fold j = 1 to length + 1
with q = 1
while q
do vLow < getValue(low, -j);
PL = if (bn > length and
vLow == lowest(vLow, length) and
ll)
then vLow
else double.NaN;
def PHBar = if !isNaN(PH)
then bn
else PHBar[1];
def PLBar = if !isNaN(PL)
then bn
else PLBar[1];
def PHL = if !isNaN(PH)
then PH
else PHL[1];
def priorPHBar = if PHL != PHL[1]
then PHBar[1]
else priorPHBar[1];
def PLL = if !isNaN(PL)
then PL
else PLL[1];
def priorPLBar = if PLL != PLL[1]
then PLBar[1]
else priorPLBar[1];
def HighPivots = bn >= highestAll(priorPHBar);
def LowPivots = bn >= highestAll(priorPLBar);
def FirstRpoint = if HighPivots
then bn - PHBar
else 0;
def PriorRpoint = if HighPivots
then bn - PriorPHBar
else 0;
def RSlope = (getvalue(PH, FirstRpoint) - getvalue(PH, PriorRpoint))
/ (PHBar - PriorPHBar);
def FirstSpoint = if LowPivots
then bn - PLBar
else 0;
def PriorSpoint = if LowPivots
then bn - PriorPLBar
else 0;
def SSlope = (getvalue(PL, FirstSpoint) - getvalue(PL, PriorSpoint))
/ (PLBar - PriorPLBar);
def RExtend = if bn == highestall(PHBar)
then 1
else RExtend[1];
def SExtend = if bn == highestall(PLBar)
then 1
else SExtend[1];

plot pivotHigh = if HighPivots
then PH
else double.NaN;
pivotHigh.SetDefaultColor(GetColor(1));
pivotHigh.setPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
pivotHigh.setHiding(!showValues);

plot pivotHighLine = if PHL > 0 and
HighPivots
then PHL
else double.NaN;
pivotHighLine.SetPaintingStrategy(PaintingStrategy.DASHES);
pivotHighLine.setDefaultColor(color.uptick);
pivotHighLine.setHiding(!showLines);

plot RLine = pivotHigh;
RLine.enableApproximation();
RLine.SetDefaultColor(Color.LIGHT_GRAY);
RLine.SetStyle(Curve.Short_DASH);

def calc_ResistanceExtension = if RExtend
then (bn - PHBar) * RSlope + PHL
else double.NaN;
plot line_ResistanceExtension = if bn <= (Currentbar + ExtensionLengthBars)
and calc_ResistanceExtension[1] >= (lowestall(vLow) * (1-(lowerExtensionPercentLimit/100)))
and calc_ResistanceExtension[1] <= (Highestall(vHigh) * (1 + (upperExtensionPercentLimit/100)))
then calc_ResistanceExtension else double.nan;
line_ResistanceExtension.SetStyle(Curve.Short_DASH);
line_ResistanceExtension.SetDefaultColor(color.LIGHT_GRAY); #was 7
line_ResistanceExtension.setLineWeight(1);

# Low Plots
plot pivotLow = if LowPivots
then PL
else double.NaN;
pivotLow.setDefaultColor(GetColor(4));
pivotLow.setPaintingStrategy(PaintingStrategy.VALUES_BELOW);
pivotLow.setHiding(!showValues);

plot pivotLowLine = if PLL > 0 and
LowPivots
then PLL
else double.NaN;
pivotLowLine.SetPaintingStrategy(PaintingStrategy.DASHES);
pivotLowLine.setDefaultColor(color.DOWNTICK);
pivotLowLine.setHiding(!showLines);

plot SupportLine = pivotLow;
SupportLine.enableApproximation();
SupportLine.SetDefaultColor(color.LIGHT_GRAY);
SUpportLine.SetStyle(Curve.Short_DASH);

def calc_SupportExtension = if SExtend
then (bn - PLBar) * SSlope + PLL
else double.NaN;
plot line_SupportExtension = if bn <= (Currentbar + ExtensionLengthBars)
and calc_SupportExtension[1] >= (lowestall(vLow) * (1-(lowerExtensionPercentLimit/100)))
and calc_SupportExtension[1] <= (Highestall(vHigh) * (1 + (upperExtensionPercentLimit/100)))
then calc_supportExtension else double.nan;
line_SupportExtension.SetDefaultColor(color.LIGHT_GRAY); #was 7
line_SupportExtension.SetStyle(Curve.Short_DASH);
line_SupportExtension.setLineWeight(1);

plot BarNumbersBelow = bn;
BarNumbersBelow.SetDefaultColor(GetColor(0));
BarNumbersBelow.setHiding(!showBarNumbers);
BarNumbersBelow.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);

plot PivotDot = if !isNaN(pivotHigh)
then pivotHigh
else if !isNaN(pivotLow)
then pivotLow
else double.NaN;
pivotDot.SetDefaultColor(GetColor(7));
pivotDot.SetPaintingStrategy(PaintingStrategy.POINTS);
pivotDot.SetLineWeight(3);
Thank you very much for the script.. a quick question why is not showing up on the dairy charts but in minutes charts is showing up? thanks in advance
 
Ruby:
#===HMA-Kahlman Trend Module / Trendlines Module===
#study("HMA-Kahlman Trend & Trendlines", precision=2, overlay=true)
#// compilation: capissimo
#
#// This script utilizes two modules, Trendlines Module (by Joris Duyck) and HMA-Kahlman Trend Module.
#// Trendlines module produces crossovers predictive of the next local trend.

#//*** HMA-Kahlman Trend Module
# ported by @bvaikunth 6/2022
#requested by @itrades42
input price = hl2;
input hkmod = Yes;
input length = 14;
input k = Yes;
input gain = 0.7;
input O = Yes;
input DisplayLabel = yes;

script nz
{
input data1 = 0;
input data2 = close;
def ret_val = if IsNaN(data1) then data1 else data2;
plot return = ret_val;
}

script hma
{
input x = hl2;
input p = 22;
def ret_val = WMA((2 * WMA(x, p / 2)) - WMA(x, p), Round(Sqrt(p)));
plot return = ret_val;
}
script hma3
{
input p = 11;
def ret_val = WMA(WMA(close, p / 3) * 3 - WMA(close, p / 2) - WMA(close, p), p);
plot return = ret_val;
}

script kahlman
{
input x = hl2;
input g = 0.7;
def kf;
def dk;
def smooth;
def velo;
dk = x - nz(kf[1], x);
smooth = nz(kf[1], x) + dk * Sqrt(g * 2);
velo = nz(velo[1], 0) + (g * dk);
kf = smooth + velo;
plot retun = kf;
}

def a = if k then kahlman(hma(price, length), gain) else hma(price, length);
def b = if k then kahlman(hma3(p = length / 2), gain) else hma3(p = length / 2);
def c = if b > a then 1 else 0;

def crossdn = a > b and a[1] < b[1];
def crossup = b > a and b[1] < a[1];
def ofs = if O then -1 else 0;

plot aa = a;
aa.AssignValueColor(if c then Color.CYAN else Color.RED);
AddCloud(if b > a then a else Double.NaN, b, Color.GREEN, Color.GREEN);
AddCloud(if b < a then b else Double.NaN, a, Color.RED, Color.RED);

AddChartBubble(displayLabel and crossdn, a, "S", Color.WHITE);
AddChartBubble(displayLabel and crossup, a, "B", Color.WHITE);

declare Once_Per_Bar;

## Inputs
input showLines = yes;
input showValues = no;
input showBarNumbers = no;
input ExtensionLengthBars = 20; # added to control length of Entension
input UpperExtensionPercentLimit = 5;
input LowerExtensionPercentLimit = 5;

def vHigh = high; # creates the variable vHigh. Use of the variable reduce data calls to tos iData server

def vLow = low;
# def initLow = CompoundValue(1, low, low);
def vOpen = open;
# def initOpen = CompoundValue(1, open, open);
def vClose = close;
# def initClose = CompoundValue(1, close, close);
def vVolume = volume;
# def initVolume = CompoundValue(1, volume, volume);
def nan = Double.NaN;
# Bar Time & Date
def bn = BarNumber();
def currentBar = HighestAll(if !IsNaN(vHigh) then bn else nan);


# Bubble Locations
def x_AxisLastExpansionBar = BarNumber() == HighestAll(BarNumber());

def PH;
def PL;
def hh = fold i = 1 to length + 1
with p = 1
while p
do vHigh > getValue(vHigh, -i);
PH = if (bn > length and
vHigh == highest(vHigh, length) and
hh)
then vHigh
else double.NaN;
def ll = fold j = 1 to length + 1
with q = 1
while q
do vLow < getValue(low, -j);
PL = if (bn > length and
vLow == lowest(vLow, length) and
ll)
then vLow
else double.NaN;
def PHBar = if !isNaN(PH)
then bn
else PHBar[1];
def PLBar = if !isNaN(PL)
then bn
else PLBar[1];
def PHL = if !isNaN(PH)
then PH
else PHL[1];
def priorPHBar = if PHL != PHL[1]
then PHBar[1]
else priorPHBar[1];
def PLL = if !isNaN(PL)
then PL
else PLL[1];
def priorPLBar = if PLL != PLL[1]
then PLBar[1]
else priorPLBar[1];
def HighPivots = bn >= highestAll(priorPHBar);
def LowPivots = bn >= highestAll(priorPLBar);
def FirstRpoint = if HighPivots
then bn - PHBar
else 0;
def PriorRpoint = if HighPivots
then bn - PriorPHBar
else 0;
def RSlope = (getvalue(PH, FirstRpoint) - getvalue(PH, PriorRpoint))
/ (PHBar - PriorPHBar);
def FirstSpoint = if LowPivots
then bn - PLBar
else 0;
def PriorSpoint = if LowPivots
then bn - PriorPLBar
else 0;
def SSlope = (getvalue(PL, FirstSpoint) - getvalue(PL, PriorSpoint))
/ (PLBar - PriorPLBar);
def RExtend = if bn == highestall(PHBar)
then 1
else RExtend[1];
def SExtend = if bn == highestall(PLBar)
then 1
else SExtend[1];

plot pivotHigh = if HighPivots
then PH
else double.NaN;
pivotHigh.SetDefaultColor(GetColor(1));
pivotHigh.setPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
pivotHigh.setHiding(!showValues);

plot pivotHighLine = if PHL > 0 and
HighPivots
then PHL
else double.NaN;
pivotHighLine.SetPaintingStrategy(PaintingStrategy.DASHES);
pivotHighLine.setDefaultColor(color.uptick);
pivotHighLine.setHiding(!showLines);

plot RLine = pivotHigh;
RLine.enableApproximation();
RLine.SetDefaultColor(Color.LIGHT_GRAY);
RLine.SetStyle(Curve.Short_DASH);

def calc_ResistanceExtension = if RExtend
then (bn - PHBar) * RSlope + PHL
else double.NaN;
plot line_ResistanceExtension = if bn <= (Currentbar + ExtensionLengthBars)
and calc_ResistanceExtension[1] >= (lowestall(vLow) * (1-(lowerExtensionPercentLimit/100)))
and calc_ResistanceExtension[1] <= (Highestall(vHigh) * (1 + (upperExtensionPercentLimit/100)))
then calc_ResistanceExtension else double.nan;
line_ResistanceExtension.SetStyle(Curve.Short_DASH);
line_ResistanceExtension.SetDefaultColor(color.LIGHT_GRAY); #was 7
line_ResistanceExtension.setLineWeight(1);

# Low Plots
plot pivotLow = if LowPivots
then PL
else double.NaN;
pivotLow.setDefaultColor(GetColor(4));
pivotLow.setPaintingStrategy(PaintingStrategy.VALUES_BELOW);
pivotLow.setHiding(!showValues);

plot pivotLowLine = if PLL > 0 and
LowPivots
then PLL
else double.NaN;
pivotLowLine.SetPaintingStrategy(PaintingStrategy.DASHES);
pivotLowLine.setDefaultColor(color.DOWNTICK);
pivotLowLine.setHiding(!showLines);

plot SupportLine = pivotLow;
SupportLine.enableApproximation();
SupportLine.SetDefaultColor(color.LIGHT_GRAY);
SUpportLine.SetStyle(Curve.Short_DASH);

def calc_SupportExtension = if SExtend
then (bn - PLBar) * SSlope + PLL
else double.NaN;
plot line_SupportExtension = if bn <= (Currentbar + ExtensionLengthBars)
and calc_SupportExtension[1] >= (lowestall(vLow) * (1-(lowerExtensionPercentLimit/100)))
and calc_SupportExtension[1] <= (Highestall(vHigh) * (1 + (upperExtensionPercentLimit/100)))
then calc_supportExtension else double.nan;
line_SupportExtension.SetDefaultColor(color.LIGHT_GRAY); #was 7
line_SupportExtension.SetStyle(Curve.Short_DASH);
line_SupportExtension.setLineWeight(1);

plot BarNumbersBelow = bn;
BarNumbersBelow.SetDefaultColor(GetColor(0));
BarNumbersBelow.setHiding(!showBarNumbers);
BarNumbersBelow.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);

plot PivotDot = if !isNaN(pivotHigh)
then pivotHigh
else if !isNaN(pivotLow)
then pivotLow
else double.NaN;
pivotDot.SetDefaultColor(GetColor(7));
pivotDot.SetPaintingStrategy(PaintingStrategy.POINTS);
pivotDot.SetLineWeight(3);
changing the gain has no impact seems there issue in the kahlman script. check this
https://usethinkscript.com/threads/kalman-filter.868/#post-75366
 
Last edited:
I think the script need some check it out because i compare with the TV is not the same signal. it is pretty close
just change kahlman script to the below

script kahlman {
input x = 0;
input g = 0;
def kf;
def dk = x - if IsNaN(kf[1]) then x else kf[1];
def smooth = if IsNaN(kf[1]) then x else kf[1] + dk * Sqrt(g * 2);
def velo = if IsNaN(velo[1]) then 0 else velo[1] + (g * dk);
kf = smooth + velo;

plot return = kf;
}
 
Ruby:
#===HMA-Kahlman Trend Module / Trendlines Module===
#study("HMA-Kahlman Trend & Trendlines", precision=2, overlay=true)
#// compilation: capissimo
#
#// This script utilizes two modules, Trendlines Module (by Joris Duyck) and HMA-Kahlman Trend Module.
#// Trendlines module produces crossovers predictive of the next local trend.

#//*** HMA-Kahlman Trend Module
# ported by @bvaikunth 6/2022
#requested by @itrades42
input price = hl2;
input hkmod = Yes;
input length = 14;
input k = Yes;
input gain = 0.7;
input O = Yes;
input DisplayLabel = yes;

script nz
{
input data1 = 0;
input data2 = close;
def ret_val = if IsNaN(data1) then data1 else data2;
plot return = ret_val;
}

script hma
{
input x = hl2;
input p = 22;
def ret_val = WMA((2 * WMA(x, p / 2)) - WMA(x, p), Round(Sqrt(p)));
plot return = ret_val;
}
script hma3
{
input p = 11;
def ret_val = WMA(WMA(close, p / 3) * 3 - WMA(close, p / 2) - WMA(close, p), p);
plot return = ret_val;
}

script kahlman
{
input x = hl2;
input g = 0.7;
def kf;
def dk;
def smooth;
def velo;
dk = x - nz(kf[1], x);
smooth = nz(kf[1], x) + dk * Sqrt(g * 2);
velo = nz(velo[1], 0) + (g * dk);
kf = smooth + velo;
plot retun = kf;
}

def a = if k then kahlman(hma(price, length), gain) else hma(price, length);
def b = if k then kahlman(hma3(p = length / 2), gain) else hma3(p = length / 2);
def c = if b > a then 1 else 0;

def crossdn = a > b and a[1] < b[1];
def crossup = b > a and b[1] < a[1];
def ofs = if O then -1 else 0;

plot aa = a;
aa.AssignValueColor(if c then Color.CYAN else Color.RED);
AddCloud(if b > a then a else Double.NaN, b, Color.GREEN, Color.GREEN);
AddCloud(if b < a then b else Double.NaN, a, Color.RED, Color.RED);

AddChartBubble(displayLabel and crossdn, a, "S", Color.WHITE);
AddChartBubble(displayLabel and crossup, a, "B", Color.WHITE);

declare Once_Per_Bar;

## Inputs
input showLines = yes;
input showValues = no;
input showBarNumbers = no;
input ExtensionLengthBars = 20; # added to control length of Entension
input UpperExtensionPercentLimit = 5;
input LowerExtensionPercentLimit = 5;

def vHigh = high; # creates the variable vHigh. Use of the variable reduce data calls to tos iData server

def vLow = low;
# def initLow = CompoundValue(1, low, low);
def vOpen = open;
# def initOpen = CompoundValue(1, open, open);
def vClose = close;
# def initClose = CompoundValue(1, close, close);
def vVolume = volume;
# def initVolume = CompoundValue(1, volume, volume);
def nan = Double.NaN;
# Bar Time & Date
def bn = BarNumber();
def currentBar = HighestAll(if !IsNaN(vHigh) then bn else nan);


# Bubble Locations
def x_AxisLastExpansionBar = BarNumber() == HighestAll(BarNumber());

def PH;
def PL;
def hh = fold i = 1 to length + 1
with p = 1
while p
do vHigh > getValue(vHigh, -i);
PH = if (bn > length and
vHigh == highest(vHigh, length) and
hh)
then vHigh
else double.NaN;
def ll = fold j = 1 to length + 1
with q = 1
while q
do vLow < getValue(low, -j);
PL = if (bn > length and
vLow == lowest(vLow, length) and
ll)
then vLow
else double.NaN;
def PHBar = if !isNaN(PH)
then bn
else PHBar[1];
def PLBar = if !isNaN(PL)
then bn
else PLBar[1];
def PHL = if !isNaN(PH)
then PH
else PHL[1];
def priorPHBar = if PHL != PHL[1]
then PHBar[1]
else priorPHBar[1];
def PLL = if !isNaN(PL)
then PL
else PLL[1];
def priorPLBar = if PLL != PLL[1]
then PLBar[1]
else priorPLBar[1];
def HighPivots = bn >= highestAll(priorPHBar);
def LowPivots = bn >= highestAll(priorPLBar);
def FirstRpoint = if HighPivots
then bn - PHBar
else 0;
def PriorRpoint = if HighPivots
then bn - PriorPHBar
else 0;
def RSlope = (getvalue(PH, FirstRpoint) - getvalue(PH, PriorRpoint))
/ (PHBar - PriorPHBar);
def FirstSpoint = if LowPivots
then bn - PLBar
else 0;
def PriorSpoint = if LowPivots
then bn - PriorPLBar
else 0;
def SSlope = (getvalue(PL, FirstSpoint) - getvalue(PL, PriorSpoint))
/ (PLBar - PriorPLBar);
def RExtend = if bn == highestall(PHBar)
then 1
else RExtend[1];
def SExtend = if bn == highestall(PLBar)
then 1
else SExtend[1];

plot pivotHigh = if HighPivots
then PH
else double.NaN;
pivotHigh.SetDefaultColor(GetColor(1));
pivotHigh.setPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
pivotHigh.setHiding(!showValues);

plot pivotHighLine = if PHL > 0 and
HighPivots
then PHL
else double.NaN;
pivotHighLine.SetPaintingStrategy(PaintingStrategy.DASHES);
pivotHighLine.setDefaultColor(color.uptick);
pivotHighLine.setHiding(!showLines);

plot RLine = pivotHigh;
RLine.enableApproximation();
RLine.SetDefaultColor(Color.LIGHT_GRAY);
RLine.SetStyle(Curve.Short_DASH);

def calc_ResistanceExtension = if RExtend
then (bn - PHBar) * RSlope + PHL
else double.NaN;
plot line_ResistanceExtension = if bn <= (Currentbar + ExtensionLengthBars)
and calc_ResistanceExtension[1] >= (lowestall(vLow) * (1-(lowerExtensionPercentLimit/100)))
and calc_ResistanceExtension[1] <= (Highestall(vHigh) * (1 + (upperExtensionPercentLimit/100)))
then calc_ResistanceExtension else double.nan;
line_ResistanceExtension.SetStyle(Curve.Short_DASH);
line_ResistanceExtension.SetDefaultColor(color.LIGHT_GRAY); #was 7
line_ResistanceExtension.setLineWeight(1);

# Low Plots
plot pivotLow = if LowPivots
then PL
else double.NaN;
pivotLow.setDefaultColor(GetColor(4));
pivotLow.setPaintingStrategy(PaintingStrategy.VALUES_BELOW);
pivotLow.setHiding(!showValues);

plot pivotLowLine = if PLL > 0 and
LowPivots
then PLL
else double.NaN;
pivotLowLine.SetPaintingStrategy(PaintingStrategy.DASHES);
pivotLowLine.setDefaultColor(color.DOWNTICK);
pivotLowLine.setHiding(!showLines);

plot SupportLine = pivotLow;
SupportLine.enableApproximation();
SupportLine.SetDefaultColor(color.LIGHT_GRAY);
SUpportLine.SetStyle(Curve.Short_DASH);

def calc_SupportExtension = if SExtend
then (bn - PLBar) * SSlope + PLL
else double.NaN;
plot line_SupportExtension = if bn <= (Currentbar + ExtensionLengthBars)
and calc_SupportExtension[1] >= (lowestall(vLow) * (1-(lowerExtensionPercentLimit/100)))
and calc_SupportExtension[1] <= (Highestall(vHigh) * (1 + (upperExtensionPercentLimit/100)))
then calc_supportExtension else double.nan;
line_SupportExtension.SetDefaultColor(color.LIGHT_GRAY); #was 7
line_SupportExtension.SetStyle(Curve.Short_DASH);
line_SupportExtension.setLineWeight(1);

plot BarNumbersBelow = bn;
BarNumbersBelow.SetDefaultColor(GetColor(0));
BarNumbersBelow.setHiding(!showBarNumbers);
BarNumbersBelow.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);

plot PivotDot = if !isNaN(pivotHigh)
then pivotHigh
else if !isNaN(pivotLow)
then pivotLow
else double.NaN;
pivotDot.SetDefaultColor(GetColor(7));
pivotDot.SetPaintingStrategy(PaintingStrategy.POINTS);
pivotDot.SetLineWeight(3);
Hello , i will like to see if it is possible to create a STRATEGY with this system.. can someone create the buy and sell order for bask testing? thanks in advance.
 
Hello , i will like to see if it is possible to create a STRATEGY with this system.. can someone create the buy and sell order for bask testing? thanks in advance.
  1. Copy&Paste the script into the STRATEGY tab (to the right of the study tab)
  2. Add these lines to the bottom of your script:
Ruby:
AddOrder(OrderType.BUY_TO_OPEN, crossup, tickcolor = GetColor(0), arrowcolor = GetColor(0), name = "LE");
AddOrder(OrderType.SELL_TO_CLOSE, crossdn, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "SE");
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
311 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