• The price of VIP membership is increasing to $199 beginning June 1. Click here to lock in the current rate of $149.

Convert this Stochastic study into a scanner for ThinkorSwim

Vinny

New member
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:

tomsk

Well-known member
VIP
@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

Code:
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


# 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
 

tomsk

Well-known member
VIP
@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);
 

Vinny

New member
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);
 

tomsk

Well-known member
VIP
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
 

Whitejustin522

New member
@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


Is there any way I could ask for your help with a similar issue?

I am trying to take my RVOL (relative volume) indicator thinkscript and adapt it to be a custom study in the TOS scanner. I cannot figure out how to do this and I have no clue how to code. I was gifted the RVOL script from a friend.

My goal is to scan only for stocks trading with an RVOL > 2.0
From that list I can go in and see what has the best catalyst to trade.

This would be life changing for me, I have a decent system but finding my set ups is very time consuming and I would like to start automating a bit more. I am learning how to use thinkscript but i've never excelled in coding,

THANK YOU THANK YOU THANK YOU.


RVOL THINKSCRIPT:

Code:
input length = 21; #number of days
input offset = 1; #offset day: 1 = dont include today

def ADV = Average(volume, length)[offset]; #ADV
def rvol = volume /ADV; #Relative Volume


# remove '#' infront of ADDlabels to select prefer choice

AddLabel(yes, round(rVol,2)); #show in ratios
#AddLabel(yes, asPercent(rVol)); #show in percentage
 

prosperity001

New member
I use a 14 period K line from the StochasticSlow study with the DoubleSmoothedStochastic (3,5,14). I would like a custom scan to identify when a stock has been above the 80 level (over_bought) for the last three bars. I tried to do this and it works as a study but I am having trouble figuring out how to produce a scan. I am including the incomplete code for the study for reference. Any help would be appreciated.

STUDY (Note, I only did the K line to see if it would work)

Code:
input over_bought = 80;
input over_sold = 20;
input KPeriod = 10;
input DPeriod = 10;
input priceH = high;
input priceL = low;
input priceC = close;
input averageType = AverageType.SIMPLE;
input showBreakoutSignals = {default "No", "On SlowK", "On SlowD", "On SlowK & SlowD"};

def SlowK = reference StochasticFull(over_bought,over_sold,KPeriod,DPeriod,priceH,priceL,priceC,3,averageType).FullK;
def SlowD = reference StochasticFull(over_bought,over_sold,KPeriod,DPeriod,priceH,priceL,priceC,3,averageType).FullD;

plot data = (Slowk > over_bought) and (Slowk[1] > over_bought) and (Slowk[2] > over_bought) and (Slowk[3] > over_bought);
 

rad14733

Well-known member
VIP
@prosperity001 Why don't you just reference your Study from the scanner and select the proper plots and condition criteria...??? Don't make it harder than it needs to be... You can reference any Study plots from the scanner...
 

Similar threads

Top