Key levels Indicator

PhckCancer80

New member
I've been trying to build a key levels indicator to help me mark out areas price likes to pivot or retouch such as session high/lows and previous day/week high/lows so far it's looking pretty good but I can't seem to get the AI to get the lines a little longer and to clean up the label placement a little better. I am hopeful someone here may be able to show us what we've been doing wrong in our attempts. The AI said this was our problem, "explain that HORIZONTAL painting strategy creates broken segments and you're looking for a clean way to draw fixed horizontal lines from a specific bar forward."
Or maybe others might find this indicator useful as well and would like to copy and paste it to their studies also, since for the most part it does work properly and looks fairly decent compared to the first couple of tries lol.

Thanks in advance
Code:
# MNQ_Key_Levels — v20.0
# Individual toggles for every session level.
# Session opens AND closes added back.
# All labels right edge only.

declare upper;

input show_prev_day      = yes;
input show_prev_week     = yes;
input show_monday_hl     = yes;
input show_ny_hl         = yes;
input show_lon_hl        = yes;
input show_asia_hl       = yes;
input show_ny_open       = yes;
input show_ny_close      = yes;
input show_lon_open      = yes;
input show_lon_close     = yes;
input show_asia_open     = yes;
input show_asia_close    = yes;
input bubble_pdh_pdl     = yes;
input bubble_pwh_pwl     = yes;
input bubble_monday_hl   = yes;
input bubble_ny_hl       = yes;
input bubble_lon_hl      = yes;
input bubble_asia_hl     = yes;
input bubble_opens       = yes;
input bubble_closes      = yes;

def lastBar    = !IsNaN(close) and IsNaN(close[-1]);
def isToday    = GetDay() == GetLastDay() and GetMonth() == GetLastMonth() and GetYear() == GetLastYear();
def isThisWeek = GetWeek() == GetLastWeek() and GetYear() == GetLastYear();

def ny_open_bar     = SecondsFromTime(0930) == 0;
def ny_close_bar    = SecondsFromTime(1600) == 0;
def lon_open_bar    = SecondsFromTime(0300) == 0;
def lon_close_bar   = SecondsFromTime(0800) == 0;
def asia_open_bar   = SecondsFromTime(1900) == 0;
def asia_close_bar  = SecondsFromTime(0200) == 0;
def in_ny     = SecondsFromTime(0930) >= 0 and SecondsTillTime(1600) > 0;
def in_london = SecondsFromTime(0300) >= 0 and SecondsTillTime(0800) > 0;
def in_asia   = SecondsFromTime(1900) >= 0 or  SecondsTillTime(0200) > 0;

# ---- Previous Day High / Low ----
def pdHigh = high(period = AggregationPeriod.DAY)[1];
def pdLow  = low(period  = AggregationPeriod.DAY)[1];

plot PD_High = if show_prev_day and isToday then pdHigh else Double.NaN;
plot PD_Low  = if show_prev_day and isToday then pdLow  else Double.NaN;
PD_High.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PD_High.SetDefaultColor(Color.RED);
PD_High.SetStyle(Curve.LONG_DASH);
PD_High.SetLineWeight(3);
PD_High.HideBubble();
PD_Low.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PD_Low.SetDefaultColor(Color.GREEN);
PD_Low.SetStyle(Curve.LONG_DASH);
PD_Low.SetLineWeight(3);
PD_Low.HideBubble();
AddChartBubble(bubble_pdh_pdl and show_prev_day and lastBar, pdHigh, "PDH " + Round(pdHigh, 2), Color.RED, yes);
AddChartBubble(bubble_pdh_pdl and show_prev_day and lastBar, pdLow,  "PDL " + Round(pdLow, 2),  Color.GREEN, no);

# ---- Previous Week High / Low ----
def pwHigh    = high(period = AggregationPeriod.WEEK)[1];
def pwLow     = low(period  = AggregationPeriod.WEEK)[1];
def pwHighVal = if IsNaN(pwHigh) then high(period = AggregationPeriod.WEEK)[2] else pwHigh;
def pwLowVal  = if IsNaN(pwLow)  then low(period  = AggregationPeriod.WEEK)[2] else pwLow;

plot PW_High = if show_prev_week and isThisWeek then pwHighVal else Double.NaN;
plot PW_Low  = if show_prev_week and isThisWeek then pwLowVal  else Double.NaN;
PW_High.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PW_High.SetDefaultColor(Color.MAGENTA);
PW_High.SetStyle(Curve.LONG_DASH);
PW_High.SetLineWeight(3);
PW_High.HideBubble();
PW_Low.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PW_Low.SetDefaultColor(Color.CYAN);
PW_Low.SetStyle(Curve.LONG_DASH);
PW_Low.SetLineWeight(3);
PW_Low.HideBubble();
AddChartBubble(bubble_pwh_pwl and show_prev_week and lastBar, pwHighVal, "PWH " + Round(pwHighVal, 2), Color.MAGENTA, yes);
AddChartBubble(bubble_pwh_pwl and show_prev_week and lastBar, pwLowVal,  "PWL " + Round(pwLowVal, 2),  Color.CYAN, no);

# ---- Monday High / Low ----
def isMondayBar = GetDayOfWeek(GetYYYYMMDD()) == 2;
def monHigh = CompoundValue(1, if isMondayBar and high(period = AggregationPeriod.DAY) > 0 then high(period = AggregationPeriod.DAY) else monHigh[1], Double.NaN);
def monLow  = CompoundValue(1, if isMondayBar and low(period  = AggregationPeriod.DAY) > 0 then low(period  = AggregationPeriod.DAY) else monLow[1],  Double.NaN);

plot MON_High = if show_monday_hl and isThisWeek and !IsNaN(monHigh) then monHigh else Double.NaN;
plot MON_Low  = if show_monday_hl and isThisWeek and !IsNaN(monLow)  then monLow  else Double.NaN;
MON_High.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
MON_High.SetDefaultColor(Color.ORANGE);
MON_High.SetStyle(Curve.LONG_DASH);
MON_High.SetLineWeight(2);
MON_High.HideBubble();
MON_Low.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
MON_Low.SetDefaultColor(Color.YELLOW);
MON_Low.SetStyle(Curve.LONG_DASH);
MON_Low.SetLineWeight(2);
MON_Low.HideBubble();
AddChartBubble(bubble_monday_hl and show_monday_hl and lastBar and !IsNaN(monHigh), monHigh, "MON H " + Round(monHigh, 2), Color.ORANGE, yes);
AddChartBubble(bubble_monday_hl and show_monday_hl and lastBar and !IsNaN(monLow),  monLow,  "MON L " + Round(monLow, 2),  Color.YELLOW, no);

# ---- Session Opens ----
def ny_open_p    = CompoundValue(1, if ny_open_bar   then open else ny_open_p[1],    Double.NaN);
def lon_open_p   = CompoundValue(1, if lon_open_bar  then open else lon_open_p[1],   Double.NaN);
def asia_open_p  = CompoundValue(1, if asia_open_bar then open else asia_open_p[1],  Double.NaN);

plot NY_Open   = if show_ny_open   and isToday and !IsNaN(ny_open_p)   then ny_open_p   else Double.NaN;
plot LON_Open  = if show_lon_open  and isToday and !IsNaN(lon_open_p)  then lon_open_p  else Double.NaN;
plot ASIA_Open = if show_asia_open and isToday and !IsNaN(asia_open_p) then asia_open_p else Double.NaN;
NY_Open.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
NY_Open.SetDefaultColor(Color.YELLOW);
NY_Open.SetStyle(Curve.FIRM);
NY_Open.SetLineWeight(2);
NY_Open.HideBubble();
LON_Open.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LON_Open.SetDefaultColor(Color.ORANGE);
LON_Open.SetStyle(Curve.FIRM);
LON_Open.SetLineWeight(2);
LON_Open.HideBubble();
ASIA_Open.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ASIA_Open.SetDefaultColor(Color.GRAY);
ASIA_Open.SetStyle(Curve.FIRM);
ASIA_Open.SetLineWeight(2);
ASIA_Open.HideBubble();
AddChartBubble(bubble_opens and show_ny_open   and lastBar and !IsNaN(ny_open_p),   ny_open_p,   "NY O "   + Round(ny_open_p, 2),   Color.YELLOW, yes);
AddChartBubble(bubble_opens and show_lon_open  and lastBar and !IsNaN(lon_open_p),  lon_open_p,  "LON O "  + Round(lon_open_p, 2),  Color.ORANGE, yes);
AddChartBubble(bubble_opens and show_asia_open and lastBar and !IsNaN(asia_open_p), asia_open_p, "Asia O " + Round(asia_open_p, 2), Color.GRAY,   yes);

# ---- Session Closes ----
def ny_close_p   = CompoundValue(1, if ny_close_bar   then close else ny_close_p[1],   Double.NaN);
def lon_close_p  = CompoundValue(1, if lon_close_bar  then close else lon_close_p[1],  Double.NaN);
def asia_close_p = CompoundValue(1, if asia_close_bar then close else asia_close_p[1], Double.NaN);

plot NY_Close   = if show_ny_close   and isToday and !IsNaN(ny_close_p)   then ny_close_p   else Double.NaN;
plot LON_Close  = if show_lon_close  and isToday and !IsNaN(lon_close_p)  then lon_close_p  else Double.NaN;
plot ASIA_Close = if show_asia_close and isToday and !IsNaN(asia_close_p) then asia_close_p else Double.NaN;
NY_Close.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
NY_Close.SetDefaultColor(Color.YELLOW);
NY_Close.SetStyle(Curve.SHORT_DASH);
NY_Close.SetLineWeight(1);
NY_Close.HideBubble();
LON_Close.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LON_Close.SetDefaultColor(Color.ORANGE);
LON_Close.SetStyle(Curve.SHORT_DASH);
LON_Close.SetLineWeight(1);
LON_Close.HideBubble();
ASIA_Close.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ASIA_Close.SetDefaultColor(Color.GRAY);
ASIA_Close.SetStyle(Curve.SHORT_DASH);
ASIA_Close.SetLineWeight(1);
ASIA_Close.HideBubble();
AddChartBubble(bubble_closes and show_ny_close   and lastBar and !IsNaN(ny_close_p),   ny_close_p,   "NY C "   + Round(ny_close_p, 2),   Color.YELLOW, no);
AddChartBubble(bubble_closes and show_lon_close  and lastBar and !IsNaN(lon_close_p),  lon_close_p,  "LON C "  + Round(lon_close_p, 2),  Color.ORANGE, no);
AddChartBubble(bubble_closes and show_asia_close and lastBar and !IsNaN(asia_close_p), asia_close_p, "Asia C " + Round(asia_close_p, 2), Color.GRAY,   no);

# ---- Session High / Low ----
def ny_run_h   = CompoundValue(1, if ny_open_bar   then high else if in_ny     then Max(ny_run_h[1], high)   else ny_run_h[1],   0);
def ny_run_l   = CompoundValue(1, if ny_open_bar   then low  else if in_ny     then Min(ny_run_l[1], low)    else ny_run_l[1],   0);
def lon_run_h  = CompoundValue(1, if lon_open_bar  then high else if in_london then Max(lon_run_h[1], high)  else lon_run_h[1],  0);
def lon_run_l  = CompoundValue(1, if lon_open_bar  then low  else if in_london then Min(lon_run_l[1], low)   else lon_run_l[1],  0);
def asia_run_h = CompoundValue(1, if asia_open_bar then high else if in_asia   then Max(asia_run_h[1], high) else asia_run_h[1], 0);
def asia_run_l = CompoundValue(1, if asia_open_bar then low  else if in_asia   then Min(asia_run_l[1], low)  else asia_run_l[1], 0);

def prev_ny_h   = CompoundValue(1, if ny_close_bar   then ny_run_h[1]   else prev_ny_h[1],   Double.NaN);
def prev_ny_l   = CompoundValue(1, if ny_close_bar   then ny_run_l[1]   else prev_ny_l[1],   Double.NaN);
def prev_lon_h  = CompoundValue(1, if lon_close_bar  then lon_run_h[1]  else prev_lon_h[1],  Double.NaN);
def prev_lon_l  = CompoundValue(1, if lon_close_bar  then lon_run_l[1]  else prev_lon_l[1],  Double.NaN);
def prev_asia_h = CompoundValue(1, if asia_close_bar then asia_run_h[1] else prev_asia_h[1], Double.NaN);
def prev_asia_l = CompoundValue(1, if asia_close_bar then asia_run_l[1] else prev_asia_l[1], Double.NaN);

plot NY_SH   = if show_ny_hl   and isToday and !IsNaN(prev_ny_h)   then prev_ny_h   else Double.NaN;
plot NY_SL   = if show_ny_hl   and isToday and !IsNaN(prev_ny_l)   then prev_ny_l   else Double.NaN;
plot LON_SH  = if show_lon_hl  and isToday and !IsNaN(prev_lon_h)  then prev_lon_h  else Double.NaN;
plot LON_SL  = if show_lon_hl  and isToday and !IsNaN(prev_lon_l)  then prev_lon_l  else Double.NaN;
plot ASIA_SH = if show_asia_hl and isToday and !IsNaN(prev_asia_h) then prev_asia_h else Double.NaN;
plot ASIA_SL = if show_asia_hl and isToday and !IsNaN(prev_asia_l) then prev_asia_l else Double.NaN;
NY_SH.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
NY_SH.SetDefaultColor(Color.YELLOW);
NY_SH.SetStyle(Curve.LONG_DASH);
NY_SH.SetLineWeight(2);
NY_SH.HideBubble();
NY_SL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
NY_SL.SetDefaultColor(Color.YELLOW);
NY_SL.SetStyle(Curve.LONG_DASH);
NY_SL.SetLineWeight(2);
NY_SL.HideBubble();
LON_SH.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LON_SH.SetDefaultColor(Color.ORANGE);
LON_SH.SetStyle(Curve.LONG_DASH);
LON_SH.SetLineWeight(2);
LON_SH.HideBubble();
LON_SL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LON_SL.SetDefaultColor(Color.ORANGE);
LON_SL.SetStyle(Curve.LONG_DASH);
LON_SL.SetLineWeight(2);
LON_SL.HideBubble();
ASIA_SH.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ASIA_SH.SetDefaultColor(Color.GRAY);
ASIA_SH.SetStyle(Curve.LONG_DASH);
ASIA_SH.SetLineWeight(2);
ASIA_SH.HideBubble();
ASIA_SL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ASIA_SL.SetDefaultColor(Color.GRAY);
ASIA_SL.SetStyle(Curve.LONG_DASH);
ASIA_SL.SetLineWeight(2);
ASIA_SL.HideBubble();
AddChartBubble(bubble_ny_hl   and show_ny_hl   and lastBar and !IsNaN(prev_ny_h),   prev_ny_h,   "NY H "   + Round(prev_ny_h, 2),   Color.YELLOW, yes);
AddChartBubble(bubble_ny_hl   and show_ny_hl   and lastBar and !IsNaN(prev_ny_l),   prev_ny_l,   "NY L "   + Round(prev_ny_l, 2),   Color.YELLOW, no);
AddChartBubble(bubble_lon_hl  and show_lon_hl  and lastBar and !IsNaN(prev_lon_h),  prev_lon_h,  "LON H "  + Round(prev_lon_h, 2),  Color.ORANGE, yes);
AddChartBubble(bubble_lon_hl  and show_lon_hl  and lastBar and !IsNaN(prev_lon_l),  prev_lon_l,  "LON L "  + Round(prev_lon_l, 2),  Color.ORANGE, no);
AddChartBubble(bubble_asia_hl and show_asia_hl and lastBar and !IsNaN(prev_asia_h), prev_asia_h, "Asia H " + Round(prev_asia_h, 2), Color.GRAY,   yes);
AddChartBubble(bubble_asia_hl and show_asia_hl and lastBar and !IsNaN(prev_asia_l), prev_asia_l, "Asia L " + Round(prev_asia_l, 2), Color.GRAY,   no);

# ---- Top labels ----
AddLabel(show_prev_day,   "PDH: " + Round(pdHigh, 2) + "  PDL: " + Round(pdLow, 2), Color.WHITE);
AddLabel(show_prev_week,  "PWH: " + Round(pwHighVal, 2) + "  PWL: " + Round(pwLowVal, 2), Color.WHITE);
AddLabel(show_monday_hl,  "MON H: " + Round(monHigh, 2) + "  MON L: " + Round(monLow, 2), Color.ORANGE);
AddLabel(show_ny_hl,      "NY H: " + Round(prev_ny_h, 2) + "  NY L: " + Round(prev_ny_l, 2), Color.YELLOW);
AddLabel(show_lon_hl,     "LON H: " + Round(prev_lon_h, 2) + "  LON L: " + Round(prev_lon_l, 2), Color.ORANGE);
AddLabel(show_asia_hl,    "Asia H: " + Round(prev_asia_h, 2) + "  Asia L: " + Round(prev_asia_l, 2), Color.GRAY);
 
Last edited by a moderator:

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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