#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © loxx
#indicator("Step-MA Filtered Stochastic [Loxx]", shorttitle='SMAFS [Loxx]',
# Converted by Sam4Cok@Samer800 - 03/2023
declare lower;
input colorBars = yes; # "Color bars?"
input showSignals = yes; # "Show signals?"
input Period = 32; # "Period"
input Smoothing = 3; # "%D Smoothing"
input Sensitivity = 4; # "Sensitivity"
input StepSize = 5; # "Step Size"
input StepMultiplier = 0.5; # "Step Multiplier"
input UpperLevel = 80; # "Upper Level"
input BottomLevel = 20; # "Bottom Level"
input SignalType = {"Signal", default "Middle Crosses", "Levels Crosses"}; # "Signal type"
def na = Double.NaN;
def last = isNaN(Close);
def SM02 = SignalType==SignalType."Signal";
def SM03 = SignalType==SignalType."Middle Crosses";
def SM04 = SignalType==SignalType."Levels Crosses";
#--- Functions
# stoch(source, high, low, length) =>
script stoch {
input src = close;
input h = high;
input l = low;
input len = 32;
def stoch = 100 * (src - lowest(l, len)) / (highest(h, len) - lowest(l, len));
plot return = stoch;
}
#_stepma(float sense, float size, float stepMulti, phigh, plow, pprice)=>
script _stepma {
input sense = 4;
input size = 5;
input stepMulti = 0.5;
input phigh = high;
input plow = low;
input pprice = close;
def trend;# = 0.
def out;# = 0.
def smax;
def smin;
def smax1;
def smin1;
def sensitivity = if sense == 0 then 0.000001 else sense;
def stepSize = if size == 0 then 0.000001 else size;
def sizea = sensitivity * stepSize;
smax1 = phigh + 2.0 * sizea * stepMulti;
smin1 = plow - 2.0 * sizea * stepMulti;
def trend1 = trend[1];
if (pprice > smax[1]) {
trend = 1;
} else
if (pprice < smin[1]) {
trend = -1;
} else {
trend = trend1;
}
if (trend == 1) {
smin = if (smin1 < smin[1]) then smin[1] else smin1;
smax = smax1;
out = smin + sizea * stepMulti;
} else
if (trend == -1) {
smin = smin1;
smax = if (smax1 > smax[1]) then smax[1] else smax1;
out = smax - sizea * stepMulti;
} else {
smin = smin[1];
smax = smax[1];
out = out[1];
}
plot return = out;
}
def out1 = stoch(close, high, low, Period);
def sig1 = Average(out1, Smoothing);
def out = _stepma(Sensitivity, StepSize, StepMultiplier, out1, out1, out1);
def sig = _stepma(Sensitivity, StepSize, StepMultiplier, sig1, sig1, sig1);
def mid = 50;
def state;
if SM02 {
state = if (out < sig) then -1 else if (out > sig) then 1 else state[1];
} else
if SM03 {
state = if (out < mid) then -1 else if (out > mid) then 1 else state[1];
} else
if SM04 {
state = if (out < BottomLevel) then -1 else if (out > UpperLevel) then 1 else state[1];
} else {
state = state[1];
}
plot StepStoch = out; # "Step Stochastic"
StepStoch.SetLineWeight(2);
StepStoch.AssignValueColor(if state ==1 then Color.GREEN else
if state ==-1 then Color.RED else Color.GRAY);
plot Overbought = if last then na else UpperLevel; # "Overbought"
plot Oversold = if last then na else BottomLevel; # "Oversold"
plot MidLine = if last then na else mid; # "Mid"
Overbought.SetDefaultColor(Color.GRAY);
Oversold.SetDefaultColor(Color.GRAY);
MidLine.SetDefaultColor(Color.GRAY);
MidLine.SetStyle(Curve.SHORT_DASH);
#--- Signals
def goLong = if showSignals then
if SM02 then (out Crosses Above sig) else
if SM03 then (out Crosses Above mid) else (out Crosses Above UpperLevel) else na;
def goShort = if showSignals then
if SM02 then (out Crosses Below sig) else
if SM03 then (out Crosses Below mid) else (out Crosses Below BottomLevel) else na;
plot Long = if goLong then Oversold else na; # "Long"
Long.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
Long.SetDefaultColor(Color.CYAN);
plot Short = if goShort then Overbought else na; # "Short"
Short.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
Short.SetDefaultColor(Color.MAGENTA);
#--- Bar Color
AssignPriceColor(if !colorbars then Color.CURRENT else
if state ==1 then Color.GREEN else
if state ==-1 then Color.RED else Color.GRAY);
#--- END CODE