Date & Time of Indicator Changes In ThinkOrSwim

FFGARCIA

New member
Dear Friends,

The script appended below continuously assigns bullish/bearish colors to stocks in a watch list (the script is an amalgamation of ideas gleaned from posts and conversions by the legendary Sam4Cok@Samer800).

I would like to record the date and time in the fifth column of the watch list each time a color change occurs for a given stock (kindly see the empty fifth column in the attached picture). This way, I can tell how long the current color has been active. Any assistance would be highly appreciated.

God bless you all!

Screenshot from 2024-05-10 18-26-50.png

Ruby:
Declare lower;

input BarColor = yes;
input showSignals = yes;
input MovAvgType = AverageType.EXPONENTIAL;
input length = 5;
input lookback = 5;
input ema1_len = 3;
input ema2_len = 8;
input ema3_len = 13;

def na = Double.NaN;
def ys1 = (high + low + close * 2) / 4;
def rk3 = MovingAverage(MovAvgType, ys1, length);
def rk4 = stdev(ys1, length);
def rk5 = (ys1 - rk3) * 100 / rk4;
def rk6 = MovingAverage(MovAvgType, rk5, length);
def up = MovingAverage(MovAvgType, rk6, length);
def down = MovingAverage(MovAvgType, up, length);

def Buy = Crosses(up, down, CrossingDirection.ABOVE);
def Sell = Crosses(up, down, CrossingDirection.BELOW);

def ema1 = MovAvgExponential(length=ema1_len);
def ema2 = MovAvgExponential(length=ema2_len);
def ema3 = MovAvgExponential(length=ema3_len);

#def ema1 = SimpleMovingAvg(length=ema1_len);
#def ema2 = SimpleMovingAvg(length=ema2_len);
#def ema3 = SimpleMovingAvg(length=ema3_len);

def bull_cross = ema1 crosses above ema2 and ema1 crosses above ema3 and ema2 crosses above ema3;
def bear_cross = ema1 crosses below ema2 and ema1 crosses below ema3 and ema2 crosses below ema3;
def bull_lookback = highest(bull_cross, lookback);
def bear_lookback = highest(bear_cross, lookback);

def S1 = Buy and bull_lookback;
def S2 = Buy and !bull_lookback;
def S3 = Sell and bear_lookback;
def S4 = Sell and !bear_lookback;

plot signal = if S1 then 4 else if S2 then 3 else if S3 then 2 else if S1 then 1 else 0;
signal.AssignValueColor(if signal == 4 then Color.Dark_Green else if signal == 3 then Color.Green else if signal == 1 then Color.Red else if signal == 2 then Color.Dark_Red else Color.Dark_Orange);
AssignBackgroundCOlor(if signal == 4 then Color.Dark_Green else if signal == 3 then Color.Green else if signal == 1 then Color.Red else if signal == 2 then Color.Dark_Red else Color.Dark_Orange);
 
Last edited by a moderator:
Dear Friends,

The script appended below continuously assigns bullish/bearish colors to stocks in a watch list (the script is an amalgamation of ideas gleaned from posts and conversions by the legendary Sam4Cok@Samer800).

I would like to record the date and time in the fifth column of the watch list each time a color change occurs for a given stock (kindly see the empty fifth column in the attached picture). This way, I can tell how long the current color has been active. Any assistance would be highly appreciated.

God bless you all!


Ruby:
Declare lower;

input BarColor = yes;
input showSignals = yes;
input MovAvgType = AverageType.EXPONENTIAL;
input length = 5;
input lookback = 5;
input ema1_len = 3;
input ema2_len = 8;
input ema3_len = 13;

def na = Double.NaN;
def ys1 = (high + low + close * 2) / 4;
def rk3 = MovingAverage(MovAvgType, ys1, length);
def rk4 = stdev(ys1, length);
def rk5 = (ys1 - rk3) * 100 / rk4;
def rk6 = MovingAverage(MovAvgType, rk5, length);
def up = MovingAverage(MovAvgType, rk6, length);
def down = MovingAverage(MovAvgType, up, length);

def Buy = Crosses(up, down, CrossingDirection.ABOVE);
def Sell = Crosses(up, down, CrossingDirection.BELOW);

def ema1 = MovAvgExponential(length=ema1_len);
def ema2 = MovAvgExponential(length=ema2_len);
def ema3 = MovAvgExponential(length=ema3_len);

#def ema1 = SimpleMovingAvg(length=ema1_len);
#def ema2 = SimpleMovingAvg(length=ema2_len);
#def ema3 = SimpleMovingAvg(length=ema3_len);

def bull_cross = ema1 crosses above ema2 and ema1 crosses above ema3 and ema2 crosses above ema3;
def bear_cross = ema1 crosses below ema2 and ema1 crosses below ema3 and ema2 crosses below ema3;
def bull_lookback = highest(bull_cross, lookback);
def bear_lookback = highest(bear_cross, lookback);

def S1 = Buy and bull_lookback;
def S2 = Buy and !bull_lookback;
def S3 = Sell and bear_lookback;
def S4 = Sell and !bear_lookback;

plot signal = if S1 then 4 else if S2 then 3 else if S3 then 2 else if S1 then 1 else 0;
signal.AssignValueColor(if signal == 4 then Color.Dark_Green else if signal == 3 then Color.Green else if signal == 1 then Color.Red else if signal == 2 then Color.Dark_Red else Color.Dark_Orange);
AssignBackgroundCOlor(if signal == 4 then Color.Dark_Green else if signal == 3 then Color.Green else if signal == 1 then Color.Red else if signal == 2 then Color.Dark_Red else Color.Dark_Orange);

you don't need to add a study, which would be a copy of this study, and use more resources.

you are plotting the signal number, but as the same color as background, so there is no point in doing that.
might as well plot a number that has meaning.

you say you want a date and time, 'so you can calculate when'....
why not just calculate the time period since a color change, and not have to do the math in your head?
do you expect the dates to be important? or the times?

this looks for when the color changes and resets a counter.
it plots a number in the cell, as the quantity of bars elapsed since the color change.


--------------------

watchlist study

zbuysellcolors
http://tos.mx/!JhpkuKOY
15minute


column study
color determined by buy/sell conditions
the number is , how many bars since last time color changed. default is bars.
can disable and enable other plots to show, bars since, minutes since, hours since,


Code:
#zbuysellcolors

#buysell_column_colors
#https://usethinkscript.com/threads/record-date-and-time-when-indicator-color-changes-in-watch-list.18664/
#Record date and time when indicator color changes in watch list

def agg = getaggregationperiod();
def aggmin = agg/60000;

input BarColor = yes;
input showSignals = yes;
input MovAvgType = AverageType.EXPONENTIAL;
input length = 5;
input lookback = 5;
input ema1_len = 3;
input ema2_len = 8;
input ema3_len = 13;

def na = Double.NaN;
def ys1 = (high + low + close * 2) / 4;
def rk3 = MovingAverage(MovAvgType, ys1, length);
def rk4 = stdev(ys1, length);
def rk5 = (ys1 - rk3) * 100 / rk4;
def rk6 = MovingAverage(MovAvgType, rk5, length);
def up = MovingAverage(MovAvgType, rk6, length);
def down = MovingAverage(MovAvgType, up, length);

def Buy = Crosses(up, down, CrossingDirection.ABOVE);
def Sell = Crosses(up, down, CrossingDirection.BELOW);

def ema1 = MovAvgExponential(length=ema1_len);
def ema2 = MovAvgExponential(length=ema2_len);
def ema3 = MovAvgExponential(length=ema3_len);

#def ema1 = SimpleMovingAvg(length=ema1_len);
#def ema2 = SimpleMovingAvg(length=ema2_len);
#def ema3 = SimpleMovingAvg(length=ema3_len);

def bull_cross = ema1 crosses above ema2 and ema1 crosses above ema3 and ema2 crosses above ema3;
def bear_cross = ema1 crosses below ema2 and ema1 crosses below ema3 and ema2 crosses below ema3;
def bull_lookback = highest(bull_cross, lookback);
def bear_lookback = highest(bear_cross, lookback);

def S1 = Buy and bull_lookback;
def S2 = Buy and !bull_lookback;
def S3 = Sell and bear_lookback;
def S4 = Sell and !bear_lookback;

def signal = if S1 then 4 else if S2 then 3 else if S3 then 2 else if S1 then 1 else 0;
#signal.AssignValueColor(if signal == 4 then Color.Dark_Green else if signal == 3 then Color.Green else if signal == 1 then Color.Red else if signal == 2 then Color.Dark_Red else Color.Dark_Orange);

def cnt = if signal != signal[1] then 1 else cnt[1] + 1;
def cntmin = cnt * aggmin;
def cnthr = round(cntmin/60,2);


#-----------------------
# column code

AssignBackgroundCOlor(if signal == 4 then Color.Dark_Green else if signal == 3 then Color.Green else if signal == 1 then Color.Red else if signal == 2 then Color.Dark_Red else Color.Dark_Orange);

# enable 1 of these plots , for bars, minutes, hours, since last change
# bars
plot z = cnt;

# minutes
#plot z = cntmin;

# hours
#plot z = cnthr;

z.setdefaultcolor(color.black);

#--------------------------
# chart code

#addlabel(1, ("minutes since - " + cntmin), color.white);
#addlabel(1, ("hours since " + cnthr), color.white);
#addlabel(1, "bars since - " + cnt, (if signal == 4 then Color.Dark_Green else if signal == 3 then Color.Green else if signal == 1 then Color.Red else if signal == 2 then Color.Dark_Red else Color.Dark_Orange));
#
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

you don't need to add a study, which would be a copy of this study, and use more resources.

you are plotting the signal number, but as the same color as background, so there is no point in doing that.
might as well plot a number that has meaning.

you say you want a date and time, 'so you can calculate when'....
why not just calculate the time period since a color change, and not have to do the math in your head?
do you expect the dates to be important? or the times?

this looks for when the color changes and resets a counter.
it plots a number in the cell, as the quantity of bars elapsed since the color change.


--------------------

watchlist study

zbuysellcolors
http://tos.mx/!JhpkuKOY
15minute


column study
color determined by buy/sell conditions
the number is , how many bars since last time color changed. default is bars.
can disable and enable other plots to show, bars since, minutes since, hours since,


Code:
#zbuysellcolors

#buysell_column_colors
#https://usethinkscript.com/threads/record-date-and-time-when-indicator-color-changes-in-watch-list.18664/
#Record date and time when indicator color changes in watch list

def agg = getaggregationperiod();
def aggmin = agg/60000;

input BarColor = yes;
input showSignals = yes;
input MovAvgType = AverageType.EXPONENTIAL;
input length = 5;
input lookback = 5;
input ema1_len = 3;
input ema2_len = 8;
input ema3_len = 13;

def na = Double.NaN;
def ys1 = (high + low + close * 2) / 4;
def rk3 = MovingAverage(MovAvgType, ys1, length);
def rk4 = stdev(ys1, length);
def rk5 = (ys1 - rk3) * 100 / rk4;
def rk6 = MovingAverage(MovAvgType, rk5, length);
def up = MovingAverage(MovAvgType, rk6, length);
def down = MovingAverage(MovAvgType, up, length);

def Buy = Crosses(up, down, CrossingDirection.ABOVE);
def Sell = Crosses(up, down, CrossingDirection.BELOW);

def ema1 = MovAvgExponential(length=ema1_len);
def ema2 = MovAvgExponential(length=ema2_len);
def ema3 = MovAvgExponential(length=ema3_len);

#def ema1 = SimpleMovingAvg(length=ema1_len);
#def ema2 = SimpleMovingAvg(length=ema2_len);
#def ema3 = SimpleMovingAvg(length=ema3_len);

def bull_cross = ema1 crosses above ema2 and ema1 crosses above ema3 and ema2 crosses above ema3;
def bear_cross = ema1 crosses below ema2 and ema1 crosses below ema3 and ema2 crosses below ema3;
def bull_lookback = highest(bull_cross, lookback);
def bear_lookback = highest(bear_cross, lookback);

def S1 = Buy and bull_lookback;
def S2 = Buy and !bull_lookback;
def S3 = Sell and bear_lookback;
def S4 = Sell and !bear_lookback;

def signal = if S1 then 4 else if S2 then 3 else if S3 then 2 else if S1 then 1 else 0;
#signal.AssignValueColor(if signal == 4 then Color.Dark_Green else if signal == 3 then Color.Green else if signal == 1 then Color.Red else if signal == 2 then Color.Dark_Red else Color.Dark_Orange);

def cnt = if signal != signal[1] then 1 else cnt[1] + 1;
def cntmin = cnt * aggmin;
def cnthr = round(cntmin/60,2);


#-----------------------
# column code

AssignBackgroundCOlor(if signal == 4 then Color.Dark_Green else if signal == 3 then Color.Green else if signal == 1 then Color.Red else if signal == 2 then Color.Dark_Red else Color.Dark_Orange);

# enable 1 of these plots , for bars, minutes, hours, since last change
# bars
plot z = cnt;

# minutes
#plot z = cntmin;

# hours
#plot z = cnthr;

z.setdefaultcolor(color.black);

#--------------------------
# chart code

#addlabel(1, ("minutes since - " + cntmin), color.white);
#addlabel(1, ("hours since " + cnthr), color.white);
#addlabel(1, "bars since - " + cnt, (if signal == 4 then Color.Dark_Green else if signal == 3 then Color.Green else if signal == 1 then Color.Red else if signal == 2 then Color.Dark_Red else Color.Dark_Orange));
#
Thank you very much Boss! May God bless you bigly.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
307 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