# Previous Day Market Close Range (30min EMA/SMA + 45min Price)
# Excludes Premarket and Aftermarket
# --- Inputs ---
input emaFastLength = 20;
input emaSlowLength = 200;
input priceMinutes = 45;
input emaMinutes = 30;
# --- Define Regular Trading Hours (RTH) ---
def isRTH = RegularTradingStart(GetYYYYMMDD()) <= SecondsFromTime(0);
def rthEnd = SecondsTillTime(1600) == 0;
# --- Define Previous Day ---
def lastDay = GetDay() == GetLastDay() - 1;
# --- Data Acquisition (Previous Day RTH Only) ---
def prevDayRTH = lastDay and isRTH;
# --- Calculate Indicators & Price (Last X Minutes) ---
# Price High/Low last 45 mins
def prevPriceHigh = if rthEnd then highest(high[1], priceMinutes) else prevPriceHigh[1];
def prevPriceLow = if rthEnd then lowest(low[1], priceMinutes) else prevPriceLow[1];
# 20/200 EMA/SMA last 30 mins
def ma20 = ExpAverage(close, emaFastLength);
def sm20 = Average(close, emaFastLength);
def ma200 = ExpAverage(close, emaSlowLength);
def sm200 = Average(close, emaSlowLength);
# Find the highest/lowest of all 4 MAs in the last 30 mins
def maHighest = if rthEnd then Max(Max(highest(ma20[1], emaMinutes), highest(sm20[1], emaMinutes)), Max(highest(ma200[1], emaMinutes), highest(sm200[1], emaMinutes))) else maHighest[1];
def maLowest = if rthEnd then Min(Min(lowest(ma20[1], emaMinutes), lowest(sm20[1], emaMinutes)), Min(lowest(ma200[1], emaMinutes), lowest(sm200[1], emaMinutes))) else maLowest[1];
# --- Define Final Range Boundaries ---
plot TopRange = Max(prevPriceHigh, maHighest);
plot BotRange = Min(prevPriceLow, maLowest);
# Styling for boundaries
TopRange.SetDefaultColor(Color.WHITE);
BotRange.SetDefaultColor(Color.WHITE);
TopRange.SetStyle(Curve.LONG_DASH);
BotRange.SetStyle(Curve.LONG_DASH);
# --- Create White Background Cloud ---
AddCloud(TopRange, BotRange, Color.WHITE, Color.WHITE);