Gapless Moving Average Indicator for ThinkorSwim

E

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;
 
T

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:
E

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:
T

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
 
E

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

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?
 
E

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.
 

Top