example, draw non horizontal lines after the last bar

halcyonguy

Moderator - Expert
VIP
Lifetime
i posted this to a thread a while ago, and was just looking for it to reference for someone else.
thought it might benefit others , so posting a copy here, to be found easier.

draws 4 different types of non horizontal lines after the last bar, diagonal, curve, sinewave
https://usethinkscript.com/threads/plotting-in-the-expansion-area.9765/#post-89607

Ruby:
# formulaafterlastbar_00C

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

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[1];

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] + 1);


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

# https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/Sin
# start with the sin example
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[1] + (formula2[1] * 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[1] )
 else slope[1];

def formula3 = if (bn < lastbarbn or bn > (lastbarbn + formula_bars)) then na
  else if  bn == (lastbarbn) then close
    else (formula3[1] + 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[1] + (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);

#addchartbubble(1,70,rand, color.cyan, no);

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

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