ADX beginner tutorial by William210

Author: Copyright 2023, GwDs
Indicators Used
Movement directional index
0 Views
0 Downloads
0 Favorites
ADX beginner tutorial by William210
ÿþ//+------------------------------------------------------------------+

//|                         ADX code for beginners by William210.mq5 |

//|                                             Copyright 2023, GwDs |

//|                                    Need more help? Contact me on |

//|                         https://www.mql5.com/fr/users/william210 |

//+------------------------------------------------------------------+

//+-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-

//+-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-



#property copyright "Copyright 2023, GwDs"

#property version   "1.03" // 964



#property description "If this code malfunctions for whatever reasons, forgetting or MQL5 upgrades\nlet me know so I can correct it, thank you\n"



#property description "May this code help you\n"

#property description "You can find all my indicator codes in CodeBase or the Marketplace, free or purchasable, by searching for William210"





//+-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-

// --- Indicator preference

input group "ADX beginner tutorial beginner tutorial to learn" // // Group title

// --- Indicator preference

input uchar   g_ADXPerid = 14;           // ADX Period



//+-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-

// --- Graph placement

#property indicator_separate_window

//+-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-

// --- Graph heigh

#property indicator_minimum 0

#property indicator_maximum 100



//+-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-

// ---  Buffer declaration

#property   indicator_buffers       3  // Number of buffer displayed    



int      g_ptADX = INVALID_HANDLE;     // Pointer of the ADX



double   g_BuffADX[];                  // Buffer ADX

#define  g_indexBuffADX             0  // Index of ADX



double   g_BuffADXPlus[];               // Buffer ADX

#define  g_indexBuffADXPlus         1   // Index of ADX



double   g_BuffADXMinus[];              // Buffer ADX

#define  g_indexBuffADXMinus        2   // Index of ADX



#property indicator_plots           3   // number of plot on the graph      



//+-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-

// --- Buffer plot characteristics



#property indicator_label1  "ADX"            // Plot Label

#property indicator_type1   DRAW_LINE        // Plot Type

#property indicator_color1  clrGreen         // Plot color

#property indicator_style1  STYLE_SOLID      // Draw a solid line

#property indicator_width1  2                // Plot width



//--- plot ADX plus

#property indicator_label2  "ADX plus"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrGreen

#property indicator_style2  STYLE_DOT        // Draws a dotted line

#property indicator_width2  1



//--- plot ADX minus

#property indicator_label3  "ADX minus"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrRed

#property indicator_style3  STYLE_DOT

#property indicator_width3  1





//+------------------------------------------------------------------+

//|   OnInit                                                         |

//+------------------------------------------------------------------+

int OnInit()



{

//+-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-

// --- Handle Creation

  g_ptADX = iADX( Symbol(), Period(), g_ADXPerid);

  if( g_ptADX == INVALID_HANDLE)



  {

    PrintFormat("%s: Error when creating ADX pointer", __FUNCTION__);

    return( INIT_FAILED);

  }



//+-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-

// --- Transforms the array into display buffer

// --- g_BuffADX Buffer

  if( !SetIndexBuffer( g_indexBuffADX,        g_BuffADX,        INDICATOR_DATA))



  {

    PrintFormat( "%s: Error %d SetIndexBuffer( g_BuffADX)", __FUNCTION__, GetLastError());

  }



//+-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-

// --- g_BuffADXPlus Buffer

  if( !SetIndexBuffer( g_indexBuffADXPlus,    g_BuffADXPlus,    INDICATOR_DATA))



  {

    PrintFormat( "%s: Error %d SetIndexBuffer( g_BuffADXPlus)", __FUNCTION__, GetLastError());

  }



//+-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-

// --- g_BuffADXMinus Buffer

  if( SetIndexBuffer( g_indexBuffADXMinus,   g_BuffADXMinus,   INDICATOR_DATA))



  {

    PrintFormat( "%s: Error %d SetIndexBuffer( g_BuffADXMinus)", __FUNCTION__, GetLastError());

  }



//+-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-

  return(INIT_SUCCEEDED);

}



//+------------------------------------------------------------------+

//|   OnCalculate                                                    |

//+------------------------------------------------------------------+

int OnCalculate( const int        rates_total,      // Total number of bars to be processed

                 const int        prev_calculated,  // Number of bars calculated in the previous call

                 const datetime   &time[],          // Array of bar times

                 const double     &open[],          // Array of bar open prices

                 const double     &high[],          // Array of bar high prices

                 const double     &low[],           // Array of bar low prices

                 const double     &close[],         // Array of bar close prices

                 const long       &tick_volume[],   // Array of tick volumes for each bar

                 const long       &volume[],        // Array of real volumes for each bar

                 const int        &spread[])        // Array of spreads for each bar



{

//+-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-

// Variables

  int i_Diff = 0; // The correct number of bars to copy

//+-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-

// --- With this code, the average is only updated when a new bar is opened

  if ( prev_calculated < rates_total)



  {

    // --- Optimization to only take into account the new bar

    i_Diff = rates_total - prev_calculated;



    //+-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-

    // ---  Copies the data for the g_BuffADX buffer

    if ( CopyBuffer( g_ptADX, 0, 0, i_Diff, g_BuffADX) < 0)



    {

      PrintFormat("%s: Error %d retrieving data for ADX", __FUNCTION__, GetLastError());

      return (0);

    }



    //+-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-

    // --- g_BuffADXPlus buffer

    if ( CopyBuffer( g_ptADX, 1, 0, i_Diff, g_BuffADXPlus) < 0)



    {

      PrintFormat("%s: Error %d retrieving data for ADXPlus", __FUNCTION__, GetLastError());

      return (0);

    }



    //+-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-

    // --- g_BuffADXMinus buffer

    if ( CopyBuffer( g_ptADX, 2, 0, i_Diff, g_BuffADXMinus) < 0)



    {

      PrintFormat("%s: Error %d retrieving data for ADXMinus", __FUNCTION__, GetLastError());

      return (0);

    }

  }



//+-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-

  return(rates_total);



}



//+------------------------------------------------------------------+

//|   OnDeinit                                                       |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)



{

  Comment( "");

//+-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-

// ---release the indicator resource

  if( g_ptADX != INVALID_HANDLE)



  {

    // ---release the indicator resource

    IndicatorRelease( g_ptADX);

    g_ptADX = INVALID_HANDLE;

  }

}

//+------------------------------------------------------------------+

//| The End, That s All Folks!                                       |

//+------------------------------------------------------------------+

//+-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__-

//+-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---