Expert Advisors: Difference of two moving averages

 

Difference of two moving averages:

An Expert Advisor based on the difference of two moving averages.

Author: Kevan Wells

 

I think the indicator needs ArraySetSeries (mf, true) and (ms, true) in OnInit () to work properly.

All operator

if (flg == 0) / / initialise on first pass
      {
       ArrayInitialize (ag, EMPTY_VALUE);
       ArrayInitialize (bg, EMPTY_VALUE);
       ArrayInitialize (cg, EMPTY_VALUE);
       hi = 0;
       I = 0;
       trend = 0;
       flg = 1;
       }

would be more effective in OnInit () and not have to use "flg"

EA regarding ... using MACD think its strategy algorithm is simplified like this:

Open buy: MACDcurrent>MACDprev && MACDcurrent<0; buyed = true.

Close buy: buyed && MACDcurrent<MACDprev && MACDcurrent>0; buyed = false

For sale reverse actions.

If we use SL and TP when not operating the EA I can say "buy" or "sell" as trend (slow MA) and wait for new signal.

 

Hello, I don't know why this indicator: madelta_inc.mq5, draws nothing when I attach it to the chart. Here I attach you an image so you can see what I say. Also I put below the code with the change josemiguel1812 suggests about don't use the flag: 'flg', and move part of the code to the OnInit() event. I didn't use: ArraySetAsSeries (mf, true) and ArraySetAsSeries (ms, true) because both 'mf' and 'ms' are only one element arrays and I think this change wont have effect, also MT5 doesn't allow use this function for static arrays. But what I would like to know is why it doesn't draw the indicator lines?. Could you try the following code and test it?

thanks in advance, Cyberglassed.

//+------------------------------------------------------------------+
//|                                                      madelta_inc |
//|                                           Copyright 2013 Winston |
//+------------------------------------------------------------------+
#property copyright "Winston 2013"
#property version "1.10"
#property description "madelta_inc"
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_plots   3

#property indicator_type1   DRAW_COLOR_LINE
#property indicator_color1  White,Yellow,Red
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2

#property indicator_type2   DRAW_LINE
#property indicator_color2  Yellow
#property indicator_style2  STYLE_DOT
#property indicator_width2  1

#property indicator_type3   DRAW_LINE
#property indicator_color3  Red
#property indicator_style3  STYLE_DOT
#property indicator_width3  1

input double d = 0.00195;                       //delta
input double m = 39.2;                          //mult

input int F = 26;                               //fast moving average
input ENUM_MA_METHOD FM = MODE_SMA;             //fast average mode
input ENUM_APPLIED_PRICE FP = PRICE_WEIGHTED;   //fast price mode

input int S = 51;                                     //slow moving average
input ENUM_MA_METHOD SM = MODE_EMA;                 //slow average mode
input ENUM_APPLIED_PRICE SP = PRICE_MEDIAN;        //slow price mode

int Ms, Mf, trend;
double px, hi, lo;
double ms[1], mf[1];
double ag[], bg[], cg[], ac[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnInit() {
        SetIndexBuffer(0, ag, INDICATOR_DATA);
        ArraySetAsSeries(ag, true);                    //signal

        SetIndexBuffer(1, ac, INDICATOR_COLOR_INDEX);
        ArraySetAsSeries(ac, true);                    //signal trend colour

        SetIndexBuffer(2, bg, INDICATOR_DATA);
        ArraySetAsSeries(bg, true);                    //hi threshold

        SetIndexBuffer(3, cg, INDICATOR_DATA);
        ArraySetAsSeries(cg, true);                    //lo threshold

        ArrayInitialize(ag, EMPTY_VALUE);
        ArrayInitialize(bg, EMPTY_VALUE);
        ArrayInitialize(cg, EMPTY_VALUE);
        hi = 0;
        lo = 0;
        trend = 0;
        
        Mf = iMA(NULL, PERIOD_H1, F, 0, FM, FP);
        Ms = iMA(NULL, PERIOD_H1, S, 0, SM, SP);

}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,           // size of the price[] array
                                const int prev_calculated,               // bars handled on a previous call
                                const int begin,                          // where the significant data start from
                                const double &price[])              // array to calculate
{

        CopyBuffer(Mf, 0, 0, 1, mf);                   //fast moving average value

        CopyBuffer(Ms, 0, 0, 1, ms);                   //slow moving average value

        px = pow(m * (mf[0] - ms[0]), 3);                //amplify and cube the difference


        if (px > hi) {                               //identify trend
                hi = px;
                lo = hi - d;
                trend = 1;
        }
        if (px < lo) {                               //with colour change
                lo = px;
                hi = lo + d;
                trend = 2;
        }

        ag[0] = px;
        bg[0] = hi;
        cg[0] = lo;
        ac[0] = trend;

        return (rates_total);
}

//+------------------------------------------------------------------+
Files:
screenshot.png  103 kb
 
cyberglassed:

Hello, I don't know why this indicator: madelta_inc.mq5, draws nothing when I attach it to the chart. Here I attach you an image so you can see what I say. Also I put below the code with the change josemiguel1812 suggests about don't use the flag: 'flg', and move part of the code to the OnInit() event. I didn't use: ArraySetAsSeries (mf, true) and ArraySetAsSeries (ms, true) because both 'mf' and 'ms' are only one element arrays and I think this change wont have effect, also MT5 doesn't allow use this function for static arrays. But what I would like to know is why it doesn't draw the indicator lines?. Could you try the following code and test it?

thanks in advance, Cyberglassed.

What do you expect ? Only values of index 0 of indicator's buffers are set.
 
angevoyageur:
What do you expect ? Only values of index 0 of indicator's buffers are set.

It was a little confusing for me see that always the index 0 of the buffers were assigned, so I expected that the ArraySetAsSeries had some influence to change the intuitive behaviour of what one could think (reverse) or something like that, I'm beginer in mql5. Anyway I had to restart my computer for some other reason and tried again the indicator and now it draws lines as I expected at the begining. I attach an image.

Thank you, Cyberglassed.

Files:
screenshot.png  118 kb
 
cyberglassed:

It was a little confusing for me see that always the index 0 of the buffers were assigned, so I expected that the ArraySetAsSeries had some influence to change the intuitive behaviour of what one could think (reverse) or something like that, I'm beginer in mql5. Anyway I had to restart my computer for some other reason and tried again the indicator and now it draws lines as I expected at the begining. I attach an image.

Thank you, Cyberglassed.

Either the code you are running isn't the same as the one you posted, either your terminal draws datas coming from some buffers.

The code you posted can't produce such result.

 

Thank you for your comments. The indicator is perhaps unusual in that it is intended to only work with the Strategy Tester in Visulization mode.

In Navigator, left click the ea and then right click test.  In Strategy tester EURUSD, H1 and tick the Visulization box and then Start, the indicator should appear.

Let me know how you get on.

Best Regards 

Winston

 

something is wrong with this EA. When I try to trade on only one pair of currency this EA didn`t do enything through 2 days, indicator somtimes show sel and buy signal at the same time.

When I try to trade on multiple pairs of currency it does the same thing, but i observed that when I manualy close positions it start to trade again,somthing is very wrong with this EA,somthing is blocking trading in Meta Trader 5

when I`ve tested it in strategy tester everything is ok, but when I try to trade its only open positions and don`t close it i need close it manualy....

could somone fix this?

 
 If you need an marker of time-frame - (Ma1-Ma2) AND (unknown indicator) will be work well, is't it? 
 
Good day! I have bad english, so sorry. I think EA is very decent. Help me please. I can not put it to work properly. I put it on the advisor several graphs and position opens on only one. If you close your hand open position, the position appears on a different schedule. What's the matter? May need several copies with different names to create advisers and how it will affect the work of the indicator?
 
The adviser, multi?
Reason: