# How to detemine cross-over point value?

I am coding an EA that open a trade (buy or sell) when MA20 cross MA5.

I want to stoploss at cross-over point.

How can I detemine cross-over point value?

Thank you very much!

The easiest way in my head is to set double variable at the top (before the init function) called something like LastValue.

At the end of each start function, set LastValue as the indicator's value... that means next tick, you can compare it to the current value. eg:

```double MA20_LastValue = 0.0;
double MA5_LastValue = 0.0;

void start
{
double MA20_Current = ////// blah blah blah
double MA5_Current = //// blah

if(MA20_Current < MA5_Current && MA20_LastValue >= MA5_LastValue)
{
// MA20 just crossed under MA5
}

if(MA20_Current > MA5_Current && MA20_LastValue <= MA5_LastValue)
{
// MA20 just crossed over MA5
}

MA20_LastValue = MA20_Current;
MA5_LastValue = MA5_Current;
return;
}```

1. That way, perhaps more efficient and easier to understand
 original ```  if(MA20_Current < MA5_Current && MA20_LastValue >= MA5_LastValue)       {       // MA20 just crossed under MA5       }    if(MA20_Current > MA5_Current && MA20_LastValue <= MA5_LastValue)       {       // MA20 just crossed over MA5       }``` efficient ```bool  isRising = MA20_Current < MA5_Current,      wasRising = MA20_LastValue < MA5_LastValue,      crossed   = isRising != wasRising; ``` mathematical `if( (MA20_Current - MA5_Current) * (MA20_LastValue - MA5_LastValue) < 0.) // crossed`
The only problem is if you miss bars (e.g. disconnections)
2. Or loop back and find the bar
```for(int iBar = 0; iBar < Bar; iBar++){
double MA20_Current = ... iBar), MA20_Previous = ... iBar+1),
double  MA5_Current = ... iBar),  MA5_Previous = ... iBar+1);
:
if (crossed) break; // at iBar
}```

nice, I need to update a few of my indicators then!

Hi,

Sorry that I may not make it clear.

What I need is the VALUE of cross-over point.

I don't know how to calculate it.

For example, if two MAs are crossed like this

`if( (MA20_Current - MA5_Current) * (MA20_LastValue - MA5_LastValue) < 0.) // crossed`

How to calculate the crossing-over point VALUE

Thanks!

Dohung:

Hi,

Sorry that I may not make it clear.

What I need is the VALUE of cross-over point.

I don't know how to calculate it.

For example, if two MAs are crossed like this

How to calculate the crossing-over point VALUE?

Work out the equations for the 2 MAs gradients between the bars where they cross and then solve them simultaneously,  that should give you your answer.

Exactly.

v20 = MA20_LastValue + (MA20_Current - MA20_LastValue) * x

v5  =  MA5_LastValue + ( MA5_Current -  MA5_LastValue) * x

Use this and you would have found this among others.

EDIT: I don't believe in deleting my posts, but I don't think this any more! I jumped to the wrong conclusion!

-----------

I think you need to get your ideas clear...

A stoploss is a price value you set in advance.

You can't tell when the MA's will cross in advance. If you want positions to close when the MA's cross, tell your EA to do that, you can't set a stoploss for this and you don't really need to know the price value at that time.

alladir: You can't tell when the MA's will cross in advance. If you want positions to close when the MA's cross, tell your EA to do that, you can't set a stoploss for this and you don't really need to know the price value at that time.
You can't tell when the MA's will cross, but you can calculate where in the current candle they will and at what market price, by doing a little math. For example:
```        /* fastCurr = fastPrev + fAlpha * (Bid - fastPrev)
* slowCurr = slowPrev + sAlpha * (Bid - slowPrev)
* macdCurr = fastCurr - slowCurr
* sigCurr  = sigPrev  + sigAlpha * (macdCurr - sigPrev)
* Compute what price results in macdCurr == sigCurr
* macdCurr = sigPrev + sigAlpha(macdCurr - sigPrev)
* macdCurr(1-sigAlpha) = sigPrev(1-sigAlpha)
* Compute what price results in macdCurr == sigPrev
* [fastPrev+fAlpha(exit-fastPrev)]-[slowPrev+sAlpha(exit-slowPrev)]=sigPrev
* exit(fAlpha-sAlpha)=sigPrev-fastPrev(1-fAlpha)+slowPrev(1-sAlpha) */
```

WHRoeder:
You can't tell when the MA's will cross, but you can calculate where in the current candle they will and at what market price, by doing a little math. For example:

Yes you can, but I thought the OP got muddled and actually wanted to close the position at the time of the crossover.. now I see I was jumping too fast to conclusions.

With regard to finding the gradients with (y1-y2)/(x1-x2),  I worry a little about price having such small changes, then divided by a large time figure.. I haven't had any problems yet, but is it possible double variables can't handle the calculations? Is it something to be weary of or not?

There is no divide by time in my example. And if you actually need to you can use bar number's not time