# Plotting in the Expansion Area

#### Spaghetti Coder

##### New member
Hi all,

I'd like to experiment with a few price prediction models, and to do this I need a way to draw a curve that goes from the bar immediately to the right of the current bar to about 20-50 bars in the future in the expansion area.

It's somewhat similar to the plot created by this study, however with the exception that the curve always exists within a predefined number of bars immediately to the right of the last bar, and the line should repaint/update itself as new price information comes in. Another example would be the built-in TTM_LRC study which most likely makes use of the InertiaAll function.

The main problems I'm facing are how can I build an array of results that can then be painted and repainted in an area that is really a pain to draw in?

I'd appreciate any pointers you can offer, thanks!

Solution
Hi all,

I'd like to experiment with a few price prediction models, and to do this I need a way to draw a curve that goes from the bar immediately to the right of the current bar to about 20-50 bars in the future in the expansion area.

It's somewhat similar to the plot created by this study, however with the exception that the curve always exists within a predefined number of bars immediately to the right of the last bar, and the line should repaint/update itself as new price information comes in. Another example would be the built-in TTM_LRC study which most likely makes use of the InertiaAll function.

The main problems I'm facing are how can I build an array of results that can then be painted and repainted in an area that...
Hi all,

I'd like to experiment with a few price prediction models, and to do this I need a way to draw a curve that goes from the bar immediately to the right of the current bar to about 20-50 bars in the future in the expansion area.

It's somewhat similar to the plot created by this study, however with the exception that the curve always exists within a predefined number of bars immediately to the right of the last bar, and the line should repaint/update itself as new price information comes in. Another example would be the built-in TTM_LRC study which most likely makes use of the InertiaAll function.

The main problems I'm facing are how can I build an array of results that can then be painted and repainted in an area that is really a pain to draw in?

I'd appreciate any pointers you can offer, thanks!
maybe these examples will help

to draw in the expansion area, define a time period and price levels.
any time period on a chart can be drawn on. even though the expansion area doesn't have bars, there are bar numbers that can be referenced.

this study plots 4 formulas, after the last bar.

pick a number, for the quantity of bars to plot, after the last bar.
input formula_bars = 50;

cnt, counts from 1 to the quantity of bars, formula_bars.
lastcls, holds the value of close from the last bar.

formula_bars = 50 , bars after last bar to plot a formula
cyan , formula1 , sine function
magenta , formula2 , a random number is added to previous value
yellow , formula3 , straight line. a number is added to previous value
green , formula4 , curve. a number x cnt is added to previous value Ruby:
``````# formulaafterlastbar_00C

def na = Double.NaN;
def bn = BarNumber();
def lastbar = !IsNaN(close) and IsNaN(close[-1]);
def lastbarbn = if lastbar then bn else lastbarbn;

input formula_bars = 50;

# ref signal , close of last bar
def lastcls = if lastbar then close
else if (bn < lastbarbn or bn > (lastbarbn + formula_bars)) then na
else lastcls;

input plot_horz_line_after = yes;
plot z1 = if plot_horz_line_after then lastcls else na;
z1.SetDefaultColor(Color.GRAY);

def start_count = 1;
# counting numbers for formula , 1 to curve_bars
def cnt = if (bn <= lastbarbn or bn > (lastbarbn + formula_bars)) then na
else if bn == (lastbarbn + 1) then start_count
else (cnt + 1);

# ////////////////////////////////////////
#  test formula 1

# https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/Sin
input amplitude_factor = 0.8;
input periodBars = 16;
def angle = 2 * Double.Pi / periodBars;

# replace my formula , with your formula.
#   baseline - equal to the close of the last bar
#   cnt - a counter with valid #'s, only during the desired bar range, 1 to formula_bars
def formula1 = lastcls + (amplitude_factor * Sin(angle * cnt));

plot f1 = formula1;
f1.SetDefaultColor(Color.CYAN);
# f1.SetStyle(Curve.MEDIUM_DASH);
f1.HideBubble();
# ////////////////////////////////////////

# ////////////////////////////////////////
#  test formula 2
# plot a line with a random number added to the previous value
# % range  -1.5% to 1.5% of prev , added to prev
input formula2_per = 1.5;
def rand = ( (2 * formula2_per / 100) * Random()) - (formula2_per / 100);

def start2 = lastcls;
# change the 3rd line of this formula to your formula
def formula2 =  if (bn < lastbarbn or bn > (lastbarbn + formula_bars)) then na
else if  bn == (lastbarbn + 1) then start2
else (formula2 + (formula2 * rand));

plot f2 = formula2;
f2.SetDefaultColor(Color.MAGENTA);
# f2.SetStyle(Curve.MEDIUM_DASH);
f2.HideBubble();
# ////////////////////////////////////////

# ////////////////////////////////////////
#  test formula 3
# plot line that has same slope as the slope between the last 2 bars
def slope = if bn < lastbarbn then na
else if lastbar then (close - close )
else slope;

def formula3 = if (bn < lastbarbn or bn > (lastbarbn + formula_bars)) then na
else if  bn == (lastbarbn) then close
else (formula3 + slope);

plot f3 = formula3;
f3.SetDefaultColor(Color.YELLOW);
# f2.SetStyle(Curve.MEDIUM_DASH);
f3.HideBubble();
# ////////////////////////////////////////

# ////////////////////////////////////////
#  test formula 4
# curve
input slopefactor = 0.024;
def formula4 = if (bn < lastbarbn or bn > (lastbarbn + formula_bars)) then na
else if  bn == (lastbarbn) then close
else (formula4 + (cnt * slope * slopefactor));

plot f4 = formula4;
f4.SetDefaultColor(Color.green);
# f2.SetStyle(Curve.MEDIUM_DASH);
f4.HideBubble();
# ////////////////////////////////////////

# --------------------------------------------

input test2_bn_label = yes;
AddLabel(test2_bn_label, " bar# after last bar " + (lastbarbn + 1), Color.CYAN);
AddLabel(test2_bn_label, "+ formula_bars: " + formula_bars, Color.CYAN);
AddLabel(test2_bn_label, " to " + (lastbarbn + formula_bars), Color.CYAN);

input test1_bn_bubbles = no;
AddChartBubble(test1_bn_bubbles and !IsNaN(cnt), lastcls, bn , Color.CYAN, no);
#``````
hal_exp extend, expansion, sine, diagonal

Last edited:
This is exactly what I'm looking for! I thought it probably couldn't be done since I didn't get any replies for a while. Thank you so much!

• halcyonguy
I tried to make this work by projecting the MidLine of the StandardErrorBands into the expansion area; but, failed to properly calculate the slope of the MidLine.

How would you calculate the slope of the MidLine at the previous two candles and then project it into the expansion area?

input price = close;
input linRegLength = 21;
input smLength = 3;

def value = Average(Inertia(price, linRegLength), smLength);

def newValue = if !IsNaN(price)
then value
else newValue + ((newValue[linRegLength] - newValue ) / (1-linRegLength)); # slope

plot MidLine = newValue;

MidLine.SetDefaultColor(GetColor(5));

I tried to make this work by projecting the MidLine of the StandardErrorBands into the expansion area; but, failed to properly calculate the slope of the MidLine.

How would you calculate the slope of the MidLine at the previous two candles and then project it into the expansion area?

input price = close;
input linRegLength = 21;
input smLength = 3;

def value = Average(Inertia(price, linRegLength), smLength);

def newValue = if !IsNaN(price)
then value
else newValue + ((newValue[linRegLength] - newValue ) / (1-linRegLength)); # slope

plot MidLine = newValue;

MidLine.SetDefaultColor(GetColor(5));

i'm going to assume your post got moved to here and you haven't looked at my post#2

study my post
look at formula#3 , that part draws a sloped straight line

leave the original code alone

add new code to draw a line after the lastbar.
calc the slope of the signal on the last 2 bars. then add that slope ( a \$\$ value) to the previous value, over and over, for 50 of bars.

Ruby:
``````# StandardErrorBands_extend

# StandardErrorBands
# TD Ameritrade IP Company, Inc. (c) 2009-2022
input price = close;
input linRegLength = 21;
input smLength = 3;
input displace = 0;
input Num_Dev_Dn = -2.0;
input Num_Dev_Up = 2.0;

def value = Average(Inertia(price[-displace], linRegLength), smLength);
def error = Average(sterr(price[-displace], linRegLength), smLength);

plot MidLine = value;
plot LowerBand = value + Num_Dev_Dn * error;
plot UpperBand = value + Num_Dev_Up * error;

MidLine.SetDefaultColor(GetColor(5));
UpperBand.SetDefaultColor(GetColor(8));
LowerBand.SetDefaultColor(GetColor(1));

#-----------------------------

def na = Double.NaN;
def bn = BarNumber();
def lastbar = !IsNaN(close) and IsNaN(close[-1]);
def lastbarbn = if lastbar then bn else lastbarbn;

input formula_bars = 50;

# ////////////////////////////////////////
#  test formula 3
# plot line that has same slope as the slope between the last 2 bars
def slope = if bn < lastbarbn then na
# else if lastbar then (close - close )
else if lastbar then (midline - midline )
else slope;

def formula3 = if (bn < lastbarbn or bn > (lastbarbn + formula_bars)) then na
else if  bn == (lastbarbn) then midline
else (formula3 + slope);

plot f3 = formula3;
f3.SetDefaultColor(Color.YELLOW);
# f2.SetStyle(Curve.MEDIUM_DASH);
f3.HideBubble();
# ////////////////////////////////////////
#``````

• ben and MerryDay
This is amazing. Thank you for implementing it with SEB for me, I understand how you did it enough to carry this forward with other indicators. This is awesome! Thank you very much.

• halcyonguy

### Not the exact question you're looking for?

87k+ Posts
332 Online ## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?