SQN Indicator For ThinkOrSwim

https://www.tradingview.com/script/ZhHf4DFD-SQN-ChrisD-Fallible/

Wondering if @samer800 or another could convert the following to thinkscript. Tradingview link above

//@version=4
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ChrisD_Macro https://www.pollinatetrading.com
// FX variation done by John D
study(title="SQN-ChrisD-Fallible", shorttitle="SQN-ChrisD-Fallible", overlay = false)
Period = input(100, title="Period")
close_difference = close/close[1]-1
Stdev = stdev(close_difference,Period)
_sma = sma(close_difference,Period)
SQN = ((_sma*sqrt(Period))/Stdev)
c = (SQN<-1.7)?#800000:
(SQN>=-1.7 and SQN<-0.6)?#FF0000:
(SQN>=-0.6 and SQN<=0.6)?#FFD700:
(SQN>0.6 and SQN<=1.7)?#008000:
(SQN>1.7)?#0000FF: color.gray
plot(SQN, title="SQN", color=c, transp=10, histbase=0, style=plot.style_area, linewidth=4)
//Bull volatile blue
//Bull Quiet Green
//Neutral yellow
//Bear Quiet red
//Bear volatile crimson
check out 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/
#// © ChrisD_Macro https://www.pollinatetrading.com
#// FX variation done by John D
#study(title="SQN-ChrisD-Fallible", shorttitle="SQN-ChrisD-Fallible", overlay = false)
# Converted by Sam4COK@Samer800 - 12/2022
declare lower;
input BarColor = yes;
input Period = 100;
input MovAvgType = AverageType.SIMPLE;

def close_difference = close/close[1]-1;
def Stdev = stdev(close_difference,Period);
def _sma = MovingAverage(MovAvgType,close_difference,Period);
def SQN = ((_sma*sqrt(Period))/Stdev);
def c = if (SQN<-1.7) then -2 else 
        if (SQN>=-1.7 and SQN<-0.6) then -1 else 
        if (SQN>=-0.6 and SQN<=0.6) then 0 else 
        if (SQN>0.6 and SQN<=1.7) then 1 else
        if (SQN>1.7) then 2 else 3;
plot SQNplot = SQN;
SQNplot.SetLineWeight(3);
SQNplot.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
SQNplot.AssignValueColor(if c==-2 then Color.RED else
                         if c==-1 then Color.DARK_RED else
                         if c==0 then Color.YELLOW else
                         if c==1 then Color.DARK_GREEN else
                         if c==2 then Color.GREEN else Color.GRAY);

plot SQNline = SQN;
SQNline.SetLineWeight(2);
SQNline.AssignValueColor(if c==-2 then Color.RED else
                         if c==-1 then Color.DARK_RED else
                         if c==0 then Color.YELLOW else
                         if c==1 then Color.DARK_GREEN else
                         if c==2 then Color.GREEN else Color.GRAY);

AssignPriceColor(if !BarColor then Color.CURRENT else
                 if c==-2 then Color.RED else
                 if c==-1 then Color.DARK_RED else
                 if c==0 then Color.YELLOW else
                 if c==1 then Color.DARK_GREEN else
                 if c==2 then Color.GREEN else Color.GRAY);


#--- END CODE
 
check out 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/
#// © ChrisD_Macro https://www.pollinatetrading.com
#// FX variation done by John D
#study(title="SQN-ChrisD-Fallible", shorttitle="SQN-ChrisD-Fallible", overlay = false)
# Converted by Sam4COK@Samer800 - 12/2022
declare lower;
input BarColor = yes;
input Period = 100;
input MovAvgType = AverageType.SIMPLE;

def close_difference = close/close[1]-1;
def Stdev = stdev(close_difference,Period);
def _sma = MovingAverage(MovAvgType,close_difference,Period);
def SQN = ((_sma*sqrt(Period))/Stdev);
def c = if (SQN<-1.7) then -2 else
        if (SQN>=-1.7 and SQN<-0.6) then -1 else
        if (SQN>=-0.6 and SQN<=0.6) then 0 else
        if (SQN>0.6 and SQN<=1.7) then 1 else
        if (SQN>1.7) then 2 else 3;
plot SQNplot = SQN;
SQNplot.SetLineWeight(3);
SQNplot.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
SQNplot.AssignValueColor(if c==-2 then Color.RED else
                         if c==-1 then Color.DARK_RED else
                         if c==0 then Color.YELLOW else
                         if c==1 then Color.DARK_GREEN else
                         if c==2 then Color.GREEN else Color.GRAY);

plot SQNline = SQN;
SQNline.SetLineWeight(2);
SQNline.AssignValueColor(if c==-2 then Color.RED else
                         if c==-1 then Color.DARK_RED else
                         if c==0 then Color.YELLOW else
                         if c==1 then Color.DARK_GREEN else
                         if c==2 then Color.GREEN else Color.GRAY);

AssignPriceColor(if !BarColor then Color.CURRENT else
                 if c==-2 then Color.RED else
                 if c==-1 then Color.DARK_RED else
                 if c==0 then Color.YELLOW else
                 if c==1 then Color.DARK_GREEN else
                 if c==2 then Color.GREEN else Color.GRAY);


#--- END CODE
@samer800 thank you so much!
 
Would it be possible to have labels?
Bull Volatile - Dark Green
Bull Quiet - Green
Sideways - Gray
Bear Quiet - Red
Bear Volatile - Dark Red

Dark Green = Bull Volatile >1.7 and higher
Green = Bull Quiet >.6 to 1.7
Yellow = Neutral between .60 to -.60
Red = Bear Quiet -.60 to -1.70
Dark Red = Bear Volatile < -1.70 and lower

@MerryDay Can you advise on this?
 
Last edited:
Would it be possible to have labels?
Bull Volatile - Dark Green
Bull Quiet - Green
Sideways - Gray
Bear Quiet - Red
Bear Volatile - Dark Red

Dark Green = Bull Volatile >1.7 and higher
Green = Bull Quiet >.6 to 1.7
Yellow = Neutral between .60 to -.60
Red = Bear Quiet -.60 to -1.70
Dark Red = Bear Volatile < -1.70 and lower

@MerryDay Can you advise on this?
Here is another code for SQN that works. If you are still interested in using it. After you load this indicator you can go into settings and adjust the colors you want.

input Period = 100;
input Price = close;

plot ZeroLine = 0;
ZeroLine.SetDefaultColor(Color.WHITE);
ZeroLine.SetLineWeight(1);

Def PChange = Price/Price[1]-1;
Def Stdev = stdev(PChange,Period);
Def PChangeSMA = Average(PChange, Period);
Plot SQN = ((PChangeSMA*sqrt(Period))/Stdev);

SQN.SetPaintingStrategy(PaintingStrategy.line);
SQN.DefineColor("BULL VOLATILE", Color.GREEN);
SQN.DefineColor("BULL QUIET", Color.Dark_GREEN);
SQN.DefineColor("NEUTRAL", Color.YELLOW);
SQN.DefineColor("BEAR QUIET", Color.Dark_RED);
SQN.DefineColor("BEAR VOLATILE", Color.RED);

SQN.AssignValueColor(
if SQN > 1.7 then SQN.Color("BULL VOLATILE")
else if (SQN <= 1.7 and SQN > 0.6) then SQN.Color("BULL QUIET")
else if (SQN < -.6 and SQN >=-1.7) then SQN.Color("BEAR QUIET")
else if SQN < -1.7
then SQN.Color("BEAR VOLATILE")
else SQN.Color("NEUTRAL"));
 
https://tos.mx/HKDFj7S
Ruby:
declare lower;

input Symbol = {default "SPY","NYA","DJIA","COMP","RUT"};

plot VeryBullish = 1.47;
VeryBullish.setDefaultColor(color.Green);
VeryBullish.setLineWeight(2);
plot Bullish = 0.75;
Bullish.setDefaultColor(color.Dark_Green);
Bullish.setLineWeight(1);
plot Neutral= 0.00;
Neutral.setStyle(Curve.SHORT_DASH);
Neutral.setDefaultColor(color.Yellow);
Neutral.setLineWeight(1);
plot Bearish =- 0.7;
Bearish.setDefaultColor(color.Dark_Red);
Bearish.setLineWeight(1);
plot VeryBearish = -1.47;
VeryBearish.setDefaultColor(color.Red);
VeryBearish.setLineWeight(2);

AddCloud(2,1.47,color.green);
AddCloud(1.47,.74,color.dark_green);
AddCloud(0.75,-0.7,color.yellow);
AddCloud(-.7,-1.47,color.dark_red);
AddCloud(-2,-1.47,color.red);

def DailyPercencChange =( close(Symbol)[100] /  close(Symbol));
plot DPC = DailyPercencChange;
AddLabel(yes,"Market SQN for  "+(Symbol)+"  "+ DPC ,color.cyan);
AddLabel(yes,if DPC >= 1.47 then "Very Bullish" else if DPC < 1.47 and DPC >= 0.75 then "Bullish" else if DPC < 0.74 and DPC >=-0.7 then "Neutral" else if DPC < -0.7 and DPC >= -1.47 then "Bearish" else "Very Bearish",if DPC >= 1.47 then color.green else if DPC < 1.47 and DPC >= 0.75 then color.dark_green else if DPC < 0.74 and DPC >=-0.7 then color.yellow else if DPC < -0.7 and DPC >= -1.47 then color.dark_red else color.red);
 
Here is another code for SQN that works. If you are still interested in using it. After you load this indicator you can go into settings and adjust the colors you want.

input Period = 100;
input Price = close;

plot ZeroLine = 0;
ZeroLine.SetDefaultColor(Color.WHITE);
ZeroLine.SetLineWeight(1);

Def PChange = Price/Price[1]-1;
Def Stdev = stdev(PChange,Period);
Def PChangeSMA = Average(PChange, Period);
Plot SQN = ((PChangeSMA*sqrt(Period))/Stdev);

SQN.SetPaintingStrategy(PaintingStrategy.line);
SQN.DefineColor("BULL VOLATILE", Color.GREEN);
SQN.DefineColor("BULL QUIET", Color.Dark_GREEN);
SQN.DefineColor("NEUTRAL", Color.YELLOW);
SQN.DefineColor("BEAR QUIET", Color.Dark_RED);
SQN.DefineColor("BEAR VOLATILE", Color.RED);

SQN.AssignValueColor(
if SQN > 1.7 then SQN.Color("BULL VOLATILE")
else if (SQN <= 1.7 and SQN > 0.6) then SQN.Color("BULL QUIET")
else if (SQN < -.6 and SQN >=-1.7) then SQN.Color("BEAR QUIET")
else if SQN < -1.7
then SQN.Color("BEAR VOLATILE")
else SQN.Color("NEUTRAL"));
Wondering how volatility is measured in this formula, as Van Tharp's SQN analysis has it as a separate 20-day ATR as a percent change? Do you use the % change from the SMA? Thanks! Your coding expertise is very much appreciated.

https://tos.mx/HKDFj7S
Ruby:
declare lower;

input Symbol = {default "SPY","NYA","DJIA","COMP","RUT"};

plot VeryBullish = 1.47;
VeryBullish.setDefaultColor(color.Green);
VeryBullish.setLineWeight(2);
plot Bullish = 0.75;
Bullish.setDefaultColor(color.Dark_Green);
Bullish.setLineWeight(1);
plot Neutral= 0.00;
Neutral.setStyle(Curve.SHORT_DASH);
Neutral.setDefaultColor(color.Yellow);
Neutral.setLineWeight(1);
plot Bearish =- 0.7;
Bearish.setDefaultColor(color.Dark_Red);
Bearish.setLineWeight(1);
plot VeryBearish = -1.47;
VeryBearish.setDefaultColor(color.Red);
VeryBearish.setLineWeight(2);

AddCloud(2,1.47,color.green);
AddCloud(1.47,.74,color.dark_green);
AddCloud(0.75,-0.7,color.yellow);
AddCloud(-.7,-1.47,color.dark_red);
AddCloud(-2,-1.47,color.red);

def DailyPercencChange =( close(Symbol)[100] /  close(Symbol));
plot DPC = DailyPercencChange;
AddLabel(yes,"Market SQN for  "+(Symbol)+"  "+ DPC ,color.cyan);
AddLabel(yes,if DPC >= 1.47 then "Very Bullish" else if DPC < 1.47 and DPC >= 0.75 then "Bullish" else if DPC < 0.74 and DPC >=-0.7 then "Neutral" else if DPC < -0.7 and DPC >= -1.47 then "Bearish" else "Very Bearish",if DPC >= 1.47 then color.green else if DPC < 1.47 and DPC >= 0.75 then color.dark_green else if DPC < 0.74 and DPC >=-0.7 then color.yellow else if DPC < -0.7 and DPC >= -1.47 then color.dark_red else color.red);
I believe the above script does not provide a %. Instead, try something like this...

def DailyPercentChange = ((close - close[100]) / close[100])*100
plot DPC = DailyPercentChange;

...still needs a little more fine-tuning to achieve achieve the results similar to Van Tharp's.
 
Wondering how volatility is measured in this formula, as Van Tharp's SQN analysis has it as a separate 20-day ATR as a percent change? Do you use the % change from the SMA? Thanks! Your coding expertise is very much appreciated.


I believe the above script does not provide a %. Instead, try something like this...

def DailyPercentChange = ((close - close[100]) / close[100])*100
plot DPC = DailyPercentChange;

...still needs a little more fine-tuning to achieve achieve the results similar to Van Tharp's.
Thank you! I'll try that change to the script.
Much appreciated.
Z
 

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