SPX Price Intervals on /ES and SPY For ThinkOrSwim

Glefdar

Active member
EDIT: Latest version including all improvements is here: https://usethinkscript.com/threads/...-es-and-spy-for-thinkorswim.16235/post-130316

I made this earlier today and it's working fine with a couple of caveats:

1) My attempt to estimate what the value of SPX is during extended hours based on its price difference from SPY is not giving the same result as estimating SPX by subtracting the /ES fair value (calculated as the price difference between SPX and /ES at the previous close) from /ES. I believe that at least on an /ES chart it should be fairly accurate during RTH but it would be good if that can be tested more.

2) I tried to write the script so that it would work on tick charts but it seems to not be possible. I don't understand why, but I get an error, from the exclamation point bubble on the upper left of the price chart, stating that secondary periods less than one day aren't allowed on tick charts. But I'm not using periods less than one day, so the error doesn't make sense. I thought this might be due to the "def check_tick_chart_type = if getaggregationperiod()<aggregationPeriod.MIN then 1 else 0;" but I still get the error with that removed, and even with all references to daily aggregation removed (e.g., when I tried using a different method such as "time() crosses above TradingHoursStart" to paraphrase, for getting a stable price reference for calculating ES fair value and SPY price differential from SPX).

(Edit regarding the above point about tick charts: I have since changed the code to improve accuracy and no longer have a switch to check if the current chart is a tick chart, but it shouldn't be making a difference to whether the study will work on a tick chart. It still doesn't work for reasons I don't understand.)

So, this was the best I could do. It might not be totally accurate but it's very close, I think, at least for the ES chart. In intervals of 5 points, the levels and bubbles appear out to +50 points and -50 points from the close rounded to the nearest five.

You can set the padding around the close price level to be higher if you are using a zoomed out chart view that causes the immediately proximate levels (e.g., +5 and -5, and the closest rounded level) to appear squished or crowded.

If anyone can get it working on a tick chart that would be fantastic but it doesn't seem to be possible.

Code:
### SPX Price Intervals of Five Points on /ES and SPY charts, in Expansion Area with Bubbles. Intervals are derived from SPX close price rounded to nearest five +/- 50 points. During extended hours the SPX price is simulated by adding its price differential from SPY while multiplying SPY by ten. Only works on charts of SPY and /ES.

# "ShowBubbleNow" code method recycled from FuturesTony at UseThinkScript.com. Study thread: https://usethinkscript.com/threads/spx-price-intervals-of-five-points-on-es-and-spy-charts-plotted-in-expansion-with-bubbles.16235/

input offset = 5; #set to the number of expansion area bars on your chart

input show_upper_labels = yes;

input hide_nearest_levels_to_close_distance = 1.5;

def showBubbleNow = !IsNaN(close) and IsNaN(close[-1]);

#def refresh_rate = .020;

#if bn == 1 then ((prev_day_rth_close-close)*100)
# else if today_en_0 then vol_today_pre_0[1] + pricechg
# else 0;

#def open_price = open(period=aggregationPeriod.DAY);
#def perc_chg = close-close[1]/close[1];

#def perc_chg_sum = if barnumber()==1 then 0 else perc_chg_sum[1]+perc_chg;

#def cnt = if (refresh_rate < perc_chg_sum) then 0
#else cnt[1] + perc_chg_sum;

#declare once_per_bar; can uncomment this to make the indicator not repaint the levels as often

#AddChartBubble(yes,close,cnt,if cnt==0 then color.red else color.white);

def current_symbol = if GetSymbol()=="SPY" then 1 else 2;

def open_time = 0930;
def close_time = 1600;
def isRTH = if secondsfromtime(open_time)>0 and secondstilltime(close_time)>0 then 1 else 0;
def spx_price_at_market_close = if isRTH then close(symbol="SPX") else spx_price_at_market_close[1];
def spy_price_at_market_close = if isRTH then close(symbol="SPY") else spy_price_at_market_close[1];
def es_price_at_market_close = if isRTH then close(symbol="/ES") else es_price_at_market_close[1];

def SPY_SPX_diff = if !isRTH and (spy_price_at_market_close*10)<=(spx_price_at_market_close) then ((spx_price_at_market_close)-(spy_price_at_market_close*10)) else if !isRTH then((spy_price_at_market_close*10)-(spx_price_at_market_close)) else if (spy_price_at_market_close*10)<=(spx_price_at_market_close) then (close(symbol="SPX")-(close(symbol="SPY")*10)) else (close(symbol="SPY")*10)-(close(symbol="SPX"));;

def es_fv = if !isRTH then round(es_price_at_market_close-spx_price_at_market_close,0) else round(close(symbol="/ES")-(close(symbol="SPX")),0);

def es_fv_no_nan = if isNan(es_fv) then es_fv_no_nan[1] else es_fv;

def es_close_no_nan = if IsNan(close(symbol="/ES")) then es_close_no_nan[1] else close(symbol="/ES");

def SPX_close_exth_incl_pre = if isNan(close(symbol="SPX")) then (close(symbol="SPY")*10)+SPY_SPX_diff else close(symbol="SPX");
def SPX_close_exth_incl = if !isRTH and isNan(SPX_close_exth_incl_pre) then SPX_close_exth_incl[1] else if !isRTH then SPX_close_exth_incl_pre else close(symbol="SPX");

def SPX_close_exth_incl_no_nan = if isNan(SPX_close_exth_incl) then SPX_close_exth_incl_no_nan[1] else SPX_close_exth_incl;

AddLabel (show_upper_labels, "SPX: " + AsPrice(SPX_close_exth_incl_pre), if isNan(close(symbol="SPX")) then CreateColor(110,110,130) else Color.Light_Gray);
AddLabel (show_upper_labels,"( /ES fv "+Round(es_fv,0)+")",Color.Light_Gray);

def close_no_nan = if isNan(close) then close_no_nan[1] else close;


def SPX_on_SPY_value_pre = close_no_nan;
def SPX_on_SPY_value = if isNan(SPX_on_SPY_value_pre) then SPX_on_SPY_value[1] else SPX_on_SPY_value_pre[-offset];

def rounded_to_five = (Round((SPX_close_exth_incl_no_nan/5),0)*5);

def rounded_to_five_no_nan = if isNan(rounded_to_five) then rounded_to_five_no_nan[1] else rounded_to_five;

def rounded_to_five_SPY_pre = ((rounded_to_five-SPY_SPX_diff));

def rounded_five_SPY_no_nan = if isNan(rounded_to_five_SPY_pre) then rounded_five_SPY_no_nan[1] else rounded_to_five_SPY_pre[-offset];

DefineGlobalColor("SPX_Close_Price",CreateColor(165,165,165));
DefineGlobalColor("SPX_Pos_Offset",CreateColor(120,155,120));
DefineGlobalColor("SPX_Neg_Offset",CreateColor(155,120,120));


plot priceline = if isNan(close) and current_symbol==1 then SPX_on_SPY_value else if isNan(close) and current_symbol==2 then SPX_on_SPY_value else double.nan;
priceline.setlineweight(1);
priceline.AssignValueColor(GlobalColor("SPX_Close_Price"));
priceline.HideBubble();
priceline.HideTitle();
AddChartBubble(isRTH==0 and showBubbleNow[offset] and current_symbol==2,priceline[0],"c|  "+(es_close_no_nan-es_fv_no_nan),priceline.TakeValueColor());
AddChartBubble(isRTH==1 and showBubbleNow[offset] and current_symbol==2,priceline[0],"c|  "+(SPX_close_exth_incl_no_nan),priceline.TakeValueColor());
AddChartBubble(showBubbleNow[offset] and current_symbol==1,priceline[0],"c|  "+(SPX_close_exth_incl_no_nan),priceline.TakeValueColor());

plot priceline_rounded_to_five = if isNan(close) and current_symbol==1 and absValue(SPX_close_exth_incl_no_nan-rounded_five_SPY_no_nan)>hide_nearest_levels_to_close_distance then rounded_five_SPY_no_nan else if isNan(close) and current_symbol==2 and absValue(SPX_close_exth_incl_no_nan-(rounded_to_five_no_nan))>hide_nearest_levels_to_close_distance then rounded_to_five_no_nan+es_fv_no_nan else double.nan;
priceline_rounded_to_five.setlineweight(1);
priceline_rounded_to_five.AssignValueColor(if priceline_rounded_to_five > priceline then GlobalColor("SPX_Pos_Offset") else GlobalColor("SPX_Neg_Offset"));
priceline_rounded_to_five.HideBubble();
priceline_rounded_to_five.HideTitle();
AddChartBubble(showBubbleNow[offset],priceline_rounded_to_five[0],"~|  "+rounded_to_five_no_nan,priceline_rounded_to_five.TakeValueColor());

plot priceline_plus_5 = if isNan(close) and current_symbol==1 and absValue (SPX_close_exth_incl_no_nan-(((rounded_to_five_no_nan+5))))>hide_nearest_levels_to_close_distance then ((rounded_five_SPY_no_nan+5)/10) else if isNan(close) and current_symbol==2 and absValue (SPX_close_exth_incl_no_nan-(((rounded_to_five_no_nan+5))))>hide_nearest_levels_to_close_distance then (rounded_to_five_no_nan+es_fv_no_nan)+5 else double.nan;
priceline_plus_5.setlineweight(1);
priceline_plus_5.AssignValueColor(GlobalColor("SPX_Pos_Offset"));
priceline_plus_5.hidebubble();
priceline_plus_5.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_5,"+5|  "+(rounded_to_five+5),priceline_plus_5.TakeValueColor());

plot priceline_plus_10 = if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+10)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+10 else double.nan;
priceline_plus_10.setlineweight(1);
priceline_plus_10.AssignValueColor(GlobalColor("SPX_Pos_Offset"));
priceline_plus_10.hidebubble();
priceline_plus_10.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_10,"+10|  "+(rounded_to_five+10),priceline_plus_10.TakeValueColor());

plot priceline_plus_15 = if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+15)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+15 else double.nan;
priceline_plus_15.setlineweight(1);
priceline_plus_15.AssignValueColor(GlobalColor("SPX_Pos_Offset"));
priceline_plus_15.hidebubble();
priceline_plus_15.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_15,"+15|  "+(rounded_to_five+15),priceline_plus_15.TakeValueColor());

plot priceline_plus_20 = if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+20)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+20 else double.nan;
priceline_plus_20.setlineweight(1);
priceline_plus_20.AssignValueColor(GlobalColor("SPX_Pos_Offset"));
priceline_plus_20.hidebubble();
priceline_plus_20.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_20,"+20|  "+(rounded_to_five+20),priceline_plus_20.TakeValueColor());

plot priceline_plus_25 = if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+25)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+25 else double.nan;
priceline_plus_25.setlineweight(1);
priceline_plus_25.AssignValueColor(GlobalColor("SPX_Pos_Offset"));
priceline_plus_25.hidebubble();
priceline_plus_25.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_25,"+25|  "+(rounded_to_five+25),priceline_plus_25.TakeValueColor());

plot priceline_plus_30 = if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+30)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+30 else double.nan;
priceline_plus_30.setlineweight(1);
priceline_plus_30.AssignValueColor(GlobalColor("SPX_Pos_Offset"));
priceline_plus_30.hidebubble();
priceline_plus_30.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_30,"+30|  "+(rounded_to_five+30),priceline_plus_30.TakeValueColor());

plot priceline_plus_35 = if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+35)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+35 else double.nan;
priceline_plus_35.setlineweight(1);
priceline_plus_35.AssignValueColor(GlobalColor("SPX_Pos_Offset"));
priceline_plus_35.hidebubble();
priceline_plus_35.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_35,"+35|  "+(rounded_to_five+35),priceline_plus_35.TakeValueColor());

plot priceline_plus_40 = if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+40)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+40 else double.nan;
priceline_plus_40.setlineweight(1);
priceline_plus_40.AssignValueColor(GlobalColor("SPX_Pos_Offset"));
priceline_plus_40.hidebubble();
priceline_plus_40.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_40,"+40|  "+(rounded_to_five+40),priceline_plus_40.TakeValueColor());

plot priceline_plus_45 = if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+45)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+45 else double.nan;
priceline_plus_45.setlineweight(1);
priceline_plus_45.AssignValueColor(GlobalColor("SPX_Pos_Offset"));
priceline_plus_45.hidebubble();
priceline_plus_45.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_45,"+45|  "+(rounded_to_five+45),priceline_plus_45.TakeValueColor());

plot priceline_plus_50 = if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+50)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+50 else double.nan;
priceline_plus_50.setlineweight(1);
priceline_plus_50.AssignValueColor(GlobalColor("SPX_Pos_Offset"));
priceline_plus_50.hidebubble();
priceline_plus_50.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_50,"+50|  "+(rounded_to_five+50),priceline_plus_50.TakeValueColor());

plot priceline_minus_5 = if isNan(close) and current_symbol==1 and absValue (SPX_close_exth_incl_no_nan-(((rounded_to_five_no_nan-5))))>hide_nearest_levels_to_close_distance then ((rounded_five_SPY_no_nan-5)/10) else if isNan(close) and current_symbol==2 and absValue(SPX_close_exth_incl_no_nan-(((rounded_to_five_no_nan-5)))>hide_nearest_levels_to_close_distance) then (rounded_to_five_no_nan+es_fv_no_nan)-5 else double.nan;
priceline_minus_5.setlineweight(1);
priceline_minus_5.AssignValueColor(GlobalColor("SPX_Neg_Offset"));
priceline_minus_5.hidebubble();
priceline_minus_5.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_5,"-5|  "+(rounded_to_five-5),priceline_minus_5.TakeValueColor());

plot priceline_minus_10 = if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-10)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-10 else double.nan;
priceline_minus_10.setlineweight(1);
priceline_minus_10.AssignValueColor(GlobalColor("SPX_Neg_Offset"));
priceline_minus_10.hidebubble();
priceline_minus_10.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_10,"-10|  "+(rounded_to_five-10),priceline_minus_10.TakeValueColor());

plot priceline_minus_15 = if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-15)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-15 else double.nan;
priceline_minus_15.setlineweight(1);
priceline_minus_15.AssignValueColor(GlobalColor("SPX_Neg_Offset"));
priceline_minus_15.hidebubble();
priceline_minus_15.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_15,"-15|  "+(rounded_to_five-15),priceline_minus_15.TakeValueColor());

plot priceline_minus_20 = if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-20)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-20 else double.nan;
priceline_minus_20.setlineweight(1);
priceline_minus_20.AssignValueColor(GlobalColor("SPX_Neg_Offset"));
priceline_minus_20.hidebubble();
priceline_minus_20.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_20,"-20|  "+(rounded_to_five-20),priceline_minus_20.TakeValueColor());

plot priceline_minus_25 = if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-25)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-25 else double.nan;
priceline_minus_25.setlineweight(1);
priceline_minus_25.AssignValueColor(GlobalColor("SPX_Neg_Offset"));
priceline_minus_25.hidebubble();
priceline_minus_25.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_25,"-25|  "+(rounded_to_five-25),priceline_minus_25.TakeValueColor());

plot priceline_minus_30 = if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-30)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-30 else double.nan;
priceline_minus_30.setlineweight(1);
priceline_minus_30.AssignValueColor(GlobalColor("SPX_Neg_Offset"));
priceline_minus_30.hidebubble();
priceline_minus_30.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_30,"-30|  "+(rounded_to_five-30),priceline_minus_30.TakeValueColor());

plot priceline_minus_35 = if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-35)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-35 else double.nan;
priceline_minus_35.setlineweight(1);
priceline_minus_35.AssignValueColor(GlobalColor("SPX_Neg_Offset"));
priceline_minus_35.hidebubble();
priceline_minus_35.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_35,"-35|  "+(rounded_to_five-35),priceline_minus_35.TakeValueColor());

plot priceline_minus_40 = if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-40)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-40 else double.nan;
priceline_minus_40.setlineweight(1);
priceline_minus_40.AssignValueColor(GlobalColor("SPX_Neg_Offset"));
priceline_minus_40.hidebubble();
priceline_minus_40.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_40,"-40|  "+(rounded_to_five-40),priceline_minus_40.TakeValueColor());

plot priceline_minus_45 = if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-45)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-45 else double.nan;
priceline_minus_45.setlineweight(1);
priceline_minus_45.AssignValueColor(GlobalColor("SPX_Neg_Offset"));
priceline_minus_45.hidebubble();
priceline_minus_45.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_45,"-45|  "+(rounded_to_five-45),priceline_minus_45.TakeValueColor());

plot priceline_minus_50 = if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-50)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-50 else double.nan;
priceline_minus_50.setlineweight(1);
priceline_minus_50.AssignValueColor(GlobalColor("SPX_Neg_Offset"));
priceline_minus_50.hidebubble();
priceline_minus_50.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_50,"-50|  "+(rounded_to_five-50),priceline_minus_50.TakeValueColor());

EDIT 2: When I first posted the script, during extended hours when comparing the SPX chart to a SPY chart with the study loaded, the levels were off by about 15 cents (e.g. if the bottom of a candle on the SPX chart was at 4573, the level labeled 4570 is wrong by being about 15 cents too low on the SPY chart.

The ES chart version seemed to be off by 1 point or so which seems at least good enough for being able to eyeball major strike levels. I don't know if it will work better during RTH.

However, I have updated the logic for getting the SPX, /ES, and SPY prices at market close and that seems to have helped.

The /ES levels are accurate within less than one point now:

Capture-ES-example.PNG


The SPY levels are accurate within 2 points, for example the pump candle on a 10m chart at the close on 07-31-23 was 17 points on SPX, and was $1.75 on SPY. The price level is off by 20 cents which 0.20/10 = 2 points:

Capture-SPY-example.PNG


I haven't tested it during RTH but I think the accuracy should be the same or maybe slightly better for both SPY and /ES during RTH.

Edit: fixed an oversight where I'd accidentally left "setdefaultcolor" instead of "assignvaluecolor", now global color transparency works. Also included "declare once_per_bar" by default with a comment that it can be removed to make the levels constantly repaint around changes in the close price.

Edit 2: I started trying to figure out a way to define a cumulative % change threshold for repainting the levels, such as 0.2% change in SPY/ES/SPX. I couldn't figure out how to count it cumulatively and then reset the count when the change threshold is reached. That would be better, if the study could be set to only repaint the levels when SPY has changed 0.15% for example. Does anyone know how to do that?

Edit 3: If anyone prefers, they can use this alternate version that doesn't plot on the first two bars of the expansion area. I prefer that so that the expansion plot isn't crowding up against the current bar. I also noticed that when the levels were repainting the default plot colors would flash back for a moment. One way to fix this would be to redefine the colors using "SetDefaultColor" but then each transparency for all 20+ plots would have to be defined separately to give them transparencies, which is annoying. I tried instead adding a conditional with "else color.current" for "AssignValueColor" but it didn't solve the problem. So the below version just uses "SetDefaultColor" for all of the plots to avoid the colors flashing back and forth as the plot repaints.

Code:
### SPX Price Intervals of Five Points on /ES and SPY charts, in Expansion Area with Bubbles. Intervals are derived from SPX close price rounded to nearest five +/- 50 points. During extended hours the SPX price is simulated by adding its price differential from SPY while multiplying SPY by ten. Only works on charts of SPY and /ES.

# "ShowBubbleNow" code method recycled from FuturesTony at UseThinkScript.com. Study thread: https://usethinkscript.com/threads/spx-price-intervals-of-five-points-on-es-and-spy-charts-plotted-in-expansion-with-bubbles.16235/

#declare once_per_bar; can uncomment this to reduce frequency of repainting levels

input offset = 5; #set to the number of expansion area bars on your chart

input show_upper_labels = yes;

input hide_nearest_levels_to_close_distance = 1.5;

def showBubbleNow = !IsNaN(close) and IsNaN(close[-1]);

#def refresh_rate = .020;

#if bn == 1 then ((prev_day_rth_close-close)*100)
# else if today_en_0 then vol_today_pre_0[1] + pricechg
# else 0;

#def open_price = open(period=aggregationPeriod.DAY);
#def perc_chg = close-close[1]/close[1];

#def perc_chg_sum = if barnumber()==1 then 0 else perc_chg_sum[1]+perc_chg;

#def cnt = if (refresh_rate < perc_chg_sum) then 0
#else cnt[1] + perc_chg_sum;

#AddChartBubble(yes,close,cnt,if cnt==0 then color.red else color.white);

def current_symbol = if GetSymbol()=="SPY" then 1 else 2;

def open_time = 0930;
def close_time = 1600;
def isRTH = if secondsfromtime(open_time)>0 and secondstilltime(close_time)>0 then 1 else 0;
def spx_price_at_market_close = if isRTH then close(symbol="SPX") else spx_price_at_market_close[1];
def spy_price_at_market_close = if isRTH then close(symbol="SPY") else spy_price_at_market_close[1];
def es_price_at_market_close = if isRTH then close(symbol="/ES") else es_price_at_market_close[1];

def SPY_SPX_diff = if !isRTH and (spy_price_at_market_close*10)<=(spx_price_at_market_close) then ((spx_price_at_market_close)-(spy_price_at_market_close*10)) else if !isRTH then((spy_price_at_market_close*10)-(spx_price_at_market_close)) else if (spy_price_at_market_close*10)<=(spx_price_at_market_close) then (close(symbol="SPX")-(close(symbol="SPY")*10)) else (close(symbol="SPY")*10)-(close(symbol="SPX"));;

def es_fv = if !isRTH then round(es_price_at_market_close-spx_price_at_market_close,0) else round(close(symbol="/ES")-(close(symbol="SPX")),0);

def es_fv_no_nan = if isNan(es_fv) then es_fv_no_nan[1] else es_fv;

def es_close_no_nan = if IsNan(close(symbol="/ES")) then es_close_no_nan[1] else close(symbol="/ES");

def SPX_close_exth_incl_pre = if isNan(close(symbol="SPX")) then (close(symbol="SPY")*10)+SPY_SPX_diff else close(symbol="SPX");
def SPX_close_exth_incl = if !isRTH and isNan(SPX_close_exth_incl_pre) then SPX_close_exth_incl[1] else if !isRTH then SPX_close_exth_incl_pre else close(symbol="SPX");

def SPX_close_exth_incl_no_nan = if isNan(SPX_close_exth_incl) then SPX_close_exth_incl_no_nan[1] else SPX_close_exth_incl;

AddLabel (show_upper_labels, "SPX: " + AsPrice(SPX_close_exth_incl_pre), if isNan(close(symbol="SPX")) then CreateColor(110,110,130) else Color.Light_Gray);
AddLabel (show_upper_labels,"( /ES fv "+Round(es_fv,0)+")",Color.Light_Gray);

def close_no_nan = if isNan(close) then close_no_nan[1] else close;


def SPX_on_SPY_value_pre = close_no_nan;
def SPX_on_SPY_value = if isNan(SPX_on_SPY_value_pre) then SPX_on_SPY_value[1] else SPX_on_SPY_value_pre;

def rounded_to_five = (Round((SPX_close_exth_incl_no_nan/5),0)*5);

def rounded_to_five_no_nan = if isNan(rounded_to_five) then rounded_to_five_no_nan[1] else rounded_to_five;

def rounded_to_five_SPY_pre = ((rounded_to_five-SPY_SPX_diff));

def rounded_five_SPY_no_nan = if isNan(rounded_to_five_SPY_pre) then rounded_five_SPY_no_nan[1] else rounded_to_five_SPY_pre;

DefineGlobalColor("SPX_Close_Price",CreateColor(165,165,165));
DefineGlobalColor("SPX_Pos_Offset",CreateColor(120,155,120));
DefineGlobalColor("SPX_Neg_Offset",CreateColor(155,120,120));


plot priceline = if (!isNan(close[1]) and isNan(close)) or (!isNan(close[2]) and isNan(close[1]) and isNan(close)) then double.nan else if isNan(close) and current_symbol==1 then SPX_on_SPY_value else if isNan(close) and current_symbol==2 then SPX_on_SPY_value else double.nan;
priceline.setlineweight(1);
priceline.SetDefaultColor(CreateColor(165,165,165));
priceline.HideBubble();
priceline.HideTitle();
AddChartBubble(isRTH==0 and showBubbleNow[offset] and current_symbol==2,priceline[0],"c|  "+(es_close_no_nan-es_fv_no_nan),priceline.TakeValueColor());
AddChartBubble(isRTH==1 and showBubbleNow[offset] and current_symbol==2,priceline[0],"c|  "+(SPX_close_exth_incl_no_nan),priceline.TakeValueColor());
AddChartBubble(showBubbleNow[offset] and current_symbol==1,priceline[0],"c|  "+(SPX_close_exth_incl_no_nan),priceline.TakeValueColor());

plot priceline_rounded_to_five = if (!isNan(close[1]) and isNan(close)) or (!isNan(close[2]) and isNan(close[1]) and isNan(close)) then double.nan else if isNan(close) and current_symbol==1 and absValue(SPX_close_exth_incl_no_nan-rounded_five_SPY_no_nan)>hide_nearest_levels_to_close_distance then rounded_five_SPY_no_nan else if isNan(close) and current_symbol==2 and absValue(SPX_close_exth_incl_no_nan-(rounded_to_five_no_nan))>hide_nearest_levels_to_close_distance then rounded_to_five_no_nan+es_fv_no_nan else double.nan;
priceline_rounded_to_five.setlineweight(1);
priceline_rounded_to_five.SetDefaultColor(CreateColor(120,120,120));
priceline_rounded_to_five.HideBubble();
priceline_rounded_to_five.HideTitle();
AddChartBubble(showBubbleNow[offset],priceline_rounded_to_five[0],"~|  "+rounded_to_five_no_nan,priceline_rounded_to_five.TakeValueColor());

plot priceline_plus_5 = if (!isNan(close[1]) and isNan(close)) or (!isNan(close[2]) and isNan(close[1]) and isNan(close)) then double.nan else if isNan(close) and current_symbol==1 and absValue (SPX_close_exth_incl_no_nan-(((rounded_to_five_no_nan+5))))>hide_nearest_levels_to_close_distance then ((rounded_five_SPY_no_nan+5)/10) else if isNan(close) and current_symbol==2 and absValue (SPX_close_exth_incl_no_nan-(((rounded_to_five_no_nan+5))))>hide_nearest_levels_to_close_distance then (rounded_to_five_no_nan+es_fv_no_nan)+5 else double.nan;
priceline_plus_5.setlineweight(1);
priceline_plus_5.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_5.hidebubble();
priceline_plus_5.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_5,"+5|  "+(rounded_to_five+5),priceline_plus_5.TakeValueColor());

plot priceline_plus_10 = if (!isNan(close[1]) and isNan(close)) or (!isNan(close[2]) and isNan(close[1]) and isNan(close)) then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+10)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+10 else double.nan;
priceline_plus_10.setlineweight(1);
priceline_plus_10.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_10.hidebubble();
priceline_plus_10.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_10,"+10|  "+(rounded_to_five+10),priceline_plus_10.TakeValueColor());

plot priceline_plus_15 = if (!isNan(close[1]) and isNan(close)) or (!isNan(close[2]) and isNan(close[1]) and isNan(close)) then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+15)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+15 else double.nan;
priceline_plus_15.setlineweight(1);
priceline_plus_15.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_15.hidebubble();
priceline_plus_15.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_15,"+15|  "+(rounded_to_five+15),priceline_plus_15.TakeValueColor());

plot priceline_plus_20 = if (!isNan(close[1]) and isNan(close)) or (!isNan(close[2]) and isNan(close[1]) and isNan(close)) then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+20)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+20 else double.nan;
priceline_plus_20.setlineweight(1);
priceline_plus_20.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_20.hidebubble();
priceline_plus_20.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_20,"+20|  "+(rounded_to_five+20),priceline_plus_20.TakeValueColor());

plot priceline_plus_25 = if (!isNan(close[1]) and isNan(close)) or (!isNan(close[2]) and isNan(close[1]) and isNan(close)) then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+25)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+25 else double.nan;
priceline_plus_25.setlineweight(1);
priceline_plus_25.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_25.hidebubble();
priceline_plus_25.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_25,"+25|  "+(rounded_to_five+25),priceline_plus_25.TakeValueColor());

plot priceline_plus_30 = if (!isNan(close[1]) and isNan(close)) or (!isNan(close[2]) and isNan(close[1]) and isNan(close)) then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+30)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+30 else double.nan;
priceline_plus_30.setlineweight(1);
priceline_plus_30.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_30.hidebubble();
priceline_plus_30.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_30,"+30|  "+(rounded_to_five+30),priceline_plus_30.TakeValueColor());

plot priceline_plus_35 = if (!isNan(close[1]) and isNan(close)) or (!isNan(close[2]) and isNan(close[1]) and isNan(close)) then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+35)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+35 else double.nan;
priceline_plus_35.setlineweight(1);
priceline_plus_35.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_35.hidebubble();
priceline_plus_35.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_35,"+35|  "+(rounded_to_five+35),priceline_plus_35.TakeValueColor());

plot priceline_plus_40 = if (!isNan(close[1]) and isNan(close)) or (!isNan(close[2]) and isNan(close[1]) and isNan(close)) then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+40)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+40 else double.nan;
priceline_plus_40.setlineweight(1);
priceline_plus_40.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_40.hidebubble();
priceline_plus_40.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_40,"+40|  "+(rounded_to_five+40),priceline_plus_40.TakeValueColor());

plot priceline_plus_45 = if (!isNan(close[1]) and isNan(close)) or (!isNan(close[2]) and isNan(close[1]) and isNan(close)) then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+45)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+45 else double.nan;
priceline_plus_45.setlineweight(1);
priceline_plus_45.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_45.hidebubble();
priceline_plus_45.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_45,"+45|  "+(rounded_to_five+45),priceline_plus_45.TakeValueColor());

plot priceline_plus_50 = if (!isNan(close[1]) and isNan(close)) or (!isNan(close[2]) and isNan(close[1]) and isNan(close)) then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+50)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+50 else double.nan;
priceline_plus_50.setlineweight(1);
priceline_plus_50.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_50.hidebubble();
priceline_plus_50.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_50,"+50|  "+(rounded_to_five+50),priceline_plus_50.TakeValueColor());

plot priceline_minus_5 = if (!isNan(close[1]) and isNan(close)) or (!isNan(close[2]) and isNan(close[1]) and isNan(close)) then double.nan else if isNan(close) and current_symbol==1 and absValue (SPX_close_exth_incl_no_nan-(((rounded_to_five_no_nan-5))))>hide_nearest_levels_to_close_distance then ((rounded_five_SPY_no_nan-5)/10) else if isNan(close) and current_symbol==2 and absValue(SPX_close_exth_incl_no_nan-(((rounded_to_five_no_nan-5)))>hide_nearest_levels_to_close_distance) then (rounded_to_five_no_nan+es_fv_no_nan)-5 else double.nan;
priceline_minus_5.setlineweight(1);
priceline_minus_5.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_5.hidebubble();
priceline_minus_5.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_5,"-5|  "+(rounded_to_five-5),priceline_minus_5.TakeValueColor());

plot priceline_minus_10 = if (!isNan(close[1]) and isNan(close)) or (!isNan(close[2]) and isNan(close[1]) and isNan(close)) then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-10)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-10 else double.nan;
priceline_minus_10.setlineweight(1);
priceline_minus_10.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_10.hidebubble();
priceline_minus_10.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_10,"-10|  "+(rounded_to_five-10),priceline_minus_10.TakeValueColor());

plot priceline_minus_15 = if (!isNan(close[1]) and isNan(close)) or (!isNan(close[2]) and isNan(close[1]) and isNan(close)) then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-15)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-15 else double.nan;
priceline_minus_15.setlineweight(1);
priceline_minus_15.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_15.hidebubble();
priceline_minus_15.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_15,"-15|  "+(rounded_to_five-15),priceline_minus_15.TakeValueColor());

plot priceline_minus_20 = if (!isNan(close[1]) and isNan(close)) or (!isNan(close[2]) and isNan(close[1]) and isNan(close)) then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-20)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-20 else double.nan;
priceline_minus_20.setlineweight(1);
priceline_minus_20.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_20.hidebubble();
priceline_minus_20.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_20,"-20|  "+(rounded_to_five-20),priceline_minus_20.TakeValueColor());

plot priceline_minus_25 = if (!isNan(close[1]) and isNan(close)) or (!isNan(close[2]) and isNan(close[1]) and isNan(close)) then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-25)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-25 else double.nan;
priceline_minus_25.setlineweight(1);
priceline_minus_25.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_25.hidebubble();
priceline_minus_25.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_25,"-25|  "+(rounded_to_five-25),priceline_minus_25.TakeValueColor());

plot priceline_minus_30 = if (!isNan(close[1]) and isNan(close)) or (!isNan(close[2]) and isNan(close[1]) and isNan(close)) then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-30)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-30 else double.nan;
priceline_minus_30.setlineweight(1);
priceline_minus_30.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_30.hidebubble();
priceline_minus_30.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_30,"-30|  "+(rounded_to_five-30),priceline_minus_30.TakeValueColor());

plot priceline_minus_35 = if (!isNan(close[1]) and isNan(close)) or (!isNan(close[2]) and isNan(close[1]) and isNan(close)) then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-35)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-35 else double.nan;
priceline_minus_35.setlineweight(1);
priceline_minus_35.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_35.hidebubble();
priceline_minus_35.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_35,"-35|  "+(rounded_to_five-35),priceline_minus_35.TakeValueColor());

plot priceline_minus_40 = if (!isNan(close[1]) and isNan(close)) or (!isNan(close[2]) and isNan(close[1]) and isNan(close)) then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-40)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-40 else double.nan;
priceline_minus_40.setlineweight(1);
priceline_minus_40.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_40.hidebubble();
priceline_minus_40.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_40,"-40|  "+(rounded_to_five-40),priceline_minus_40.TakeValueColor());

plot priceline_minus_45 = if (!isNan(close[1]) and isNan(close)) or (!isNan(close[2]) and isNan(close[1]) and isNan(close)) then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-45)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-45 else double.nan;
priceline_minus_45.setlineweight(1);
priceline_minus_45.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_45.hidebubble();
priceline_minus_45.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_45,"-45|  "+(rounded_to_five-45),priceline_minus_45.TakeValueColor());

plot priceline_minus_50 = if (!isNan(close[1]) and isNan(close)) or (!isNan(close[2]) and isNan(close[1]) and isNan(close)) then double.nan else if (!isNan(close[1]) and isNan(close)) or (!isNan(close[2]) and isNan(close[1]) and isNan(close)) then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-50)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-50 else double.nan;
priceline_minus_50.setlineweight(1);
priceline_minus_50.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_50.hidebubble();
priceline_minus_50.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_50,"-50|  "+(rounded_to_five-50),priceline_minus_50.TakeValueColor());
 
Last edited:
One last update: if anyone wants to preserve screen real estate, I incorporated the "trim" script that @halcyonguy wrote here to only display the last three digits and make the bubbles a lot more compact. This also makes it easier to see the price level lines extending out beyond the bubbles. Also you can now easily edit the variable "priceline_truncation" to control how many bars of the expansion area are skipped before the price level plot lines start appearing.

Code:
### SPX Price Intervals of Five Points on /ES and SPY charts, in Expansion Area with Bubbles. Intervals are derived from SPX close price rounded to nearest five +/- 50 points. During extended hours the SPX price is simulated by adding its price differential from SPY while multiplying SPY by ten. Only works on charts of SPY and /ES. Updated 08-06-23 with "trim" script by halcyonguy to shorten price bubbles above and below the close price.

# "ShowBubbleNow" code method recycled from FuturesTony at UseThinkScript.com. Study thread: https://usethinkscript.com/threads/spx-price-intervals-of-five-points-on-es-and-spy-charts-plotted-in-expansion-with-bubbles.16235/

#declare once_per_bar; can uncomment this to reduce frequency of repainting levels

input offset = 5; #set to the number of expansion area bars on your chart

input show_upper_labels = yes;

input hide_nearest_levels_to_close_distance = 1.5;

def showBubbleNow = !IsNaN(close) and IsNaN(close[-1]);

def priceline_truncation = (!isNan(close[1]) and isNan(close)) or (!isNan(close[2]) and isNan(close[1]) and isNan(close)); #change this to choose how many of the first bars of the expansion area the price level plots won't appear on

script trim {
# trim the digits left of the decimal point, down to x digits
input x = 0;
input digits = 3; #input number of digits to show here
# move decimal point left 2
def dig = Power(10, digits);
def x2 = x / dig;
# remove integer portion
def x3 = x2 - Floor(x2);
# shift digits, move decimal point
plot z = x3 * dig;
}

#def refresh_rate = .020;

#if bn == 1 then ((prev_day_rth_close-close)*100)
# else if today_en_0 then vol_today_pre_0[1] + pricechg
# else 0;

#def open_price = open(period=aggregationPeriod.DAY);
#def perc_chg = close-close[1]/close[1];

#def perc_chg_sum = if barnumber()==1 then 0 else perc_chg_sum[1]+perc_chg;

#def cnt = if (refresh_rate < perc_chg_sum) then 0
#else cnt[1] + perc_chg_sum;

#AddChartBubble(yes,close,cnt,if cnt==0 then color.red else color.white);

def current_symbol = if GetSymbol()=="SPY" then 1 else 2;

def open_time = 0930;
def close_time = 1600;
def isRTH = if secondsfromtime(open_time)>0 and secondstilltime(close_time)>0 then 1 else 0;
def spx_price_at_market_close = if isRTH then close(symbol="SPX") else spx_price_at_market_close[1];
def spy_price_at_market_close = if isRTH then close(symbol="SPY") else spy_price_at_market_close[1];
def es_price_at_market_close = if isRTH then close(symbol="/ES") else es_price_at_market_close[1];

def SPY_SPX_diff = if !isRTH and (spy_price_at_market_close*10)<=(spx_price_at_market_close) then ((spx_price_at_market_close)-(spy_price_at_market_close*10)) else if !isRTH then((spy_price_at_market_close*10)-(spx_price_at_market_close)) else if (spy_price_at_market_close*10)<=(spx_price_at_market_close) then (close(symbol="SPX")-(close(symbol="SPY")*10)) else (close(symbol="SPY")*10)-(close(symbol="SPX"));;

def es_fv = if !isRTH then round(es_price_at_market_close-spx_price_at_market_close,0) else round(close(symbol="/ES")-(close(symbol="SPX")),0);

def es_fv_no_nan = if isNan(es_fv) then es_fv_no_nan[1] else es_fv;

def es_close_no_nan = if IsNan(close(symbol="/ES")) then es_close_no_nan[1] else close(symbol="/ES");

def SPX_close_exth_incl_pre = if isNan(close(symbol="SPX")) then (close(symbol="SPY")*10)+SPY_SPX_diff else close(symbol="SPX");
def SPX_close_exth_incl = if !isRTH and isNan(SPX_close_exth_incl_pre) then SPX_close_exth_incl[1] else if !isRTH then SPX_close_exth_incl_pre else close(symbol="SPX");

def SPX_close_exth_incl_no_nan = if isNan(SPX_close_exth_incl) then SPX_close_exth_incl_no_nan[1] else SPX_close_exth_incl;

AddLabel (show_upper_labels, "SPX: " + AsPrice(SPX_close_exth_incl_pre), if isNan(close(symbol="SPX")) then CreateColor(110,110,130) else Color.Light_Gray);
AddLabel (show_upper_labels,"( /ES fv "+Round(es_fv,0)+")",Color.Light_Gray);

def close_no_nan = if isNan(close) then close_no_nan[1] else close;


def SPX_on_SPY_value_pre = close_no_nan;
def SPX_on_SPY_value = if isNan(SPX_on_SPY_value_pre) then SPX_on_SPY_value[1] else SPX_on_SPY_value_pre;

def rounded_to_five = (Round((SPX_close_exth_incl_no_nan/5),0)*5);

def rounded_to_five_no_nan = if isNan(rounded_to_five) then rounded_to_five_no_nan[1] else rounded_to_five;

def rounded_to_five_SPY_pre = ((rounded_to_five-SPY_SPX_diff));

def rounded_five_SPY_no_nan = if isNan(rounded_to_five_SPY_pre) then rounded_five_SPY_no_nan[1] else rounded_to_five_SPY_pre;

DefineGlobalColor("SPX_Close_Price",CreateColor(165,165,165));
DefineGlobalColor("SPX_Pos_Offset",CreateColor(120,155,120));
DefineGlobalColor("SPX_Neg_Offset",CreateColor(155,120,120));


plot priceline = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then SPX_on_SPY_value else if isNan(close) and current_symbol==2 then SPX_on_SPY_value else double.nan;
priceline.setlineweight(1);
priceline.SetDefaultColor(CreateColor(165,165,165));
priceline.HideBubble();
priceline.HideTitle();
AddChartBubble(isRTH==0 and showBubbleNow[offset] and current_symbol==2,priceline[0],(es_close_no_nan-es_fv_no_nan),priceline.TakeValueColor());
AddChartBubble(isRTH==1 and showBubbleNow[offset] and current_symbol==2,priceline[0],(SPX_close_exth_incl_no_nan),priceline.TakeValueColor());
AddChartBubble(showBubbleNow[offset] and current_symbol==1,priceline[0],(SPX_close_exth_incl_no_nan),priceline.TakeValueColor());

plot priceline_rounded_to_five = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 and absValue(SPX_close_exth_incl_no_nan-rounded_five_SPY_no_nan)>hide_nearest_levels_to_close_distance then rounded_five_SPY_no_nan else if isNan(close) and current_symbol==2 and absValue(SPX_close_exth_incl_no_nan-(rounded_to_five_no_nan))>hide_nearest_levels_to_close_distance then rounded_to_five_no_nan+es_fv_no_nan else double.nan;
priceline_rounded_to_five.setlineweight(1);
priceline_rounded_to_five.SetDefaultColor(CreateColor(120,120,120));
priceline_rounded_to_five.HideBubble();
priceline_rounded_to_five.HideTitle();
AddChartBubble(showBubbleNow[offset],priceline_rounded_to_five[0]," ~ "+trim(rounded_to_five_no_nan),priceline_rounded_to_five.TakeValueColor());

plot priceline_plus_5 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 and absValue (SPX_close_exth_incl_no_nan-(((rounded_to_five_no_nan+5))))>hide_nearest_levels_to_close_distance then ((rounded_five_SPY_no_nan+5)/10) else if isNan(close) and current_symbol==2 and absValue (SPX_close_exth_incl_no_nan-(((rounded_to_five_no_nan+5))))>hide_nearest_levels_to_close_distance then (rounded_to_five_no_nan+es_fv_no_nan)+5 else double.nan;
priceline_plus_5.setlineweight(1);
priceline_plus_5.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_5.hidebubble();
priceline_plus_5.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_5,"`"+trim(rounded_to_five+5),priceline_plus_5.TakeValueColor());

plot priceline_plus_10 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+10)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+10 else double.nan;
priceline_plus_10.setlineweight(1);
priceline_plus_10.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_10.hidebubble();
priceline_plus_10.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_10,"`"+trim(rounded_to_five+10),priceline_plus_10.TakeValueColor());

plot priceline_plus_15 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+15)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+15 else double.nan;
priceline_plus_15.setlineweight(1);
priceline_plus_15.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_15.hidebubble();
priceline_plus_15.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_15,"`"+trim(rounded_to_five+15),priceline_plus_15.TakeValueColor());

plot priceline_plus_20 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+20)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+20 else double.nan;
priceline_plus_20.setlineweight(1);
priceline_plus_20.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_20.hidebubble();
priceline_plus_20.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_20,"`"+trim(rounded_to_five+20),priceline_plus_20.TakeValueColor());

plot priceline_plus_25 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+25)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+25 else double.nan;
priceline_plus_25.setlineweight(1);
priceline_plus_25.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_25.hidebubble();
priceline_plus_25.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_25,"`"+trim(rounded_to_five+25),priceline_plus_25.TakeValueColor());

plot priceline_plus_30 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+30)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+30 else double.nan;
priceline_plus_30.setlineweight(1);
priceline_plus_30.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_30.hidebubble();
priceline_plus_30.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_30,"`"+trim(rounded_to_five+30),priceline_plus_30.TakeValueColor());

plot priceline_plus_35 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+35)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+35 else double.nan;
priceline_plus_35.setlineweight(1);
priceline_plus_35.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_35.hidebubble();
priceline_plus_35.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_35,"`"+trim(rounded_to_five+35),priceline_plus_35.TakeValueColor());

plot priceline_plus_40 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+40)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+40 else double.nan;
priceline_plus_40.setlineweight(1);
priceline_plus_40.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_40.hidebubble();
priceline_plus_40.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_40,"`"+trim(rounded_to_five+40),priceline_plus_40.TakeValueColor());

plot priceline_plus_45 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+45)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+45 else double.nan;
priceline_plus_45.setlineweight(1);
priceline_plus_45.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_45.hidebubble();
priceline_plus_45.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_45,"`"+trim(rounded_to_five+45),priceline_plus_45.TakeValueColor());

plot priceline_plus_50 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+50)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+50 else double.nan;
priceline_plus_50.setlineweight(1);
priceline_plus_50.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_50.hidebubble();
priceline_plus_50.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_plus_50,"`"+trim(rounded_to_five+50),priceline_plus_50.TakeValueColor());

plot priceline_minus_5 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 and absValue (SPX_close_exth_incl_no_nan-(((rounded_to_five_no_nan-5))))>hide_nearest_levels_to_close_distance then ((rounded_five_SPY_no_nan-5)/10) else if isNan(close) and current_symbol==2 and absValue(SPX_close_exth_incl_no_nan-(((rounded_to_five_no_nan-5)))>hide_nearest_levels_to_close_distance) then (rounded_to_five_no_nan+es_fv_no_nan)-5 else double.nan;
priceline_minus_5.setlineweight(1);
priceline_minus_5.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_5.hidebubble();
priceline_minus_5.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_5,"`"+trim(rounded_to_five-5),priceline_minus_5.TakeValueColor());

plot priceline_minus_10 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-10)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-10 else double.nan;
priceline_minus_10.setlineweight(1);
priceline_minus_10.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_10.hidebubble();
priceline_minus_10.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_10,"`"+trim(rounded_to_five-10),priceline_minus_10.TakeValueColor());

plot priceline_minus_15 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-15)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-15 else double.nan;
priceline_minus_15.setlineweight(1);
priceline_minus_15.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_15.hidebubble();
priceline_minus_15.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_15,"`"+trim(rounded_to_five-15),priceline_minus_15.TakeValueColor());

plot priceline_minus_20 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-20)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-20 else double.nan;
priceline_minus_20.setlineweight(1);
priceline_minus_20.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_20.hidebubble();
priceline_minus_20.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_20,"`"+trim(rounded_to_five-20),priceline_minus_20.TakeValueColor());

plot priceline_minus_25 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-25)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-25 else double.nan;
priceline_minus_25.setlineweight(1);
priceline_minus_25.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_25.hidebubble();
priceline_minus_25.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_25,"`"+trim(rounded_to_five-25),priceline_minus_25.TakeValueColor());

plot priceline_minus_30 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-30)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-30 else double.nan;
priceline_minus_30.setlineweight(1);
priceline_minus_30.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_30.hidebubble();
priceline_minus_30.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_30,"`"+trim(rounded_to_five-30),priceline_minus_30.TakeValueColor());

plot priceline_minus_35 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-35)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-35 else double.nan;
priceline_minus_35.setlineweight(1);
priceline_minus_35.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_35.hidebubble();
priceline_minus_35.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_35,"`"+trim(rounded_to_five-35),priceline_minus_35.TakeValueColor());

plot priceline_minus_40 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-40)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-40 else double.nan;
priceline_minus_40.setlineweight(1);
priceline_minus_40.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_40.hidebubble();
priceline_minus_40.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_40,"`"+trim(rounded_to_five-40),priceline_minus_40.TakeValueColor());

plot priceline_minus_45 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-45)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-45 else double.nan;
priceline_minus_45.setlineweight(1);
priceline_minus_45.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_45.hidebubble();
priceline_minus_45.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_45,"`"+trim(rounded_to_five-45),priceline_minus_45.TakeValueColor());

plot priceline_minus_50 = if priceline_truncation then double.nan else if (!isNan(close[1]) and isNan(close)) or (!isNan(close[2]) and isNan(close[1]) and isNan(close)) then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-50)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-50 else double.nan;
priceline_minus_50.setlineweight(1);
priceline_minus_50.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_50.hidebubble();
priceline_minus_50.hidetitle();
AddChartBubble(showBubbleNow[offset],priceline_minus_50,"`"+trim(rounded_to_five-50),priceline_minus_50.TakeValueColor());
 
Last edited:
Another small update with two changes:

1) Removed the comma from the current close price bubble of SPX to decrease intrusion of the bubble beyond the expansion area.
2) Added option to show /ES price level after the SPX level when using the study on a SPY chart:

1692601579518.png


The /ES price at a given level appears to the right and is always larger than the SPX price so it's easy to not confuse the two.

New code:

Code:
### SPX Price Intervals of Five Points on /ES and SPY charts, in Expansion Area with Bubbles. Intervals are derived from SPX close price rounded to nearest five +/- 50 points. During extended hours the SPX price is simulated by adding its price differential from SPY while multiplying SPY by ten. Only works on charts of SPY and /ES. Updated 08-06-23 with "trim" script by halcyonguy to shorten price bubbles above and below the close price. Updated on 08-21-23 to allow option of displaying the /ES price after the SPX price in the bubble when using on a SPY chart.

# "ShowBubbleNow" code method recycled from FuturesTony at UseThinkScript.com. Study thread: https://usethinkscript.com/threads/spx-price-intervals-of-five-points-on-es-and-spy-charts-plotted-in-expansion-with-bubbles.16235/

#declare once_per_bar; can uncomment this to reduce frequency of repainting levels

input offset = 5; #set to the number of expansion area bars on your chart

input show_upper_labels = yes;

input hide_nearest_levels_to_close_distance = 1.5;

def showBubbleNow = !IsNaN(close) and IsNaN(close[-1]);

def priceline_truncation = (!isNan(close[1]) and isNan(close)) ; #change this to choose how many of the first bars of the expansion area the price level plots won't appear on #or (!isNan(close[2]) and isNan(close[1]) and isNan(close))

input show_ES_levels_in_bubbles_on_SPY = yes;

script trim {
# trim the digits left of the decimal point, down to x digits
input x = 0;
input digits = 3; #input number of digits to show here
# move decimal point left 2
def dig = Power(10, digits);
def x2 = x / dig;
# remove integer portion
def x3 = x2 - Floor(x2);
# shift digits, move decimal point
plot z = x3 * dig;
}

#def refresh_rate = .020;

#if bn == 1 then ((prev_day_rth_close-close)*100)
# else if today_en_0 then vol_today_pre_0[1] + pricechg
# else 0;

#def open_price = open(period=aggregationPeriod.DAY);
#def perc_chg = close-close[1]/close[1];

#def perc_chg_sum = if barnumber()==1 then 0 else perc_chg_sum[1]+perc_chg;

#def cnt = if (refresh_rate < perc_chg_sum) then 0
#else cnt[1] + perc_chg_sum;

#AddChartBubble(yes,close,cnt,if cnt==0 then color.red else color.white);

def current_symbol = if GetSymbol()=="SPY" then 1 else 2;

def open_time = 0930;
def close_time = 1600;
def isRTH = if secondsfromtime(open_time)>0 and secondstilltime(close_time)>0 then 1 else 0;
def spx_price_at_market_close = if isRTH then close(symbol="SPX") else spx_price_at_market_close[1];
def spy_price_at_market_close = if isRTH then close(symbol="SPY") else spy_price_at_market_close[1];
def es_price_at_market_close = if isRTH then close(symbol="/ES") else es_price_at_market_close[1];

def SPY_SPX_diff = if !isRTH and (spy_price_at_market_close*10)<=(spx_price_at_market_close) then ((spx_price_at_market_close)-(spy_price_at_market_close*10)) else if !isRTH then((spy_price_at_market_close*10)-(spx_price_at_market_close)) else if (spy_price_at_market_close*10)<=(spx_price_at_market_close) then (close(symbol="SPX")-(close(symbol="SPY")*10)) else (close(symbol="SPY")*10)-(close(symbol="SPX"));;

def es_fv = if !isRTH then round(es_price_at_market_close-spx_price_at_market_close,0) else round(close(symbol="/ES")-(close(symbol="SPX")),0);

def es_fv_no_nan = if isNan(es_fv) then es_fv_no_nan[1] else es_fv;

def es_close_no_nan = if IsNan(close(symbol="/ES")) then es_close_no_nan[1] else close(symbol="/ES");

def SPX_close_exth_incl_pre = if isNan(close(symbol="SPX")) then (close(symbol="SPY")*10)+SPY_SPX_diff else close(symbol="SPX");
def SPX_close_exth_incl = if !isRTH and isNan(SPX_close_exth_incl_pre) then SPX_close_exth_incl[1] else if !isRTH then SPX_close_exth_incl_pre else close(symbol="SPX");

def SPX_close_exth_incl_no_nan = if isNan(SPX_close_exth_incl) then SPX_close_exth_incl_no_nan[1] else SPX_close_exth_incl;

AddLabel (show_upper_labels, "SPX: " + AsPrice(SPX_close_exth_incl_pre), if isNan(close(symbol="SPX")) then CreateColor(110,110,130) else Color.Light_Gray);
AddLabel (show_upper_labels,"( /ES fv "+Round(es_fv,0)+")",Color.Light_Gray);

def close_no_nan = if isNan(close) then close_no_nan[1] else close;


def SPX_on_SPY_value_pre = close_no_nan;
def SPX_on_SPY_value = if isNan(SPX_on_SPY_value_pre) then SPX_on_SPY_value[1] else SPX_on_SPY_value_pre;

def rounded_to_five = (Round((SPX_close_exth_incl_no_nan/5),0)*5);

def rounded_to_five_no_nan = if isNan(rounded_to_five) then rounded_to_five_no_nan[1] else rounded_to_five;

def rounded_to_five_SPY_pre = ((rounded_to_five-SPY_SPX_diff));

def rounded_five_SPY_no_nan = if isNan(rounded_to_five_SPY_pre) then rounded_five_SPY_no_nan[1] else rounded_to_five_SPY_pre;

DefineGlobalColor("SPX_Close_Price",CreateColor(165,165,165));
DefineGlobalColor("SPX_Pos_Offset",CreateColor(120,155,120));
DefineGlobalColor("SPX_Neg_Offset",CreateColor(155,120,120));


plot priceline = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then SPX_on_SPY_value else if isNan(close) and current_symbol==2 then SPX_on_SPY_value else double.nan;
priceline.setlineweight(1);
priceline.SetDefaultColor(CreateColor(165,165,165));
priceline.HideBubble();
priceline.HideTitle();
AddChartBubble(isRTH==0 and showBubbleNow[offset] and current_symbol==2,priceline[0],AsPrice((es_close_no_nan-es_fv_no_nan)),priceline.TakeValueColor());
AddChartBubble(isRTH==1 and showBubbleNow[offset] and current_symbol==2,priceline[0],AsPrice((SPX_close_exth_incl_no_nan)),priceline.TakeValueColor());
AddChartBubble(showBubbleNow[offset] and current_symbol==1,priceline[0],AsPrice((SPX_close_exth_incl_no_nan)),priceline.TakeValueColor());

plot priceline_rounded_to_five = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 and absValue(SPX_close_exth_incl_no_nan-rounded_five_SPY_no_nan)>hide_nearest_levels_to_close_distance then rounded_five_SPY_no_nan else if isNan(close) and current_symbol==2 and absValue(SPX_close_exth_incl_no_nan-(rounded_to_five_no_nan))>hide_nearest_levels_to_close_distance then rounded_to_five_no_nan+es_fv_no_nan else double.nan;
priceline_rounded_to_five.setlineweight(1);
priceline_rounded_to_five.SetDefaultColor(CreateColor(120,120,120));
priceline_rounded_to_five.HideBubble();
priceline_rounded_to_five.HideTitle();
AddChartBubble(showBubbleNow[offset],priceline_rounded_to_five[0]," ~ "+trim(rounded_to_five_no_nan),priceline_rounded_to_five.TakeValueColor());

plot priceline_plus_5 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 and absValue (SPX_close_exth_incl_no_nan-(((rounded_to_five_no_nan+5))))>hide_nearest_levels_to_close_distance then ((rounded_five_SPY_no_nan+5)/10) else if isNan(close) and current_symbol==2 and absValue (SPX_close_exth_incl_no_nan-(((rounded_to_five_no_nan+5))))>hide_nearest_levels_to_close_distance then (rounded_to_five_no_nan+es_fv_no_nan)+5 else double.nan;
priceline_plus_5.setlineweight(1);
priceline_plus_5.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_5.hidebubble();
priceline_plus_5.hidetitle();
AddChartBubble((current_symbol==2 or (current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==no)) and showBubbleNow[offset],priceline_plus_5,"`"+trim(rounded_to_five+5),priceline_plus_5.TakeValueColor());
AddChartBubble((current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==yes) and showBubbleNow[offset],priceline_plus_5,"`"+trim(rounded_to_five+5)+" | "+"`"+trim(rounded_to_five+5+es_fv_no_nan),priceline_plus_5.TakeValueColor());

plot priceline_plus_10 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+10)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+10 else double.nan;
priceline_plus_10.setlineweight(1);
priceline_plus_10.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_10.hidebubble();
priceline_plus_10.hidetitle();
AddChartBubble((current_symbol==2 or (current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==no)) and showBubbleNow[offset],priceline_plus_10,"`"+trim(rounded_to_five+10),priceline_plus_10.TakeValueColor());
AddChartBubble((current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==yes) and showBubbleNow[offset],priceline_plus_10,"`"+trim(rounded_to_five+10)+" | "+"`"+trim(rounded_to_five+10+es_fv_no_nan),priceline_plus_10.TakeValueColor());

plot priceline_plus_15 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+15)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+15 else double.nan;
priceline_plus_15.setlineweight(1);
priceline_plus_15.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_15.hidebubble();
priceline_plus_15.hidetitle();
AddChartBubble((current_symbol==2 or (current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==no)) and showBubbleNow[offset],priceline_plus_15,"`"+trim(rounded_to_five+15),priceline_plus_15.TakeValueColor());
AddChartBubble((current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==yes) and showBubbleNow[offset],priceline_plus_15,"`"+trim(rounded_to_five+15)+" | "+"`"+trim(rounded_to_five+15+es_fv_no_nan),priceline_plus_15.TakeValueColor());

plot priceline_plus_20 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+20)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+20 else double.nan;
priceline_plus_20.setlineweight(1);
priceline_plus_20.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_20.hidebubble();
priceline_plus_20.hidetitle();
AddChartBubble((current_symbol==2 or (current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==no)) and showBubbleNow[offset],priceline_plus_20,"`"+trim(rounded_to_five+20),priceline_plus_20.TakeValueColor());
AddChartBubble((current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==yes) and showBubbleNow[offset],priceline_plus_20,"`"+trim(rounded_to_five+20)+" | "+"`"+trim(rounded_to_five+20+es_fv_no_nan),priceline_plus_20.TakeValueColor());

plot priceline_plus_25 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+25)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+25 else double.nan;
priceline_plus_25.setlineweight(1);
priceline_plus_25.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_25.hidebubble();
priceline_plus_25.hidetitle();
AddChartBubble((current_symbol==2 or (current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==no)) and showBubbleNow[offset],priceline_plus_25,"`"+trim(rounded_to_five+25),priceline_plus_25.TakeValueColor());
AddChartBubble((current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==yes) and showBubbleNow[offset],priceline_plus_25,"`"+trim(rounded_to_five+25)+" | "+"`"+trim(rounded_to_five+25+es_fv_no_nan),priceline_plus_25.TakeValueColor());

plot priceline_plus_30 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+30)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+30 else double.nan;
priceline_plus_30.setlineweight(1);
priceline_plus_30.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_30.hidebubble();
priceline_plus_30.hidetitle();
AddChartBubble((current_symbol==2 or (current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==no)) and showBubbleNow[offset],priceline_plus_30,"`"+trim(rounded_to_five+30),priceline_plus_30.TakeValueColor());
AddChartBubble((current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==yes) and showBubbleNow[offset],priceline_plus_30,"`"+trim(rounded_to_five+30)+" | "+"`"+trim(rounded_to_five+30+es_fv_no_nan),priceline_plus_30.TakeValueColor());

plot priceline_plus_35 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+35)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+35 else double.nan;
priceline_plus_35.setlineweight(1);
priceline_plus_35.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_35.hidebubble();
priceline_plus_35.hidetitle();
AddChartBubble((current_symbol==2 or (current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==no)) and showBubbleNow[offset],priceline_plus_35,"`"+trim(rounded_to_five+35),priceline_plus_35.TakeValueColor());
AddChartBubble((current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==yes) and showBubbleNow[offset],priceline_plus_35,"`"+trim(rounded_to_five+35)+" | "+"`"+trim(rounded_to_five+35+es_fv_no_nan),priceline_plus_35.TakeValueColor());

plot priceline_plus_40 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+40)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+40 else double.nan;
priceline_plus_40.setlineweight(1);
priceline_plus_40.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_40.hidebubble();
priceline_plus_40.hidetitle();
AddChartBubble((current_symbol==2 or (current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==no)) and showBubbleNow[offset],priceline_plus_40,"`"+trim(rounded_to_five+40),priceline_plus_40.TakeValueColor());
AddChartBubble((current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==yes) and showBubbleNow[offset],priceline_plus_40,"`"+trim(rounded_to_five+40)+" | "+"`"+trim(rounded_to_five+40+es_fv_no_nan),priceline_plus_40.TakeValueColor());

plot priceline_plus_45 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+45)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+45 else double.nan;
priceline_plus_45.setlineweight(1);
priceline_plus_45.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_45.hidebubble();
priceline_plus_45.hidetitle();
AddChartBubble((current_symbol==2 or (current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==no)) and showBubbleNow[offset],priceline_plus_45,"`"+trim(rounded_to_five+45),priceline_plus_45.TakeValueColor());
AddChartBubble((current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==yes) and showBubbleNow[offset],priceline_plus_45,"`"+trim(rounded_to_five+45)+" | "+"`"+trim(rounded_to_five+45+es_fv_no_nan),priceline_plus_45.TakeValueColor());

plot priceline_plus_50 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan+50)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)+50 else double.nan;
priceline_plus_50.setlineweight(1);
priceline_plus_50.SetDefaultColor(CreateColor(120,155,120));
priceline_plus_50.hidebubble();
priceline_plus_50.hidetitle();
AddChartBubble((current_symbol==2 or (current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==no)) and showBubbleNow[offset],priceline_plus_50,"`"+trim(rounded_to_five+50),priceline_plus_50.TakeValueColor());
AddChartBubble((current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==yes) and showBubbleNow[offset],priceline_plus_50,"`"+trim(rounded_to_five+50)+" | "+"`"+trim(rounded_to_five+50+es_fv_no_nan),priceline_plus_50.TakeValueColor());

plot priceline_minus_5 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 and absValue (SPX_close_exth_incl_no_nan-(((rounded_to_five_no_nan-5))))>hide_nearest_levels_to_close_distance then ((rounded_five_SPY_no_nan-5)/10) else if isNan(close) and current_symbol==2 and absValue(SPX_close_exth_incl_no_nan-(((rounded_to_five_no_nan-5)))>hide_nearest_levels_to_close_distance) then (rounded_to_five_no_nan+es_fv_no_nan)-5 else double.nan;
priceline_minus_5.setlineweight(1);
priceline_minus_5.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_5.hidebubble();
priceline_minus_5.hidetitle();
AddChartBubble((current_symbol==2 or (current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==no)) and showBubbleNow[offset],priceline_minus_5,"`"+trim(rounded_to_five-5),priceline_minus_5.TakeValueColor());
AddChartBubble((current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==yes) and showBubbleNow[offset],priceline_minus_5,"`"+trim(rounded_to_five-5)+" | "+"`"+trim(rounded_to_five-5+es_fv_no_nan),priceline_minus_5.TakeValueColor());

plot priceline_minus_10 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-10)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-10 else double.nan;
priceline_minus_10.setlineweight(1);
priceline_minus_10.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_10.hidebubble();
priceline_minus_10.hidetitle();
AddChartBubble((current_symbol==2 or (current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==no)) and showBubbleNow[offset],priceline_minus_10,"`"+trim(rounded_to_five-10),priceline_minus_10.TakeValueColor());
AddChartBubble((current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==yes) and showBubbleNow[offset],priceline_minus_10,"`"+trim(rounded_to_five-10)+" | "+"`"+trim(rounded_to_five-10+es_fv_no_nan),priceline_minus_10.TakeValueColor());

plot priceline_minus_15 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-15)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-15 else double.nan;
priceline_minus_15.setlineweight(1);
priceline_minus_15.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_15.hidebubble();
priceline_minus_15.hidetitle();
AddChartBubble((current_symbol==2 or (current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==no)) and showBubbleNow[offset],priceline_minus_15,"`"+trim(rounded_to_five-15),priceline_minus_15.TakeValueColor());
AddChartBubble((current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==yes) and showBubbleNow[offset],priceline_minus_15,"`"+trim(rounded_to_five-15)+" | "+"`"+trim(rounded_to_five-15+es_fv_no_nan),priceline_minus_15.TakeValueColor());

plot priceline_minus_20 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-20)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-20 else double.nan;
priceline_minus_20.setlineweight(1);
priceline_minus_20.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_20.hidebubble();
priceline_minus_20.hidetitle();
AddChartBubble((current_symbol==2 or (current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==no)) and showBubbleNow[offset],priceline_minus_20,"`"+trim(rounded_to_five-20),priceline_minus_20.TakeValueColor());
AddChartBubble((current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==yes) and showBubbleNow[offset],priceline_minus_20,"`"+trim(rounded_to_five-20)+" | "+"`"+trim(rounded_to_five-20+es_fv_no_nan),priceline_minus_20.TakeValueColor());

plot priceline_minus_25 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-25)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-25 else double.nan;
priceline_minus_25.setlineweight(1);
priceline_minus_25.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_25.hidebubble();
priceline_minus_25.hidetitle();
AddChartBubble((current_symbol==2 or (current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==no)) and showBubbleNow[offset],priceline_minus_25,"`"+trim(rounded_to_five-25),priceline_minus_25.TakeValueColor());
AddChartBubble((current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==yes) and showBubbleNow[offset],priceline_minus_25,"`"+trim(rounded_to_five-25)+" | "+"`"+trim(rounded_to_five-25+es_fv_no_nan),priceline_minus_25.TakeValueColor());

plot priceline_minus_30 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-30)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-30 else double.nan;
priceline_minus_30.setlineweight(1);
priceline_minus_30.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_30.hidebubble();
priceline_minus_30.hidetitle();
AddChartBubble((current_symbol==2 or (current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==no)) and showBubbleNow[offset],priceline_minus_30,"`"+trim(rounded_to_five-30),priceline_minus_30.TakeValueColor());
AddChartBubble((current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==yes) and showBubbleNow[offset],priceline_minus_30,"`"+trim(rounded_to_five-30)+" | "+"`"+trim(rounded_to_five-30+es_fv_no_nan),priceline_minus_30.TakeValueColor());

plot priceline_minus_35 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-35)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-35 else double.nan;
priceline_minus_35.setlineweight(1);
priceline_minus_35.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_35.hidebubble();
priceline_minus_35.hidetitle();
AddChartBubble((current_symbol==2 or (current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==no)) and showBubbleNow[offset],priceline_minus_35,"`"+trim(rounded_to_five-35),priceline_minus_35.TakeValueColor());
AddChartBubble((current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==yes) and showBubbleNow[offset],priceline_minus_35,"`"+trim(rounded_to_five-35)+" | "+"`"+trim(rounded_to_five-35+es_fv_no_nan),priceline_minus_35.TakeValueColor());

plot priceline_minus_40 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-40)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-40 else double.nan;
priceline_minus_40.setlineweight(1);
priceline_minus_40.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_40.hidebubble();
priceline_minus_40.hidetitle();
AddChartBubble((current_symbol==2 or (current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==no)) and showBubbleNow[offset],priceline_minus_40,"`"+trim(rounded_to_five-40),priceline_minus_40.TakeValueColor());
AddChartBubble((current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==yes) and showBubbleNow[offset],priceline_minus_40,"`"+trim(rounded_to_five-40)+" | "+"`"+trim(rounded_to_five-40+es_fv_no_nan),priceline_minus_40.TakeValueColor());

plot priceline_minus_45 = if priceline_truncation then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-45)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-45 else double.nan;
priceline_minus_45.setlineweight(1);
priceline_minus_45.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_45.hidebubble();
priceline_minus_45.hidetitle();
AddChartBubble((current_symbol==2 or (current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==no)) and showBubbleNow[offset],priceline_minus_45,"`"+trim(rounded_to_five-45),priceline_minus_45.TakeValueColor());
AddChartBubble((current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==yes) and showBubbleNow[offset],priceline_minus_45,"`"+trim(rounded_to_five-45)+" | "+"`"+trim(rounded_to_five-45+es_fv_no_nan),priceline_minus_45.TakeValueColor());

plot priceline_minus_50 = if priceline_truncation then double.nan else if (!isNan(close[1]) and isNan(close)) or (!isNan(close[2]) and isNan(close[1]) and isNan(close)) then double.nan else if isNan(close) and current_symbol==1 then ((rounded_five_SPY_no_nan-50)/10) else if isNan(close) and current_symbol==2 then (rounded_to_five_no_nan+es_fv_no_nan)-50 else double.nan;
priceline_minus_50.setlineweight(1);
priceline_minus_50.SetDefaultColor(CreateColor(155,120,120));
priceline_minus_50.hidebubble();
priceline_minus_50.hidetitle();
AddChartBubble((current_symbol==2 or (current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==no)) and showBubbleNow[offset],priceline_minus_50,"`"+trim(rounded_to_five-50),priceline_minus_50.TakeValueColor());
AddChartBubble((current_symbol==1 and show_ES_levels_in_bubbles_on_SPY==yes) and showBubbleNow[offset],priceline_minus_50,"`"+trim(rounded_to_five-50)+" | "+"`"+trim(rounded_to_five-50+es_fv_no_nan),priceline_minus_50.TakeValueColor());
 

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
402 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