# Need help with moving average distance calculations looks liek a Bug

397

‌How do I code the movingaverage variable‌ so that it gives the exact correct data ?

‌In other words , I cant correctly retrieve the iMA data.

```double  iMA(
string       symbol,           // symbol
int          timeframe,        // timeframe
int          ma_period,        // MA averaging period
int          ma_shift,         // MA shift
int          ma_method,        // averaging method
int          applied_price,    // applied price
int          shift             // shift
);```

‌Searched online and cant find a solution. These are my variables:

```double movingaverage = iMA(Symbol(),0,2,0,MODE_LWMA,0,0);
double selldistance = MathFloor(MathAbs((Bid-movingaverage)/Point)) ;

```

Notice in the movingaverage calculation, in the Timeframe, I put a 0 there instead of the 1 (1m) which is the one that should work, because putting ‌PERIOD_M1 or 1 (default for 1m) gives off the wrong data. Looks like it may even be a bug .
After many hours I found a workaround by setting Timeframe to 0 and just attaching the EA to a chart that is already set to 1m timeframe instead . However, it works only like half the time.

‌Ive included 2 screenshots to make it more clear what I want:. The first picture is of the actual moving average settings in the dialogue window that I need in variable form so I can place orders at a distance from it.

The second screen is about the actual data per se from chart so its even more clear. its what results from those settings , which I just cant get in variable form. ‌

Moderator
33339

All I can say is : if there is a bug, it's in your code. Debug it ?

```double movingaverage = iMA(Symbol(),0,2,0,MODE_LWMA,0,0);
double selldistance = MathFloor(MathAbs((Bid-average)/Point)) ;

Moderator
33339

397

```double movingaverage = iMA(Symbol(),0,2,0,MODE_LWMA,0,0);
double selldistance = MathFloor(MathAbs((Bid-movingaverage)/Point)) ;

‌Thanks for pointing that typo out.

1. Can you provide any way to get the exact correct ima data as per the pictures attached ?

Seems like its supposed to work as intended, yet its still not working for some reason. Need the exact data on the Value field of the moving average box in variable form. Is this what is supposed to be returned by the

`iMA(Symbol(),0,2,0,MODE_LWMA,0,0);`

?

2. Im running MT4 under Wine in Debian 9, which may be the reason for this apparent glitchism. If you can confirm that the yellow box's value field in the second picture is indeed correctly returned by the above iMA command in other OS's, then its definately a Wine issue and I should look maybe at a custom indicator

Its Wine then?‌

Step on New Rails: Custom Indicators in MQL5
• 2009.11.23
• Комбинатор
• www.mql5.com
I will not list all of the new possibilities and features of the new terminal and language. They are numerous, and some novelties are worth the discussion in a separate article. Also there is no code here, written with object-oriented programming, it is a too serous topic to be simply mentioned in a context as additional advantages for developers. In this article we will consider the indicators, their structure, drawing, types and their programming details, as compared to MQL4. I hope that this article will be useful both for beginners and experienced developers, maybe some of them will find something new.
Moderator
2326

Notice in the movingaverage calculation, in the Timeframe, I put a 0 there instead of the 1 (1m) which is the one that should work, because putting ‌PERIOD_M1 or 1 (default for 1m) gives off the wrong data. Looks like it may even be a bug .
After many hours I found a workaround by setting Timeframe to 0 and just attaching the EA to a chart that is already set to 1m timeframe instead . However, it works only like half the time.

‌Ive included 2 screenshots to make it more clear what I want:. The first picture is of the actual moving average settings in the dialogue window that I need in variable form so I can place orders at a distance from it.

Does it "work" on all the sell orders, but not the buys?

I‌ can't recreate a problem:

```int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
{
double ma_value=iMA(_Symbol,PERIOD_CURRENT,2,0,MODE_LWMA,PRICE_CLOSE,0);
Comment(DoubleToString(ma_value,_Digits));
return(rates_total);
}```
397

honest_knave:

Does it "work" on all the sell orders, but not the buys?

Ive confirmed its independent of what type of order is placed.

Moderator
2326

Ive confirmed once more, its independent of order placement.

Its a problem with the actual returned value of the ‌following commands:

As I said, I can't recreate the issue. I think your problem is elsewhere.

```int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
{
double ma_value1=iMA(_Symbol,PERIOD_CURRENT,2,0,MODE_LWMA,PRICE_CLOSE,0),
ma_value2=iMA(Symbol(),1,2,0,MODE_LWMA,0,0),
ma_value3=iMA(Symbol(),PERIOD_M1,2,0,MODE_LWMA,0,0),
ma_value4=iMA(Symbol(),0,2,0,MODE_LWMA,0,0);
string text=DoubleToString(ma_value1,_Digits)+"\n";
text+=DoubleToString(ma_value2,_Digits)+"\n";
text+=DoubleToString(ma_value3,_Digits)+"\n";
text+=DoubleToString(ma_value4,_Digits);
Comment(text);
return(rates_total);
}```

397

OK its just Wine then.

Moderator
2326

OK its just Wine then.

Thanks all .

God bless‌

Why don't you try the code I posted on Wine?‌

I‌ don't have a linux box running right now to test.

Moderator
33339

honest_knave:

Why don't you try the code I posted on Wine?‌

I‌ don't have a linux box running right now to test.

It's always easier to think the problem comes from an external source.
397

I've created:

1. Custom Indicator with your code, which I've attached (‌average.mq4)

2. A Test EA (icustomcallwithalert.mq4) with the call to your Custom Indicator code to test it out.

Your code indicator (average.mq4) works perfect when its being used on its own on a chart. However, when I try to call it in Test EA (icustomcallwithalert.mq4) with :

`‌double realaverage = iCustom(Symbol(), 0, "average.ex4", 0, 0);`

all I get is a weird 2123412.00 value, which I've included in the Screen-shot (more wine glitch or not.png). Never used iCustom before; is this being executed correctly?

Notice correct value in the top left of the chart because I'm using the custom indicator on it, however when I call it with the test ea function call through alert, all I get is that 21123424.00 . Its in the Indicator folder.

Ive never done this before, so dont know if its more bad whine  or more bad wine?‌

Step on New Rails: Custom Indicators in MQL5
• 2009.11.23
• Комбинатор
• www.mql5.com
I will not list all of the new possibilities and features of the new terminal and language. They are numerous, and some novelties are worth the discussion in a separate article. Also there is no code here, written with object-oriented programming, it is a too serous topic to be simply mentioned in a context as additional advantages for developers. In this article we will consider the indicators, their structure, drawing, types and their programming details, as compared to MQL4. I hope that this article will be useful both for beginners and experienced developers, maybe some of them will find something new.
Files:
average.mq4 2 kb