# Need help coding conditions

##### New member
Hello everyone, I'm new to the forum and I just want to say you guys have done a great job and I love all of your work!!

I'm trying to code an indicator with 2 conditions, this is using 3 MA's (MA 6, MA 24 and MA 90)

Declare upper
First Condition:
If Line 1 (MA 6) crosses line 3 (MA 24) at Line 2 (MA 90) then plot red arrow above
The candle and plot a red cloud above the candle until condition 2 hits

Second condition:
If Line 3 (MA 24), crosses line 1 (MA 6) at line 2 (MA 90) then plot Green arrow below
The candle and plot green cloud below the candle until condition 1 hits

But it has to be when they cross at Line 2. I'm using MA's as an example.

Here is an image of what I'm trying to accomplish! any help is truly appreciate it!

Solution
Hello everyone, I'm new to the forum and I just want to say you guys have done a great job and I love all of your work!!

I'm trying to code an indicator with 2 conditions, this is using 3 MA's (MA 6, MA 24 and MA 90)

Declare upper
First Condition:
If Line 1 (MA 6) crosses line 3 (MA 24) at Line 2 (MA 90) then plot red arrow above
The candle and plot a red cloud above the candle until condition 2 hits

Second condition:
If Line 3 (MA 24), crosses line 1 (MA 6) at line 2 (MA 90) then plot Green arrow below
The candle and plot green cloud below the candle until condition 1 hits

But it has to be when they cross at Line 2. I'm using MA's as an example.

Here is an image of what I'm trying to accomplish! any help is truly appreciate...
Hello everyone, I'm new to the forum and I just want to say you guys have done a great job and I love all of your work!!

I'm trying to code an indicator with 2 conditions, this is using 3 MA's (MA 6, MA 24 and MA 90)

Declare upper
First Condition:
If Line 1 (MA 6) crosses line 3 (MA 24) at Line 2 (MA 90) then plot red arrow above
The candle and plot a red cloud above the candle until condition 2 hits

Second condition:
If Line 3 (MA 24), crosses line 1 (MA 6) at line 2 (MA 90) then plot Green arrow below
The candle and plot green cloud below the candle until condition 1 hits

But it has to be when they cross at Line 2. I'm using MA's as an example.

Here is an image of what I'm trying to accomplish! any help is truly appreciate it!

EDIT: changed code.
. added average formulas so can do actual testing

i'm going to guess that 3 lines will almost never cross at the same time. i think you will need to check if the 2 other lines have crossed in the current bar gap or the previous bar gap.

i think the direction of the line doing the crossing is important and should be mentioned in your rules.
Line 1 crosses below line 3

in rule 2, you ref line 3 has an action instead of line 1. i know, it works out the same, but it is confusing. easier to think about it if , 1 condition has a line going down and a 2nd condition has it going up.

this has 2 sets of rules formulas , for you to experiment with.
bubbles will show a 0 or 1 , for the 2 rules. 1 when it is true.

this variable is used in the 2nd set of rules, to look that many bars back for a crossing of line2. ( crosses within 'xbars' bars)
xbars = 1

Code:
``````# checkfor2_crossings_00

def na = double.nan;
input avg1_len = 6;
input avg1_type =  AverageType.simple;
def line1 = MovingAverage(avg1_type, close, avg1_len);

input avg2_len = 90;
input avg2_type =  AverageType.simple;
def line2 = MovingAverage(avg2_type, close, avg2_len);

input avg3_len = 24;
input avg3_type =  AverageType.simple;
def line3 = MovingAverage(avg3_type, close, avg3_len);

input show_averages = yes;
plot z1 = if show_averages then line1 else na;
plot z2 = if show_averages then line2 else na;
plot z3 = if show_averages then line3 else na;
z1.setdefaultcolor(getcolor(6));
z2.setdefaultcolor(getcolor(5));
z3.setdefaultcolor(getcolor(3));

addlabel(show_averages , "Line1 " + avg1_len, getcolor(6));
addlabel(show_averages , "Line2 " + avg2_len, getcolor(5));
addlabel(show_averages , "Line3 " + avg3_len, getcolor(3));

input show_bubbles_just_true_rules = yes;
input xbars = 1;

def rule1 = Line1 crosses below line3 and line1 crosses line2;
def rule2 = Line1 crosses above line3 and line1 crosses line2;

def t1 = if !show_bubbles_just_true_rules then 1 else ( rule1 or rule2);
addchartbubble(t1, high*1.006, rule1 + "  R1\n" + rule2 + "  R2", color.yellow, yes);

# what i think you will need
#  check if a 2nd line was crossed within x bars.

def rule1b = Line1 crosses below line3
and (( line1 crosses line2 within xbars bars)
or ( line3 crosses line2 within xbars bars));

def rule2b =
Line1 crosses above line3
and (( line1 crosses line2 within xbars bars )
or ( line3 crosses line2 within xbars bars ));

def t2 = if !show_bubbles_just_true_rules then 1 else ( rule1b or rule2b);
addchartbubble(t2, low*0.994, rule1b + "  R1\n" + rule2b + "  R2\n" + "within" + "\n" + xbars + " bars", color.magenta, no);
#``````

Last edited:

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