Hi all,
I'm a newbie to MQL4 programming with some knowledge in other programming languages. I've been struggling with this seemingly easy code that I attached below. Basically what I'd want to achieve is having the OBV (On Balance Volume) indicator with a moving average (SMA, EMA, SMMA, or LWMA) in a separate indicator window at the bottom of the chart. This is how it should look like:
I used the OBV.mq4 for a start and gradually started building the code up:
Interestingly though, with the MABuffer[i] = nOBVMA line commented out, the OBV works fine:
Here is the code snippet but I also attached the full code below.
If someone could help out an enthusiastic newbie, I'd be extremely grateful! If you have any other feedback to me that comes to your minds when looking at my issue, please don't hesitate to share, I am really determined to improve my MQL4 coding skills as quickly and efficiently as possible.
P.S: I am aware that I can add a moving average to an OBV indicator on MetaTrader. However, the OBV with MA will be part of a future EA that I am planning to program and hence I need the indicator with an OBV and an MA buffers to pass on to the rest of the EA later on.
Thanks a lot in advance for your help!
I'm a newbie to MQL4 programming with some knowledge in other programming languages. I've been struggling with this seemingly easy code that I attached below. Basically what I'd want to achieve is having the OBV (On Balance Volume) indicator with a moving average (SMA, EMA, SMMA, or LWMA) in a separate indicator window at the bottom of the chart. This is how it should look like:
I used the OBV.mq4 for a start and gradually started building the code up:
- I added the SetIndexBuffer and SetIndexStyle functions in init() for the second buffer which will hold my MA values
- I created a loop which looks back MA_period = 200 time units in the past, takes the OBV values of those, places them all into an OBV_vals array, and then uses the iMAOnArray() function to get the SMA, EMA, SMMA, or LWMA.
- I placed the moving average value returned by the iMAOnArray() into the MABuffer in the hope that it will show up on the indicator window.
I managed to get everything until the line where I save the moving averages to the buffer. I have the MA values, I printed them on the journal and they seemed valid. However, the moment I uncomment the line MABuffer[i] = nOBVMA with the intention of showing these MA values on the indicator screen, everything gets corrupted as you can see on this image:
Interestingly though, with the MABuffer[i] = nOBVMA line commented out, the OBV works fine:
Here is the code snippet but I also attached the full code below.
Inserted Code
// loop looking back MA_period=200 periods and taking // the OBV values corresponding to those candles for(int j=1; j<=MA_period; j++) { nOBV_vals[j-1] = iOBV(NULL, 0, PRICE_CLOSE, i+j); } // using the freshly populated nOBV_vals array, // this line calculates the MA nOBVMA = iMAOnArray(nOBV_vals, 0, MA_period, 0, MA_method, i); // print out the moving average value Print("OBV EMA value is " + DoubleToStr(nOBVMA)); // add the MA to the MABuffer so that it shows on the indicator window // when this line is enabled, even the OBVBuffer data gets corrupted! MABuffer[i] = nOBVMA;
If someone could help out an enthusiastic newbie, I'd be extremely grateful! If you have any other feedback to me that comes to your minds when looking at my issue, please don't hesitate to share, I am really determined to improve my MQL4 coding skills as quickly and efficiently as possible.
P.S: I am aware that I can add a moving average to an OBV indicator on MetaTrader. However, the OBV with MA will be part of a future EA that I am planning to program and hence I need the indicator with an OBV and an MA buffers to pass on to the rest of the EA later on.
Thanks a lot in advance for your help!
Attached File(s)
OBV_TO_MODIFY.mq4
5 KB
|
686 downloads