Moving Average Excludes Extended Hours

Trigun1127

Member
I saw couple threads about this but no solution. Is it possible to have two moving averages for futures, one where the moving average includes extended hours data (just a normal moving average, so no extra legwork is required), and the second moving average excluding extended hours? I'd like to see both moving averages at the same time for a study.
 
Solution
Code:
input Length = 20;
def isOpen =
    between(
        GetTime(),
        RegularTradingStart(GetYYYYMMDD()),
        RegularTradingEnd(GetYYYYMMDD())
    )
;
def NoExMa =
    if isOpen then
        (
            Fold Index = 0 to Max(Max(Length,BarNumber()),0)
            with Data while Data - Floor(Data) < Length * 0.001 do
            if GetValue(isOpen,index)
            then Data + Floor(Round(GetValue(Close,Index),2) * 100) + 0.001
            else Data
        ) 
   else NoExMa[1]
   # or else Double.NaN
;
def Len =
    (NoExMa - floor(NoExMa)) * 1000
;
def Tot =
    Floor(NoExMa) * 0.01   
;
Plot NoExtHourMA = Tot / Len;
Code:
input Length = 20;
def isOpen =
    between(
        GetTime(),
        RegularTradingStart(GetYYYYMMDD()),
        RegularTradingEnd(GetYYYYMMDD())
    )
;
def NoExMa =
    if isOpen then
        (
            Fold Index = 0 to Max(Max(Length,BarNumber()),0)
            with Data while Data - Floor(Data) < Length * 0.001 do
            if GetValue(isOpen,index)
            then Data + Floor(Round(GetValue(Close,Index),2) * 100) + 0.001
            else Data
        ) 
   else NoExMa[1]
   # or else Double.NaN
;
def Len =
    (NoExMa - floor(NoExMa)) * 1000
;
def Tot =
    Floor(NoExMa) * 0.01   
;
Plot NoExtHourMA = Tot / Len;
 
Solution
Thanks Josh! Everything looks good but it looks like its off by a point or so. I could be wrong but it looks like it starts just a tad early before market open? I cant tell.
 
What symbol are you looking at, which aggregation, and do you have the start aggregation at market open setting checked or unchecked?
UGElVB9.png


TSTtpFl.png
 
What aggregation and average length? I need a scenario that creates whatever error in order to fix it, but I can't find one.

Could you post a screen shot?

uJVJwf3.png
I'll report back on Tuesday when market opens so i can share a screenshot of it. 20 EMA. Aggregates at market open
 
Last edited:
TOS sort of hides extra digits beyond the decimal with automatic rounding, but they're there, and they add up. This should bring the script back into alignment with TOS's rounding.

Code:
input Length = 10;
def isOpen =
    between(
        GetTime(),
        RegularTradingStart(GetYYYYMMDD()),
        RegularTradingEnd(GetYYYYMMDD())
    )
;
def NoExIndex =
    if isOpen then
        AbsValue(
            Fold i = 0 to Max(0,BarNumber())
            with d = 0 while d >= 0 do
            if d == Length then -i
            else if GetValue(isOpen,i) then d + 1
            else d
        )
    else 0   
;
def NoExMa =
    if isOpen then
        (
            Fold Index = 0 to NoExIndex with Data do
            if GetValue(isOpen,index)
            then Data + GetValue(close,index)
            else Data
        ) 
    else Double.NaN
;
def Len =
    (NoExMa - floor(NoExMa)) * 1000
;
def Tot =
    Floor(NoExMa) * 0.01   
;
Plot NoExtHourMA = NoExMa / Length;
NoExtHourMA.setpaintingStrategy(paintingStrategy.Line_Vs_POINTS);
NoExtHourMA.setdefaultColor(color.white);

Plot WithExHours = Average(close,Length);
WithExHours.setdefaultColor(color.white);

NoExtHourMA.assignValueColor(if NoExtHourMA == WithExHours then color.cyan else color.yellow);
 
TOS sort of hides extra digits beyond the decimal with automatic rounding, but they're there, and they add up. This should bring the script back into alignment with TOS's rounding.

Code:
input Length = 10;
def isOpen =
    between(
        GetTime(),
        RegularTradingStart(GetYYYYMMDD()),
        RegularTradingEnd(GetYYYYMMDD())
    )
;
def NoExIndex =
    if isOpen then
        AbsValue(
            Fold i = 0 to Max(0,BarNumber())
            with d = 0 while d >= 0 do
            if d == Length then -i
            else if GetValue(isOpen,i) then d + 1
            else d
        )
    else 0 
;
def NoExMa =
    if isOpen then
        (
            Fold Index = 0 to NoExIndex with Data do
            if GetValue(isOpen,index)
            then Data + GetValue(close,index)
            else Data
        )
    else Double.NaN
;
def Len =
    (NoExMa - floor(NoExMa)) * 1000
;
def Tot =
    Floor(NoExMa) * 0.01 
;
Plot NoExtHourMA = NoExMa / Length;
NoExtHourMA.setpaintingStrategy(paintingStrategy.Line_Vs_POINTS);
NoExtHourMA.setdefaultColor(color.white);

Plot WithExHours = Average(close,Length);
WithExHours.setdefaultColor(color.white);

NoExtHourMA.assignValueColor(if NoExtHourMA == WithExHours then color.cyan else color.yellow);
For some reason it seems like things are still off. Here is a picture of a regular 20 EMA and just "withExhours" on. For some reason it appears they are slightly off.
 
Its not an EMA, its an SMA. It would have to be completely recoded independently for EMA, WMA, and others.

The color change from yellow to blue is when the two come back into complete alignment, its auto-detected.

UBz1rqR.png
 
Oh no. I thought I had written EMA down in the past. My failure. If you can write it up for the ema that would be great. If not I totally understand! Btw really loved your touch of adding the convergence when they are identical.
 
Last edited:
Its not an EMA, its an SMA. It would have to be completely recoded independently for EMA, WMA, and others.

The color change from yellow to blue is when the two come back into complete alignment, its auto-detected.

UBz1rqR.png
Hey Josh I'm sorry to be a bother but if you feel up to it could you make this for the EMA? I'm really needing this in a study I'm conducting.
 

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