Highlight defined time range on chart

wilmanv

Member
VIP
Is there any script that shows me the charts only for a certain time that I define, sample: just betwen 9:00 to 10:00 AM or 2:00 to 4:00PM, for any timeframe: 2 min, 5min, daily, weekly. thanks
 
Here is a simple script that lets you highlight a defined time.

Code:
# TS_TRADEZONES
# http://www.thinkscripter.com
# [email protected]
# Last Update 30 JUN 2009

input zoneStart = 1500;
input zoneEnd = 1615;
input type = {default NOTRADE, REVERSAL};
plot highBar;
plot lowBar;

switch (type){

case NOTRADE:
highBar = if secondsTillTime(zoneStart) <= 0 and secondsTillTime(zoneEnd) >= 0 then highestAll(high) else double.nan;
lowBar = if secondsTillTime(zoneStart) <= 0 and secondsTillTime(zoneEnd) >= 0 then lowestAll(low) else double.nan;

case REVERSAL:
lowBar = if secondsTillTime(zoneStart) <= 0 and secondsTillTime(zoneEnd) >= 0 then highestAll(high) else double.nan;
highBar = if secondsTillTime(zoneStart) <= 0 and secondsTillTime(zoneEnd) >= 0 then lowestAll(low) else double.nan;
}
highBar.assignValueColor(if highBar>lowBar then color.dark_red else color.green);
lowBar.assignValueColor(if highBar>lowBar then color.dark_red else color.green);
addCloud(lowBar, highBar);
#---------------- End Of Code --------------------
 
@BenTen This is probably the best way to highlight via an AddCloud the times of day one would be interested in. Run this on a 5 min chart for example

Code:
# Add Cloud Time Based
# tomsk
# 12.16.2019

declare hide_on_daily;

input Time1 = 1100;
input Time2 = 1330;

def active = SecondsTillTime(Time1) <= 0 and SecondsTillTime(Time2) >= 0;
AddCloud(if active then Double.NEGATIVE_INFINITY else Double.NaN, Double.POSITIVE_INFINITY, Color.Gray, Color.Gray);
# End Add Cloud Time Based
 
Last edited:
@BenTen This is probably the best way to highlight via an AddCloud the times of day one would be interested in. Run this on a 5 min chart for example

Code:
# Add Cloud Time Based
# tomsk
# 12.16.2019

declare hide_on_daily;

input Time1 = 1100;
input Time2 = 1330;

def active = SecondsTillTime(Time1) <= 0 and SecondsTillTime(Time2) >= 0;
AddCloud(if active then Double.NEGATIVE_INFINITY else Double.NaN, Double.POSITIVE_INFINITY, Color.Gray, Color.Gray);
# End Add Cloud Time Based
This worked great for me... i'm highlighting time frames for futures. One thing I ran into was an issue highlighting timeframes that carried over from 2300 - 0200 the next day... doesn't seem like TOS likes the idea of assigning a time value that are different days? Not sure if there is a way to code around that.
 
This worked great for me... i'm highlighting time frames for futures. One thing I ran into was an issue highlighting timeframes that carried over from 2300 - 0200 the next day... doesn't seem like TOS likes the idea of assigning a time value that are different days? Not sure if there is a way to code around that.

This should help. The image shows the cloud color magenta, which was changed at the input screen

Screenshot-2022-10-17-082903.png
Ruby:
# Add Cloud Time Based
# tomsk
# 12.16.2019

declare hide_on_daily;

input Time1 = 1100;
input Time2 = 1330;

def sec1 = SecondsFromTime(Time1);
def sec2 = SecondsFromTime(Time2);
def isTime1 = (sec1 >= 0 and sec1[1] < 0) or (sec1 < sec1[1] and sec1 >= 0);
def isTime2 = (sec2 >= 0 and sec2[1] < 0) or (sec2 < sec2[1] and sec2 >= 0);
def inRange = CompoundValue(1, if isTime1 then 1 else if isTime2 then 0 else inRange[1], 0);

def active = inRange;
DefineGlobalColor("Cloud", Color.GRAY);
AddCloud(if active then Double.NEGATIVE_INFINITY else Double.NaN, Double.POSITIVE_INFINITY, GlobalColor("Cloud"), GlobalColor("Cloud"));
# End Add Cloud Time Based
 
@SleepyZ or others, this is a great tool. Is there a way to have the cloud stop a the high and low of the candles in the time frame?

Yes,

Screenshot-2023-04-11-103614.png
Code:
# Add Cloud Time Based
# tomsk
# 12.16.2019

declare hide_on_daily;

input Time1 = 1100;
input Time2 = 1330;

def sec1 = SecondsFromTime(Time1);
def sec2 = SecondsFromTime(Time2);
def isTime1 = (sec1 >= 0 and sec1[1] < 0) or (sec1 < sec1[1] and sec1 >= 0);
def isTime2 = (sec2 >= 0 and sec2[1] < 0) or (sec2 < sec2[1] and sec2 >= 0);
def inRange = CompoundValue(1, if isTime1 then 1 else if isTime2 then 0 else inRange[1], 0);

def active = inRange;
def bars   = 200000;

input pricePerRowHeightMode = { AUTOMATIC, default TICKSIZE, CUSTOM};
input customRowHeight = 1.0;
input timePerProfile = {default BAR};
input onExpansion = no;
input profiles = 1000;

def period;

switch (timePerProfile) {
case BAR:
    period = BarNumber() - 1;
}

def count = CompoundValue(1, if inRange and period != period[1] then (count[1] + period - period[1]) % bars else count[1], 0);
def cond = count < count[1] + period - period[1];
def height;
switch (pricePerRowHeightMode) {
case AUTOMATIC:
    height = PricePerRow.AUTOMATIC;
case TICKSIZE:
    height = PricePerRow.TICKSIZE;
case CUSTOM:
    height = customRowHeight;
}

profile vol = VolumeProfile("startNewProfile" = cond, "onExpansion" = no, "numberOfProfiles" = 1000, "pricePerRow" = height, "value area percent" = 0);
def con = CompoundValue(1, onExpansion, no);

def hProfile = if inRange and IsNaN(vol.GetHighest()) and con then hProfile[1] else vol.GetHighest();
def lProfile = if inRange and IsNaN(vol.GetLowest()) and con then lProfile[1] else vol.GetLowest();
def plotsDomain = IsNaN(close) == onExpansion;
def ProfileHigh = if inRange and plotsDomain then hProfile else Double.NaN;
def ProfileLow  = if inRange and plotsDomain then lProfile else Double.NaN;

def dataCount = CompoundValue(1, if cond != cond[1] then dataCount[1] + 1 else dataCount[1]  , 0);

plot hrange = if inRange then ProfileHigh else Double.NaN;
plot lrange = if inRange  then ProfileLow else Double.NaN;

DefineGlobalColor("Cloud", Color.GRAY);
AddCloud(if active then hrange else Double.NaN, lrange, GlobalColor("Cloud"), GlobalColor("Cloud"));
# End Add Cloud Time Based
 
@SleepyZ do you know if there is an option on this to go back to the far left as much as possible?

Yes, but it requires a separate plot for each pair of high/low. The following creates 5 pairs. More pairs can be created using the logic creating the 5 pairs.

Screenshot-2023-04-25-130428.png
Code:
# Add Cloud Time Based
# tomsk
# 12.16.2019

declare hide_on_daily;

script ac {

    input days        = 0;
    input extend_left = no;

    input Time1 = 1100;
    input Time2 = 1330;


    def sec1 = SecondsFromTime(Time1);
    def sec2 = SecondsFromTime(Time2);
    def isTime1 = (sec1 >= 0 and sec1[1] < 0) or (sec1 < sec1[1] and sec1 >= 0);
    def isTime2 = (sec2 >= 0 and sec2[1] < 0) or (sec2 < sec2[1] and sec2 >= 0);
    def inRange = CompoundValue(1, if isTime1 then 1 else if isTime2 then 0 else inRange[1], 0);

    def active = inRange;
    def bars   = 200000;

    input pricePerRowHeightMode = { AUTOMATIC, default TICKSIZE, CUSTOM};
    input customRowHeight = 1.0;
    input timePerProfile = {default BAR};
    input onExpansion = no;
    input profiles = 1000;

    def period;

    switch (timePerProfile) {
    case BAR:
        period = BarNumber() - 1;
}

    def count = CompoundValue(1, if inRange and period != period[1] then (count[1] + period - period[1]) % bars else count[1], 0);
    def cond = count < count[1] + period - period[1];
    def height;
    switch (pricePerRowHeightMode) {
    case AUTOMATIC:
        height = PricePerRow.AUTOMATIC;
    case TICKSIZE:
        height = PricePerRow.TICKSIZE;
    case CUSTOM:
        height = customRowHeight;
}

    profile vol = VolumeProfile("startNewProfile" = cond, "onExpansion" = no, "numberOfProfiles" = 1000, "pricePerRow" = height, "value area percent" = 0);
    def con = CompoundValue(1, onExpansion, no);

    def hProfile = if inRange and IsNaN(vol.GetHighest()) and con then hProfile[1] else vol.GetHighest();
    def lProfile = if inRange and IsNaN(vol.GetLowest()) and con then lProfile[1] else vol.GetLowest();
    def plotsDomain = IsNaN(close) == onExpansion;
    def ProfileHigh = if inRange and plotsDomain then hProfile else Double.NaN;
    def ProfileLow  = if inRange and plotsDomain then lProfile else Double.NaN;

    def dataCount = CompoundValue(1, if cond != cond[1] then dataCount[1] + 1 else dataCount[1]  , 0);

    def ymd      = GetYYYYMMDD();
    def candles  = !IsNaN(close);
    def capture  = candles and ymd != ymd[1];
    def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
    def thisDay  = (HighestAll(dayCount) - dayCount) ;
    def bn       = BarNumber();
    def hr       = if thisDay == days and inRange then ProfileHigh else hr[1];
    def hlbn     = if thisDay == days and sec1 == 0 then bn else hlbn[1];
    def lr       = if thisDay == days and inRange then ProfileLow else lr[1];

    plot hrange  = if extend_left and bn <= HighestAll(hlbn)
                   then HighestAll(hr)
                   else if thisDay == days and inRange
                   then ProfileHigh else Double.NaN;
    plot lrange  = if extend_left and bn <= HighestAll(hlbn)
                   then HighestAll(lr)
                   else if thisDay == days and inRange 
                   then ProfileLow
                   else Double.NaN;
}

input extend_left = yes;
DefineGlobalColor("Cloud", Color.GRAY);
DefineGlobalColor("H", Color.GREEN);
DefineGlobalColor("L", Color.RED);

plot hr0 = ac(0, extend_left).hrange;
plot lr0 = ac(0, extend_left).lrange;
AddCloud(hr0, lr0, GlobalColor("Cloud"), GlobalColor("Cloud"));

plot hr1 = ac(1, extend_left).hrange;
plot lr1 = ac(1, extend_left).lrange;
AddCloud(hr1, lr1, GlobalColor("Cloud"), GlobalColor("Cloud"));

plot hr2 = ac(2, extend_left).hrange;
plot lr2 = ac(2, extend_left).lrange;
AddCloud(hr2, lr2, GlobalColor("Cloud"), GlobalColor("Cloud"));

plot hr3 = ac(3, extend_left).hrange;
plot lr3 = ac(3, extend_left).lrange;
AddCloud(hr3, lr3, GlobalColor("Cloud"), GlobalColor("Cloud"));

plot hr4 = ac(4, extend_left).hrange;
plot lr4 = ac(4, extend_left).lrange;
AddCloud(hr4, lr4, GlobalColor("Cloud"), GlobalColor("Cloud"));

hr0.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lr0.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hr1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lr1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hr2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lr2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hr3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lr3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hr4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lr4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

hr0.SetDefaultColor(GlobalColor("H"));
lr0.SetDefaultColor(GlobalColor("L"));
hr1.SetDefaultColor(GlobalColor("H"));
lr1.SetDefaultColor(GlobalColor("L"));
hr2.SetDefaultColor(GlobalColor("H"));
lr2.SetDefaultColor(GlobalColor("L"));
hr3.SetDefaultColor(GlobalColor("H"));
lr3.SetDefaultColor(GlobalColor("L"));
hr4.SetDefaultColor(GlobalColor("H"));
lr4.SetDefaultColor(GlobalColor("L"));

# End Add Cloud Time Based
 

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