Parabolic SAR and Supertrend Zone for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
CiPlitY.png

I combined PSAR and ST to select from with options. I added option to enable HeikinAshi source instead of normal source, Not totally new indicator but it can be helpful tool.
Author Message:
The tool designed to help traders identify the best zone to enter in a position revisiting the usage of the standard Parabolic SAR and SuperTrend indicator.

CODE:
CSS:
#https://www.tradingview.com/v/tABUf27g/
#/ © OmegaTools
#indicator("Parabolic SAR Zone", overlay = true)
#indicator("SuperTrend Zone", overlay = true, timeframe = "")
# Converted, Combined and mod by Sam4Cok@Samer800    - 08/2023
input colorBars = yes;
input showSignals = yes;
input signalWaitingBar = 2;
input useHeikinAshi = no;
input selectIndicator = {Default "Parabolic SAR Zone", "SuperTrend Zone"};
input supertrendPriceType = hl2;
input SupertrendFactor = 4.00;            # "Supertrend factor"
input TrendCalculationMethod = {default "High/Low" , "Open/Close"};
input sarStartingvalue = 0.02;            # "Starting value"
input sarIncrement = 0.02;                # "Increment"
input sarMaxValue = 0.02;                 # "Max Value"
input changeATR = yes;
input secondPlotMulti = 2.0;
input atrLength = 100;
input Zonewidth  = 1.00;                 # "Zone width"

def na = Double.NaN;
def PAR = selectIndicator==selectIndicator."Parabolic SAR Zone";
def f = secondPlotMulti;
def haClose = (open + high + low + close) / 4;
def haOpen = if !haOpen[1] then (open + close) / 2 else
                (haOpen[1] + haClose[1]) / 2;
def haHigh = Max(Max(high, haOpen), haClose);
def haLow  = Min(Min(low, haOpen), haClose);
def haSrc = (haHigh + haLow) / 2;
def Src  = if useHeikinAshi then haSrc else supertrendPriceType;
def c    = if useHeikinAshi then haClose else close;
def op   = if useHeikinAshi then haOpen else open;
def hi   = if useHeikinAshi then haHigh else high;
def lo   = if useHeikinAshi then haLow else low;
def max = Max(op, c);
def min = Min(op, c);
def h; def l;
switch (TrendCalculationMethod) {
case "High/Low" :
    h = hi;
    l = lo;
case "Open/Close" :
    h = Max;
    l = Min;
}
def h1 = h;
def l1 = l;
def c1 = c;
#-- Color
DefineGlobalColor("up", CreateColor(41, 98, 255));
DefineGlobalColor("Dn", CreateColor(233, 30, 99));
DefineGlobalColor("dup", CreateColor(0, 27, 100));
DefineGlobalColor("dDn", CreateColor(97, 9, 39));

#pine_supertrend(src, factor, atrPeriod) =>
script supertrend {
    input src = hl2;
    input factor = 3;
    input atrPeriod = 10;
    input changeATR = yes;
    input h = high;
    input l = low;
    input c = close;
    def tr = TrueRange(h, c, l);
    def nATR = if changeATR then WildersAverage(tr, atrPeriod) else Average(tr, atrPeriod);
    def lowerBand;
    def upperBand;
    def upBand = src + factor * nATR;
    def loBand = src - factor * nATR;
    def prevLowerBand = if lowerBand[1] then lowerBand[1] else loBand;
    def prevUpperBand = if upperBand[1] then upperBand[1] else upBand;
        lowerBand = if (loBand > prevLowerBand) or (c[1] < prevLowerBand) then loBand else prevLowerBand;
        upperBand = if (upBand < prevUpperBand) or (c[1] > prevUpperBand) then upBand else prevUpperBand;
    def direction ;# = na
    def superTrend;# = na
    def prevSuperTrend = if superTrend[1] then superTrend[1] else prevUpperBand;
    if (IsNaN(prevUpperBand) or IsNaN(prevLowerBand)) {
        direction  = 1;
    } else
    if prevSuperTrend == prevUpperBand {
        direction  = if c > upperBand then -1 else 1;
    } else {
        direction  = if c < lowerBand then  1 else -1;
    }
    superTrend = if direction  == -1 then lowerBand else upperBand;
    plot ST = superTrend;
    plot dir = direction ;
}
#-- TV PSAR
script sar {
    input start = 0.02;
    input inc = 0.02;
    input max = 0.2;
    input h   = high;
    input l   = low;
    input c   = close;
    Assert(start > 0, "'acceleration factor' must be positive: " + start);
    Assert(max >= start, "'acceleration limit' (" + max + ") must be greater than or equal to 'acceleration factor' (" + start + ")");
    def nATR = ATR(Length = 14);
    def state = {default init, long, short};
    def extreme;
    def SAR;
    def acc;
    switch (state[1]) {
    case init:
        state = if c > c[1] then state.long else state.short;
        acc = start;
        extreme = if c > c[1] then h else l;
        SAR = if c > c[1] then l else h;
    case short:
        if (SAR[1] < h)
        then {
            state = state.long;
            acc = start;
            extreme = h;
            SAR = extreme[1];
        } else {
            state = state.short;
            if (l < extreme[1])
            then {
                acc = Min(acc[1] + inc, max);
                extreme = l;
            } else {
                acc = acc[1];
                extreme = extreme[1];
            }
            SAR = Max(Max(h, h[1]), SAR[1] + acc * (extreme - SAR[1]));
        }
    case long:
        if (SAR[1] > l)
        then {
            state = state.short;
            acc = start;
            extreme = l;
            SAR = extreme[1];
        } else {
            state = state.long;
            if (h > extreme[1])
            then {
                acc = Min(acc[1] + inc, max);
                extreme = h;
            } else {
                acc = acc[1];
                extreme = extreme[1];
            }
            SAR = Min(Min(l, l[1]), SAR[1] + acc * (extreme - SAR[1]));
        }
}
    def trend_dir = if state == state.long then 1 else if state == state.short then -1 else trend_dir[1];
    plot psar = SAR;
    plot dir  = trend_dir;
}

def atrs = ATR(Length = atrLength) * Zonewidth;

# PSAR Calc
def sar1 = sar(sarStartingvalue, sarIncrement, sarMaxValue, h1, l1, c1).psar;
def dir1 = sar(sarStartingvalue, sarIncrement, sarMaxValue, h1, l1, c1).dir;
def sar2 = sar(sarStartingvalue / f, sarIncrement / f, sarMaxValue / f, h1, l1, c1).psar;
def dir2 = sar(sarStartingvalue / f, sarIncrement / f, sarMaxValue / f, h1, l1, c1).dir;

def sarOne = sar1;
def dirOne = dir1;
def sarTwo = sar2;
def dirTwo = dir2;

def sarZone = if dirOne > 0 then sarone + atrs else
              if dirOne < 0 then sarone - atrs else na;

# ST Calc
def ST1     = supertrend(Src, SupertrendFactor, atrLength, changeATR, h1, l1, c1).ST;
def stDir1  = supertrend(Src, SupertrendFactor, atrLength, changeATR, h1, l1, c1).dir;
def ST2     = supertrend(Src, SupertrendFactor * f, atrLength * f, changeATR, h1, l1, c1).ST;
def stDir2  = supertrend(Src, SupertrendFactor * f, atrLength * f, changeATR, h1, l1, c1).dir;

def supertrend  = ST1;
def direction   = stDir1;
def supertrend2 = ST2;
def direction2  = stDir2;

def STzone = if direction < 0 then supertrend + atrs else
             if direction > 0 then supertrend - atrs else na;

def ind1;def ind2; def ZoneInd; def trend; def trend2;
Switch (selectIndicator) {
Case "Parabolic SAR Zone" :
    ind1 = sarone;
    ind2 = sartwo;
    ZoneInd = sarzone;
    trend = dirOne;
    trend2 = dirTwo;
Case "SuperTrend Zone" :
    ind1 = supertrend;
    ind2 = supertrend2;
    ZoneInd = STzone;
    trend = -direction;
    trend2 = -direction2;
}

plot FirstIndicator = if par then ind1 else na;     # "Indicator Line"
FirstIndicator.SetPaintingStrategy(PaintingStrategy.POINTS);
FirstIndicator.AssignValueColor(if trend < 0 then GlobalColor("dn") else GlobalColor("up"));

plot STUp = if trend > 0 then ind1 else na;
plot STDn = if trend > 0 then na else ind1;

STUp.SetDefaultColor(GlobalColor("up"));
STDn.SetDefaultColor(GlobalColor("dn"));


plot SecondIndicator = if trend2 == trend2[1] then ind2 else na;#, "Second SAR"
SecondIndicator.AssignValueColor(if trend2 < 0 then GlobalColor("dn") else GlobalColor("up"));

#-- Signal - Clouds - Bar Color
def dirLong = if (trend > 0 and trend2 > 0) then dirLong[1] + 1 else 0;
def dirShort = if (trend < 0 and trend2 < 0) then dirShort[1] + 1 else 0;

AddChartBubble(showSignals and dirLong == signalWaitingBar, low, "L", Color.GREEN, no);
AddChartBubble(showSignals and dirShort == signalWaitingBar, high, "S", Color.RED);

AddCloud(if trend == trend[1] then ind1 else na, ZoneInd, GlobalColor("ddn"), GlobalColor("dup"), yes);

AssignPriceColor(if !colorBars then Color.CURRENT else
                 if trend > 0 and trend2 > 0 then Color.GREEN else
                 if trend < 0 and trend2 > 0 then Color.DARK_GREEN else
                 if trend < 0 and trend2 < 0 then Color.RED else
                 if trend > 0 and trend2 < 0 then Color.DARK_RED else Color.GRAY);

#-- END of 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
374 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