def agg_ticks = GetAggregationPeriod(); # milliseconds per candle
def market_1day_ticks = 23400000; # 6.5 h x 60 x 60 x 1000
def length = (5 * market_1day_ticks) / agg_ticks; # candles in 5 days
plot D5_MovAvg = MovingAverage(averageType.SIMPLE, close, length);
plot D5_MovAvg = MovingAverage(averageType.SIMPLE, close, 117000000/GetAggregationPeriod());
def agg_ticks = GetAggregationPeriod(); # milliseconds per candle
def market_1day_ticks = 23400000; # 6.5 h x 60 x 60 x 1000
def length = (5 * market_1day_ticks) / agg_ticks; # candles in 5 days
plot D5_MovAvg = MovingAverage(averageType.SIMPLE, close, length);
plot D5_MovAvg = MovingAverage(averageType.SIMPLE, close, 117000000/GetAggregationPeriod());
This code is not working. Received error message. Thank youEnjoy!
Code:def agg_ticks = GetAggregationPeriod(); # milliseconds per candle def market_1day_ticks = 23400000; # 6.5 h x 60 x 60 x 1000 def length = (5 * market_1day_ticks) / agg_ticks; # candles in 5 days plot 5D_MovAvg = MovingAverage(averageType.SIMPLE, close, length);
Or if you'd like just one line of code: The above equals this.
Code:plot 5D_MovAvg = MovingAverage(averageType.SIMPLE, close, 117000000/GetAggregationPeriod());
This code is not working. Received error message. Thank you
has been changed to:5D_MovAvg
The script above has been fixed.D5_MovAvg
Thank you. Yes: I cannot use a variable name with starting with a number...There was a slight error in @whoDAT's awesome solution.
Definition names cannot start with numbers.
this:
has been changed to:
The script above has been fixed.
Thank you all.There was a slight error in @whoDAT's awesome solution.
Definition names cannot start with numbers.
this:
has been changed to:
The script above has been fixed.
# Scanner for 5-Day Simple Moving Average Cross DAILY ONLY
# Adapted from: plot D5_MovAvg = MovingAverage(averageType.SIMPLE, close, 117000000/GetAggregationPeriod());
# v.1 scanner by Antwerks 07/26/2025
# --- Inputs ---
input crossType = {default "Above", "Below"}; # Choose cross direction
input priceType = {default "Close", "Open", "High", "Low"}; # Price to compare
# --- Variables ---
def aggPeriod = GetAggregationPeriod();
def barsPerDay = if aggPeriod <= 86400000 then 86400000 / aggPeriod else 1; # Bars per day (approx.)
def lookbackPeriod = if aggPeriod <= 86400000 then 5 * (86400000 / aggPeriod) else 5; # 5-day equivalent
def movAvg = MovingAverage(averageType.SIMPLE, close, lookbackPeriod);
def comparePrice = if priceType == priceType.Close then close
else if priceType == priceType.Open then open
else if priceType == priceType.High then high
else low;
# --- Scan Condition ---
plot scan = if crossType == crossType.Above and comparePrice crosses above movAvg then 1
else if crossType == crossType.Below and comparePrice crosses below movAvg then 1
else 0;
DEF D5_MovAvg = MovingAverage(averageType.SIMPLE, close, 117000000/GetAggregationPeriod());
input showSMA = yes;
input colorSlope = yes;
input showLabel = yes;
def isDaily = GetAggregationPeriod();
def sma = MovingAverage(averageType.SIMPLE, close, 117000000/GetAggregationPeriod());
plot SMA5 = if showSMA then sma else Double.NaN;
# Slope coloring
def slopeUp = sma > sma[1];
def slopeDn = sma < sma[1];
SMA5.AssignValueColor(
if !colorSlope then Color.WHITE
else if slopeUp then Color.GREEN
else if slopeDn then Color.RED
else Color.GRAY
);
SMA5.SetLineWeight(3);
SMA5.SetStyle(Curve.FIRM);
# Label
AddLabel(showLabel, "5-Day SMA: " + Round(sma, 2),
if slopeUp then Color.GREEN
else if slopeDn then Color.RED
else Color.GRAY
);
Love this but would love the ability to color bars when slope color changes and ability to change the colors of the slope ( and obviously this will change the bar colors).Indicator with thermal and labels:
Code:DEF D5_MovAvg = MovingAverage(averageType.SIMPLE, close, 117000000/GetAggregationPeriod()); input showSMA = yes; input colorSlope = yes; input showLabel = yes; def isDaily = GetAggregationPeriod(); def sma = MovingAverage(averageType.SIMPLE, close, 117000000/GetAggregationPeriod()); plot SMA5 = if showSMA then sma else Double.NaN; # Slope coloring def slopeUp = sma > sma[1]; def slopeDn = sma < sma[1]; SMA5.AssignValueColor( if !colorSlope then Color.WHITE else if slopeUp then Color.GREEN else if slopeDn then Color.RED else Color.GRAY ); SMA5.SetLineWeight(3); SMA5.SetStyle(Curve.FIRM); # Label AddLabel(showLabel, "5-Day SMA: " + Round(sma, 2), if slopeUp then Color.GREEN else if slopeDn then Color.RED else Color.GRAY );
TRY THISLove this but would love the ability to color bars when slope color changes and ability to change the colors of the slope ( and obviously this will change the bar colors).
# Enhanced 5-Day SMA with Matching Candle Coloring (Fixed to Red/Green)
# --- Inputs ---
input showSMA = yes;
input colorSlope = yes;
input showLabel = yes;
input slopeUpColor = {default GREEN, RED, WHITE, YELLOW, CYAN, MAGENTA, GRAY}; # Default GREEN
input slopeDownColor = {default RED, GREEN, WHITE, YELLOW, CYAN, MAGENTA, GRAY}; # Default RED
# --- SMA Calculation ---
def isDaily = GetAggregationPeriod();
def sma = MovingAverage(averageType.SIMPLE, close, 117000000 / GetAggregationPeriod());
plot SMA5 = if showSMA then sma else Double.NaN;
# --- Slope Logic ---
def slopeUp = sma > sma[1];
def slopeDn = sma < sma[1];
# --- SMA Coloring and Styling ---
SMA5.AssignValueColor(
if !colorSlope then Color.WHITE
else if slopeUp then if slopeUpColor == slopeUpColor.GREEN then Color.GREEN else GetColor(slopeUpColor)
else if slopeDn then if slopeDownColor == slopeDownColor.RED then Color.RED else GetColor(slopeDownColor)
else Color.GRAY
);
SMA5.SetLineWeight(3);
SMA5.SetStyle(Curve.FIRM);
# --- Candle Coloring ---
AssignPriceColor(
if !colorSlope then Color.CURRENT
else if slopeUp then if slopeUpColor == slopeUpColor.GREEN then Color.GREEN else GetColor(slopeUpColor)
else if slopeDn then if slopeDownColor == slopeDownColor.RED then Color.RED else GetColor(slopeDownColor)
else Color.CURRENT
);
# --- Label ---
AddLabel(showLabel, "5-Day SMA: " + Round(sma, 2),
if !colorSlope then Color.GRAY
else if slopeUp then if slopeUpColor == slopeUpColor.GREEN then Color.GREEN else GetColor(slopeUpColor)
else if slopeDn then if slopeDownColor == slopeDownColor.RED then Color.RED else GetColor(slopeDownColor)
else Color.GRAY
);
Cool, but maybe allow for the colors to be in globals and allow to change both the line/slope and candle bars. In this instance I see orange candles above and below, which I believe should be different for above and below the sma
OOPS gonna have to readdress TOS coloringCool, but maybe allow for the colors to be in globals and allow to change both the line/slope and candle bars. In this instance I see orange candles above and below, which I believe should be different for above and below the sma
View attachment 25306
the candle coloring is denotes the slope angles - price action can be above the 5EMA but heading down ...Cool, but maybe allow for the colors to be in globals and allow to change both the line/slope and candle bars. In this instance I see orange candles above and below, which I believe should be different for above and below the sma
View attachment 25306
Join useThinkScript to post your question to a community of 21,000+ developers and traders.
Thread starter | Similar threads | Forum | Replies | Date |
---|---|---|---|---|
![]() |
any way to add simple spread% or ask/bid column | Questions | 1 | |
R | Volume Profile (POC) Simple Moving Avg | Questions | 2 | |
J | Help with Simple recursive statement | Questions | 2 | |
![]() |
Simple bar color script using AddChart() not working or am I overthinking it? | Questions | 3 | |
C | Simple MACD + RSI Indicator | Questions | 1 |
Start a new thread and receive assistance from our community.
useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.
We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.
If you are new, or just looking for guidance, here are some helpful links to get you started.