Download MetaTrader 5
To add comments, please log in or register
Download MetaTrader 5 and start trading today!
n22alpha
63
n22alpha 2015.02.17 10:58 

Hi guys,

 I am messing about with MQL4 and only recently learned to code. As a project for myself I have decided to create numerous indicators I always wanted starting with an MA angle indicator. The code I have created is as such however it is not working an the MA angle constantly returns 0. Could anyone point me to where I've gone wrong. Also could someone tell me when I set a breakpoint how do I get the value of the variables at that breakpoint. Thank you guys and please go easy on me I'm new to coding. 

 

//+------------------------------------------------------------------+
//|                                                MAangleperiod.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_minimum -180
#property indicator_maximum 180
#property indicator_buffers 1
#property indicator_plots   1
//--- plot Angle
#property indicator_label1  "Angle"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

input int TimePeriod;
input int MovingAveragePeriod;
input ENUM_MA_METHOD MovingAverageType;

//--- indicator buffers
double         AngleBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping

   
   SetIndexBuffer(0,AngleBuffer);
   IndicatorDigits(Digits - 5);
   
      if(TimePeriod<=0 || MovingAveragePeriod <= 0)
     {
      Print("Wrong parameters");
      return(INIT_FAILED);
     }
     
     if (TimePeriod >= MovingAveragePeriod)
        {
        Print("Timeperiod must be less than moving average period");
        return(INIT_FAILED);
        }
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
      int i, pos;
      
      if (rates_total <= TimePeriod || rates_total <= MovingAveragePeriod)
      return(0);
      
      ArraySetAsSeries(AngleBuffer, false);
    
      //--- initial zero  //sets buffers to empty values
      if(prev_calculated<1)
         {
            for(i=0; i<MovingAveragePeriod; i++)
               {
                  AngleBuffer[i]=EMPTY_VALUE;
                
               }
         }
         
   if(prev_calculated>1)
      pos=prev_calculated-1;
   else
      pos=0;
      

         
       for(i=pos; i<rates_total && !IsStopped(); i++)
     {  
                
          double _MAdifference = iMA(_Symbol, _Period, MovingAveragePeriod, 0, MovingAverageType,PRICE_CLOSE, i) - iMA(_Symbol, _Period, MovingAveragePeriod, 0, MovingAverageType, PRICE_CLOSE, (i + 1)); 
         
         _MAdifference = (_MAdifference * 10000)/TimePeriod;
         
         AngleBuffer[i] = MathArctan(_MAdifference);
     }
   
   return(rates_total);
  }
//+------------------------------------------------------------------+
Keith Watford
Moderator
7887
Keith Watford 2015.02.17 11:11  

One line in your code is so wide that it makes your post impossible to read without scrolling back and forth

If you make your post easier to read, you may find more people will be willing to help you 

n22alpha
63
n22alpha 2015.02.17 12:33  
GumRai:

One line in your code is so wide that it makes your post impossible to read without scrolling back and forth

If you make your post easier to read, you may find more people will be willing to help you 

Ok sorry about that still learning to write clean code. Here is the new code and I have fixed some bugs.

 

 

//+------------------------------------------------------------------+
//|                                                MAangleperiod.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_minimum -180
#property indicator_maximum 180
#property indicator_buffers 1
#property indicator_plots   1
//--- plot Angle
#property indicator_label1  "Angle"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

input int AnglePeriod;
input int MovingAveragePeriod;
input ENUM_MA_METHOD MovingAverageType;

//--- indicator buffers
double         AngleBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping

   
   SetIndexBuffer(0,AngleBuffer);
   
      if(AnglePeriod <=0 || MovingAveragePeriod <= 0)
     {
      Print("Wrong parameters");
      return(INIT_FAILED);
     }
     
     if (AnglePeriod >= MovingAveragePeriod)
        {
        Print("Number of bars must be less than moving average period and greater than 1");
        return(INIT_FAILED);
        }
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
      int i, pos;
      
      if (rates_total <= AnglePeriod || rates_total <= MovingAveragePeriod)
      return(0);
      
      ArraySetAsSeries(AngleBuffer, false);
    
      //--- initial zero  //sets buffers to empty values
      if(prev_calculated<1)
         {
            for(i=0; i<MovingAveragePeriod; i++)
               {
                  AngleBuffer[i]=EMPTY_VALUE;
                
               }
         }
         
   if(prev_calculated>1)
      pos=prev_calculated-1;
   else
      pos=0;
      

         
       for(i=pos; i<rates_total && !IsStopped(); i++)
     {  
          double MovingAverageOne = iMA(_Symbol, _Period, MovingAveragePeriod, 0, MovingAverageType,PRICE_CLOSE, i);
          double MovingAverageTwo = iMA(_Symbol, _Period, MovingAveragePeriod, 0, MovingAverageType, PRICE_CLOSE, (i + AnglePeriod)); 
          
          double _MAdifference = MovingAverageOne - MovingAverageTwo;
          
         _MAdifference = (_MAdifference * 10000)/ AnglePeriod + 1;
         
         AngleBuffer[i] = MathArctan(_MAdifference);
     }
   
   return(rates_total);
  }
//+------------------------------------------------------------------+
whroeder1
13596
whroeder1 2015.02.17 13:28  
n62alpha: I always wanted starting with an MA angle indicator.
Angle comes from arctan(x/y) a unit less number. A chart has price/time. You can get the slope but asking for an angle is like asking "what is the angle of driving 30 miles in 20 minutes." Meaningless.
n22alpha
63
n22alpha 2015.02.17 17:00  
WHRoeder:
Angle comes from arctan(x/y) a unit less number. A chart has price/time. You can get the slope but asking for an angle is like asking "what is the angle of driving 30 miles in 20 minutes." Meaningless.

What I'm trying to do is take price1 and price2 at the close. Subtract one from the other and take the matharctan of the result which should give me the angle I think unless I'm mistaken :)

 

price1 is a constant

as is price2

 

and so is the number of bars (angleperiod). 

whroeder1
13596
whroeder1 2015.02.17 22:06  

You are mistaken. You MUST pass a unit-less number to it.

You are passing change-price/change-time which is rate of price change. Your result is meaningless.

Answer the question: "what is the angle of driving 30 miles in 20 minutes?"

/
To add comments, please log in or register