Help with coding please

TY_Joe

New member
I'm trying to code for watchlist column. Stochasticslow from TOS.
1. K crosses D and crosses above "buy" dark green color
2. as K and D upward "rising" light green
3. as K crosses D and crosses below " sell" Red color
4. as K and D moving down "failing" light red.

I put some codes down, but not sure this is right coding.

Thanks


#StochasticSlow Watch Column
#
#testing




input over_bought = 80;
input over_sold = 20;
input KPeriod = 5;
input DPeriod = 3;
input priceH = high;
input priceL = low;
input priceC = close;
input averageType = AverageType.SIMPLE;

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;
def OverBought = over_bought;
def OverSold = over_sold;

def upK = SlowK crosses above OverSold;
def upD = SlowD crosses above OverSold;
def downK = SlowK crosses below OverBought;
def downD = SlowD crosses below OverBought;


AddLabel(yes,
if slowK crosses slowD and slowK > slowD[1] then "Buy" else
if slowK crosses slowD and slowK < slowD[1] then "Sell" else
if slowk > slowd and slowk < slowk[1] then "Rising" else
"falling" );


AssignBackgroundColor(
if slowK crosses slowD and slowk > slowD[1] then color.dark_green else
if slowK crosses slowD and slowK < slowD[1] then color.DARK_RED else
if slowk > slowd and slowk < slowk[1] then Color.LIGHT_GREEN else
Color.LIGHT_RED);
 
Last edited:
@TY_Joe Welcome to the usethnkscript forums... Could you just describe the logic you desire without referencing colors please... We code based on conditional logic, not colors... Did you write this code from scratch or is it a modified version and, if so, from where...??? Your request can probably be almost entirely coded right in the Stock Hacker Scan panel...
 
Slow Stochastic CrossOver Watch List, Study & Labels
@TY_Joe Good News--Bad News
Good News: I think that the code below gives you enough to work with that you will be able to start tweaking on your own.
Bad News: I could not get it to work on all aggregations. I tested on 1hr charts and had up to 3candle lag, yet it worked perfectly on Daily chart.
I am handing it off to you to investigate across your desired time periods and let us know how it works for you.
Watch List
r6gVzjF.png

Ruby:
# Slow Stochastic Watchlist
# @TY_Joe 6/21
declare lower;
input over_bought = 80;
input over_sold = 20;
input KPeriod = 5;
input DPeriod = 3;
# ########################################################
def SlowK = reference StochasticSlow("k period" = KPeriod, "d period" = DPeriod)."SlowK" ;
def SlowD = reference StochasticSlow("k period" = KPeriod, "d period" = DPeriod)."SlowD" ;
# ########################################################
AddLabel(yes,
         if SlowK> over_bought and SlowK> SlowK[1] then "maxxing" else
         if SlowK> over_bought and SlowK <= SlowD then "reversing" else 
         if SlowK crosses below over_bought and SlowK <= SlowD then "Sell" else
         if SlowK crosses above over_sold and SlowK> SlowK[1] and
            SlowK >= SlowD and SlowD[1] < over_sold then "buy" else
         if SlowK< over_sold and SlowK> SlowK[1] then "pre-trend" else   
         if SlowK> SlowK[1] then "rising " +round(SlowK,1) else
         if SlowK< SlowK[1] then "failing " +round(SlowK,1)  else "Neutral");
AssignBackgroundColor(       
         if SlowK> over_bought and SlowK> SlowK[1] then color.dark_green else
         if SlowK> over_bought and SlowK <= SlowD then  color.dark_orange else
         if SlowK crosses below over_bought and SlowK <= SlowD then color.dark_red else
         if SlowK crosses above over_sold and SlowK> SlowK[1] and
            SlowK >= SlowD and SlowD[1] < over_sold then color.cyan else
         if SlowK< over_sold and SlowK> SlowK[1] then color.violet else   
         if SlowK> SlowK[1] then color.green else
         if SlowK< SlowK[1] then color.red else color.gray ) ;
# ########################################################
Study & Labels
mdHHUeW.png

Ruby:
# Slow Stochastic Study & Labels
# @TY_Joe 6/21
declare lower;
input show_label = yes ;
input over_bought = 80;
input over_sold = 20;
input KPeriod = 5;
input DPeriod = 3;
# ########################################################
#Using GlobalColors makes the colors modifiable
DefineGlobalColor("maxxing", color.dark_green) ;
DefineGlobalColor("reverse", color.dark_orange) ;
DefineGlobalColor("sell", color.dark_red) ;
DefineGlobalColor("rising",  color.green) ;
DefineGlobalColor("falling",  color.red) ;
DefineGlobalColor("buy", color.cyan) ;
DefineGlobalColor("ob_os", color.plum) ;
# ########################################################
plot SlowK = reference StochasticSlow("k period" = KPeriod, "d period" = DPeriod)."SlowK" ;
plot SlowD = reference StochasticSlow("k period" = KPeriod, "d period" = DPeriod)."SlowD" ;
SlowK.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
SlowK.AssignValueColor(
         if SlowK> over_bought and SlowK> SlowK[1] then GlobalColor("maxxing")else
         if SlowK> over_bought and SlowK <= SlowD then GlobalColor("reverse") else
         if SlowK crosses below over_bought and SlowK <= SlowD then GlobalColor("sell") else
         if SlowK crosses above over_sold and SlowK> SlowK[1] and
            SlowK >= SlowD and SlowD[1] < over_sold then GlobalColor("buy") else
         if SlowK< over_sold and SlowK> SlowK[1] then GlobalColor("ob_os") else   
         if SlowK> SlowK[1] then GlobalColor("rising") else
         if SlowK< SlowK[1] then GlobalColor("falling")  else color.gray ) ;
plot ob = over_bought ;
plot os = over_sold ;

def BuyCriteria =
  SlowK crosses above over_sold and SlowK> SlowK[1] and
            SlowK >= SlowD and SlowD[1] < over_sold ;

def SellCriteria =
  SlowK crosses below over_bought and SlowK <= SlowD ;

# ########################################################
Plot BuyTrigger = if BuyCriteria then os else double.NaN ;
BuyTrigger.SetPaintingStrategy(PaintingStrategy.ARROW_up);
BuyTrigger.SetLineWeight(1);
BuyTrigger.SetDefaultColor(color.dark_green) ;

Plot SellTrigger = if SellCriteria then ob else double.NaN ;
SellTrigger.SetPaintingStrategy(PaintingStrategy.ARROW_down);
SellTrigger.SetLineWeight(1);
SellTrigger.SetDefaultColor(color.red) ;

# ########################################################
AddLabel(show_label,
         if SlowK> over_bought and SlowK> SlowK[1] then "SlowK maxxing" else
         if SlowK> over_bought and SlowK <= SlowD then "SlowK reversing" else 
         if SlowK crosses below over_bought and SlowK <= SlowD then "SlowK Sell" else
         if SlowK crosses above over_sold and SlowK> SlowK[1] and
            SlowK >= SlowD and SlowD[1] < over_sold then "SlowK Buy" else
         if SlowK< over_sold and SlowK> SlowK[1] then "SlowK pre-trend" else   
         if SlowK> SlowK[1] then "SlowK rising " +round(SlowK,1) else
         if SlowK< SlowK[1] then "SlowK failing " +round(SlowK,1)  else "SlowK Neutral" ,
        
         if SlowK> over_bought and SlowK> SlowK[1] then GlobalColor("maxxing")else
         if SlowK> over_bought and SlowK <= SlowD then GlobalColor("reverse") else
         if SlowK crosses below over_bought and SlowK <= SlowD then GlobalColor("sell") else
         if SlowK crosses above over_sold and SlowK> SlowK[1] and
            SlowK >= SlowD and SlowD[1] < over_sold then GlobalColor("buy") else 
         if SlowK< over_sold and SlowK> SlowK[1] then GlobalColor("ob_os") else   
         if SlowK> SlowK[1] then GlobalColor("rising") else
         if SlowK< SlowK[1] then GlobalColor("falling")  else color.gray ) ;
      
AddCloud(if SlowK >= over_bought then SlowK else Double.NaN, over_bought,
GlobalColor("reverse"), GlobalColor("reverse"));
AddCloud(if SlowK <= over_sold then over_sold else Double.NaN, SlowK,
GlobalColor("buy"), GlobalColor("buy"));
# ########################################################
 
Slow Stochastic CrossOver Watch List, Study & Labels
@TY_Joe Good News--Bad News
Good News: I think that the code below gives you enough to work with that you will be able to start tweaking on your own.
Bad News: I could not get it to work on all aggregations. I tested on 1hr charts and had up to 3candle lag, yet it worked perfectly on Daily chart.
I am handing it off to you to investigate across your desired time periods and let us know how it works for you.
Watch List
r6gVzjF.png

Ruby:
# Slow Stochastic Watchlist
# @TY_Joe 6/21
declare lower;
input over_bought = 80;
input over_sold = 20;
input KPeriod = 5;
input DPeriod = 3;
# ########################################################
def SlowK = reference StochasticSlow("k period" = KPeriod, "d period" = DPeriod)."SlowK" ;
def SlowD = reference StochasticSlow("k period" = KPeriod, "d period" = DPeriod)."SlowD" ;
# ########################################################
AddLabel(yes,
         if SlowK> over_bought and SlowK> SlowK[1] then "maxxing" else
         if SlowK> over_bought and SlowK <= SlowD then "reversing" else
         if SlowK crosses below over_bought and SlowK <= SlowD then "Sell" else
         if SlowK crosses above over_sold and SlowK> SlowK[1] and
            SlowK >= SlowD and SlowD[1] < over_sold then "buy" else
         if SlowK< over_sold and SlowK> SlowK[1] then "pre-trend" else  
         if SlowK> SlowK[1] then "rising " +round(SlowK,1) else
         if SlowK< SlowK[1] then "failing " +round(SlowK,1)  else "Neutral");
AssignBackgroundColor(      
         if SlowK> over_bought and SlowK> SlowK[1] then color.dark_green else
         if SlowK> over_bought and SlowK <= SlowD then  color.dark_orange else
         if SlowK crosses below over_bought and SlowK <= SlowD then color.dark_red else
         if SlowK crosses above over_sold and SlowK> SlowK[1] and
            SlowK >= SlowD and SlowD[1] < over_sold then color.cyan else
         if SlowK< over_sold and SlowK> SlowK[1] then color.violet else  
         if SlowK> SlowK[1] then color.green else
         if SlowK< SlowK[1] then color.red else color.gray ) ;
# ########################################################
Study & Labels
mdHHUeW.png

Ruby:
# Slow Stochastic Study & Labels
# @TY_Joe 6/21
declare lower;
input show_label = yes ;
input over_bought = 80;
input over_sold = 20;
input KPeriod = 5;
input DPeriod = 3;
# ########################################################
#Using GlobalColors makes the colors modifiable
DefineGlobalColor("maxxing", color.dark_green) ;
DefineGlobalColor("reverse", color.dark_orange) ;
DefineGlobalColor("sell", color.dark_red) ;
DefineGlobalColor("rising",  color.green) ;
DefineGlobalColor("falling",  color.red) ;
DefineGlobalColor("buy", color.cyan) ;
DefineGlobalColor("ob_os", color.plum) ;
# ########################################################
plot SlowK = reference StochasticSlow("k period" = KPeriod, "d period" = DPeriod)."SlowK" ;
plot SlowD = reference StochasticSlow("k period" = KPeriod, "d period" = DPeriod)."SlowD" ;
SlowK.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
SlowK.AssignValueColor(
         if SlowK> over_bought and SlowK> SlowK[1] then GlobalColor("maxxing")else
         if SlowK> over_bought and SlowK <= SlowD then GlobalColor("reverse") else
         if SlowK crosses below over_bought and SlowK <= SlowD then GlobalColor("sell") else
         if SlowK crosses above over_sold and SlowK> SlowK[1] and
            SlowK >= SlowD and SlowD[1] < over_sold then GlobalColor("buy") else
         if SlowK< over_sold and SlowK> SlowK[1] then GlobalColor("ob_os") else  
         if SlowK> SlowK[1] then GlobalColor("rising") else
         if SlowK< SlowK[1] then GlobalColor("falling")  else color.gray ) ;
plot ob = over_bought ;
plot os = over_sold ;

def BuyCriteria =
  SlowK crosses above over_sold and SlowK> SlowK[1] and
            SlowK >= SlowD and SlowD[1] < over_sold ;

def SellCriteria =
  SlowK crosses below over_bought and SlowK <= SlowD ;

# ########################################################
Plot BuyTrigger = if BuyCriteria then os else double.NaN ;
BuyTrigger.SetPaintingStrategy(PaintingStrategy.ARROW_up);
BuyTrigger.SetLineWeight(1);
BuyTrigger.SetDefaultColor(color.dark_green) ;

Plot SellTrigger = if SellCriteria then ob else double.NaN ;
SellTrigger.SetPaintingStrategy(PaintingStrategy.ARROW_down);
SellTrigger.SetLineWeight(1);
SellTrigger.SetDefaultColor(color.red) ;

# ########################################################
AddLabel(show_label,
         if SlowK> over_bought and SlowK> SlowK[1] then "SlowK maxxing" else
         if SlowK> over_bought and SlowK <= SlowD then "SlowK reversing" else
         if SlowK crosses below over_bought and SlowK <= SlowD then "SlowK Sell" else
         if SlowK crosses above over_sold and SlowK> SlowK[1] and
            SlowK >= SlowD and SlowD[1] < over_sold then "SlowK Buy" else
         if SlowK< over_sold and SlowK> SlowK[1] then "SlowK pre-trend" else  
         if SlowK> SlowK[1] then "SlowK rising " +round(SlowK,1) else
         if SlowK< SlowK[1] then "SlowK failing " +round(SlowK,1)  else "SlowK Neutral" ,
       
         if SlowK> over_bought and SlowK> SlowK[1] then GlobalColor("maxxing")else
         if SlowK> over_bought and SlowK <= SlowD then GlobalColor("reverse") else
         if SlowK crosses below over_bought and SlowK <= SlowD then GlobalColor("sell") else
         if SlowK crosses above over_sold and SlowK> SlowK[1] and
            SlowK >= SlowD and SlowD[1] < over_sold then GlobalColor("buy") else
         if SlowK< over_sold and SlowK> SlowK[1] then GlobalColor("ob_os") else  
         if SlowK> SlowK[1] then GlobalColor("rising") else
         if SlowK< SlowK[1] then GlobalColor("falling")  else color.gray ) ;
     
AddCloud(if SlowK >= over_bought then SlowK else Double.NaN, over_bought,
GlobalColor("reverse"), GlobalColor("reverse"));
AddCloud(if SlowK <= over_sold then over_sold else Double.NaN, SlowK,
GlobalColor("buy"), GlobalColor("buy"));
# ########################################################
Thank you so much, I want it to be in 1min, I’m trying to use as entrance point.
 
@TY_Joe Welcome to the usethnkscript forums... Could you just describe the logic you desire without referencing colors please... We code based on conditional logic, not colors... Did you write this code from scratch or is it a modified version and, if so, from where...??? Your request can probably be almost entirely coded right in the Stock Hacker Scan panel...
It’s mod from StochasticSlow from TOS. I’m trying to use for timing for entrance in 1 min chart. Thanks.
 
Is it possible to have four different stochastics being compared and returning which color is overbought?

For example:

1) Stoch Red (overbought at 75) returns "Red"
2) Stoch Blue (overbought at 75) returns "Blue"
3) Stoch Green (overbought at 75) returns "Green"
4) Stoch Yellow (overbought at 75) returns "Yellow"

I updated the code to reflect the concept.

Would someone be so kind as to help fit it to the correct syntax? Thank you!

I believe TOS only allows for two stochastics to be used to compare against as opposed to four.

Code:
# Slow Stochastic Watchlist
# @TY_Joe 6/21
declare lower;
input over_bought = 75;
input over_sold = 25;
input KPeriod = 10; #StochRed
input DPeriod = 3;

input KPeriod2 = 20; #StochBlue
input DPeriod2 = 3;

input KPeriod3 = 30; #StochGreen
input DPeriod3 = 3;

input KPeriod4 = 60; #StochYellow
input DPeriod4 = 8;

# ########################################################
def SlowK = reference StochasticSlow("k period" = KPeriod, "d period" = DPeriod)."SlowK" ;
def SlowD = reference StochasticSlow("k period" = KPeriod, "d period" = DPeriod)."SlowD" ;

def SlowK2 = reference StochasticSlow("k period" = KPeriod2, "d period" = DPeriod2)."SlowK" ;
def SlowD2 = reference StochasticSlow("k period" = KPeriod2, "d period" = DPeriod2)."SlowD" ;

def SlowK3 = reference StochasticSlow("k period" = KPeriod3, "d period" = DPeriod3)."SlowK" ;
def SlowD3 = reference StochasticSlow("k period" = KPeriod3, "d period" = DPeriod3)."SlowD" ;

def SlowK4 = reference StochasticSlow("k period" = KPeriod4, "d period" = DPeriod4)."SlowK" ;
def SlowD4 = reference StochasticSlow("k period" = KPeriod4, "d period" = DPeriod4)."SlowD" ;

# ########################################################



AddLabel(yes,
         if SlowK> over_bought then "Red" else
         if SlowK2> over_bought then "Blue" else
     if SlowK> overbought and SlowK2 > overbought then "Red Blue"
         if SlowK3> over_bought then "Green" else
         if SlowK4> over_bought then "Yellow" else "";
 
AssignBackgroundColor(  
         if SlowK> over_bought and SlowK2 > overbought then  color.dark_yellow else
         if SlowK3> overbought then color.orange else
         if SlowK4> overbought then color.red else
         if SlowK3> overbought and SlowK4> overbought then color.violet else
         color.black) ;
# #########################################################
 
Last edited:
@perseverance_trading1 The error says it all. The script you're working on is too complicated to run in watch list and/or scans.
Complicated data in if statements are the cause. Reducing the number of if statements and simplifying the data defs can sometimes help. Otherwise, there is nothing to do about it, unfortunately
 
@perseverance_trading1 The error says it all. The script you're working on is too complicated to run in watch list and/or scans.
Complicated data in if statements are the cause. Reducing the number of if statements and simplifying the data defs can sometimes help. Otherwise, there is nothing to do about it, unfortunately

The sweet spot for the watchlist is two different stochastics. I can work around this using a scanner (or devote another custom variable to the cause) but the way I am using my watchlist is capture recent runners in one big master list. I find it faster to order the watchlist column where I can slice and dice different details right from the master list itself.

Thank you for providing that initial code snippet. Although I am not a coder, It's already helped me pull together some useful things from it.
 
@perseverance_trading1 Try making two references to the Stochastics Study, using different sets of settings, which will yield four plots, and see if you can work with those results to achieve your goal...

I like your idea of working toward a better solution. For some reason, I am not able to visualize the approach using two references with my background. I removed two stoch instances and was able to achieve a partial solution.

Can you elaborate more so I could try your idea?

Code:
# Slow Stochastic Watchlist
# @TY_Joe 6/21
declare lower;
input over_bought = 80;
input over_sold = 25;

input KPeriod2 = 20; #StochBlue
input DPeriod2 = 3;

input KPeriod4 = 60; #StochYellow
input DPeriod4 = 8;

# ########################################################


def SlowK2 = reference StochasticSlow("k period" = KPeriod2, "d period" = DPeriod2)."SlowK" ;
def SlowD2 = reference StochasticSlow("k period" = KPeriod2, "d period" = DPeriod2)."SlowD" ;

def SlowK4 = reference StochasticSlow("k period" = KPeriod4, "d period" = DPeriod4)."SlowK" ;
def SlowD4 = reference StochasticSlow("k period" = KPeriod4, "d period" = DPeriod4)."SlowD" ;

# ########################################################



AddLabel(yes,
         if SlowK2> over_bought then "Blue" else
         if SlowK4> over_bought then "Yellow" else "");
      
AssignBackgroundColor(       
        
         if SlowK2> over_bought then color.red else
         if SlowK4> over_bought then color.blue else
         color.black) ;
# #########################################################
 

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