Previous Day High/Low/Close For ThinkOrSwim

Try this code. It has been working for me. It does HLC of the previous day and seems to work on all instruments.

Code:
input aggregationPeriod = AggregationPeriod.DAY;
input length = 1;
input displace = -1;
input showOnlyLastPeriod = yes;
plot PrevDayHigh;
plot PrevDayLow;
plot PrevDayClose;

if showOnlyLastPeriod and !IsNaN(high(period = aggregationPeriod)[-1]) and !IsNaN(low(period = aggregationPeriod)[-1]) and !IsNaN(close(period = aggregationPeriod)[-1])
{
    PrevDayHigh = Double.NaN;
    PrevDayLow = Double.NaN;
    PrevDayClose = Double.NaN;
}
else
{
    PrevDayHigh = Highest(high(period = aggregationPeriod)[-displace], length);
    PrevDayLow = Highest(low(period = aggregationPeriod)[-displace], length);
    PrevDayClose = Highest(close(period = aggregationPeriod)[-displace], length);
}
PrevDayHigh.SetDefaultColor(CreateColor(0,255,255));
PrevDayHigh.SetPaintingStrategy(PaintingStrategy.LINE);

PrevDayLow.SetDefaultColor(CreateColor(0,255,255));
PrevDayLow.SetPaintingStrategy(PaintingStrategy.LINE);

PrevDayClose.SetDefaultColor(CreateColor(116,189,239));
PrevDayClose.SetPaintingStrategy(PaintingStrategy.LINE);
PrevDayClose.SetStyle(Curve.LONG_DASH);
 
@iambkm01 As requested, the Open should be added as well:

Code:
input aggregationPeriod = AggregationPeriod.DAY;
input length = 1;
input displace = -1;
input showOnlyLastPeriod = yes;

plot PrevDayOpen;
plot PrevDayHigh;
plot PrevDayLow;
plot PrevDayClose;

if showOnlyLastPeriod and !IsNaN(open(period = aggregationPeriod)[-1]) and !IsNaN(high(period = aggregationPeriod)[-1]) and !IsNaN(low(period = aggregationPeriod)[-1]) and !IsNaN(close(period = aggregationPeriod)[-1])
{
    PrevDayOpen = Double.NaN;
    PrevDayHigh = Double.NaN;
    PrevDayLow = Double.NaN;
    PrevDayClose = Double.NaN;
}
else
{
    PrevDayOpen = Highest(open(period = aggregationPeriod)[-displace], length);
    PrevDayHigh = Highest(high(period = aggregationPeriod)[-displace], length);
    PrevDayLow = Highest(low(period = aggregationPeriod)[-displace], length);
    PrevDayClose = Highest(close(period = aggregationPeriod)[-displace], length);
}
PrevDayOpen.SetDefaultColor(CreateColor(116,189,239));
PrevDayOpen.SetPaintingStrategy(PaintingStrategy.LINE);
PrevDayOpen.SetStyle(Curve.LONG_DASH);

PrevDayHigh.SetDefaultColor(CreateColor(0,255,255));
PrevDayHigh.SetPaintingStrategy(PaintingStrategy.LINE);

PrevDayLow.SetDefaultColor(CreateColor(0,255,255));
PrevDayLow.SetPaintingStrategy(PaintingStrategy.LINE);

PrevDayClose.SetDefaultColor(CreateColor(116,189,239));
PrevDayClose.SetPaintingStrategy(PaintingStrategy.LINE);
PrevDayClose.SetStyle(Curve.LONG_DASH);

Hope this helps...

Good Luck and Good Trading :)
 
@netarchitech This is amazing, exactly what I needed. May the trading gods send profits down your way man. THANKS!!!!

Hi Man, quick question...sorry for the follow up. Futures are displaying fine but for stocks, it is keeping yesterdays lines and todays in the graphics:

G4AioOy.png
 
Hi @iambkm01

It looks like there is an additional parameter that affects the display of the OHLC lines...I had to rework the code a little bit to accommodate this...

Below is the new code to replace the old OHLC code...
Code:
input aggregationPeriod = AggregationPeriod.DAY;
input length = 1;
#input displace = -1;
input displace_amount = {default zero, negative_one};

def displacement;
switch (displace_amount) {
case zero:
    displacement = 0;
case negative_one:
    displacement = -1;
}

input showOnlyLastPeriod = yes;

plot PrevDayOpen;
plot PrevDayHigh;
plot PrevDayLow;
plot PrevDayClose;

if showOnlyLastPeriod and !IsNaN(open(period = aggregationPeriod)[-1]) and !IsNaN(high(period = aggregationPeriod)[-1]) and !IsNaN(low(period = aggregationPeriod)[-1]) and !IsNaN(close(period = aggregationPeriod)[-1])
{
    PrevDayOpen = Double.NaN;
    PrevDayHigh = Double.NaN;
    PrevDayLow = Double.NaN;
    PrevDayClose = Double.NaN;
}
else
{
    PrevDayOpen = Highest(open(period = aggregationPeriod)[displace_amount], length);
    PrevDayHigh = Highest(high(period = aggregationPeriod)[displace_amount], length);
    PrevDayLow = Highest(low(period = aggregationPeriod)[displace_amount], length);
    PrevDayClose = Highest(close(period = aggregationPeriod)[displace_amount], length);
}
PrevDayOpen.SetDefaultColor(CreateColor(116, 189, 239));
PrevDayOpen.SetPaintingStrategy(PaintingStrategy.LINE);
PrevDayOpen.SetStyle(Curve.LONG_DASH);

PrevDayHigh.SetDefaultColor(CreateColor(0, 255, 255));
PrevDayHigh.SetPaintingStrategy(PaintingStrategy.LINE);

PrevDayLow.SetDefaultColor(CreateColor(0, 255, 255));
PrevDayLow.SetPaintingStrategy(PaintingStrategy.LINE);

PrevDayClose.SetDefaultColor(CreateColor(116, 189, 239));
PrevDayClose.SetPaintingStrategy(PaintingStrategy.LINE);
PrevDayClose.SetStyle(Curve.LONG_DASH);

Once the old code is replaced, just follow the steps below to resolve this issue...

1.png


2.png


3.png


4.png


5.png



The chart below shows the extra lines you wanted removed...Just in case, I retained the lines, but you don't have to view them if you don't want to...Just keep the "displace amount" set to "zero"...

c.png


To view just the current lines shown below, just make sure to keep the "displace amount" set to "zero"...

b.png


Hope this helps...

Good Luck and Good Trading :)
 
@netarchitech Hi Dude - once again thank you and happy new year. I am so thankful for the code you created last time I was here. I am back because I would like to ask you if you would have or be able to write a code for Overnight/PreMarket session High and Low. It would be great if you could write this in a separate code file that I could save. Let me know if this is possible. Thank you!!
 
@shahgols this may help. Below is a script I have for a label that will provide you with the Pre-market gap up/down $ and %. Give it a shot! I hope that helps. I have attached a screenshot of the label as well. Good luck. @cabe1332

Code:
#Start code

def AP = AggregationPeriod.DAY;
def Priorclose = close(period = AP)[1];
def PctChange = (close - Priorclose) / Priorclose;
def PMGapUP = Round(close - Priorclose, 2);
def PC1 = Round(PctChange, 2);

AddLabel (1, "PM Gap Up/Down $: " + PMGapUP +" | " + AsPercent(PC1),
    if  Priorclose < close then color.light_green else CreateColor(255, 153, 153));

AddLabel (1, "Prior Close Price $: " + Round(Priorclose, 2),
    if  Priorclose < close then color.light_green else CreateColor(255, 153, 153));

#End code
 
Last edited by a moderator:
Hello,

I have been trying to write a piece of simple code to compare the previous day's closing price to the current candle close price, and although this seems like it should be very simple, I have not been able to get the results I would expect. The following is the code I have been trying to use. Is this correct or what am I doing wrong. Thank you for any help you can provide!

plot gap = close(period = AggregationPeriod.DAY)[-1] > close[1];
 
@generic, thx! Will that look at the current candle's close price and compare it to the previous day's close price? Also, will this work in premarket? thx again!

Just to restate, this is being used in a scanner, not in a chart. Not sure that matters in this case, but wanted to point that out. Thx!
 
@Jobbers It compares the current daily close to yesterday's close. I don't think it'll work in PM since the new daily bar isn't plotted yet but I'm not too sure on that.

@standai The quote price should be the current close so I'm not sure what you mean.
 
Hi Everyone,

First post on the forum! I found a study that plots the prior day's high/low onto a chart. I find this useful for directional bias intraday. I'm a complete newb when it comes to scripting on ThinkorSwim and wanted to see if it's possible to add Up/Down arrows to the code for the following scenarios below:
  1. Stock crosses above yesterday's high: Up Arrow (Green Color)
  2. Stock crosses above yesterday's low: Up Arrow (Green Color)
  3. Stock crosses below yesterday's high: Down Arrow (Red Color)
  4. Stock crosses below yesterday's low: Down Arrow (Red Color)
How do I create a scanner for these scenarios as well?

Code:
#Study:Common Level
#by thetrader.top
declare hide_on_daily;
declare once_per_bar;

input timeFrame = {default DAY, WEEK, MONTH};

plot high = If(GetAggregationPeriod() <= AggregationPeriod.FIFTEEN_MIN, high(period = timeFrame)[1], Double.NaN);
plot Low = If(GetAggregationPeriod() <= AggregationPeriod.FIFTEEN_MIN, low(period = timeFrame)[1], Double.NaN);

high.SetDefaultColor (Color.GREEN);
high.SetPaintingStrategy(PaintingStrategy.DASHES);
Low.SetDefaultColor(Color.RED);
Low.SetPaintingStrategy(PaintingStrategy.DASHES);
 
@swetrades
Code:
# Study: Common Level
# by thetrader.top
# 06.01.21 - arrows added

declare hide_on_daily;
declare once_per_bar;

input timeFrame = {default DAY, WEEK, MONTH};

plot high = If(GetAggregationPeriod() <= AggregationPeriod.FIFTEEN_MIN, high(period = timeFrame)[1], Double.NaN);
plot Low = If(GetAggregationPeriod() <= AggregationPeriod.FIFTEEN_MIN, low(period = timeFrame)[1], Double.NaN);
plot crossabovehigh = close crosses above high;
plot crossbelowhigh = close crosses below high;
plot crossbelowlow = close crosses below low;
plot crossabovelow = close crosses above low;

high.SetDefaultColor (Color.GREEN);
high.SetPaintingStrategy(PaintingStrategy.DASHES);
Low.SetDefaultColor(Color.RED);
Low.SetPaintingStrategy(PaintingStrategy.DASHES);
crossabovehigh.setpaintingstrategy(paintingstrategy.boolean_arrow_up);
crossabovehigh.setdefaultcolor(color.green);
crossbelowhigh.setpaintingstrategy(paintingstrategy.boolean_arrow_down);
crossbelowhigh.setdefaultcolor(color.yellow);
crossbelowlow.setpaintingstrategy(paintingstrategy.boolean_arrow_down);
crossbelowlow.setdefaultcolor(color.red);
crossabovelow.setpaintingstrategy(paintingstrategy.boolean_arrow_up);
crossabovelow.setdefaultcolor(color.dark_orange);

# end code
 
below is a label by cabe 1332 that shows pre-market gaps. i'm trying to modify it a bit ...

1/ to show results for both after hours and premarket, depending on where we are, time-wise.

2/ meanwhile, i'm just trying to understand what i'm seeing, as in the pic below. the closing price on 6/4 was 99.57. right now, the label is showing the closing price for 5/3, ie 96.50. my assumption is that the label will change to 99.57 once premarket trading begins. think that's right?

3/ and if we could add AH to the label, then it would already reflect the 6/4 price of 99.57 and any after-hour changes that occurred once the regular trading session closed. right?

4/ if AH can't be added the current label, perhaps someone could help me construct a label that only uses AH and I can add that separately. i have an idea about how to this but if someone beats me to the punch, well, okay, thanks!

Code:
# by cabe 1332

#Start code

def AP = AggregationPeriod.DAY;
def Priorclose = close(period = AP)[1];
def PctChange = (close - Priorclose) / Priorclose;
def PMGapUP = Round(close - Priorclose, 2);
def PC1 = Round(PctChange, 2);

AddLabel (1, "PM Gap Up/Down $: " + PMGapUP +" | " + AsPercent(PC1),
    if  Priorclose < close then color.light_green else CreateColor(255, 153, 153));

AddLabel (1, "Prior Close Price $: " + Round(Priorclose, 2),
    if  Priorclose < close then color.light_green else CreateColor(255, 153, 153));

#End code
 
Last edited by a moderator:
okay, lookin' good. but ... any way to get it to work on daily charts?

thanks again!
 
Last edited by a moderator:
okay, lookin' good. but ... any way to get it to work on daily charts?
thanks again!
Below is the updated code. Good luck! @cabe1332

#Start code

# PM info
def AP = AggregationPeriod.DAY;
def Priorclose = close(period = AP)[1];
def PctChange = (close - Priorclose) / Priorclose;
def PMGapUP = Round(close - Priorclose, 2);
def PC1 = Round(PctChange, 2);

AddLabel (1, "PM Gap Up/Down $: " + PMGapUP +" | " + AsPercent(PC1),
if Priorclose < close then color.light_green else CreateColor(255, 153, 153));

AddLabel (1, "Prior Close Price $: " + Round(Priorclose, 2),
if Priorclose < close then color.light_green else CreateColor(255, 153, 153));

# AH info
def close1 = if SecondsFromTime(0930) > 0 and SecondsFromTime(1600) < 0 then close else close1[1];
def change = (close / close1) - 1;

def AH = AggregationPeriod.DAY;
def INTclose = close(period = AH);
def AHGapUP = Round(close - INTclose, 2);
def PctChange2= (close - Intclose) / Intclose;
def PC2 = Round(PctChange2, 2);

AddLabel(ah, "AH Gap Up/Down: " + AHGapUP + " | " + AsPercent(PC2), if change < 0 then CreateColor(255, 153, 153) else CreateColor(197, 239, 161));

#End code
 
Last edited by a moderator:

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

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
484 Online
Create Post

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