Plot 25% and 50% of previous day’s range?

Trading51

Active member
2019 Donor
VIP
Could someone make this code, plot the 25% and 50% extension of the previous day's range automatically

jp10ebk.png
 
@Trading51 Per your request, here is a study that plots the 25% and 50% extension of the previous day's range. This is to be used on intraday charts only

Code:
# Range Extension of Previous Day Range
# tomsk
# 1.11.2020

# This study plots the 25% and 50% extension of the previous day's range
# To be used on intraday charts only

declare hide_on_daily;

def CurrentDay = GetDay() == GetLastDay();
def prevHigh = if CurrentDay then high(period = AggregationPeriod.Day)[1] else Double.NaN;
def prevLow = if CurrentDay then low(period = AggregationPeriod.Day)[1] else Double.NaN;
def prevRange = if CurrentDay then prevHigh - prevLow else Double.NaN;

plot UExt25 = prevHigh + (prevRange * 0.25);
plot UExt50 = prevHigh + (prevRange * 0.50);
plot LExt25 = prevLow  - (prevRange * 0.25);
plot LExt50 = prevLow  - (prevRange * 0.50);

UExt25.SetDefaultColor(Color.Cyan);
UExt50.SetDefaultColor(Color.Pink);
LExt25.SetDefaultColor(Color.White);
LExt50.SetDefaultColor(Color.Green);

# End Range Extension of Previous Day Range
 

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

perfect thank you, is it possible to add the previous day's midpoint?


Here is version 1.1 of the Range Extension study with the previous day's midpoint plotted as dotted yellow line to make it distinct from the other plots

Code:
# Range Extension of Previous Day Range
# tomsk
# 1.11.2020

# V1.0 - 01.11.2020 - tomsk - Range Extension of Previous Day Range
# V1.1 - 01.11.2020 - tomsk - Added plot of previous day range midpoint

# This study plots the 25% and 50% extension of the previous day's range
# To be used on intraday charts only

declare hide_on_daily;

def CurrentDay = GetDay() == GetLastDay();
def prevHigh = if CurrentDay then high(period = AggregationPeriod.Day)[1] else Double.NaN;
def prevLow = if CurrentDay then low(period = AggregationPeriod.Day)[1] else Double.NaN;
def prevRange = if CurrentDay then prevHigh - prevLow else Double.NaN;

plot UExt25 = prevHigh + (prevRange * 0.25);
plot UExt50 = prevHigh + (prevRange * 0.50);
plot LExt25 = prevLow  - (prevRange * 0.25);
plot LExt50 = prevLow  - (prevRange * 0.50);
plot PrevMid = prevLow + (prevRange / 2);

UExt25.SetDefaultColor(Color.Cyan);
UExt50.SetDefaultColor(Color.Pink);
LExt25.SetDefaultColor(Color.White);
LExt50.SetDefaultColor(Color.Green);

PrevMid.SetDefaultColor(Color.Yellow);
PrevMid.SetStyle(Curve.Short_Dash);
PrevMid.SetLineWeight(3);

# End Range Extension of Previous Day Range
 
perfect, if I wanted to use that on a higher time frame like the weekly, what would I have to adjust in the code would it be simple?


@Trading51 OK, here is a study that plots the 25% and 50% extension of the previous week's range. Remember this is to be used on intraday charts only

Code:
# Range Extension of Previous Week Range
# tomsk
# 1.11.2020

# This study plots the 25% and 50% extension of the previous week's range
# To be used on intraday charts only

declare hide_on_daily;

def CurrentDay = GetDay() == GetLastDay();
def prevHigh = if CurrentDay then high(period = AggregationPeriod.Week)[1] else Double.NaN;
def prevLow = if CurrentDay then low(period = AggregationPeriod.Week)[1] else Double.NaN;
def prevRange = if CurrentDay then prevHigh - prevLow else Double.NaN;

plot UExt25 = prevHigh + (prevRange * 0.25);
plot UExt50 = prevHigh + (prevRange * 0.50);
plot LExt25 = prevLow  - (prevRange * 0.25);
plot LExt50 = prevLow  - (prevRange * 0.50);
plot PrevMid = prevLow + (prevRange / 2);

UExt25.SetDefaultColor(Color.Cyan);
UExt50.SetDefaultColor(Color.Pink);
LExt25.SetDefaultColor(Color.White);
LExt50.SetDefaultColor(Color.Green);

PrevMid.SetDefaultColor(Color.Yellow);
PrevMid.SetStyle(Curve.Short_Dash);
PrevMid.SetLineWeight(3);

# End Range Extension of Previous Week Range
 
I gave it a shot to add a label but I can tell I'm way off, this is what I have (addchartBubble price location" = PrevHigh, text = "+25% Ext " color = Color.White); Zero coding ability :)
 
I gave it a shot to add a label but I can tell I'm way off, this is what I have (addchartBubble price location" = PrevHigh, text = "+25% Ext " color = Color.White); Zero coding ability :)


@Trading51 Adding chart bubbles is a bit more involved than that. Since you asked, here then is Version 1.2 of the Range Extension of Previous Day Range study COMPLETE with chart bubbles added to all 5 plots. Have at it ...

Code:
# Range Extension of Previous Day Range
# tomsk
# 1.11.2020

# V1.0 - 01.11.2020 - tomsk - Range Extension of Previous Day Range
# V1.1 - 01.11.2020 - tomsk - Added plot of previous day range midpoint
# V1.2 - 01.11.2020 - tomsk - Added chart bubbles to identify all plot lines

# This study plots the 25% and 50% extension of the previous day's range
# To be used on intraday charts only

declare hide_on_daily;

input n = 3;

def bar = barNumber();
def CurrentDay = GetDay() == GetLastDay();
def prevHigh = if CurrentDay then high(period = AggregationPeriod.Day)[1] else Double.NaN;
def prevLow = if CurrentDay then low(period = AggregationPeriod.Day)[1] else Double.NaN;
def prevRange = if CurrentDay then prevHigh - prevLow else Double.NaN;

plot UExt25 = prevHigh + (prevRange * 0.25);
plot UExt50 = prevHigh + (prevRange * 0.50);
plot LExt25 = prevLow  - (prevRange * 0.25);
plot LExt50 = prevLow  - (prevRange * 0.50);
plot PrevMid = prevLow + (prevRange / 2);

UExt25.SetDefaultColor(Color.Cyan);
UExt50.SetDefaultColor(Color.Pink);
LExt25.SetDefaultColor(Color.White);
LExt50.SetDefaultColor(Color.Green);

PrevMid.SetDefaultColor(Color.Yellow);
PrevMid.SetStyle(Curve.Short_Dash);
PrevMid.SetLineWeight(3);

def lastBar = if isNaN(close[-1]) and !isNaN(close) then bar else lastBar[1];
def UExt25Loc = if isNaN(close[-1]) and !isNaN(close) then UExt25 else UExt25Loc[1];
def UExt50Loc = if isNaN(close[-1]) and !isNaN(close) then UExt50 else UExt50Loc[1];
def PrevMidLoc = if isNaN(close[-1]) and !isNaN(close) then PrevMid else PrevMidLoc[1];
def LExt25Loc = if isNaN(close[-1]) and !isNaN(close) then LExt25 else LExt25Loc[1];
def LExt50Loc = if isNaN(close[-1]) and !isNaN(close) then LExt50 else LExt50Loc[1];

addChartBubble(bar == HighestAll(lastBar+n), UExt25Loc, "Upper 25%", UExt25.TakeValueColor());
addChartBubble(bar == HighestAll(lastBar+n), UExt50Loc, "Upper 50%", UExt50.TakeValueColor());
addChartBubble(bar == HighestAll(lastBar+n), PrevMidLoc, "Prev Mid", PrevMid.TakeValueColor());
addChartBubble(bar == HighestAll(lastBar+n), LExt25Loc, "Lower 25%", LExt25.TakeValueColor());
addChartBubble(bar == HighestAll(lastBar+n), LExt50Loc, "Lower 50%", LExt50.TakeValueColor());
# End Range Extension of Previous Day Range
 
@Trading51 Adding chart bubbles is a bit more involved than that. Since you asked, here then is Version 1.2 of the Range Extension of Previous Day Range study COMPLETE with chart bubbles added to all 5 plots. Have at it ...

Code:
# Range Extension of Previous Day Range
# tomsk
# 1.11.2020

# V1.0 - 01.11.2020 - tomsk - Range Extension of Previous Day Range
# V1.1 - 01.11.2020 - tomsk - Added plot of previous day range midpoint
# V1.2 - 01.11.2020 - tomsk - Added chart bubbles to identify all plot lines

# This study plots the 25% and 50% extension of the previous day's range
# To be used on intraday charts only

declare hide_on_daily;

input n = 3;

def bar = barNumber();
def CurrentDay = GetDay() == GetLastDay();
def prevHigh = if CurrentDay then high(period = AggregationPeriod.Day)[1] else Double.NaN;
def prevLow = if CurrentDay then low(period = AggregationPeriod.Day)[1] else Double.NaN;
def prevRange = if CurrentDay then prevHigh - prevLow else Double.NaN;

plot UExt25 = prevHigh + (prevRange * 0.25);
plot UExt50 = prevHigh + (prevRange * 0.50);
plot LExt25 = prevLow  - (prevRange * 0.25);
plot LExt50 = prevLow  - (prevRange * 0.50);
plot PrevMid = prevLow + (prevRange / 2);

UExt25.SetDefaultColor(Color.Cyan);
UExt50.SetDefaultColor(Color.Pink);
LExt25.SetDefaultColor(Color.White);
LExt50.SetDefaultColor(Color.Green);

PrevMid.SetDefaultColor(Color.Yellow);
PrevMid.SetStyle(Curve.Short_Dash);
PrevMid.SetLineWeight(3);

def lastBar = if isNaN(close[-1]) and !isNaN(close) then bar else lastBar[1];
def UExt25Loc = if isNaN(close[-1]) and !isNaN(close) then UExt25 else UExt25Loc[1];
def UExt50Loc = if isNaN(close[-1]) and !isNaN(close) then UExt50 else UExt50Loc[1];
def PrevMidLoc = if isNaN(close[-1]) and !isNaN(close) then PrevMid else PrevMidLoc[1];
def LExt25Loc = if isNaN(close[-1]) and !isNaN(close) then LExt25 else LExt25Loc[1];
def LExt50Loc = if isNaN(close[-1]) and !isNaN(close) then LExt50 else LExt50Loc[1];

addChartBubble(bar == HighestAll(lastBar+n), UExt25Loc, "Upper 25%", UExt25.TakeValueColor());
addChartBubble(bar == HighestAll(lastBar+n), UExt50Loc, "Upper 50%", UExt50.TakeValueColor());
addChartBubble(bar == HighestAll(lastBar+n), PrevMidLoc, "Prev Mid", PrevMid.TakeValueColor());
addChartBubble(bar == HighestAll(lastBar+n), LExt25Loc, "Lower 25%", LExt25.TakeValueColor());
addChartBubble(bar == HighestAll(lastBar+n), LExt50Loc, "Lower 50%", LExt50.TakeValueColor());
# End Range Extension of Previous Day Range
I was way off, thanks :)
 
@tomsk could you add the option to add the previous high and low with the labels?


@Trading51 Sure thing, here's Version 1.3 of the code with the previous day's high/low added

Code:
# Range Extension of Previous Day Range
# tomsk
# 1.12.2020

# https://usethinkscript.com/threads/plot-25-and-50-of-previous-day%E2%80%99s-range.1472/#post-13666

# V1.0 - 01.11.2020 - tomsk - Range Extension of Previous Day Range
# V1.1 - 01.11.2020 - tomsk - Added plot of previous day range midpoint
# V1.2 - 01.11.2020 - tomsk - Added chart bubbles to identify all plot lines
# V1.3 - 01.12.2020 - tomsk - Added labels for previous day's high/low

# This study plots the 25% and 50% extension of the previous day's range
# To be used on intraday charts only

declare hide_on_daily;

input n = 3;

def bar = barNumber();
def CurrentDay = GetDay() == GetLastDay();
def prevHigh = if CurrentDay then high(period = AggregationPeriod.Day)[1] else Double.NaN;
def prevLow = if CurrentDay then low(period = AggregationPeriod.Day)[1] else Double.NaN;
def prevRange = if CurrentDay then prevHigh - prevLow else Double.NaN;

plot UExt25 = prevHigh + (prevRange * 0.25);
plot UExt50 = prevHigh + (prevRange * 0.50);
plot LExt25 = prevLow  - (prevRange * 0.25);
plot LExt50 = prevLow  - (prevRange * 0.50);
plot PrevMid = prevLow + (prevRange / 2);

UExt25.SetDefaultColor(Color.Cyan);
UExt50.SetDefaultColor(Color.Pink);
LExt25.SetDefaultColor(Color.White);
LExt50.SetDefaultColor(Color.Green);

PrevMid.SetDefaultColor(Color.Yellow);
PrevMid.SetStyle(Curve.Short_Dash);
PrevMid.SetLineWeight(3);

AddLabel(1, "Previous Day's High = " + prevHigh, Color.Yellow);
AddLabel(1, "Previous Day's Low = " + prevLow, Color.Pink);

def lastBar = if isNaN(close[-1]) and !isNaN(close) then bar else lastBar[1];
def UExt25Loc = if isNaN(close[-1]) and !isNaN(close) then UExt25 else UExt25Loc[1];
def UExt50Loc = if isNaN(close[-1]) and !isNaN(close) then UExt50 else UExt50Loc[1];
def PrevMidLoc = if isNaN(close[-1]) and !isNaN(close) then PrevMid else PrevMidLoc[1];
def LExt25Loc = if isNaN(close[-1]) and !isNaN(close) then LExt25 else LExt25Loc[1];
def LExt50Loc = if isNaN(close[-1]) and !isNaN(close) then LExt50 else LExt50Loc[1];

addChartBubble(bar == HighestAll(lastBar+n), UExt25Loc, "Upper 25%", UExt25.TakeValueColor());
addChartBubble(bar == HighestAll(lastBar+n), UExt50Loc, "Upper 50%", UExt50.TakeValueColor());
addChartBubble(bar == HighestAll(lastBar+n), PrevMidLoc, "Prev Mid", PrevMid.TakeValueColor());
addChartBubble(bar == HighestAll(lastBar+n), LExt25Loc, "Lower 25%", LExt25.TakeValueColor());
addChartBubble(bar == HighestAll(lastBar+n), LExt50Loc, "Lower 50%", LExt50.TakeValueColor());
# End Range Extension of Previous Day Range
 
the labels you added are fine, I meant could you add the previous high and low lines with labels that go with the ones on the charts? sorry for the confusion :)
 
@Trading51 Per your enhanced request, I have made the changes you requested in the revised version 1.3. Additionally to give you an added option, I have included an input "displayLabel" that you can set to "no" in the user interface should you no longer wish to see the labels. Here then is the revised code. Hope this helps

Code:
# Range Extension of Previous Day Range
# tomsk
# 1.12.2020

# https://usethinkscript.com/threads/plot-25-and-50-of-previous-day%E2%80%99s-range.1472/#post-13757

# V1.0 - 01.11.2020 - tomsk - Range Extension of Previous Day Range
# V1.1 - 01.11.2020 - tomsk - Added plot of previous day range midpoint
# V1.2 - 01.11.2020 - tomsk - Added chart bubbles to identify all plot lines
# V1.3 - 01.12.2020 - tomsk - Added labels and plot lines for previous day's high/low

# This study plots the 25% and 50% extension of the previous day's range
# To be used on intraday charts only

declare hide_on_daily;

input n = 3;
input displayLabel = yes;

def bar = barNumber();
def CurrentDay = GetDay() == GetLastDay();
def prevHigh = if CurrentDay then high(period = AggregationPeriod.Day)[1] else Double.NaN;
def prevLow = if CurrentDay then low(period = AggregationPeriod.Day)[1] else Double.NaN;
def prevRange = if CurrentDay then prevHigh - prevLow else Double.NaN;

plot UExt25 = prevHigh + (prevRange * 0.25);
plot UExt50 = prevHigh + (prevRange * 0.50);
plot LExt25 = prevLow  - (prevRange * 0.25);
plot LExt50 = prevLow  - (prevRange * 0.50);

plot PrevHigh_ = prevHigh;
plot PrevLow_ = prevLow;
plot PrevMid = prevLow + (prevRange / 2);

UExt25.SetDefaultColor(Color.Cyan);
UExt50.SetDefaultColor(Color.Pink);
LExt25.SetDefaultColor(Color.White);
LExt50.SetDefaultColor(Color.Green);

PrevHigh_.SetDefaultColor(Color.Green);
PrevHigh_.SetStyle(Curve.Short_Dash);
PrevHigh_.SetLineWeight(3);
PrevLow_.SetDefaultColor(Color.Red);
PrevLow_.SetStyle(Curve.Short_Dash);
PrevLow_.SetLineWeight(3);
PrevMid.SetDefaultColor(Color.Yellow);
PrevMid.SetStyle(Curve.Short_Dash);
PrevMid.SetLineWeight(3);

AddLabel(displayLabel, "Previous Day's High = " + prevHigh, Color.Yellow);
AddLabel(displayLabel, "Previous Day's Low = " + prevLow, Color.Pink);

def lastBar = if isNaN(close[-1]) and !isNaN(close) then bar else lastBar[1];
def UExt25Loc = if isNaN(close[-1]) and !isNaN(close) then UExt25 else UExt25Loc[1];
def UExt50Loc = if isNaN(close[-1]) and !isNaN(close) then UExt50 else UExt50Loc[1];
def PrevHighLoc = if isNaN(close[-1]) and !isNaN(close) then PrevHigh else PrevHighLoc[1];
def PrevLowLoc = if isNaN(close[-1]) and !isNaN(close) then PrevLow else PrevLowLoc[1];
def PrevMidLoc = if isNaN(close[-1]) and !isNaN(close) then PrevMid else PrevMidLoc[1];
def LExt25Loc = if isNaN(close[-1]) and !isNaN(close) then LExt25 else LExt25Loc[1];
def LExt50Loc = if isNaN(close[-1]) and !isNaN(close) then LExt50 else LExt50Loc[1];

addChartBubble(bar == HighestAll(lastBar+n), UExt25Loc, "Upper 25%", UExt25.TakeValueColor());
addChartBubble(bar == HighestAll(lastBar+n), UExt50Loc, "Upper 50%", UExt50.TakeValueColor());
addChartBubble(bar == HighestAll(lastBar+n), PrevHighLoc, "Prev Day High", PrevHigh_.TakeValueColor());
addChartBubble(bar == HighestAll(lastBar+n), PrevLowLoc, "Prev Day Low", PrevLow_.TakeValueColor());
addChartBubble(bar == HighestAll(lastBar+n), PrevMidLoc, "Prev Mid", PrevMid.TakeValueColor());
addChartBubble(bar == HighestAll(lastBar+n), LExt25Loc, "Lower 25%", LExt25.TakeValueColor());
addChartBubble(bar == HighestAll(lastBar+n), LExt50Loc, "Lower 50%", LExt50.TakeValueColor());
# End Range Extension of Previous Day Range
 
Last edited:
While the intraday high/low two days ago can easily be determined, logistically balancing the display between the previous day and the previous previous day is going to induce a layer of complexity. My suggestion is to just keep things simple and stick with the last version posted. If you still wish to reference the high/lows 2 days ago, all you need is to index it as follows. Personally I see no value add there.

Code:
def prevPrevHigh = if CurrentDay then high(period = AggregationPeriod.Day)[2] else Double.NaN;
def prevPrevLow = if CurrentDay then low(period = AggregationPeriod.Day)[2] else Double.NaN;
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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