Download MetaTrader 5

Help creating arrows

To add comments, please log in or register
Have you found a good programmer? Make a targeted order!
nerobot
85
nerobot 2010.08.11 16:48 

Hi,

 

I've created the following code as a very simple example of finding highs and lows in the data and then place an arrow on the graph to show if it is a high or a low. The program also creates an indicator that also shows if there is a high or a low. However, as can be seen by the attached screen shot, the code finds the points, shows them with the indicator, but the arrow isn't being shown. Any help sorting this out would be great.

 

Regards,

Steven 

 

//+------------------------------------------------------------------+
//|                                              LookingForPeaks.mq5 |
//|                        Copyright 2010, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+

/* For this indicator, I want to calculate the peaks in the data, within
   n points either side.
*/

#include <ChartObjects\ChartObjectsArrows.mqh> 

#property copyright "Copyright 2010, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot Label1
#property indicator_label1  "Label1"
#property indicator_type1   DRAW_LINE
#property indicator_color1  Red
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- indicator buffers
double         Label1Buffer[];
int n = 2;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,Label1Buffer,INDICATOR_DATA);
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[],
                const int& spread[])
  {
//---
//--- return value of prev_calculated for next call

   // Checking how many bars to calculate.
   int first;
   if (prev_calculated == 0)
      first = rates_total - 100;
   else
      first = prev_calculated - 1 - n;   
      

   CChartObjectArrowUp arrow;  
   CChartObjectArrowDown arrow_low;  
   bool peak;
   static int peak_no = 0;
   
   // Deleting all of the old objects.
   if (peak_no == 0)
      ObjectsDeleteAll(0);
   
   for (int i = first; i < rates_total-n; i++){
      // Checking for peak.
      peak = true;
      // Checking that the left side of going down.
      for (int j = 0; j < n; j++){      
         if (high[i-j-1] < high[i-j] && peak){
            peak = true;
         }
         else{
            peak = false;
         }
      }
      // Checking that it does down to the right      
      for (int j = 0; j < n; j++){
         if (high[i+j+1] < high[i+j] && peak){
            peak = true;
         }
         else{
            peak = false;
         }
      }
      if (peak){
         Label1Buffer[i] = high[i];
         if (!arrow.Create(0,(string)(peak_no++),0,time[i],high[i]))
            Alert("Arrow wasn't created");            
      }
      
      // Checking for lows
      peak = true;
      // Checking that the left side of going down.
      for (int j = 0; j < n; j++){      
         if (low[i-j-1] > low[i-j] && peak){
            peak = true;
         }
         else{
            peak = false;
         }
      }
      // Checking that it does down to the right      
      for (int j = 0; j < n; j++){
         if (low[i+j+1] > low[i+j] && peak){
            peak = true;
         }
         else{
            peak = false;
         }
      }
      if (peak){
         Label1Buffer[i] = low[i];
         if (!arrow_low.Create(0,(string)(peak_no++),0,time[i],low[i]))
            Alert("Low array wasn't created");
      }
   }

   return(rates_total);
  }
//+------------------------------------------------------------------+
Alexey Da
Moderator
6604
Alexey Da 2010.08.11 17:14  
Try to open a source code of Fractal custom indicator which goes in standard delivery. I think you will find it similar to your code.
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.
nerobot
85
nerobot 2010.08.11 19:11  

Thanks, that helped me get the indicator working. However, it doesn't help explain why the newest arrows weren't coming up on the graph.


Steven


To add comments, please log in or register