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; }
- That way, perhaps more efficient and easier to understand
The only problem is if you miss bars (e.g. disconnections)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
- 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!
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?
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.
/* 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) */
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?
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi, Please help me.
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!