I am looking in mql5 the same as a function in Tradingview crossover, crossunder...

 

Hi,

I am looking for a function in mql5 the same as a function in Tradingview:

crossover(close, ema)

crossunder(close, ema)

how to do it in mql5?

 
alien1983:

Hi,

I am looking for a function in mql5 the same as a function in Tradingview:

crossover(close, ema)

crossunder(close, ema)

how to do it in mql5?


on Mql5 you can do much more than the pre-defined functions of the other software. 

anything is possible on MQL5.


For that you should compare both arrays, at the same position.. 

if value of one is greater than the other, and the value before it is NOT , than this is a cross

if it is under ou above is a point of view depending on which array you are using as a base for comparison


Two MA arrays are like this:


Array1: 09 08 07 06 05 04 03

Array2: 03 04 05 06 07 08 09  



At the element #4 they crossed.. Array1 crossed under array 2.. and Array2 crossed over array1

 

THX,

do you have some sample code with a bar closing price for h1?

I do not know how to compare the price of bar closing price to an ema:/

 
alien1983:

THX,

do you have some sample code with a bar closing price for h1?

I do not know how to compare the price of bar closing price to an ema:/

There is a complete source code, of a full working example of an EA which compare Close Prices of bars to an EMA,

on the Examples folder of every Metatrader instalation.

 

Open the File Navigator, using menu View -> Navigator, or press CTRL+D

once opened, go to folder Experts -> Examples -> Moving average.


Right click on it, select MODIFY, and it will open its source code on the Language Editor.


The function your are looking for as example is this one on the image, which is inside the source code of that Example


It has an array of prices: rt[ ] 

and an array of an EMA: ma[ ]

and makes exactly the comparison your are asking about


function



rt[1].close contains the Close price of Candle1, and rt[0].close contains the Close price of the current candle 

ma[1] contains the EMA Price corresponding the Candle1.. etc..  similar to the colored example of my previous answer..

the comparison I sinalized via the arrows i painted over the picture.. and are simple comparison of which is greater [or less] than the other array

the function then generates a Signal, and you can modify it to fit your needs