SMA/EMA with variable length?

I'm trying to calculate the SMA and EMA using a variable length, which you cannot do without getting the "CL constant value" error of course.

What I want to do is calculate the SMA and EMA for the premarket session of each day, so from time 00:00 to 09:30, which can be dynamic each day. After 09:30, the value can just be premarketSMA[1] for the rest of the day.

The code I have so far looks like this:

Code:
# Check if we are at midnight/new trading day, pre-market starts
def newDayTrigger = GetDay() <> GetDay()[1];

# Store the midnight/new day's bar number
def premarketOpenBarNumber = if newDayTrigger then BarNumber() else premarketOpenBarNumber[1];

# Wait for 0930
def isRegularTradingSession = SecondsFromTime(0930) >= 0 and SecondsTillTime(1600) > 0;

# Get the premarket close's bar number at 0930
def premarketCloseBarNumber = if isRegularTradingSession and isRegularTradingSession[1]==no then BarNumber() else premarketCloseBarNumber[1];

def premarketTotalBars = premarketCloseBarNumber - premarketOpenBarNumber;

def premarketSMA = if isRegularTradingSession and isRegularTradingSession[1]==no then SimpleMovingAvg(close, premarketTotalBars) else premarketSMA[1]; # Error
def premarketEMA = if isRegularTradingSession and isRegularTradingSession[1]==no then MovAvgExponential(close, premarketTotalBars) else premarketEMA[1]; # Error

As you see, I want the "length" value of the moving averages to be "premarketCloseBarNumber - premarketOpenBarNumber", once trading has officially started for the day, basically simulating the average and exponential close prices for the pre-market sessions. Possible?
 
Solution
I'm trying to calculate the SMA and EMA using a variable length, which you cannot do without getting the "CL constant value" error of course.

What I want to do is calculate the SMA and EMA for the premarket session of each day, so from time 00:00 to 09:30, which can be dynamic each day. After 09:30, the value can just be premarketSMA[1] for the rest of the day.

The code I have so far looks like this:

Code:
# Check if we are at midnight/new trading day, pre-market starts
def newDayTrigger = GetDay() <> GetDay()[1];

# Store the midnight/new day's bar number
def premarketOpenBarNumber = if newDayTrigger then BarNumber() else premarketOpenBarNumber[1];

# Wait for 0930
def isRegularTradingSession = SecondsFromTime(0930) >= 0 and...
I'm trying to calculate the SMA and EMA using a variable length, which you cannot do without getting the "CL constant value" error of course.

What I want to do is calculate the SMA and EMA for the premarket session of each day, so from time 00:00 to 09:30, which can be dynamic each day. After 09:30, the value can just be premarketSMA[1] for the rest of the day.

The code I have so far looks like this:

Code:
# Check if we are at midnight/new trading day, pre-market starts
def newDayTrigger = GetDay() <> GetDay()[1];

# Store the midnight/new day's bar number
def premarketOpenBarNumber = if newDayTrigger then BarNumber() else premarketOpenBarNumber[1];

# Wait for 0930
def isRegularTradingSession = SecondsFromTime(0930) >= 0 and SecondsTillTime(1600) > 0;

# Get the premarket close's bar number at 0930
def premarketCloseBarNumber = if isRegularTradingSession and isRegularTradingSession[1]==no then BarNumber() else premarketCloseBarNumber[1];

def premarketTotalBars = premarketCloseBarNumber - premarketOpenBarNumber;

def premarketSMA = if isRegularTradingSession and isRegularTradingSession[1]==no then SimpleMovingAvg(close, premarketTotalBars) else premarketSMA[1]; # Error
def premarketEMA = if isRegularTradingSession and isRegularTradingSession[1]==no then MovAvgExponential(close, premarketTotalBars) else premarketEMA[1]; # Error

As you see, I want the "length" value of the moving averages to be "premarketCloseBarNumber - premarketOpenBarNumber", once trading has officially started for the day, basically simulating the average and exponential close prices for the pre-market sessions. Possible?

could calculate a simple average , by adding up the close prices and the quantity of bars, in the pre market. then divide them.


Code:
def bn = barnumber();
def newDay = GetDay() <> GetDay()[1]; 
def newdaybn = if newday then bn else newdaybn[1];

#def premarket =....  true/false formula
def premarket = bn >=newdaybn and SecondsTillTime(0930) > 0;

def barz;
def clsz;
if newday then {
  barz = 1;
  clsz = close;
} else if premarket then {
  barz = barz[1]+1;
  clsz=clsz[1]+close;
} else {
  barz = barz[1];
  clsz=clsz[1];
}

def preavg = clsz/barz;
plot z = preavg;


if you want an EMA, study the formulas here
https://school.stockcharts.com/doku.php?id=technical_indicators:moving_averages
 
Solution

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

Thread starter Similar threads Forum Replies Date
S SMA or EMA begin calculating at the open? Questions 1
K SMA/EMA Strategy Help Questions 7
X DEMA EMA and SMA Questions 2
D SMA/EMA Labels Help Questions 8
C Colored EMA/SMA Questions 5

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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