Displaced Moving average extension For ThinkOrSwim

stecla

New member
VIP
For those of you who like displacing their moving averages to line up better here's a simple hack
to add the displacement back. This way your history lines up nicely but you get a decent prediction of what is not yet displaced.

########################################
# Displaced Extended Moving average
# Displaced Hull MA with an exponential
# extension to make it line up.
# MtxDev 2024 SLC
# Use. 20 and 4 or 10 and 2 give good charts.
# I like running two plots one for low and one for high.#
# that gives a really nice trading range. tighter than
# standard deviation.
########################################
input price = low;
input length = 20;
input displace = 4;


# Calculate moving averages
def HMA = MovingAverage(AverageType.HULL, price, length)[-displace];
def MA = MovingAverage(AverageType.EXPONENTIAL, price, displace);

# set up and calculate the extension starting at the last
# bar of the displaced HMA
def lastBar = HighestAll(if !IsNaN(price[-1]) then BarNumber() else Double.NaN);
def isLastBarOfHMA = BarNumber() == lastBar - displace;

#Shift the bar down to line up with the HMA
def MADiff = if BarNumber() <= lastBar - displace+1 then HMA - MA else MADiff[1];

#calculate the extension
def SMA_Extension = if BarNumber() > lastBar - displace then MA + MADiff else Double.NaN;

#do plotting
plot HMA_Plot = HMA;
HMA_Plot.DefineColor("Up", GetColor(1));
HMA_Plot.DefineColor("Down", GetColor(0));
HMA_Plot.AssignValueColor(if HMA > HMA[1] then HMA_Plot.Color("Up") else HMA_Plot.Color("Down"));
plot SMA_Extension_Plot = SMA_Extension;
SMA_Extension_Plot.SetDefaultColor(GetColor(4)); # Using a visible color to ensure it stands out
SMA_Extension_Plot.SetPaintingStrategy(PaintingStrategy.LINE);
SMA_Extension_Plot.SetLineWeight(2);
SMA_Extension_Plot.HideBubble();
SMA_Extension_Plot.HideTitle();
 

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

Here's an updated version with much better prediction. it takes the slope of the moving average and the rate of change of the moving average so that you can get a curve instead of a straight line.


#PROJECTED MOVING AVERAGE
#hint:<b>Projected Moving Average</b>\nThis script calculates the selected Moving Average type based on the selected price input and projects it into the future x number of bars, accounting for the slope and rate of change of the slope.\nThe projection is limited to a user-defined number of bars.\n Because you can project into the future you can displace and it still works.\nCopyright 2024 MTXDEV all rights reserved.
# Projected Moving Average
# Project the moving average into the future.
# Copyright 2024 MtxDev
#


input MovingAverageType = AverageType.Hull; #hint MovingAverageType: Select what type of Moving Average you want.
input maLength = 14; #hint maLength: Length for Hull Moving Average
input projectionBars = 14; #hint projectionBars: Number of bars to project into the future
input selprice = close; #hint price: What do you want averaged?Price input for both HMA and projection logic
input displace = 3; #hint displace: Enter the number of bars to displace so the average lines up.
input ProjectedSlopeLength = 10;#hint ProjectedSlopeLength: over how many bars do you want to calculate the slopeChange higher is more responsive
# Calculate the Hull Moving Average
def price = selprice[-displace];
def maValue = MovingAverage(MovingAverageType,price, maLength);

# Calculate the slope of the Hull Moving Average
def slopeLength = Round(maLength / projectedslopelength);
def currentSlope = (maValue - maValue[slopeLength]) / slopeLength;

# Store the last valid slope
def storedSlope = if !IsNaN(currentSlope) then currentSlope else CompoundValue(1, storedSlope[1], 0);

# Calculate and store the rate of change (ROC) of the slope
def slopeChange = if !IsNaN(currentSlope) then currentSlope - storedSlope[1] else Double.NaN;
def storedSlopeChange = if !IsNaN(slopeChange) then slopeChange else CompoundValue(1, storedSlopeChange[1], 0);

# Initialize projected slope and price
def lastProjectionPrice = if !IsNaN(price) then price else CompoundValue(1, lastProjectionPrice[1], 0);
def projectedSlope = CompoundValue(1, if IsNaN(price) then projectedSlope[1] + storedSlopeChange else storedSlope, storedSlope);
def projectedPrice = CompoundValue(1, if IsNaN(price) then projectedPrice[1] + projectedSlope else maValue, maValue);

# Capture the bar number of the last valid price
def lastValidBar = if !IsNaN(price) then BarNumber() else CompoundValue(1, lastValidBar[1], 0);

# Calculate future bar index relative to the last valid bar
def futureBarIndex = BarNumber() - lastValidBar;

# Limit projections to the specified number of bars
def withinProjectionLimit = futureBarIndex <= projectionBars and futureBarIndex > 0;

# Plot the projected HMA
plot pFutureHMA = if IsNaN(price) and withinProjectionLimit then projectedPrice else Double.NaN;
pFutureHMA.SetDefaultColor(Color.orange);
pFutureHMA.SetLineWeight(1);
pFutureHMA.SetPaintingStrategy(PaintingStrategy.points);

# Plot the current Hull Moving Average
plot pCurrentHMA = maValue;
pCurrentHMA.SetLineWeight(1);
 
#PROJECTED DISPLACED MOVING AVERAGE
#hint:<b>Projected Moving Average</b>\nThis script calculates the selected Moving Average based on the selected price input and projects it into the future, accounting for the slope and rate of change of the slope.\nThe projection is limited to a user-defined number of bars.\n Because you can project into the future you can displace and it still works.\n
# Projected Moving Average
# Project the moving average into the future.
#2024 MtxDev
#
# hint: <b>Projected Moving Average</b>\nThis script calculates the selected Moving Average based on the selected price input and projects it into the future, accounting for the slope and rate of change of the slope.\nThe projection is limited to a user-defined number of bars.

input MovingAverageType = AverageType.Hull; #hint MovingAverageType: Select what type of Moving Average you want.
input maLength = 14; #hint maLength: Length for Hull Moving Average
input projectionBars = 14; #hint projectionBars: Number of bars to project into the future
input selprice = close; #hint price: What do you want averaged?Price input for both HMA and projection logic
input displace = 3; #hint displace: Enter the number of bars to displace so the average lines up.
input ProjectedSlopeLength = 10;#hint ProjectedSlopeLength: over how many bars do you want to calculate the slopeChange higher is more responsive
# Calculate the Hull Moving Average
def price = selprice[-displace];
def maValue = MovingAverage(MovingAverageType,price, maLength);

# Calculate the slope of the Hull Moving Average
def slopeLength = Round(maLength / projectedslopelength);
def currentSlope = (maValue - maValue[slopeLength]) / slopeLength;

# Store the last valid slope
def storedSlope = if !IsNaN(currentSlope) then currentSlope else CompoundValue(1, storedSlope[1], 0);

# Calculate and store the rate of change (ROC) of the slope
def slopeChange = if !IsNaN(currentSlope) then currentSlope - storedSlope[1] else Double.NaN;
def storedSlopeChange = if !IsNaN(slopeChange) then slopeChange else CompoundValue(1, storedSlopeChange[1], 0);

# Initialize projected slope and price
def lastProjectionPrice = if !IsNaN(price) then price else CompoundValue(1, lastProjectionPrice[1], 0);
def projectedSlope = CompoundValue(1, if IsNaN(price) then projectedSlope[1] + storedSlopeChange else storedSlope, storedSlope);
def projectedPrice = CompoundValue(1, if IsNaN(price) then projectedPrice[1] + projectedSlope else maValue, maValue);

# Capture the bar number of the last valid price
def lastValidBar = if !IsNaN(price) then BarNumber() else CompoundValue(1, lastValidBar[1], 0);

# Calculate future bar index relative to the last valid bar
def futureBarIndex = BarNumber() - lastValidBar;

# Limit projections to the specified number of bars
def withinProjectionLimit = futureBarIndex <= projectionBars and futureBarIndex > 0;

# Plot the projected HMA
plot pFutureHMA = if IsNaN(price) and withinProjectionLimit then projectedPrice else Double.NaN;
pFutureHMA.SetDefaultColor(Color.orange);
pFutureHMA.SetLineWeight(1);
pFutureHMA.SetPaintingStrategy(PaintingStrategy.points);

# Plot the current Hull Moving Average
plot pCurrentHMA = maValue;
pCurrentHMA.SetLineWeight(1);
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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