Combining Custom Indicators with EAs

 

Hi

I'm new to MQL4 but have been programming C/C++ for years. I am trying to understand a concept regarding the difference between indicators and EAs.

I have written a Custom Indicator that draws a few moving averages and places arrows at appropriate buy and sell locations.

What I want to do now is create an Expert Advisor that buys and sells when the Custom Indicator tells it to, but I don't know how.

First I tried just converting the entire Custom Indicator into an EA, but this doesnt work because apparently you cannot plot data as lines in an EA, so I cannot plot my moving averages in an EA. So my conclusion is that I must have the line drawing logic in the Custom Indicator, and the buy logic in the EA.

I thought of calling iCustom() from my EA but Custom Indicators are state machines that continue to loop forever - so this doesn't make sense.

Am I missing something? My custom indicator algorithm basically looks like this:


start()
 
{
 
   plot EMA5
 
   plot EMA10
 
   if the EMAs are crossing for the first time
 
      place an arrow
 
}

When I place an arrow, I want to buy. How do I do this?

Thanks.

 

https://docs.mql4.com/trading

To buy now, use OrderSend().

---

An EA can draw lines.

Take your indicator and copy it from /experts/indicators to /experts. Open the new copy in the editor and compile it.

See if that doesn't work.

Attach the new Expert to a chart.

 
phy:

https://docs.mql4.com/trading

To buy now, use OrderSend().

---

An EA can draw lines.

Take your indicator and copy it from /experts/indicators to /experts. Open the new copy in the editor and compile it.

See if that doesn't work.

Attach the new Expert to a chart.



OrderSend() doesn't work from a custom indicator, only an EA.

An EA, as far as I know, cannot plot data on a curve. It can only draw basic horizontal/vertical lines.

I have tried copying my indicator to an expert file - it compiled, but it didn't work. The initialisation screen at the beginning didn't even come up. I am assuming this is because of the various #property indicator lines that are not allowed in EAs.

Can anyone help? Thanks.

 

Ok, indicator buffers don't work in an expert.

Sorry to trouble you.

"I thought of calling iCustom() from my EA but Custom Indicators are state machines that continue to loop forever - so this doesn't make sense."

Yes it does.

Use iCustom(). That is what it is for.


"First I tried just converting the entire Custom Indicator into an EA, but this doesnt work because apparently you cannot plot data as lines in an EA, so I cannot plot my moving averages in an EA. "

No need to plot.

Use iCustom to call up a temporary instance of the EA, and collect values from one or more of its indexes. That is common usage.

Example, inside an EA:

Get the values of ema 5 and ema 10 at bar 0 and bar 1 using the included "Moving Averages" custom indicator.

There are three input parameters for the "Moving Averages" indicator, they go into the iCustom() call after the indicator name.

double ema5_bar0 = iCustom(Symbol(), 0, "Moving Averages", 5, 0, MODE_EMA, 0, 0);
double ema5_bar1 = iCustom(Symbol(), 0, "Moving Averages", 5, 0, MODE_EMA, 0, 1);
double ema10_bar0 = iCustom(Symbol(), 0, "Moving Averages", 10, 0, MODE_EMA, 0, 0);
double ema10_bar1 = iCustom(Symbol(), 0, "Moving Averages", 10, 0, MODE_EMA, 0, 1);

 
You need 2 parts: indicator which computes values & give signals for opening positions and EA which takes data from indicator (signals) & realize orders on the market.
 

Thanks guys, you have been of great help :)

I understand it better now

 
LesioS:
You need 2 parts: indicator which computes values & give signals for opening positions and EA which takes data from indicator (signals) & realize orders on the market.
Hi,

I am not advanced user with metatrader, just know the basics. For example I have written my own indicator and the file is in /experts/indicators folder. So what I need to do know is to copy this file to jsut /experts/ directory and run it from there? Because in indicator I cant use Order functions. Then I need to attach my indicator from EA to chart and thats it? Then Order funtions should work without any problems?

sorry for my english language
 
If you program in c, you can write same logic as in the indicator but buffers(arrays you must create from scratch) will be much smaller dimensions (size of arrays/buffers will be the bars number actually you will use), you can't plot the arrow as standard(custom) indicator, but you can get the value and use bool function (if condition is met), and alternatively create object with arrow or continue with program logic.