Gapless Moving Average Indicator for ThinkorSwim

Enable

New member
So since I have started looking at charts with extended hours taken off, I also ran across this little indicator online that adjusts a moving average in relation to the overnight gap that occurs. What I am hoping for is if someone could do the same thing, but for indicators, specifically the MACD, Stoch RSI, and DMI. Could be potentially helpful for those using charts with extended hours off, as those gap make charts difficult to read.

Code:
input AvgType = AverageType.EXPONENTIAL;

input Length = 8;

def avg;

def newDay = GetYYYYMMDD() <> GetYYYYMMDD()[1];

def todaysOpen = open(period = AggregationPeriod.DAY);

def yesterdaysClose = close(period = AggregationPeriod.DAY)[1];

def gapCount = gapCount[1] + newDay;

def gap = if newDay and gapCount > 1 then todaysOpen - yesterdaysClose else gap[1];

def accum = if newDay then accum[1] + gap else accum[1];

def relC = close - accum;

avg = MovingAverage(AvgType, relC, Length);

plot avg_gl = avg + accum;
 

tomsk

Well-known member
VIP
So since I have started looking at charts with extended hours taken off, I also ran across this little indicator online that adjusts a moving average in relation to the overnight gap that occurs. What I am hoping for is if someone could do the same thing, but for indicators, specifically the MACD, Stoch RSI, and DMI. Could be potentially helpful for those using charts with extended hours off, as those gap make charts difficult to read.

Code:
input AvgType = AverageType.EXPONENTIAL;

input Length = 8;

def avg;

def newDay = GetYYYYMMDD() <> GetYYYYMMDD()[1];

def todaysOpen = open(period = AggregationPeriod.DAY);

def yesterdaysClose = close(period = AggregationPeriod.DAY)[1];

def gapCount = gapCount[1] + newDay;

def gap = if newDay and gapCount > 1 then todaysOpen - yesterdaysClose else gap[1];

def accum = if newDay then accum[1] + gap else accum[1];

def relC = close - accum;

avg = MovingAverage(AvgType, relC, Length);

plot avg_gl = avg + accum;

@Enable I would be a bit careful with this indicator as it does not determine if there are any gaps
To test this out, I wrote a simple test study using the logic from your study.
If a gap condition is detected it plots an arrow whenever a gap is detected.

Code:
input AvgType = AverageType.EXPONENTIAL;
input Length = 8;

def newDay = GetYYYYMMDD() <> GetYYYYMMDD()[1];
def todaysOpen = open(period = AggregationPeriod.DAY);
def yesterdaysClose = close(period = AggregationPeriod.DAY)[1];
def gapCount = gapCount[1] + newDay;
def gap = if newDay and gapCount > 1 then todaysOpen - yesterdaysClose else gap[1];

plot GapCondition = if gap then low else Double.NaN;
GapCondition.SetPaintingStrategy(PaintingStrategy.Arrow_Up);
GapCondition.SetLineWeight(4);

Now, the ticker TIF gapped up massively today, and I ran my test on the chart of TIF
Seems like a "gap" condition is triggered every day which is just not true.

Hence you might like to take a note of that before anyone uses this indicator at face value
 
Last edited:

Enable

New member
@tomsk I don't actually mean whether or not someone considers it a gap or not, I just mean I want it to get adjusted for the overnight difference (not counting the AH and PM). Even if it is only a few cents I want it to adjust overnight, which is what it is doing. That being said, I would still like this done for the indicators I mentioned in my first post, if anyone here would have the time to help me. :)

You're looking at it too "technically".
 
Last edited:

tomsk

Well-known member
VIP
Personally price action should always factor in gaps, otherwise it would not reflect true market sentiment.
To do what you're suggesting - essentially replace the value of "close" with whatever adjustments needed as you detailed
 

Enable

New member
@tomsk I believe the "close" part is supposed to stay as "close" and I am supposed to adjust the "avg" part, because that is where the moving average is factored in. But I do not know how to do that with regards to more complex moving averages such as the ones in MACD, for example.
 

markos

Well-known member
VIP
@Enable Something tells me that you are looking at this wrong. (maybe)
If one price point to the next price point, whether it is AH, BH, or Normal Trading Hours, gaps higher or lower, there is no adjustment needed.
Using an SMA of 8 for example, Every morning at the open the average would show day 1, day 2, ect / 8 looking back the previous 8 days and you would have a mathematically correct number for the average. What are we missing? Can you trade after hours? Generally not. It's straight forward math.
How long have you been trading, what is your time frame and instruments traded?
 

Enable

New member
Well lets just say I want it for personal reasons then @markos . I want to insert a picture to show my point but do not know how. I have been trading since 2012. I just believe there's no reason for me to wait for a moving average to reach the spot it should be at due to the overnight price change. If you see the script, they both end up at the same place on lets say a 5 min chart, its just the open that makes the difference.
 

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.
Top