SMA extension

Christopher84

Well-known member
VIP
I know this is an odd question, but is it possible to extend the SMA 20 bars out? If so, how? Here is the SMA script. Any help is appreciated.

Code:
input price = close;
input length = 9;
input displace = 0;
input showBreakoutSignals = no;

plot SMA = Average(price[-displace], length);
 
Solution
Code:
declare weak_volume_dependency;

input price = close;
input ATR_length = 15;
input SMA_length = 6;
input displace = 0;
input multiplier_factor = 1.5;

def val = Average(price, sma_length);

def average_true_range = Average(TrueRange(high, close, low), length = atr_length);

plot Upper_Band = val[-displace] + multiplier_factor * average_true_range[-displace];
Upper_Band.SetStyle(Curve.SHORT_DASH);
Upper_Band.SetLineWeight(2);
Upper_Band.SetDefaultColor(Color.Gray);

def Middle_Band = val[-displace];
#Middle_Band.SetDefaultColor(GetColor(1));

plot Lower_Band = val[-displace] - multiplier_factor * average_true_range[-displace];
Lower_Band.SetStyle(Curve.SHORT_DASH);
Lower_Band.SetLineWeight(2)...
i tried adding the following code to return the bar number but its not working.

Code:
def crossing_bar = if close and extension crosses  extension2 then BarNumber() else crossing_bar[1];
AddLabel (yes, "crossing bar" +  (crossing_bar)  );

If i can get the bar number i can get the price its at but i must first get the bar number and the code isnt working. Im not really to familiar with extending to the right where there is are no bars to retrieve data. Perhaps @SleepyZ can chime in and help.
 

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

i tried adding the following code to return the bar number but its not working.

Code:
def crossing_bar = if close and extension crosses  extension2 then BarNumber() else crossing_bar[1];
AddLabel (yes, "crossing bar" +  (crossing_bar)  );

If i can get the bar number i can get the price its at but i must first get the bar number and the code isnt working. Im not really to familiar with extending to the right where there is are no bars to retrieve data. Perhaps @SleepyZ can chime in and help.

See if this helps
Code:
def cross = if extension crosses extension2 then barnumber() else double.nan;
plot x    = if barnumber()==highestall(cross) then extension else double.nan;
x.setpaintingStrategy(PaintingStrategy.ARROW_UP);
addchartbubble(barnumber()==highestall(cross), highestall(x), round(highestall(x)),color.white);
Capture.jpg
 
Code:
declare weak_volume_dependency;

input price = close;
input ATR_length = 15;
input SMA_length = 6;
input displace = 0;
input multiplier_factor = 1.5;

def val = Average(price, sma_length);

def average_true_range = Average(TrueRange(high, close, low), length = atr_length);

plot Upper_Band = val[-displace] + multiplier_factor * average_true_range[-displace];
Upper_Band.SetStyle(Curve.SHORT_DASH);
Upper_Band.SetLineWeight(2);
Upper_Band.SetDefaultColor(Color.Gray);

def Middle_Band = val[-displace];
#Middle_Band.SetDefaultColor(GetColor(1));

plot Lower_Band = val[-displace] - multiplier_factor * average_true_range[-displace];
Lower_Band.SetStyle(Curve.SHORT_DASH);
Lower_Band.SetLineWeight(2);
Lower_Band.SetDefaultColor(Color.Gray);

#Plotting STARC slope into extension area
#input Extension_length = 20
input extension_length_limited_to = 50;
def lastbar = if isnan(close[-1]) and !isnan(close) then barnumber() else double.nan;
def inertline = inertiaall(Upper_Band,2);
def EXT_Upper_Band = if !IsNaN(close()) then inertline else EXT_Upper_Band[1] + ((EXT_Upper_Band[1] - EXT_Upper_Band[2]) / (2 - 1));
plot extension = if barnumber()<=highestall(lastbar)+ extension_length_limited_to then EXT_Upper_Band else double.nan;
extension.SetStyle(Curve.SHORT_DASH);
extension.SetLineWeight(2);
extension.SetDefaultColor(Color.white);

def inertline2 = inertiaall(Lower_Band,2);
def EXT_Lower_Band = if !IsNaN(close()) then inertline2 else EXT_Lower_Band[1] + ((EXT_Lower_Band[1] - EXT_Lower_Band[2]) / (2 - 1));
plot extension2 = if barnumber()<=highestall(lastbar)+ extension_length_limited_to then EXT_Lower_Band else double.nan;
extension2.SetStyle(Curve.SHORT_DASH);
extension2.SetLineWeight(2);
extension2.SetDefaultColor(Color.white);
def cross = if extension crosses extension2 then barnumber() else double.nan;
plot x    = if barnumber()==highestall(cross) then extension else double.nan;
x.setpaintingStrategy(PaintingStrategy.ARROW_UP);
addchartbubble(barnumber()==highestall(cross), highestall(x), round(highestall(x)),color.white);
AddLabel (yes, "TEST " +  (cross)  );
AddLabel (yes, "Cross " +  (highestall(x))  );

nice work SleepyZ, i battled that for like 30 mins going crazy trying to figure it out, i used double.nan too but was strange the label wouldnt record the number.
even with your code Cross wont return the bar number on a label as the example above. thats strange, thats what had me stumped, i still dont understand why it doesnt. but i guess if the code is properly coded and finished like you did it then the final code works. thats strange. im baffled as to why cross doesnt return barnumber value in
AddLabel (yes, "TEST " + (cross) );
yet it still works correctly for final code.

all my previous codings for getting barnumber always returns a value in the label when testing if i have it coded right....this is coded right but the bar number value doesnt show, super strange.
 
Last edited:
Code:
declare weak_volume_dependency;

input price = close;
input ATR_length = 15;
input SMA_length = 6;
input displace = 0;
input multiplier_factor = 1.5;

def val = Average(price, sma_length);

def average_true_range = Average(TrueRange(high, close, low), length = atr_length);

plot Upper_Band = val[-displace] + multiplier_factor * average_true_range[-displace];
Upper_Band.SetStyle(Curve.SHORT_DASH);
Upper_Band.SetLineWeight(2);
Upper_Band.SetDefaultColor(Color.Gray);

def Middle_Band = val[-displace];
#Middle_Band.SetDefaultColor(GetColor(1));

plot Lower_Band = val[-displace] - multiplier_factor * average_true_range[-displace];
Lower_Band.SetStyle(Curve.SHORT_DASH);
Lower_Band.SetLineWeight(2);
Lower_Band.SetDefaultColor(Color.Gray);

#Plotting STARC slope into extension area
#input Extension_length = 20
input extension_length_limited_to = 50;
def lastbar = if isnan(close[-1]) and !isnan(close) then barnumber() else double.nan;
def inertline = inertiaall(Upper_Band,2);
def EXT_Upper_Band = if !IsNaN(close()) then inertline else EXT_Upper_Band[1] + ((EXT_Upper_Band[1] - EXT_Upper_Band[2]) / (2 - 1));
plot extension = if barnumber()<=highestall(lastbar)+ extension_length_limited_to then EXT_Upper_Band else double.nan;
extension.SetStyle(Curve.SHORT_DASH);
extension.SetLineWeight(2);
extension.SetDefaultColor(Color.white);

def inertline2 = inertiaall(Lower_Band,2);
def EXT_Lower_Band = if !IsNaN(close()) then inertline2 else EXT_Lower_Band[1] + ((EXT_Lower_Band[1] - EXT_Lower_Band[2]) / (2 - 1));
plot extension2 = if barnumber()<=highestall(lastbar)+ extension_length_limited_to then EXT_Lower_Band else double.nan;
extension2.SetStyle(Curve.SHORT_DASH);
extension2.SetLineWeight(2);
extension2.SetDefaultColor(Color.white);
def cross = if extension crosses extension2 then barnumber() else double.nan;
plot x    = if barnumber()==highestall(cross) then extension else double.nan;
x.setpaintingStrategy(PaintingStrategy.ARROW_UP);
addchartbubble(barnumber()==highestall(cross), highestall(x), round(highestall(x)),color.white);
AddLabel (yes, "TEST " +  (cross)  );
AddLabel (yes, "Cross " +  (highestall(x))  );

nice work SleepyZ, i battled that for like 30 mins going crazy trying to figure it out, i used double.nan too but was strange the label wouldnt record the number.
even with your code Cross wont return the bar number on a label as the example above. thats strange, thats what had me stumped, i still dont understand why it doesnt. but i guess if the code is properly coded and finished like you did it then the final code works. thats strange. im baffled as to why cross doesnt return barnumber value in
AddLabel (yes, "TEST " + (cross) );
yet it still works correctly for final code.

all my previous codings for getting barnumber always returns a value in the label when testing if i have it coded right....this is coded right but the bar number value doesnt show, super strange.
Sometimes the extensions do not cross within the extended chart area, causing no values for the labels. When a cross is present, this should produce values for your labels.
Code:
AddLabel (yes, "TEST " +  highestall(cross)  );
AddLabel (yes, "Cross " +  (highestall(x))  );
 
Solution
Huge thank you to everyone who contributed their time and effort on this! XeoNox and SleepyZ especially. I will be using this through the week. Definitely excited to see how this works.
 
I know this is an odd question, but is it possible to extend the SMA 20 bars out? If so, how? Here is the SMA script. Any help is appreciated.

Code:
input price = close;
input length = 9;
input displace = 0;
input showBreakoutSignals = no;

plot SMA = Average(price[-displace], length);


the extension value doesnt refresh itself. i had to change the timeframe of the chart and come back then it shows the value. otherwise it is stuck. Any idea what could be done? TIA
 
the extension value doesnt refresh itself. i had to change the timeframe of the chart and come back then it shows the value. otherwise it is stuck. Any idea what could be done? TIA
I’m not really sure. I haven’t used the SMA extension indicator really. I do use the version with the STARC band extensions all the time though. I haven’t had trouble with that version. I’m sorry I can’t be of more help.
 
I’m not really sure. I haven’t used the SMA extension indicator really. I do use the version with the STARC band extensions all the time though. I haven’t had trouble with that version. I’m sorry I can’t be of more help.
Thanks a lot for the response. The best way to solve this problem is to somehow refresh the chart. Any idea how we can do that? TIA
 
Code:
declare weak_volume_dependency;

input price = close;
input ATR_length = 15;
input SMA_length = 6;
input displace = 0;
input multiplier_factor = 1.5;

def val = Average(price, sma_length);

def average_true_range = Average(TrueRange(high, close, low), length = atr_length);

plot Upper_Band = val[-displace] + multiplier_factor * average_true_range[-displace];
Upper_Band.SetStyle(Curve.SHORT_DASH);
Upper_Band.SetLineWeight(2);
Upper_Band.SetDefaultColor(Color.Gray);

def Middle_Band = val[-displace];
#Middle_Band.SetDefaultColor(GetColor(1));

plot Lower_Band = val[-displace] - multiplier_factor * average_true_range[-displace];
Lower_Band.SetStyle(Curve.SHORT_DASH);
Lower_Band.SetLineWeight(2);
Lower_Band.SetDefaultColor(Color.Gray);

#Plotting STARC slope into extension area
#input Extension_length = 20
input extension_length_limited_to = 50;
def lastbar = if isnan(close[-1]) and !isnan(close) then barnumber() else double.nan;
def inertline = inertiaall(Upper_Band,2);
def EXT_Upper_Band = if !IsNaN(close()) then inertline else EXT_Upper_Band[1] + ((EXT_Upper_Band[1] - EXT_Upper_Band[2]) / (2 - 1));
plot extension = if barnumber()<=highestall(lastbar)+ extension_length_limited_to then EXT_Upper_Band else double.nan;
extension.SetStyle(Curve.SHORT_DASH);
extension.SetLineWeight(2);
extension.SetDefaultColor(Color.white);

def inertline2 = inertiaall(Lower_Band,2);
def EXT_Lower_Band = if !IsNaN(close()) then inertline2 else EXT_Lower_Band[1] + ((EXT_Lower_Band[1] - EXT_Lower_Band[2]) / (2 - 1));
plot extension2 = if barnumber()<=highestall(lastbar)+ extension_length_limited_to then EXT_Lower_Band else double.nan;
extension2.SetStyle(Curve.SHORT_DASH);
extension2.SetLineWeight(2);
extension2.SetDefaultColor(Color.white);
def cross = if extension crosses extension2 then barnumber() else double.nan;
plot x    = if barnumber()==highestall(cross) then extension else double.nan;
x.setpaintingStrategy(PaintingStrategy.ARROW_UP);
addchartbubble(barnumber()==highestall(cross), highestall(x), round(highestall(x)),color.white);
AddLabel (yes, "TEST " +  (cross)  );
AddLabel (yes, "Cross " +  (highestall(x))  );

nice work SleepyZ, i battled that for like 30 mins going crazy trying to figure it out, i used double.nan too but was strange the label wouldnt record the number.
even with your code Cross wont return the bar number on a label as the example above. thats strange, thats what had me stumped, i still dont understand why it doesnt. but i guess if the code is properly coded and finished like you did it then the final code works. thats strange. im baffled as to why cross doesnt return barnumber value in
AddLabel (yes, "TEST " + (cross) );
yet it still works correctly for final code.

all my previous codings for getting barnumber always returns a value in the label when testing if i have it coded right....this is coded right but the bar number value doesnt show, super strange.
I’m not really sure. I haven’t used the SMA extension indicator really. I do use the version with the STARC band extensions all the time though. I haven’t had trouble with that version. I’m sorry I can’t be of more help.
Hi Christopher84,

Just had a quick question. How exactly are you using the cross? Can you please elaborate?

Any help and guidance is highly appreciated.

Thank you in Advance
 
Hi Christopher84,

Just had a quick question. How exactly are you using the cross? Can you please elaborate?

Any help and guidance is highly appreciated.

Thank you in Advance
Hi chinks1980!
When I see a consistent zone where the extensions are crossing, it can indicate support/resistance zones as well as directionality of future price movements. More than anything, I look for the extensions to agree on direction and not be crossed. When this happens price will be moving the indicated direction. When the extensions cross, especially when their direction is contradictory (one up and one down), price movement will start to stagnate and potentially shift into a reversal.
 
See if this helps
Code:
# Plotting MA slope into extension area (Modded v2021.1)
# Mobius
# Chat Room Request
#Modded by XeoNoX for more recent data and incorporation of inertia in v2021.1
#draws extended line to the right of the chart based off recent linear regression curve of the Moving Average
#Mod by Sleepyz to limit length of extension
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);
Hey @SleepyZ!
Are you on the discord channel?
 
I added on to Mobius's code, all credits to him, i incorporated inertia plus made it so it uses the more recent data.

Plotting MA slope into extension area (Modded) .. All original credits to MOBIUS
Modded by XeoNoX for more recent data and incorporation of inertia in v2021.1
Draws extended line to the right of the chart based off recent linear regression curve of the Moving Average

(Most likely used for attempting future moving average prediction)
Code:
# Plotting MA slope into extension area (Modded v2021.1)
# Mobius
# Chat Room Request
#Modded by XeoNoX for more recent data and incorporation of inertia in v2021.1
#draws extended line to the right of the chart based off recent linear regression curve of the Moving Average
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));
plot extension = EXT_MA;
extension.SetDefaultColor(CreateColor(0, 255, 255));
extension.SetLineWeight(1);


lMOtui8.jpg
Is it possible to have the extension color dynamically base on the slope of the current average? AssignValueColor will only work on the current bar (won't color into the future), and setdefaultcolor will color into the future, but it doesn't allow for dynamic coloring based on slope. Have any thoughts on how to do this?
 
Is it possible to have the extension color dynamically base on the slope of the current average? AssignValueColor will only work on the current bar (won't color into the future), and setdefaultcolor will color into the future, but it doesn't allow for dynamic coloring based on slope. Have any thoughts on how to do this?
Tried various scenarios w/ [-1] and isNan, etc.. My conclusion is that there is not the capability for dynamic conditional coloring
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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