• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • User/Email: Password:
  • 9:19am
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 9:19am
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

MQL Help : Array resize -> Access violation write to... 2 replies

Need help to convert this indi into MTF indi 3 replies

Pls Any Body Help Me Convert This Adx Crossing Indicator Into Ea 2 replies

Two Dimensional Array vs One Dimensional Array 13 replies

Calculate Stochastic Formula on a array (non indicator buffer array) 2 replies

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
  • 1
Attachments: MQL Help: Convert indi ADX to run on Array
Exit Attachments
Tags: MQL Help: Convert indi ADX to run on Array
Cancel

MQL Help: Convert indi ADX to run on Array

  • Post #1
  • Quote
  • First Post: Dec 21, 2011 7:18am Dec 21, 2011 7:18am
  •  gilben
  • | Commercial Member | Joined Sep 2011 | 79 Posts
I am struggling to convert the ADX indicator code (by Metaquotes) to work with array data instead of MT4 data centre.

I did successfully converted the feed source so it works OK for drawing buffers (for visual indicator purposes),
but when I try to use the buffer arrays just to pull data from (without drawing vectors), I get no values.

I really can't figure out, why the calculation of adx values are accessable only when i draw the visual vectors.

It must be something I don't realy aware of how MQL works!!!
Maybe any of you can help me out with this issue?

I attach original ADX code + my revision intend to work independntly, no drawing, only supply values through arrays.

Will Appreciate any Help.

REVISED CODE:: INTEND TO SUPPLY ADX VALUES FROM ARRAYS:
PHP Code
 //+------------------------------------------------------------------+

//|                                                          ADX.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"
 


//---- input parameters
extern int ADXPeriod=14;
//---- buffers
double a_ADX[];
double PlusDi[];
double MinusDi[];
double PlusSdi[];
double MinusSdi[];
double TempBuf[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {

   return(
0);
  }
//+------------------------------------------------------------------+
//| Average Directional Movement Index                               |
//+------------------------------------------------------------------+
int start()
  {
   
double pdm,mdm,tr;
   
double price_high,price_low;
   
int    starti,i,counted_bars=IndicatorCounted();
   
double tempRates[][6];
   
ArrayCopyRates(tempRates,"EURUSD",60);
   
Comment(ArraySize(tempRates)+" first bar="+tempRates[ArraySize(tempRates)/6-1][3]);
      
//----
   
i=ArraySize(tempRates)/6-1-2;
   
PlusSdi[i+1]=0;
   
MinusSdi[i+1]=0;
   if(
counted_bars>=i) i=ArraySize(tempRates)/6-1-counted_bars-1;
   
starti=i;
//----
   
while(i>=0)
     {
      
price_low=tempRates[i][2];
      
price_high=tempRates[i][3];
      
//----
      
pdm=price_high-tempRates[i+1][3];
      
mdm=tempRates[i+1][2]-price_low;
      if(
pdm<0) pdm=0;  // +DM
      
if(mdm<0) mdm=0;  // -DM
      
if(pdm==mdm) { pdm=0; mdm=0; }
      else if(
pdm<mdm) pdm=0;
           else if(
mdm<pdm) mdm=0;
      
//---- ????????? ???????? ????????
      
double num1=MathAbs(price_high-price_low);
      
double num2=MathAbs(price_high-tempRates[i+1][4]);
      
double num3=MathAbs(price_low-tempRates[i+1][4]);
      
tr=MathMax(num1,num2);
      
tr=MathMax(tr,num3);
      
//---- counting plus/minus direction
      
if(tr==0) { PlusSdi[i]=0; MinusSdi[i]=0; }
      else      { 
PlusSdi[i]=100.0*pdm/tr; MinusSdi[i]=100.0*mdm/tr; }
      
//----
      
i--;
     }
//---- last counted bar will be recounted
   
if(counted_bars>0) counted_bars--;
   
int limit=ArraySize(tempRates)/6-1-counted_bars;
//---- apply EMA to +DI
   
for(i=0; i<=limit; i++)
      
PlusDi[i]=iMAOnArray(PlusSdi,ArraySize(tempRates)/6-1,ADXPeriod,0,MODE_EMA,i);
//---- apply EMA to -DI
   
for(i=0; i<=limit; i++)
      
MinusDi[i]=iMAOnArray(MinusSdi,ArraySize(tempRates)/6-1,ADXPeriod,0,MODE_EMA,i);
//---- Directional Movement (DX)
   
i=ArraySize(tempRates)/6-1-2;
   
TempBuf[i+1]=0;
   
i=starti;
   while(
i>=0)
     {
      
double div=MathAbs(PlusDi[i]+MinusDi[i]);
      if(
div==0.00) TempBuf[i]=0;
      else 
TempBuf[i]=100*(MathAbs(PlusDi[i]-MinusDi[i])/div);
      
i--;
     }
//---- ADX is exponential moving average on DX
   
for(i=0; i<limit; i++)
   {
      
a_ADX[i]=iMAOnArray(TempBuf,ArraySize(tempRates)/6-1,ADXPeriod,0,MODE_EMA,i);
      
   }
// ******* HELP:: WHY I DONT GET VALUES FOR a_ADX array or any other? *****************
// ******* Why if i would draw buffer line as indicator, it would supply values? ******
   
   
Comment("ADX Mian="+a_ADX[0]);   
   
   
   return(
0);
  } 
Attached File(s)
File Type: mq4 ADX.mq4   4 KB | 225 downloads
  • Post #2
  • Quote
  • Edited 8:23am Dec 21, 2011 8:16am | Edited 8:23am
  •  Xaphod
  • Joined Mar 2010 | Status: Member | 1,380 Posts
Problems with the code everywhere....
Inserted Code
 
double a_ADX[]; 
double PlusDi[]; 
double MinusDi[]; 
double PlusSdi[]; 
double MinusSdi[]; 
double TempBuf[];
It appears that none of your arrays are initialized. You cannot remove the indicator buffer initialization functions and not replace them with your own:
http://docs.mql4.com/array/ArrayInitialize
http://docs.mql4.com/array/ArrayIsSeries

Note. Due to mql4 idiosyncrasies, you will need to switch back and forth with ArrayIsSeries if you resize arrays.


Another problem:
Inserted Code
 a_ADX[i]=iMAOnArray(TempBuf,ArraySize(tempRates)/6-1,ADXPeriod,0,MODE_EMA,i);
iMAOnArray() requires a one dimensional array with values to average. You are passing a 2 dimensional array with candle data. That is wrong.

Another one:
Inserted Code
 MinusDi[i]=iMAOnArray(MinusSdi,ArraySize(tempRates)/6-1,ADXPeriod,0,MODE_EMA,i);
MinusSdi is null.
Why use 'ArraySize(tempRates)/6-1' and not 'ArraySize(MinusSdi)' ? Ideally, one would only want to use the nr of newbars to minimize processing. I do not know if that is possible here. Have not studied the code.

Study this article:
http://articles.mql4.com/501
and this thread:
http://www.forexfactory.com/showthread.php?t=165411
 
 
  • Post #3
  • Quote
  • Last Post: Dec 21, 2011 8:30am Dec 21, 2011 8:30am
  •  gilben
  • | Commercial Member | Joined Sep 2011 | 79 Posts
Xaphod

Thank you for your comments.
I am through your marked notes,,, you provide me very good hints of what I should do to work it out.

10x
 
 
  • Platform Tech
  • /
  • MQL Help: Convert indi ADX to run on Array
  • Reply to Thread
0 traders viewing now
Top of Page
  • Facebook
  • Twitter
About FF
  • Mission
  • Products
  • User Guide
  • Media Kit
  • Blog
  • Contact
FF Products
  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer
FF Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow FF
  • Facebook
  • Twitter

FF Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Forex Factory® is a brand of Fair Economy, Inc.

Terms of Service / ©2023