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

T

Trading51

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

 
T

tomsk

Well-known member
VIP
@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
 
T

Trading51

Active member
2019 Donor
perfect thank you, is it possible to add the previous day's midpoint?
 
T

tomsk

Well-known member
VIP
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
 
T

Trading51

Active member
2019 Donor
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?
 
T

tomsk

Well-known member
VIP
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
 
T

Trading51

Active member
2019 Donor
@tomsk thanks for taking the time and doing this, cheers mate!
 
T

tomsk

Well-known member
VIP
@tomsk thanks for taking the time and doing this, cheers mate!


Glad to help @Trading51 I had the essential framework from a similar study I did for a forum requestor a few days ago and it was a seamless extension to meet your requirements
 
T

Trading51

Active member
2019 Donor
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 :)
 
T

tomsk

Well-known member
VIP
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
 
T

Trading51

Active member
2019 Donor
@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 :)
 
T

Trading51

Active member
2019 Donor
@tomsk could you add the option to add the previous high and low with the labels?
 
T

tomsk

Well-known member
VIP
@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
 
T

Trading51

Active member
2019 Donor
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 :)
 
T

tomsk

Well-known member
VIP
@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:
T

Trading51

Active member
2019 Donor
@tomsk thanks for taking the time have a great week!!
 
T

Trading51

Active member
2019 Donor
@tomsk Could you please add a preference or input that can allow me to click on the previous two days verse the previous day? Thnaks
 
T

tomsk

Well-known member
VIP
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;
 
T

Trading51

Active member
2019 Donor
@tomsk question if I wanted to change this code to the current day verse the previous day is this all I would have to do is remove the area where its says "Prev" ?
 
J

john3

Active member
2019 Donor
Is it possible to make it plot the history of prior days? Currently, it only plots the levels from one day ago. Thank you.
 

Similar threads

Top