Stochastic Watchlist, Scan, Alert, Arrows & Other For ThinkOrSwim

jr2146

New member
Hi all,

Does anyone have or can please create a thinkscript fast stochastic lower study with the %D 3 only showing in a histogram instead of a line. Thank you
 
Just use ToS full stochastic at K 5 D 3. No need to create a study. If you really want a histogram then use Stochasticfulldiff .
 
@horserider ok thank you... If i use the Stochasticfulldiff like you said for the histogram do you know what the setting would be to mimic the fast stoch on the %K side? Thanks
 
Just experiment and see what you like. I might try 5 7 3 but then I would not use the histogram. What is wrong with just the full stochastic?
 
@horserider ok thanks. looks good with the 7,5,3 I use the fast stoch showing only the %D 3 for divergence only on a 1 min chart and was thinking a histogram would help to see it a little better.
 
Below is the code for the study that I have. I would like to use it as two different scans/watchlists but do not know how to adjust the code myself in order to make that conversion. I am hoping that one of the experts in here could adjust the code and send it back to me in a way that would work for the scanner in ThinkorSwim.

I know this is asking a lot from a stranger, but after countless attempts I am at a loss and don't know where else to go to figure this out.

Thank you for your time and consideration.
.....................................................

From what I can tell, there are four alerts included in this study, however I'd prefer the two different scans to each work off of one of the alert conditions shown below, (other alerts are not needed for the scan, unless you feel they are important to its proper function):

Code:
alert(AlertsOn && LTF from 1 bar ago >= 20 and LTF < 20 ,”Stochastic HPS Buy Setup”,alert.BAR,sound.Bell);


alert(AlertsOn && LTF from 1 bar ago <= 80 and LTF > 80 , ”Stochastic HPS Sell setup”, alert.BAR,sound.Ding);

...............................................................................................................................................................................................................................................................
plot Data = close;declare lower;
Input AlertsOn = yes;
Input ShowTodayOnly = yes;


Def Today = if !ShowTodayOnly then 1 else if getday() == getLastDay() then 1 else 0;


def over_bought = 75;
def over_sold = 25;
def priceH = high;
def priceL = low;
def priceC = (high + low) / 2;input ShortKPeriod = 14;
input ShortPercentK = 7;
input smoothing_period = 6;
input MidKPeriod = 24;
input MidPercentK = 8;
input LongKPeriod = 35;
input LongPercentK = 10;


def Sc1 = priceC - Lowest(priceL, ShortKPeriod);
def Sc2 = Highest(priceH, ShortKPeriod) - Lowest(priceL, ShortKPeriod);
def SFastK = Sc1 / Sc2 * 100;


def Mc1 = priceC - Lowest(priceL, MidKPeriod);
def Mc2 = Highest(priceH, MidKPeriod) - Lowest(priceL, MidKPeriod);
def MFastK = Mc1 / Mc2 * 100;


def Lc1 = priceC - Lowest(priceL, LongKPeriod);
def Lc2 = Highest(priceH, LongKPeriod) - Lowest(priceL, LongKPeriod);
def LFastK = Lc1 / Lc2 * 100;


plot STF;
plot STF2;


plot MTF;
plot MTF2;


plot LTF;
plot LTF2;








STF = Average(SFastK, ShortPercentK);
STF2 = Average(SFastK, ShortPercentK);


MTF = Average(MFastK, MidPercentK);
MTF2 = Average(MFastK, MidPercentK);


LTF = Average(LFastK, LongPercentK);
LTF2 = Average(LFastK, LongPercentK);


STF.SetDefaultColor(color.Light_Gray);


STF.SetLineWeight(1);
STF2.SetLineWeight(2);


MTF.SetDefaultColor(color.Light_Gray);
MTF.SetLineWeight(1);
MTF2.SetLineWeight(2);


LTF.SetDefaultColor(color.Light_Gray);
LTF.SetLineWeight(1);
LTF2.SetLineWeight(2);


STF.AssignValueColor(if STF < 25 then color.green else if STF > 80 then color.red else color.Light_Gray);


STF2.AssignValueColor(if STF2 < 50 and STF2 > 40 then color.yellow else if STF2 <= 40 and STF2 > 25 then color.Light_GREEN else if STF2 < 25 then color.green else if STF2 > 70 and STF2 < 75 then color.pink else if STF2 > 75 then color.red else if STF2 > 50 and STF2 < 60 then color.yellow else if STF2 >= 60 and STF2 < 75 then color.pink else color.Light_Gray );




MTF.AssignValueColor(if MTF < 20 then color.green else if MTF > 80 then color.red else color.Light_Gray);


MTF2.AssignValueColor(if MTF2 < 50 and MTF2 > 40 then color.yellow else if MTF2 <= 40 and MTF2 > 20 then color.Light_GREEN else if MTF2 < 20 then color.green else if MTF2 > 70 and MTF2 < 80 then color.pink else if MTF2 > 80 then color.red else if MTF2 > 50 and MTF2 < 60 then color.yellow else if MTF2 >= 60 and MTF2 < 80 then color.pink else color.Light_Gray );


LTF.AssignValueColor(if LTF < 20 then color.green else if LTF > 80 then color.red else color.Light_Gray);


LTF2.AssignValueColor(if LTF2 < 50 and LTF2 > 40 then color.yellow else if LTF2 <= 40 and LTF2 > 20 then color.Light_GREEN else if LTF2 < 20 then color.green else if LTF2 > 70 and LTF2 < 80 then color.pink else if LTF2 > 80 then color.red else if LTF2 > 50 and LTF2 < 60 then color.yellow else if LTF2 >= 60 and LTF2 < 80 then color.pink else color.Light_Gray );


# -- ALERTS --


alert(AlertsOn && LTF <= 25 and MTF <= 25 and STF from 1 bar ago <= 25 and STF > 25,”Stochastic Breaking Above 25”,alert.BAR,sound.Bell);


alert(AlertsOn && LTF >= 75 and MTF >= 75 and STF from 1 bar ago >= 75 and STF < 75 , ”Stochastic Breaking Below 75”, alert.BAR,sound.Ding);

alert(AlertsOn && LTF from 1 bar ago >= 20 and LTF < 20 ,”Stochastic HPS Buy Setup”,alert.BAR,sound.Bell);


alert(AlertsOn && LTF from 1 bar ago <= 80 and LTF > 80 , ”Stochastic HPS Sell setup”, alert.BAR,sound.Ding);

#---------------


plot OverBought = over_bought;
OverBought.SetDefaultColor(color.Green);


plot OverSold = over_sold;
OverSold.SetDefaultColor(color.Red);


plot MidLine = 50;
MidLine.SetDefaultColor(color.white);
 
Last edited by a moderator:
@Vinny I have cleaned up your code ready to work in a scanner. Please note that I have defined 4 scan conditions.
The scanner only accepts one plot, so make sure you turn the other 3.

From your description above, seems like you are looking for either cond3 or cond4 as follows

cond3 = LTF from 1 bar ago >= 20 and LTF < 20; # Stochastic HPS Buy
cond4 = LTF from 1 bar ago <= 80 and LTF > 80; # Stochastic HPS Sell

I have just scanned using cond3 on the S&P 500 using a daily aggregation and obtained 8 results

Here then is your requested scan, place this dorectly into the scanner

Code:
# START CODE

def priceH = high;
def priceL = low;
def priceC = (high + low) / 2;

input ShortKPeriod = 14;
input ShortPercentK = 7;
input MidKPeriod = 24;
input MidPercentK = 8;
input LongKPeriod = 35;
input LongPercentK = 10;

def Sc1 = priceC - Lowest(priceL, ShortKPeriod);
def Sc2 = Highest(priceH, ShortKPeriod) - Lowest(priceL, ShortKPeriod);
def SFastK = Sc1 / Sc2 * 100;

def Mc1 = priceC - Lowest(priceL, MidKPeriod);
def Mc2 = Highest(priceH, MidKPeriod) - Lowest(priceL, MidKPeriod);
def MFastK = Mc1 / Mc2 * 100;

def Lc1 = priceC - Lowest(priceL, LongKPeriod);
def Lc2 = Highest(priceH, LongKPeriod) - Lowest(priceL, LongKPeriod);
def LFastK = Lc1 / Lc2 * 100;

def STF  = Average(SFastK, ShortPercentK);
def MTF  = Average(MFastK, MidPercentK);
def LTF  = Average(LFastK, LongPercentK);

# Four scan conditions are defined.
# Make sure you only use one and the other 3 must be commented out
# The scanner only expects a single plot statement
# Choose the scan of your choice from one of the following

#plot cond1 = LTF <= 25 and MTF <= 25 and STF from 1 bar ago <= 25 and STF > 25;  # Stochastic Breaking Above 25
#plot cond2 = LTF >= 75 and MTF >= 75 and STF from 1 bar ago >= 75 and STF < 75;  # Stochastic Breaking Below 75
plot cond3 = LTF from 1 bar ago >= 20 and LTF < 20;                              # Stochastic HPS Buy
#plot cond4 = LTF from 1 bar ago <= 80 and LTF > 80;                              # Stochastic HPS Sell

# END CODE
 
Last edited by a moderator:
@Vinny BTW please note that in your study you have 3 pairs of identical conditions. Not sure if this is a typo.
But not to worry, this does not impact your scan conditions as none of them use STF2, MTF2 or LTF2

def STF = Average(SFastK, ShortPercentK);
def STF2 = Average(SFastK, ShortPercentK);
def MTF = Average(MFastK, MidPercentK);
def MTF2 = Average(MFastK, MidPercentK);
def LTF = Average(LFastK, LongPercentK);
def LTF2 = Average(LFastK, LongPercentK);
 
To be honest, I am not sure what that even means. I know how to read charts and trade but when it comes to any type of coding, I can barely do a simple SQL search lol
That code was given to me from a friend and after a little back testing it looks like it could be helpful with scalping, which is what prompted me to try to turn it into a scan.
I was also given a similar indicator that I wanted to try to convert myself into a similar scan as you did and figured if I looked at your work I'd be able to figure it out but it is proving more difficult/time consuming that I had anticipated.
Below is the code to that new one; if you could help me with this it would be greatly appreciated. Otherwise I'll just keep taking stabs at it over the week to try to get it figured out haha.
Thanks again!
........................................

Code:
plot Data = close;declare lower;
Input AlertsOn = yes;
Input ShowTodayOnly = yes;


Def Today = if !ShowTodayOnly then 1 else if getday() == getLastDay() then 1 else 0;


def over_bought = 75;
def over_sold = 25;
def priceH = high;
def priceL = low;
def priceC = (high + low) / 2;input ShortKPeriod = 14;
input ShortPercentK = 3;
input smoothing_period = 3;
input MidKPeriod = 10;
input MidPercentK = 10;
input LongKPeriod = 9;
input LongPercentK = 3;


def Sc1 = priceC - Lowest(priceL, ShortKPeriod);
def Sc2 = Highest(priceH, ShortKPeriod) - Lowest(priceL, ShortKPeriod);
def SFastK = Sc1 / Sc2 * 100;


def Mc1 = priceC - Lowest(priceL, MidKPeriod);
def Mc2 = Highest(priceH, MidKPeriod) - Lowest(priceL, MidKPeriod);
def MFastK = Mc1 / Mc2 * 100;


def Lc1 = priceC - Lowest(priceL, LongKPeriod);
def Lc2 = Highest(priceH, LongKPeriod) - Lowest(priceL, LongKPeriod);
def LFastK = Lc1 / Lc2 * 100;


plot STF;
plot STF2;


plot MTF;
plot MTF2;


plot LTF;
plot LTF2;








STF = Average(SFastK, ShortPercentK);
STF2 = Average(SFastK, ShortPercentK);


MTF = Average(MFastK, MidPercentK);
MTF2 = Average(MFastK, MidPercentK);


LTF = Average(LFastK, LongPercentK);
LTF2 = Average(LFastK, LongPercentK);


STF.SetDefaultColor(color.Light_Gray);


STF.SetLineWeight(1);
STF2.SetLineWeight(2);


MTF.SetDefaultColor(color.Light_Gray);
MTF.SetLineWeight(1);
MTF2.SetLineWeight(2);


LTF.SetDefaultColor(color.Light_Gray);
LTF.SetLineWeight(1);
LTF2.SetLineWeight(2);


STF.AssignValueColor(if STF < 25 then color.green else if STF > 80 then color.red else color.Light_Gray);


STF2.AssignValueColor(if STF2 < 50 and STF2 > 40 then color.yellow else if STF2 <= 40 and STF2 > 25 then color.Light_GREEN else if STF2 < 25 then color.green else if STF2 > 70 and STF2 < 75 then color.pink else if STF2 > 75 then color.red else if STF2 > 50 and STF2 < 60 then color.yellow else if STF2 >= 60 and STF2 < 75 then color.pink else color.Light_Gray );




MTF.AssignValueColor(if MTF < 20 then color.green else if MTF > 80 then color.red else color.Light_Gray);


MTF2.AssignValueColor(if MTF2 < 50 and MTF2 > 40 then color.yellow else if MTF2 <= 40 and MTF2 > 20 then color.Light_GREEN else if MTF2 < 20 then color.green else if MTF2 > 70 and MTF2 < 80 then color.pink else if MTF2 > 80 then color.red else if MTF2 > 50 and MTF2 < 60 then color.yellow else if MTF2 >= 60 and MTF2 < 80 then color.pink else color.Light_Gray );


LTF.AssignValueColor(if LTF < 20 then color.green else if LTF > 80 then color.red else color.Light_Gray);


LTF2.AssignValueColor(if LTF2 < 50 and LTF2 > 40 then color.yellow else if LTF2 <= 40 and LTF2 > 20 then color.Light_GREEN else if LTF2 < 20 then color.green else if LTF2 > 70 and LTF2 < 80 then color.pink else if LTF2 > 80 then color.red else if LTF2 > 50 and LTF2 < 60 then color.yellow else if LTF2 >= 60 and LTF2 < 80 then color.pink else color.Light_Gray );


# -- ALERTS --


alert(AlertsOn && LTF <= 25 and MTF <= 25 and STF from 1 bar ago <= 25 and STF > 25,”Stochastic Breaking Above 25”,alert.BAR,sound.Bell);


alert(AlertsOn && LTF >= 75 and MTF >= 75 and STF from 1 bar ago >= 75 and STF < 75 , ”Stochastic Breaking Below 75”, alert.BAR,sound.Ding);

alert(AlertsOn && LTF from 1 bar ago >= 20 and LTF < 20 ,”Stochastic HPS Buy Setup”,alert.BAR,sound.Bell);


alert(AlertsOn && LTF from 1 bar ago <= 80 and LTF > 80 , ”Stochastic HPS Sell setup”, alert.BAR,sound.Ding);

#---------------


plot OverBought = over_bought;
OverBought.SetDefaultColor(color.Green);


plot OverSold = over_sold;
OverSold.SetDefaultColor(color.Red);


plot MidLine = 50;
MidLine.SetDefaultColor(color.white);
 
To be honest, I am not sure what that even means. I know how to read charts and trade but when it comes to any type of coding, I can barely do a simple SQL search lol
That code was given to me from a friend and after a little back testing it looks like it could be helpful with scalping, which is what prompted me to try to turn it into a scan.
I was also given a similar indicator that I wanted to try to convert myself into a similar scan as you did and figured if I looked at your work I'd be able to figure it out but it is proving more difficult/time consuming that I had anticipated.
Below is the code to that new one; if you could help me with this it would be greatly appreciated. Otherwise I'll just keep taking stabs at it over the week to try to get it figured out haha.
Thanks again!
........................................

@Vinny In post #5 I mentioned that you had the pairs of identical conditions and wondered if there was a problem.
Since you mentioned you're not a coder, I'll take it nice and slow and explain a bit more shall we?

Let's consider just one of these pairs

def STF = Average(SFastK, ShortPercentK);
def STF2 = Average(SFastK, ShortPercentK);

These 2 variables STF and STF2 calculate exactly the same thing, namely the simple moving average of "SFasrK" over "ShortPercentK"periods
The computation of both statements are going to be exactly the same, so that was why I wondered if there was a typo.
Now you have 3 of these pairs of statements as I mentioned in my post #5 above.

I did compare the code you posted above with your earlier posted code. In post #1, you had the following definitions

input ShortPercentK = 7;
input smoothing_period = 6;
input MidKPeriod = 24;
input MidPercentK = 8;
input LongKPeriod = 35;
input LongPercentK = 10;

And in your post#6 you have the following definitions

input ShortPercentK = 3;
input smoothing_period = 3;
input MidKPeriod = 10;
input MidPercentK = 10;
input LongKPeriod = 9;
input LongPercentK = 3;

Notice the different numbers? The second set of numbers look like the range of numbers I'd usually find in a stochastic study
But not to worry, as the scan engine I posted above for you will indeed scan for the results you want.
The only difference is whether those numbers/periods as defined above are what you want.

If so, feel free to change those numbers in the scan code. I'll be happy to do this for you if you're not sure.

Hope this helps
 
Hello,

I'm trying to create a lower study (StochasticFull) with just the FullK input (no FullD) but at multiple periods. So instead of just one FullK stochastic showing at say 14 period, I want to show multiple FullK's, for example 7, 14 and 21, but all on the same study. I am having a heck of a time though. Any suggestions?
 
Would need to input your KPeriod lengths. Kperiod, KPeriod2, KPeriod3
Replicate the KPeriod calculations for each KPeriod length.
Plot each calculation.

Do not peek. Code is below if you cannot figure it out.

Code:
# Three different lengths of StochasticCrossover FullK
# Horserider 1/12/2020 requested by wsjiii

declare lower;

input over_bought = 80;
input over_sold = 20;
input KPeriod = 7;
input KPeriod2 = 14;
input KPeriod3 = 21;
input priceH = high;
input priceL = low;
input priceC = close;
input slowing_period = 3;
input averageType = AverageType.SIMPLE;

# First K period
def lowest_k = Lowest(priceL, KPeriod);
def c1 = priceC - lowest_k;
def c2 = Highest(priceH, KPeriod) - lowest_k;
def FastK = if c2 != 0 then c1 / c2 * 100 else 0;

# Second K period
def lowest_k2 = Lowest(priceL, KPeriod2);
def c12 = priceC - lowest_k2;
def c22 = Highest(priceH, KPeriod2) - lowest_k2;
def FastK2 = if c22 != 0 then c12 / c22 * 100 else 0;

# Third K period
def lowest_k3 = Lowest(priceL, KPeriod3);
def c13 = priceC - lowest_k3;
def c23 = Highest(priceH, KPeriod3) - lowest_k3;
def FastK3 = if c23 != 0 then c13 / c23 * 100 else 0;

# Plots
plot FullK = MovingAverage(averageType, FastK, slowing_period);
plot FullK2 = MovingAverage(averageType, FastK2, slowing_period);
plot FullK3 = MovingAverage(averageType, FastK3, slowing_period);

plot OverBought = over_bought;
plot OverSold = over_sold;

FullK.SetDefaultColor(GetColor(2));
FullK2.SetDefaultColor(GetColor(3));
FullK3.SetDefaultColor(GetColor(4));
OverBought.SetDefaultColor(GetColor(1));
OverSold.SetDefaultColor(GetColor(1));
 
Last edited by a moderator:
Hi,
I'm looking for Stochastics Oscillator. Can someone help on this technical indicator in TOS?

Thank you.
 
Last edited by a moderator:
What kind of stochastic oscillator are you looking for? There are a few built-in ones in ThinkorSwim already.

zVFPh1a.png
 
Hi, does anyone know if it is possible to set up a watchlist that alerts when Stochastic crosses at either 20 or 80 on one or multiple timeframes.
I have a custom watchlist for FX pairs.
Thank you
 
Create a scanner with those condition first > save the scanner > Alert when scan results change...

8UqiNgd.png
 

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