I'm looking for:
Previous day high and low for the last 10 days script with lines on the chart.
Any suggestions
Code:#Example_Previous_Days_HL_for_show_x_previous_days # script hl { input lookback = 0; def n = lookback; def na = Double.NaN; def h = high; def l = low; def bn = BarNumber(); def ymd = GetYYYYMMDD(); def capture = !IsNaN(close) and ymd != ymd[1]; def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0); def thisDay...
I'm looking for:
Previous day high and low for the last 10 days script with lines on the chart.
Any suggestions
Code:#Example_Previous_Days_HL_for_show_x_previous_days # script hl { input lookback = 0; def n = lookback; def na = Double.NaN; def h = high; def l = low; def bn = BarNumber(); def ymd = GetYYYYMMDD(); def capture = !IsNaN(close) and ymd != ymd[1]; def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0); def thisDay = (HighestAll(dayCount) - dayCount) ; def hh = if thisDay == n and thisDay[1] == n + 1 then h else if thisDay == n and h > hh[1] then h else hh[1]; def hhigh = if thisDay == n and h == hh then BarNumber() else na; def hhnan = if IsNaN(hh) then hhnan[1] else hh; plot hhplot = if BarNumber() >= HighestAll(hhigh) then (hhnan) else na; def ll = if thisDay == n and thisDay[1] == n + 1 then l else if thisDay == n and l < ll[1] then l else ll[1]; def llow = if thisDay == n and l == ll then BarNumber() else na; def llnan = if IsNaN(ll) then llnan[1] else ll; plot llplot = if BarNumber() >= HighestAll(llow) then (llnan) else na; } input show_x_previous = 10; input show_today = no; def na = Double.NaN; plot H0 = if !show_today then na else hl(0).hhplot; plot L0 = if !show_today then na else hl(0).llplot; plot H1 = if show_x_previous < 1 then na else hl(1).hhplot; plot L1 = if show_x_previous < 1 then na else hl(1).llplot; plot H2 = if show_x_previous < 2 then na else hl(2).hhplot; plot L2 = if show_x_previous < 2 then na else hl(2).llplot; plot H3 = if show_x_previous < 3 then na else hl(3).hhplot; plot L3 = if show_x_previous < 3 then na else hl(3).llplot; plot H4 = if show_x_previous < 4 then na else hl(4).hhplot; plot L4 = if show_x_previous < 4 then na else hl(4).llplot; plot H5 = if show_x_previous < 5 then na else hl(5).hhplot; plot L5 = if show_x_previous < 5 then na else hl(5).llplot; plot H6 = if show_x_previous < 6 then na else hl(6).hhplot; plot L6 = if show_x_previous < 6 then na else hl(6).llplot; plot H7 = if show_x_previous < 7 then na else hl(7).hhplot; plot L7 = if show_x_previous < 7 then na else hl(7).llplot; plot H8 = if show_x_previous < 8 then na else hl(8).hhplot; plot L8 = if show_x_previous < 8 then na else hl(8).llplot; plot H9 = if show_x_previous < 9 then na else hl(9).hhplot; plot L9 = if show_x_previous < 9 then na else hl(9).llplot; plot H10 = if show_x_previous < 10 then na else hl(10).hhplot; plot L10 = if show_x_previous < 10 then na else hl(10).llplot; DefineGlobalColor("H", Color.GREEN); DefineGlobalColor("L", Color.RED); H0.SetDefaultColor(GlobalColor("H")); L0.SetDefaultColor(GlobalColor("L")); H1.SetDefaultColor(GlobalColor("H")); L1.SetDefaultColor(GlobalColor("L")); H2.SetDefaultColor(GlobalColor("H")); L2.SetDefaultColor(GlobalColor("L")); H3.SetDefaultColor(GlobalColor("H")); L3.SetDefaultColor(GlobalColor("L")); H4.SetDefaultColor(GlobalColor("H")); L4.SetDefaultColor(GlobalColor("L")); H5.SetDefaultColor(GlobalColor("H")); L5.SetDefaultColor(GlobalColor("L")); H6.SetDefaultColor(GlobalColor("H")); L6.SetDefaultColor(GlobalColor("L")); H7.SetDefaultColor(GlobalColor("H")); L7.SetDefaultColor(GlobalColor("L")); H8.SetDefaultColor(GlobalColor("H")); L8.SetDefaultColor(GlobalColor("L")); H9.SetDefaultColor(GlobalColor("H")); L9.SetDefaultColor(GlobalColor("L")); H10.SetDefaultColor(GlobalColor("H")); L10.SetDefaultColor(GlobalColor("L")); input showbubbles = yes; input bubblemover = 3; def bn = BarNumber(); def mover = showbubbles and bn == HighestAll(bn - bubblemover); AddChartBubble(mover , H0, "H0", H0.TakeValueColor()); AddChartBubble(mover , H1, "H1", H1.TakeValueColor()); AddChartBubble(mover , H2, "H2", H2.TakeValueColor()); AddChartBubble(mover , H3, "H3", H3.TakeValueColor()); AddChartBubble(mover , H4, "H4", H4.TakeValueColor()); AddChartBubble(mover , H5, "H5", H5.TakeValueColor()); AddChartBubble(mover , H6, "H6", H6.TakeValueColor()); AddChartBubble(mover , H7, "H7", H7.TakeValueColor()); AddChartBubble(mover , H8, "H8", H8.TakeValueColor()); AddChartBubble(mover , H9, "H9", H9.TakeValueColor()); AddChartBubble(mover , H10, "H10", H10.TakeValueColor()); AddChartBubble(mover , L0, "L0", L0.TakeValueColor()); AddChartBubble(mover , L1, "L1", L1.TakeValueColor()); AddChartBubble(mover , L2, "L2", L2.TakeValueColor()); AddChartBubble(mover , L3, "L3", L3.TakeValueColor()); AddChartBubble(mover , L4, "L4", L4.TakeValueColor()); AddChartBubble(mover , L5, "L5", L5.TakeValueColor()); AddChartBubble(mover , L6, "L6", L6.TakeValueColor()); AddChartBubble(mover , L7, "L7", L7.TakeValueColor()); AddChartBubble(mover , L8, "L8", L8.TakeValueColor()); AddChartBubble(mover , L9, "L9", L9.TakeValueColor()); AddChartBubble(mover , L10, "L10", L10.TakeValueColor()); #
This will plot up to 10 previous day's highs/lows.
You can limit how many of these that you want to use at input show_x_previous
Image shows 6 of 10 based upon input, for example
Code:#Example_Previous_Days_HL_for_show_x_previous_days # script hl { input method = {default TradingDays, StockTradingHours}; input lookback = 1; def n = lookback; def na = Double.NaN; def h = high; def l = low; def bn = BarNumber(); def ymd = if TickValue() != .01 then GetYYYYMMDD() else GetDay(); def rth = if method == method.StockTradingHours then SecondsFromTime(0930) >= 0 and SecondsFromTime(1600) < 0 else ymd; def capture = !IsNaN(close) and ymd != ymd[1]; def dayCount = CompoundValue(1, if capture and GetDayOfWeek(GetYYYYMMDD()) != 7 then dayCount[1] + 1 else dayCount[1], 0); def thisDay = (HighestAll(dayCount) - (dayCount)) ; def hh = if bn == 1 then 0 else if (thisDay == n and thisDay[1] == n + 1) and rth then h else if thisDay == n and rth and h > hh[1] then h else hh[1]; def hhigh = if thisDay == n and rth and h == hh then bn else na; def hhnan = if IsNaN(hh) then hhnan[1] else hh; plot hhplot = if BarNumber() >= HighestAll(hhigh) then (hhnan) else na; def ll = if bn == 1 then 99999 else if thisDay == n and thisDay[1] == n + 1 and rth then l else if thisDay == n and rth and l < ll[1] then l else ll[1]; def llow = if thisDay == n and rth and l == ll then bn else na; def llnan = if IsNaN(ll) then llnan[1] else ll; plot llplot = if BarNumber() >= HighestAll(llow) then (llnan) else na; } input method = {default TradingDays, StockTradingHours}; input show_x_previous = 10; input show_today = no; input verticalline = yes; def na = Double.NaN; def rth = SecondsFromTime(0930) >= 0 and SecondsFromTime(1600) < 0; def ymd = if method == method.TradingDays and TickValue() != .01 then GetYYYYMMDD() else GetDay(); AddVerticalLine(verticalline and GetDayOfWeek(GetYYYYMMDD()) != 7 and if method == method.StockTradingHours then rth != rth[1] else ymd != ymd[1], ""); plot H0 = if !show_today then na else hl(method, 0).hhplot; plot L0 = if !show_today then na else hl(method, 0).llplot; plot H1 = if show_x_previous < 1 then na else hl(method, 1).hhplot; plot L1 = if show_x_previous < 1 then na else hl(method, 1).llplot; plot H2 = if show_x_previous < 2 then na else hl(method, 2).hhplot; plot L2 = if show_x_previous < 2 then na else hl(method, 2).llplot; plot H3 = if show_x_previous < 3 then na else hl(method, 3).hhplot; plot L3 = if show_x_previous < 3 then na else hl(method, 3).llplot; plot H4 = if show_x_previous < 4 then na else hl(method, 4).hhplot; plot L4 = if show_x_previous < 4 then na else hl(method, 4).llplot; plot H5 = if show_x_previous < 5 then na else hl(method, 5).hhplot; plot L5 = if show_x_previous < 5 then na else hl(method, 5).llplot; plot H6 = if show_x_previous < 6 then na else hl(method, 6).hhplot; plot L6 = if show_x_previous < 6 then na else hl(method, 6).llplot; plot H7 = if show_x_previous < 7 then na else hl(method, 7).hhplot; plot L7 = if show_x_previous < 7 then na else hl(method, 7).llplot; plot H8 = if show_x_previous < 8 then na else hl(method, 8).hhplot; plot L8 = if show_x_previous < 8 then na else hl(method, 8).llplot; plot H9 = if show_x_previous < 9 then na else hl(method, 9).hhplot; plot L9 = if show_x_previous < 9 then na else hl(method, (9)).llplot; plot H10 = if show_x_previous < 10 then na else hl(method, 10).hhplot; plot L10 = if show_x_previous < 10 then na else hl(method, 10).llplot; DefineGlobalColor("H", Color.GREEN); DefineGlobalColor("L", Color.RED); H0.SetDefaultColor(GlobalColor("H")); L0.SetDefaultColor(GlobalColor("L")); H1.SetDefaultColor(GlobalColor("H")); L1.SetDefaultColor(GlobalColor("L")); H2.SetDefaultColor(GlobalColor("H")); L2.SetDefaultColor(GlobalColor("L")); H3.SetDefaultColor(GlobalColor("H")); L3.SetDefaultColor(GlobalColor("L")); H4.SetDefaultColor(GlobalColor("H")); L4.SetDefaultColor(GlobalColor("L")); H5.SetDefaultColor(GlobalColor("H")); L5.SetDefaultColor(GlobalColor("L")); H6.SetDefaultColor(GlobalColor("H")); L6.SetDefaultColor(GlobalColor("L")); H7.SetDefaultColor(GlobalColor("H")); L7.SetDefaultColor(GlobalColor("L")); H8.SetDefaultColor(GlobalColor("H")); L8.SetDefaultColor(GlobalColor("L")); H9.SetDefaultColor(GlobalColor("H")); L9.SetDefaultColor(GlobalColor("L")); H10.SetDefaultColor(GlobalColor("H")); L10.SetDefaultColor(GlobalColor("L")); input showbubbles = yes; input bubblemover = 3; def bn = BarNumber(); def mover = showbubbles and bn == HighestAll(bn - bubblemover); AddChartBubble(mover , H0, "H0", H0.TakeValueColor()); AddChartBubble(mover , H1, "H1", H1.TakeValueColor()); AddChartBubble(mover , H2, "H2", H2.TakeValueColor()); AddChartBubble(mover , H3, "H3", H3.TakeValueColor()); AddChartBubble(mover , H4, "H4", H4.TakeValueColor()); AddChartBubble(mover , H5, "H5", H5.TakeValueColor()); AddChartBubble(mover , H6, "H6", H6.TakeValueColor()); AddChartBubble(mover , H7, "H7", H7.TakeValueColor()); AddChartBubble(mover , H8, "H8", H8.TakeValueColor()); AddChartBubble(mover , H9, "H9", H9.TakeValueColor()); AddChartBubble(mover , H10, "H10", H10.TakeValueColor()); AddChartBubble(mover , L0, "L0", L0.TakeValueColor()); AddChartBubble(mover , L1, "L1", L1.TakeValueColor()); AddChartBubble(mover , L2, "L2", L2.TakeValueColor()); AddChartBubble(mover , L3, "L3", L3.TakeValueColor()); AddChartBubble(mover , L4, "L4", L4.TakeValueColor()); AddChartBubble(mover , L5, "L5", L5.TakeValueColor()); AddChartBubble(mover , L6, "L6", L6.TakeValueColor()); AddChartBubble(mover , L7, "L7", L7.TakeValueColor()); AddChartBubble(mover , L8, "L8", L8.TakeValueColor()); AddChartBubble(mover , L9, "L9", L9.TakeValueColor()); AddChartBubble(mover , L10, "L10", L10.TakeValueColor()); #
The above code only used the getyyyymmdd() function to define DAY
I have improved the above version to automatically change the Tradingdays method selection to use for futures, the getyyyymdd() function, and for stocks, the getday() function
There is now an option added to only use the StockTradingHours of 0930-1600.
# Previous Day High/Low/Close + Premarket High/Low + High/Low/Open of Day + ATR Lines
# Created by Wiinii
# V1.6
# Some code based on code by Mobius (premarket) and TraderKevin (ATR lines
# https://usethinkscript.com/threads/...w-high-low-open-of-day-for-thinkorswim.13139/
declare hide_on_daily;
input length = 1;
input showOnlyLastPeriod = yes;
input ShowBubbles = yes;
input locate_bubbles_at = {default Expansion, Time};
input locate_bubbles_at_time = 800;
input BarsFromExpansion = 1;
input ShowPricesInBubbles = yes;
def bn = BarNumber();
def na = Double.NaN;
def h = high;
def l = low;
def o = open;
def c = close;
def v = volume;
def aggregationPeriod = AggregationPeriod.DAY;
def displace = -1;
def timeopen = SecondsFromTime(locate_bubbles_at_time) == 0;
def isExpansion = locate_bubbles_at == locate_bubbles_at.Expansion and IsNaN(close);
def firstExpansionBar = if !IsNaN(close[-1]) and isExpansion then 1 else if isExpansion then firstExpansionBar[1] + 1 else 0;
def BubbleLocation = if locate_bubbles_at == locate_bubbles_at.Time then timeopen else isExpansion and firstExpansionBar == BarsFromExpansion;
#----- Previous Day High/Low/Close -----#
plot PD_High;
plot PD_Low;
plot PD_Close;
if showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]) {
PD_High = na;
PD_Low = na;
PD_Close = na;
} else {
PD_High = Highest(high(period = aggregationPeriod)[-displace], length);
PD_Low = Lowest(low(period = aggregationPeriod)[-displace], length);
PD_Close = close(period = aggregationPeriod)[-displace];
}
PD_High.SetDefaultColor(Color.DARK_ORANGE);
PD_High.SetStyle(Curve.LONG_DASH);
PD_High.SetLineWeight(2);
PD_High.HideTitle();
PD_Low.SetDefaultColor(Color.DARK_ORANGE);
PD_Low.SetStyle(Curve.LONG_DASH);
PD_Low.SetLineWeight(2);
PD_Low.HideTitle();
PD_Close.SetDefaultColor(Color.WHITE);
PD_Close.SetStyle(Curve.LONG_DASH);
PD_Close.SetLineWeight(2);
PD_Close.HideTitle();
DefineGlobalColor("PD_High", CreateColor(255, 204, 153));
AddChartBubble(ShowBubbles and BubbleLocation, PD_High, "PDH: " + (if ShowPricesInBubbles then AsText(PD_High) else ""), GlobalColor("PD_High"));
DefineGlobalColor("PD_Low", CreateColor(255, 204, 153));
AddChartBubble(ShowBubbles and BubbleLocation, PD_Low, "PDL: " + (if ShowPricesInBubbles then AsText(PD_Low) else ""), GlobalColor("PD_Low"), no);
DefineGlobalColor("PD_Close", Color.WHITE);
AddChartBubble(ShowBubbles and BubbleLocation, PD_Close, "PDC: " + (if ShowPricesInBubbles then AsText(PD_Close) else ""), GlobalColor("PD_Close"));
PD_High.HideBubble();
PD_Low.HideBubble();
PD_Close.HideBubble();
#----- Premarket High/Low -----# Thanks to Mobius
def GlobeX = GetTime() < RegularTradingStart(GetYYYYMMDD());
def vol = if GlobeX and !GlobeX[1]
then v
else if GlobeX
then vol[1] + v
else na;
def GlobeX_Volume = vol;
def ONhigh = if GlobeX and !GlobeX[1]
then h
else if GlobeX and
h > ONhigh[1]
then h
else ONhigh[1];
def ONhighBar = if GlobeX and h == ONhigh
then bn
else na;
def ONlow = if GlobeX and !GlobeX[1]
then l
else if GlobeX and
l < ONlow[1]
then l
else ONlow[1];
def ONlowBar = if GlobeX and l == ONlow
then bn
else na;
def OverNightHigh = if BarNumber() == HighestAll(ONhighBar)
then ONhigh
else OverNightHigh[1];
def OverNightLow = if BarNumber() == HighestAll(ONlowBar)
then ONlow
else OverNightLow[1];
plot PM_High;
plot PM_Low;
if showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]) {
PM_High = na;
PM_Low = na;
} else {
PM_High = if OverNightHigh > 0 then OverNightHigh else na;
PM_Low = if OverNightLow > 0 then OverNightLow else na;
}
#PM_High.SetHiding(!PlotOverNightExtremes);
PM_High.SetLineWeight(2);
PM_High.SetDefaultColor(Color.CYAN);
PM_High.SetStyle(Curve.LONG_DASH);
PM_High.HideBubble();
PM_High.HideTitle();
#PM_Low.SetHiding(!PlotOverNightExtremes);
PM_Low.SetStyle(Curve.LONG_DASH);
PM_Low.SetDefaultColor(Color.CYAN);
PM_Low.HideBubble();
PM_Low.HideTitle();
DefineGlobalColor("PM_High", CreateColor(102, 255, 255));
AddChartBubble(ShowBubbles and if locate_bubbles_at == locate_bubbles_at.Time then bn == ONhighBar else isExpansion and firstExpansionBar == BarsFromExpansion, PM_High, "PMH: " + (if ShowPricesInBubbles then AsText(PM_High) else ""), GlobalColor("PM_High"));
DefineGlobalColor("PM_Low", CreateColor(102, 255, 255));
AddChartBubble(ShowBubbles and if locate_bubbles_at == locate_bubbles_at.Time then bn == ONlowBar else isExpansion and firstExpansionBar == BarsFromExpansion, PM_Low, "PML: " + (if ShowPricesInBubbles then AsText(PM_Low) else ""), GlobalColor("PM_Low"), no);
#----- Today Open/High/Low -----#
plot High_of_Day;
plot Low_of_Day;
plot DayOpen;
if showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]) {
DayOpen = na;
High_of_Day = na;
Low_of_Day = na;
} else {
DayOpen = open(period = aggregationPeriod)[0];
High_of_Day = Highest(high(period = aggregationPeriod), length);
Low_of_Day = Lowest(low(period = aggregationPeriod), length);
}
DayOpen.SetDefaultColor (Color.GRAY);
DayOpen.SetPaintingStrategy(PaintingStrategy.DASHES);
DayOpen.SetLineWeight(2);
DayOpen.HideTitle();
High_of_Day.SetDefaultColor(CreateColor(0, 128, 255));
High_of_Day.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
High_of_Day.SetLineWeight(2);
High_of_Day.HideTitle();
Low_of_Day.SetDefaultColor(CreateColor(0, 128, 255));
Low_of_Day.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Low_of_Day.SetLineWeight(2);
Low_of_Day.HideTitle();
DefineGlobalColor("Open", Color.LIGHT_GRAY);
AddChartBubble(ShowBubbles and BubbleLocation, DayOpen, "Open: " + (if ShowPricesInBubbles then AsText(DayOpen) else ""), GlobalColor("Open"));
DefineGlobalColor("High_of_Day", CreateColor(102, 178, 255));
AddChartBubble(ShowBubbles and BubbleLocation, High_of_Day, "HOD: " + (if ShowPricesInBubbles then AsText(High_of_Day) else ""), GlobalColor("High_of_Day"));
DefineGlobalColor("Low_of_Day", CreateColor(102, 178, 255));
AddChartBubble(ShowBubbles and BubbleLocation, Low_of_Day, "LOD: " + (if ShowPricesInBubbles then AsText(Low_of_Day) else ""), GlobalColor("Low_of_Day"), no);
#----- ATR Lines -----# Thaanks to TraderKevin
input showAtrLines = Yes;
input atrLinesFrom = {default pdc, dayOpen};
input ATRlength = 14;
input averageType = AverageType.WILDERS;
def ATR = MovingAverage(averageType, TrueRange(high(period = ”DAY”)[1], close(period = ”DAY”)[1], low(period = ”DAY”)[1]), ATRlength);
plot hatr = if atrLinesFrom == atrLinesFrom .dayOpen then DayOpen + ATR else PD_Close + ATR;
plot latr = if atrLinesFrom == atrLinesFrom .dayOpen then DayOpen - ATR else PD_Close - ATR;
hatr.SetLineWeight(5);
hatr.SetDefaultColor(Color.RED);
hatr.SetStyle(Curve.LONG_DASH);
hatr.HideBubble();
hatr.HideTitle();
hatr.SetHiding(showAtrLines == no);
latr.SetLineWeight(5);
latr.SetStyle(Curve.LONG_DASH);
latr.SetDefaultColor(Color.GREEN);
latr.HideBubble();
latr.HideTitle();
latr.SetHiding(showAtrLines == no);
DefineGlobalColor("hatr", Color.PINK);
AddChartBubble(ShowBubbles and showAtrLines and BubbleLocation, hatr, "ATRH: " + (if ShowPricesInBubbles then AsText(hatr) else ""), GlobalColor("hatr"));
DefineGlobalColor("latr", Color.LIGHT_GREEN);
AddChartBubble(ShowBubbles and showAtrLines and BubbleLocation, latr, "ATRL: " + (if ShowPricesInBubbles then AsText(latr) else ""), GlobalColor("latr"), no);
Join useThinkScript to post your question to a community of 21,000+ developers and traders.
Start a new thread and receive assistance from our community.
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.
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.