Horizontal ray from triggered from simple condition

gee frank

New member
I need to project a horizontal ray, 5 bars out(to the right) every time a condition is triggered. Let's say two moving averages cross. I work mostly on intraday charts. I've seen the lookbacks but I need this ray generated in real time. Looked at Halcyon's work on lines but nothing seems to fit. My horizontal lines project out one bar at a time so are practically useless as a visual level, while I need a fixed short line drawn all at once when the indicators are triggered. How do I do this? TIA.

input price = close;
input length1 = 9;
input length2 = 14;
input averageType1 = AverageType.SIMPLE;
input averageType2 = AverageType.SIMPLE;

def avg1 = MovingAverage(averageType1, price, length1);
def avg2 = MovingAverage(averageType2, price, length2);

def sigtestLine1 = if avg1 crosses above avg2 then avg1 else sigtestLine1[1];
def sigtestLine2 = if avg1 crosses below avg2 then avg1 else sigtestLine2[1];

plot signalup = sigtestLine1;
signalup.setDefaultColor(color.green);
signalup.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

Plot signaldown = sigtestLine2;
signaldown.setDefaultColor(color.red);
signaldown.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
 
Solution
I need to project a horizontal ray, 5 bars out(to the right) every time a condition is triggered. Let's say two moving averages cross. I work mostly on intraday charts. I've seen the lookbacks but I need this ray generated in real time. Looked at Halcyon's work on lines but nothing seems to fit. My horizontal lines project out one bar at a time so are practically useless as a visual level, while I need a fixed short line drawn all at once when the indicators are triggered. How do I do this? TIA.

input price = close;
input length1 = 9;
input length2 = 14;
input averageType1 = AverageType.SIMPLE;
input averageType2 = AverageType.SIMPLE;

def avg1 = MovingAverage(averageType1, price, length1);
def avg2 = MovingAverage(averageType2...
I need to project a horizontal ray, 5 bars out(to the right) every time a condition is triggered. Let's say two moving averages cross. I work mostly on intraday charts. I've seen the lookbacks but I need this ray generated in real time. Looked at Halcyon's work on lines but nothing seems to fit. My horizontal lines project out one bar at a time so are practically useless as a visual level, while I need a fixed short line drawn all at once when the indicators are triggered. How do I do this? TIA.

input price = close;
input length1 = 9;
input length2 = 14;
input averageType1 = AverageType.SIMPLE;
input averageType2 = AverageType.SIMPLE;

def avg1 = MovingAverage(averageType1, price, length1);
def avg2 = MovingAverage(averageType2, price, length2);

def sigtestLine1 = if avg1 crosses above avg2 then avg1 else sigtestLine1[1];
def sigtestLine2 = if avg1 crosses below avg2 then avg1 else sigtestLine2[1];

plot signalup = sigtestLine1;
signalup.setDefaultColor(color.green);
signalup.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

Plot signaldown = sigtestLine2;
signaldown.setDefaultColor(color.red);
signaldown.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);


try adding this to the end of your study

Code:
def x = avg1 crosses above avg2;
def y = if (x[1] or x[2] or x[3] or x[4] or x[5]) then sigtestLine1 else double.nan;
plot z = y;
 
Solution
Thanks for the prompt reply!! I tried it on OnDemand to see how it works in real time. It still prints one bar at a time while I need a five bar wide horizontal ray drawn from the trigger condition all at once. That is the trigger bar line and that line continued 4 bars into future at that point.

kdX0b7l.jpg
 
Thanks for the prompt reply!! I tried it on OnDemand to see how it works in real time. It still prints one bar at a time while I need a five bar wide horizontal ray drawn from the trigger condition all at once. That is the trigger bar line and that line continued 4 bars into future at that point.

here is an updated version

this will draw lines , after 2 lines cross.
if the crossing is near the last bar, the line will extend after the last bar.


i ended up adding a check, if a price is an error, so the formula would be able to hold a value onto future bars
def sigtestLine1 = if isnan(close) then sigtestLine1[1] else if xup then avg1 else sigtestLine1[1];


Code:
# avg_cross_line_01


def na = Double.NaN;
def bn = BarNumber();

input line_length_bars = 5;

def lastbar = !isnan(close[0]) and isnan(close[-1]);
def lastbn = if lastbar then bn else lastbn[1];


input price = close;
input length1 = 9;
input length2 = 14;
input averageType1 = AverageType.SIMPLE;
input averageType2 = AverageType.SIMPLE;

def avg1 = MovingAverage(averageType1, price, length1);
def avg2 = MovingAverage(averageType2, price, length2);

def xup = avg1 crosses above avg2;
def xdwn = avg1 crosses below avg2;

def sigtestLine1 = if isnan(close) then sigtestLine1[1] else if xup then avg1 else sigtestLine1[1];
def sigtestLine2 = if isnan(close) then sigtestLine2[1] else if xdwn then avg1 else sigtestLine2[1];

def line1bn = if isnan(close) then line1bn[1] else if xup then bn else line1bn[1];
def line2bn = if isnan(close) then line2bn[1] else if xdwn then bn else line2bn[1];

input show_avg_lines = yes;
plot z1 = if show_avg_lines then avg1 else na;
z1.SetDefaultColor(GetColor(1));
#z1.setlineweight(1);
z1.HideBubble();

plot z2 = if show_avg_lines then avg2 else na;
z2.SetDefaultColor(GetColor(2));
#z2.setlineweight(1);
z2.HideBubble();

input test1 = no;
plot signalup = if test1 then sigtestLine1 else na;
signalup.SetDefaultColor(Color.GREEN);
#signalup.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
plot signaldown = if test1 then sigtestLine2 else na;
signaldown.SetDefaultColor(Color.RED);
#signaldown.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);


#def line1ok = (bn >= line1bn) and bn <= (line1bn + line_length_bars);
def line1ok = between(bn, line1bn, (line1bn + line_length_bars));
def line2ok = between(bn, line2bn, (line2bn + line_length_bars));


#def xu = xup;
#def yu = if (xu[1] or xu[2] or xu[3] or xu[4] or xu[5]) then sigtestLine1 else Double.NaN;

def yu = if line1ok then sigtestLine1 else Double.NaN;
plot zu = yu;
zu.SetDefaultColor(Color.GREEN);
zu.SetLineWeight(2);
zu.HideBubble();

#def xd = xdwn;
#def yd = if (xd[1] or xd[2] or xd[3] or xd[4] or xd[5]) then sigtestLine2 else Double.NaN;
def yd = if line2ok then sigtestLine2 else Double.NaN;
plot zd = yd;
zd.SetDefaultColor(Color.RED);
zd.SetLineWeight(2);
zd.HideBubble();


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

addchartbubble(0,80.6,
sigtestLine1 + "\n" +
sigtestLine2
, color.yellow, no);


addchartbubble(0, 80.3,
 bn + "\n" +
 lastbn + "\n" +
 line1bn + "\n" +
 (line1bn + line_length_bars) + "\n" +
 line1ok 
, color.yellow, no);
#

line length set to 14
line extends into expansion area, after the last bar
xff2CX5.jpg
 
Works like a charm. You sir, are a true stable genius.
I should have just locked the condition into the low or high of the bar for simplicity sake. The "linelock" check is a gamechanger.
Thanks again.
 

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