Moving Average Extension

dbenz45

New member
What I am looking for is a Hurst Channel Indicator to be made in TOS.
I am looking for an indicator that will have 3 simple moving averages calculated based on the closing price of a stock to be displayed on the chart. These simple moving averages are displaced into the past by half the moving average number selected. There is the first moving average which is simply a moving average displaced into the past by half its length; then the other 2 moving averages: one is displaced vertically upwards and one is displaced vertically downwards by the same distance, these other two moving averages form a channel around the stock price and these other two moving averages use the first moving average as the centerline of the channel.

the trick is not doing a moving average with bands around it like Bollinger bands or Keltner channels because these don't enforce a constant vertical channel width
then the real trick is getting dotted lines to start where the simple moving averages left off (keep in mind these moving averages end before the most recent bar because they are displaced into the past) and project a potential channel continuation with same slope and same curvature projected forward to the present day and beyond into the future by half the number of bars selected for the moving average. The distance between the moving average in the middle the upper and lower bounds of the channel is not only must it be constant vertical width, but the channels must contain most of the price data, if the vertical width has to be changed significantly to incorporate an outlying price bar then don't widen the channel just for a few price points on the chart. then the last final touch is allow for adjustment of a smoothing factor of the moving average projections if desired. I have made a few stabs at this and still working on it. Any help would be greatly appreciated! And obviously will benefit anyone else who wants to tryout Hurst Channels in TOS!!
 
Solution
this is a start, but i cant displace this into the past still

See if the code below is what you want. The image shows the extended MA displaced and the non-displaced MA (dashes).

Screenshot-2022-11-15-032235.png
Ruby:
#MovingAverage_Extended

input displace = 30;
input MAlength = 20;
input AvgType = AverageType.SIMPLE;
input price = close;

def MA = if !IsNaN(close())
         then MovingAverage(AvgType, price, malength)
         else MA[1] + ((MA[1] - MA[malength]) / (malength - 1));

plot moveavg = MA[-displace];
moveavg.SetDefaultColor(CreateColor(0, 255, 0));
moveavg.SetLineWeight(1);
I use moving averages frequently that I displace into the past, can anyone help me add some code to add a moving average extension line? I have a code that plots 3 vertically displaced moving averages to form a price envelope, what i am hoping is to have extension lines plotted as hypothetical future extensions of the moving averages if they continued their present slope and curvature, this should be able to be done by taking a moving average of the last 2 points on a moving average and extending that into the future, here is a code I found on here, but when I try and displace these moving averages into the past, the code no longer will plot an extension, any ideas? (this code works, just looking for a way to displace the MA into the past with the extension line still being able to work and be plotted on the chart.

input MAlength = 20;
input AvgType = AverageType.SIMPLE;
input price = close;

plot moveavg = MovingAverage(AvgType, price, MAlength);
moveavg.SetDefaultColor(CreateColor(0, 255, 0));
moveavg.SetLineWeight(1);

def inertline = inertiaall(moveavg,2);
def EXT_MA = if !IsNaN(close()) then inertline else EXT_MA[1] + ((EXT_MA[1] - EXT_MA[2]) / (2 - 1));

input extension_length_limited_to = 20;
def lastbar = if isnan(close[-1]) and !isnan(close) then barnumber() else double.nan;

plot extension = if barnumber()<=highestall(lastbar)+ extension_length_limited_to then EXT_MA else double.nan;
extension.SetDefaultColor(CreateColor(0, 255, 255));
extension.SetLineWeight(1);
 
I use moving averages frequently that I displace into the past, can anyone help me add some code to add a moving average extension line? I have a code that plots 3 vertically displaced moving averages to form a price envelope, what i am hoping is to have extension lines plotted as hypothetical future extensions of the moving averages if they continued their present slope and curvature, this should be able to be done by taking a moving average of the last 2 points on a moving average and extending that into the future, here is a code I found on here, but when I try and displace these moving averages into the past, the code no longer will plot an extension, any ideas? (this code works, just looking for a way to displace the MA into the past with the extension line still being able to work and be plotted on the chart.

input MAlength = 20;
input AvgType = AverageType.SIMPLE;
input price = close;

plot moveavg = MovingAverage(AvgType, price, MAlength);
moveavg.SetDefaultColor(CreateColor(0, 255, 0));
moveavg.SetLineWeight(1);

def inertline = inertiaall(moveavg,2);
def EXT_MA = if !IsNaN(close()) then inertline else EXT_MA[1] + ((EXT_MA[1] - EXT_MA[2]) / (2 - 1));

input extension_length_limited_to = 20;
def lastbar = if isnan(close[-1]) and !isnan(close) then barnumber() else double.nan;

plot extension = if barnumber()<=highestall(lastbar)+ extension_length_limited_to then EXT_MA else double.nan;
extension.SetDefaultColor(CreateColor(0, 255, 255));
extension.SetLineWeight(1);

See if this helps

Screenshot-2022-11-14-155445.png
Ruby:
#MovingAverage_Extended

input MAlength = 20;
input AvgType = AverageType.SIMPLE;
input price = close;

def MA = if !IsNaN(close())
         then MovingAverage(AvgType, price, malength)
         else MA[1] + ((MA[1] - MA[malength]) / (malength - 1));

plot moveavg = MA;
moveavg.SetDefaultColor(CreateColor(0, 255, 0));
moveavg.SetLineWeight(1);
 
this is a start, but i cant displace this into the past still

See if the code below is what you want. The image shows the extended MA displaced and the non-displaced MA (dashes).

Screenshot-2022-11-15-032235.png
Ruby:
#MovingAverage_Extended

input displace = 30;
input MAlength = 20;
input AvgType = AverageType.SIMPLE;
input price = close;

def MA = if !IsNaN(close())
         then MovingAverage(AvgType, price, malength)
         else MA[1] + ((MA[1] - MA[malength]) / (malength - 1));

plot moveavg = MA[-displace];
moveavg.SetDefaultColor(CreateColor(0, 255, 0));
moveavg.SetLineWeight(1);
 
Solution

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