attaching indicator to chart from within EA

 

hi,

i have an EA which uses a couple of indicator functions as part of the trading strategy. however, to monitor the progress of the EA it would be good to have the indicators showing on the chart as well (running with the same parameters that are being used from within the EA). setting this up is possible... just rather an effort. and when the parameters of the EA are changed, one needs to go back and modify all of the indicators as well to be consistent.

so, is it possible to attach an indicator to a chart from within an EA? so that, in principle, when the EA is attached to the chart, all of the appropriate indicator curves are automatically added to the chart as well? i have diligently been through the MQL manual and checked out the forums and not found an answer to this question.

thanks!

best regards,

andrew.

 
  1. Manually add the indicators to the chart and save a template.
  2. Have the EA draw the lines
    //+------------------------------------------------------------------+
    //| EA equivalent of indicator buffers                               |
    //+------------------------------------------------------------------+
    /*  Example 1:
     *  if (...) Ordermodify(...);
     *  Polyline("SL"+(oo.ticket%99), oo.SL, Color.SL, 0);
     *
     *  Example 2:
     *  double  ELineCurr = iMA(NULL,0, ELine.Period, 0, MODE_EMA, PRICE_CLOSE, 1);
     *  Polyline("ELine", ELineCurr, Color.ELine, 1);
     ******************************************************************************/
    #define POLYLINE_MAX 20 // Must match priceMM[]
    void Polyline(string name, double price, color clr, int shift=0){
        if (!Show.Objects)  return;
        static int      LRU[POLYLINE_MAX];
        static string   lineName[POLYLINE_MAX];
        for (int idx=0; idx < POLYLINE_MAX; idx++){
            bool new = lineName[idx] != name;   if (!new) break;    }
        if (new){
            for (idx=0; idx < POLYLINE_MAX; idx++)  LRU[idx]++;
            idx=ArrayMaximum(LRU);  lineName[idx]=name; }
        LRU[idx] = 0;
        double  price00[], price01[], price02[], price03[], price04[],
                price05[], price06[], price07[], price08[], price09[],
                price10[], price11[], price12[], price13[], price14[],
                price15[], price16[], price17[], price18[], price19[];
        switch (idx){
        case  0: PLHelper(name, price, clr, idx, new, shift, price00); return;
        case  1: PLHelper(name, price, clr, idx, new, shift, price01); return;
        case  2: PLHelper(name, price, clr, idx, new, shift, price02); return;
        case  3: PLHelper(name, price, clr, idx, new, shift, price03); return;
        case  4: PLHelper(name, price, clr, idx, new, shift, price04); return;
        case  5: PLHelper(name, price, clr, idx, new, shift, price05); return;
        case  6: PLHelper(name, price, clr, idx, new, shift, price06); return;
        case  7: PLHelper(name, price, clr, idx, new, shift, price07); return;
        case  8: PLHelper(name, price, clr, idx, new, shift, price08); return;
        case  9: PLHelper(name, price, clr, idx, new, shift, price09); return;
        case 10: PLHelper(name, price, clr, idx, new, shift, price10); return;
        case 11: PLHelper(name, price, clr, idx, new, shift, price11); return;
        case 12: PLHelper(name, price, clr, idx, new, shift, price12); return;
        case 13: PLHelper(name, price, clr, idx, new, shift, price13); return;
        case 14: PLHelper(name, price, clr, idx, new, shift, price14); return;
        case 15: PLHelper(name, price, clr, idx, new, shift, price15); return;
        case 16: PLHelper(name, price, clr, idx, new, shift, price16); return;
        case 17: PLHelper(name, price, clr, idx, new, shift, price17); return;
        case 18: PLHelper(name, price, clr, idx, new, shift, price18); return;
        case 19: PLHelper(name, price, clr, idx, new, shift, price19); return;
    }   }
    void PLHelper( string name, double price, color clr, int idx, bool new
                 , int shift, double& mem[] ){
        datetime    t0  = Time[shift];  int firstBar = ArraySize(mem)-1;
        static datetime time0[POLYLINE_MAX];
        if (time0[idx] < Time[shift+1]) new = true; // Missing bars, leave a gap.
        if (new){   firstBar = -1;
            static int      segNo[POLYLINE_MAX];    segNo[idx]++;
            static datetime time1[POLYLINE_MAX];    time1[idx] = t0;    }
        string objName=name+"-"+segNo[idx];
        if (new || t0 != time0[idx]){
            ArraySetAsSeries(mem, false);       // Shift values m[2]=m[1]; m[1]=m[0]
            firstBar=ArrayResize(mem, firstBar+2)-1;    if (firstBar < 0){  Alert(
                "ArrayResize failed: ",GetLastError());                     return;}
            ArraySetAsSeries(mem, true);
        }
        time0[idx]=t0;  mem[0]=price;
        if (t0 == time1[idx])   t0 += 60*Period();  // Minimum 1 bar wide.
        /**/ if(ObjectMove(objName, 1, t0,          price))
                ObjectMove(objName, 0, time1[idx],  mem[firstBar]); // New or tmplt.
        else if (!ObjectCreate( objName, OBJ_TREND, WINDOW_MAIN
                              , time1[idx], mem[firstBar], t0,  price )){   Alert(
            "ObjectCreate(", objName, "Trend) [1] failed: ",GetLastError());return;}
        else if (!ObjectSet( objName, OBJPROP_COLOR, clr ))                 Alert(
            "ObjectSet(", objName, "Color) [3] failed: ",   GetLastError());
        else if (!ObjectSet( objName, OBJPROP_RAY, false ))                 Alert(
            "ObjectSet(", objName, "Ray) [1] failed: ",     GetLastError());
        double maxError=0;  int maxBar;
        for (int pos=1; pos < firstBar; pos++){
            double error=MathAbs(ObjectGetValueByShift(objName, pos+shift)-mem[pos]);
            if (error > maxError){  maxError=error; maxBar=pos; }
        }
        if (maxError >= pips2dbl){  // Split the line into two segments at the max.
            if (!ObjectMove(objName, 1, Time[shift+maxBar], mem[maxBar]))   Alert(
                "ObjectMove(", objName, "Trend) failed: ",  GetLastError());
            segNo[idx]++; objName=name+"-"+segNo[idx];
            ArrayResize(mem, maxBar+1); // Drop firstBar..(maxBar+1)
            time1[idx] = Time[shift+maxBar];
            /**/ if(ObjectMove(objName, 0, time1[idx],  mem[maxBar]))
                    ObjectMove(objName, 1, t0,          price);
            else if (!ObjectCreate( objName, OBJ_TREND, WINDOW_MAIN
                                  , time1[idx], mem[maxBar], t0, price )){  Alert(
                "ObjectCreate(", objName, "Trend) [2] failed: ",GetLastError()); }
            else if (!ObjectSet( objName, OBJPROP_COLOR, clr ))             Alert(
                "ObjectSet(", objName, "Color) [4] failed: ",   GetLastError());
            else if (!ObjectSet( objName, OBJPROP_RAY, false ))             Alert(
                "ObjectSet(", objName, "Ray) [3] failed: ",     GetLastError());
        }   // Split the line into two segments at the max.
    }   // PLHelper
    //++++ These are adjusted for 5 digit brokers.
    double  pips2points,    // slippage  3 pips    3=points    30=points
            pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int init(){
        if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    
 
WHRoeder:
  1. Manually add the indicators to the chart and save a template.
  2. Have the EA draw the lines

Great, Thanks WHRoeder.

I'm an old C programmer and like programming for own meta4 Ea + indicators (BB, EMA,Macd,CCI)

My test would be have the EA draw this lines used by the EA.

It would be very useful so separate indicators don't be adjusted every time after a EA parameter change.

Any example available about all this ?

Kind regards

 
collierab:

hi,

i have an EA which uses a couple of indicator functions as part of the trading strategy. however, to monitor the progress of the EA it would be good to have the indicators showing on the chart as well (running with the same parameters that are being used from within the EA). setting this up is possible... just rather an effort. and when the parameters of the EA are changed, one needs to go back and modify all of the indicators as well to be consistent.

so, is it possible to attach an indicator to a chart from within an EA? so that, in principle, when the EA is attached to the chart, all of the appropriate indicator curves are automatically added to the chart as well? i have diligently been through the MQL manual and checked out the forums and not found an answer to this question.

thanks!

best regards,

andrew.

Good question collierab, that was i also looking for...

 
WHRoeder:
  1. Manually add the indicators to the chart and save a template.
  2. Have the EA draw the lines

hi, yes, thanks. that is indeed one way of drawing the lines. and, i agree that setting up a template will get the right configuration of indicators. but the my question really relates to the fact that if you go and change the parameters of the indicator which are being used WITHIN the EA then these changes are not reflected on the indicator displayed on the chart. so this means that what you are looking at one the chart will (unless you are really careful) get out of sync with what the EA is "thinking" about.
 
WHRoeder:
  1. Manually add the indicators to the chart and save a template.
  2. Have the EA draw the lines
#define POLYLINE_MAX 20 // Must match priceMM[]

I try to implement it on an existing EA (Blessing).....

What do you mean matching the price Money Management ?

And why is it needed ?

 
the only drawback to this is that you need to reinvent the wheel. the indicators already do all of the plotting. so if you could start/stop an indicator from within an EA it would avoid all of this duplication. i am guessing that this is just not possible though... which is surprising since it is something that i think would be really handy.
 
mate41:

What do you mean matching the price Money Management ?

#define POLYLINE_MAX 20 // Must match priceMM[]
    double  price00[], price01[], price02[], price03[], price04[],
            price05[], price06[], price07[], price08[], price09[],
            price10[], price11[], price12[], price13[], price14[],
            price15[], price16[], price17[], price18[], price19[];
Not Money Management. Count of arrays: 0 to 19 == 20
 
collierab:
the only drawback to this is that you need to reinvent the wheel. the indicators already do all of the plotting. so if you could start/stop an indicator from within an EA it would avoid all of this duplication. i am guessing that this is just not possible though... which is surprising since it is something that i think would be really handy.
Having the EA display the values during testing allow you to add the indicator and verify that the two match.
 

Hi can someone add this indicator to mt4 ea? here is indicator

Files: