• Memorial Day Sale! Claim the biggest discount of the year with $50 off VIP using code MEM50. Sign up here.

OHLC (open-high-low-close) Indicator for ThinkorSwim


New member
Hi guys,

I'm running into a wall when tweaking this code to plot HLOC for the previous week and was hoping someone could help. It plots previous OH, but plots LC of the previous week:

declare upper;

declare hide_on_daily;

input WeeksAgo = 1;#hint WeeksAgo: Excludes today

input PlotHigh = yes;

input PlotLow = yes;

input PlotClose = yes;

input PlotOpen = yes;

input ShowBubbles = no;

def  AdjWeeksAgo = WeeksAgo + 1;#Adjusted to match a true LastDate which includes today

def week = GetWeek();

def lastWeek = GetLastWeek();

def year = GetYear();

def lastYear = GetLastYear();

def yyyymmdd = GetYYYYMMDD();

def lastDate = HighestAll( if week == lastWeek and year == lastYear then yyyymmdd else Double.NaN );

def currentDate = if yyyymmdd < lastDate then yyyymmdd else lastDate;

def tradingWeeksAgo = CountTradingDays( currentDate, lastDate );

def previousWeekPlus = tradingWeeksAgo <= AdjWeeksAgo;

def today = lastDate == currentDate;

script previousPlot {

input today = no;

input previousWeekPlus = no;

input data = high;

plot previousPlot = if previousWeekPlus then

                        HighestAll(if today then data else Double.NaN)

                    else Double.NaN;


plot previousHigh = previousPlot(today, plotHigh and previousWeekPlus, high( period = "WEEK" )[WeeksAgo]);

plot PreviousLow = previousPlot(today, plotLow and previousWeekPlus, low( period = "WEEK" )[WeeksAgo]);

plot PreviousClose =  previousPlot(today, plotClose and previousWeekPlus, close( period = "WEEK" )[WeeksAgo]);

plot PreviousOpen =  previousPlot(today, plotOpen and previousWeekPlus, open( period = "WEEK" )[WeeksAgo]);

#=========== Chart Bubbles ===================

def FirstBar = ShowBubbles and today != today[1];

addchartbubble(FirstBar, PreviousHigh, ("High of " + WeeksAgo + " week(s) ago"), color = PreviousHigh.takeValueColor());

addchartbubble(FirstBar, PreviousLow, ("Low of " + WeeksAgo + " week(s) ago"), color = PreviousLow.takeValueColor());

addchartbubble(FirstBar, PreviousClose, ("Close of " + WeeksAgo + " week(s) ago"), color = PreviousClose.takeValueColor());

addchartbubble(FirstBar, PreviousOpen, ("Open of " + WeeksAgo + " week(s) ago"), color = PreviousOpen.takeValueColor());


Well-known member
@hydroflask At first glance, and I'm too tired and lazy at the moment to dig deeper, it seems like you have a lot of unnecessary code with all that date calculation. If you just want to plot OHLC for a week ago here's simpler code modeled after TOS's built in DailyHighLow study. And, actually, the built in DailyHighLow and DailyOpen studies take the aggregation period as an input so you could just use those rather than needing anything custom. It's weird they don't have one for close.

input aggregationPeriod = AggregationPeriod.WEEK;
input length = 1;
input displace = -1;
input showOnlyLastPeriod = yes;

plot PlotOpen;
plot PlotHigh;
plot PlotLow;
plot PlotClose;

if showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]) {
    PlotOpen = Double.NaN;
    PlotHigh = Double.NaN;
    PlotLow = Double.NaN;
    PlotClose = Double.NaN;
} else {
    PlotOpen = Highest(open(period = aggregationPeriod)[-displace], length);
    PlotHigh = Highest(high(period = aggregationPeriod)[-displace], length);
    PlotLow = Lowest(low(period = aggregationPeriod)[-displace], length);
    PlotClose = Highest(close(period = aggregationPeriod)[-displace], length);

The use of Highest/Lowest in their code seems odd. I'm guessing it's a hack to not have to check for IsNaN(). It doesn't seem to have any real impact.


New member
Hi Guys, I have an indicator from here that plots the Prior days Open, High, Low, Close, and overnight Highs/Lows for my futures trading on TOS. Is it possible to somehow program an alert code to send me a sound / phone notification anytime ES, NQ, CL or GC hit the aforementioned pivots so I can quickly check my phone/pc if I want to take the trade or not vs waiting all day and manually checking?

All the best,


New member
I'm trying to get the Open, High, Low and Closing prices for futures contracts (NQ, ES, etc...) during Regular Trading Hours (8:30 Central to 3:00). Ideally, I would like to add custom columns in Marketwatch/Quotes section of TOS as individual custom columns. Another option would be adding them to my existing charts if that is an easier process.

Similar threads