Drawing EMA Indicator Lines in an EA

 

Hi All.

I have an EA based on EMA crosses in multiple timeframes.

I want the EA to draw all of the EMAs I am using on the chart that it is attached to when I attach it. Planning on having it change colours based on trend conditions etc.

I couldn't many instances of this or much help, not sure whether it is possible using the same methods as drawing indicator lines in a Custom Indicator.


If it wasn't really possible I did have one idea of using a loop which cycles through the bars on the chart and draws a line (possibly with the ObjectCreate function) from the value of an EMA at shift X to the value of the EMA at shift X+1 and does this for every bar.

This is the code I tried for that:

for(int Counter = 0; Counter < Bars; Counter++)
{
ObjectCreate("FastTrendEMA", OBJ_TREND, 0, (iTime(NULL,0,Counter)), (iMA(NULL,0,(FastEMAPeriod*TrendPeriodMultiplier),0,MODE_EMA,PRICE_WEIGHTED,Counter)), (iTime(NULL,0,Counter+1)), (iMA(NULL,0,(FastEMAPeriod*TrendPeriodMultiplier),0,MODE_EMA,PRICE_WEIGHTED,Counter+1)));

}

I realize that this, if it worked, would draw every line extending to the edges of the chart, I was going to try this and fix that later.

When the EA is run, it displays only 1 line. I don't know why.


Any help would be very much appreciated. I have only been coding for a few weeks but I'm happy to help anyone else in any way I can.

I have attached the EA for anyone that is interested.

Files:
 

it would be a lot easier to use a custom indicator to both draw lines and include trade criteria signals to change line colors etc than it is to make an EA replicate the functionality of an indicator

 
SDC:

it would be a lot easier to use a custom indicator to both draw lines and include trade criteria signals to change line colors etc than it is to make an EA replicate the functionality of an indicator


I second that

It is also more efficient use of memory & CPU cycles

-BB-

 

Because you tell the EA to draw only 1 line:

ObjectCreate("FastTrendEMA", OBJ_TREND, 0, (iTime(NULL,0,Counter)), (iMA(NULL,0,(FastEMAPeriod*TrendPeriodMultiplier),0,MODE_EMA,PRICE_WEIGHTED,Counter)), (iTime(NULL,0,Counter+1)), (iMA(NULL,0,(FastEMAPeriod*TrendPeriodMultiplier),0,MODE_EMA,PRICE_WEIGHTED,Counter+1)));

You create only 1 object and that object is a trend line ( OBJ_TREND). You need 2 points in space to define where a line goes through (base mathematics) and you did that:

ObjectCreate(Create trend line with name FasTrendEMA, point1 (time, price), point2( time, price)) Hope that's transaprent enoguh. If you dont want the trend line to extend beyond the points that define it, tell it to not do so after you create the line with:

ObjectSet( "FastTrendEMA", OBJPROP_RAY, false); ray means that it goes beyond points, so this should be false.

Now to do multiple lines you need to do multiple "ObjectCreate" but you do them only inside 1 for loop. That means that you'll effectively draw a type of moving average line, but only of 1 type. If you want multiple moving average types, you need to put additional "ObjectCreate"s inside that for loop with different timeframes.

Know also that tehre's a better way to do that in mql4, it's called indicators, and they exist for a reason (and a post above already tells you why).

 
Nick_Mas:
I want the EA to draw all of the EMAs I am using on the chart that it is attached to when I attach it.
  1. Put the indicators on the chart making sure their parameters always match what the EA is using in the iMA/iCustom calls.
  2. Have the EA draw the equivalent. EA equivalent of indicator buffers
 

Thanks for the replys everyone.


I guess standard procedure is to create a custom indicator and manually change indicator settings as I change the EA.


The main reason for me wanting them to be coming from the same place is so if a trade went badly, I could probably see which setting most caused the problem, then keep records and see what needs to be changed. Easy enough with a CI and an EA running on the same chart.


Thanks again.

 

For anyone interested, i did get this to work.

Here is the code to display an EMA indicator in an expert advisor:


int Counter = 1;
while(Counter < Bars)
{
string EMAName = 5+Counter;
ObjectCreate(EMAName,OBJ_TREND,0,iTime(NULL,0,Counter),(iMA(NULL,0,EMAPeriod,0,MODE_EMA,PRICE_WEIGHTED,Counter)),iTime(NULL,0,Counter-1),(iMA(NULL,0,EMAPeriod,0,MODE_EMA,PRICE_WEIGHTED,Counter-1)));
ObjectSet(EMAName,OBJPROP_RAY,false);
Counter++;

}


I'll probably make it delete all objects then run once at the first tick of every bar, it does create a ton of objects on the chart.

Anyone got a reason why this method for drawing EMAs would not be a good idea to use? it doesnt seem to chug the hell out of my computer so far...

 

The only reason not to is the use of resources and weird things happen when missing chart bars are filled in (having said that it is a good way to see when missing bars were filled in and how many), one chart drawn like that would probably not be a problem but several of them at the same time might get a little RAM greedy.

 
Nick Masman:

Hi All.

I have an EA based on EMA crosses in multiple timeframes.

I want the EA to draw all of the EMAs I am using on the chart that it is attached to when I attach it. Planning on having it change colours based on trend conditions etc.

I couldn't many instances of this or much help, not sure whether it is possible using the same methods as drawing indicator lines in a Custom Indicator.


If it wasn't really possible I did have one idea of using a loop which cycles through the bars on the chart and draws a line (possibly with the ObjectCreate function) from the value of an EMA at shift X to the value of the EMA at shift X+1 and does this for every bar.

This is the code I tried for that:

for(int Counter = 0; Counter < Bars; Counter++)
{
ObjectCreate("FastTrendEMA", OBJ_TREND, 0, (iTime(NULL,0,Counter)), (iMA(NULL,0,(FastEMAPeriod*TrendPeriodMultiplier),0,MODE_EMA,PRICE_WEIGHTED,Counter)), (iTime(NULL,0,Counter+1)), (iMA(NULL,0,(FastEMAPeriod*TrendPeriodMultiplier),0,MODE_EMA,PRICE_WEIGHTED,Counter+1)));

}

I realize that this, if it worked, would draw every line extending to the edges of the chart, I was going to try this and fix that later.

When the EA is run, it displays only 1 line. I don't know why.


Any help would be very much appreciated. I have only been coding for a few weeks but I'm happy to help anyone else in any way I can.

I have attached the EA for anyone that is interested.

Well, I have a similar problem, I want to draw my own Bollinger Bands and Moving Average, but I still have not made it. I imagine that in your case you should draw point by point as the "for" loop runs Bar by Bar on the chart. I will thry to use your idea, if I succeed I will let you know.

 
Posted my polyline code at Why is there NO Complete EA within the Code-Base? - MQL4 forum It has not been adjusted for Build 600+.
Reason: