# How to tell if two indicators have the same slope?

I understand the CROSS() function and am looking for something like an AGREE() function.

I would like a signal that triggers when two indicators agree on a direction. Something like

``````// buy if both MA(10) and MA(50) have positive slopes over the last 2 periods
BuySignal = AGREE(CalcMA(10), CalcMA(50), UP, 2);

// sell if both MA(10) and MA(50) have negative slopes over the last 2 periods
BuySignal = AGREE(CalcMA(10), CalcMA(50), DOWN, 2);
``````

Is there such a function?

Dave

Perhaps this is what you want.

The following AGREE function tests if each of two arrays goes up (down) every bar within last periods and returns moments where both go up or down.

For example: If “periods=5” it returns TRUE in moments when both arrays went up each bar for the last 5 bars.

( It is possible that your AGREE tests just the first and the last bars of the given period. Such function would be simplier: a slope UP is Ref(Close, -periods) < Close. )

How it works.
It places TRUE to bars where an array goes UP:

``````up1 = Ref(array1,-1) < array1;
``````

We can compute a cumulative sum of those TRUEs within last periods.

``````cum_up1 = CalcSum(up1,periods);
``````

If that sum is equal to the “periods” number, we know the array went up in all last bars.

Do it with both arrays and return moments where both went up.

This is it:

``````// The "updown" parameter is boolean: TRUE means UP
function AGREE(array1, array2, updown, periods){
if (updown) {
// Points where array1 goes up
up1 = Ref(array1,-1) < array1;
// Sum those values in the last periods
cum_up1 = CalcSum(up1,periods);
// The same with array2
up2 = Ref(array2,-1) < array2;
cum_up2 = CalcSum(up2,periods);

result = cum_up1 == periods and cum_up2 == periods;
}
else {
down1 = Ref(array1,-1) > array1;
cum_down1 = CalcSum(down1,periods);
down2 = Ref(array2,-1) > array2;
cum_down2 = CalcSum(down2,periods);

result = cum_down1 == periods and cum_down2 == periods;
}

return result;
}
``````

Example:

``````Setoption("Symbols","MSFT");

// The "updown" parameter is boolean: TRUE means UP
function AGREE(array1, array2, updown, periods){
if (updown) {
// Points where array1 goes up
up1 = Ref(array1,-1) < array1;
// Sum those values in the last periods
cum_up1 = CalcSum(up1,periods);
// The same with array2
up2 = Ref(array2,-1) < array2;
cum_up2 = CalcSum(up2,periods);

result = cum_up1 == periods and cum_up2 == periods;
}
else {
down1 = Ref(array1,-1) > array1;
cum_down1 = CalcSum(down1,periods);
down2 = Ref(array2,-1) > array2;
cum_down2 = CalcSum(down2,periods);

result = cum_down1 == periods and cum_down2 == periods;
}

return result;
}

ma1 = CalcMA(Close,10);
ma2 = CalcMA(Close,50);

GoesUp = AGREE(ma1, ma2, TRUE, 5);
GoesDown = AGREE(ma1, ma2, FALSE, 5);

// Sell = GoesUp ...

Filter = 1;
AppendColumn(Ref(ma1,-1) < ma1, "ma1 Up");
AppendColumn(Ref(ma2,-1) < ma2, "ma2 Up");
AppendColumn(GoesUp, "AGREE Up");

AppendColumn(Ref(ma1,-1) > ma1, "ma1 Down");
AppendColumn(Ref(ma2,-1) > ma2, "ma2 Down");
AppendColumn(GoesDown, "AGREE Down");
``````

Results:

Enjoy!

2 Likes