Step-MA Filtered Stochastic For ThinkOrSwim

check the below

CSS:
#// 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
 

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
361 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