function Mathlog

 

Why in the following code, does not draw logarithms?

//+------------------------------------------------------------------+
//|                                                   Logaritmos.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_buffers 2
#property indicator_color2 Red
#property indicator_minimum    0
#property indicator_maximum    1
double A[];
double B[];



//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexBuffer(0,B);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
   IndicatorDigits(Digits+5);

   
    return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int start()
  {
 int counted_bars=IndicatorCounted();


if (counted_bars>0)
counted_bars--;
int limit= Bars-counted_bars;


for (int i=0;i<limit;i++)
{
//----
  
  
   
     A[i]=(High[i]);
     B[i]=MathLog(A[i]);
   
   }
   return(0);
} 

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

 Thank You very much

 

I'm guessing you have an array out of range error in your log? You do not SetIndexBuffer for A[].

 I suspect you'll also have problems with your color assignment (indicator_color2 Red)

And possibly also your indicator minimum (indicator_minimum    0) .... natural log of values >0  && <1 are negative (think USDCHF, for example).

 
honest_knave:

I'm guessing you have an array out of range error in your log? You do not SetIndexBuffer for A[].

 I suspect you'll also have problems with your color assignment (indicator_color2 Red)

And possibly also your indicator minimum (indicator_minimum    0) .... natural log of values >0  && <1 are negative (think USDCHF, for example).

Thank you very much for your reply.
I want to represent the logarithms of the high price.
The logarithms are represented by "B []."
Therefore, in SetIndexBuffer I use B [], right?

I modified the values in [-1,1] But I still get not the values of log.

#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_buffers 2
#property indicator_color2 Red
#property indicator_minimum    -1
#property indicator_maximum    1
double A[];
double B[];



//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexBuffer(0,B);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,indicator_color2);
   

   
    return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int start()
  {
 int counted_bars=IndicatorCounted();


if (counted_bars>0)
counted_bars--;
int limit= Bars-counted_bars;


for (int i=0;i<limit;i++)
{
//----
  
  
   
     A[i]=(High[i]);
     B[i]=MathLog(A[i]);
   
   }
   return(0);
} 

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

 

 

The problem is that you have declared an array A[] but it has no size.

So when you access A[i] it is going to be out of range. Have you checked your Experts Tab?

Why don't you dispense with A altogether:

 

#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_buffers 2
#property indicator_buffers 1
//#property indicator_color2 Red
#property indicator_color1 Red
//#property indicator_minimum    -1
//#property indicator_maximum    1
double A[];
//double B[];



//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
//   SetIndexBuffer(0,B);
//   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,indicator_color2);
   SetIndexBuffer(0,A);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID);
 
    return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int start()
  {
 int counted_bars=IndicatorCounted();


if (counted_bars>0)
counted_bars--;
int limit= Bars-counted_bars;


for (int i=0;i<limit;i++)
{
//----
  
  
//     A[i]=(High[i]);
//     B[i]=MathLog(A[i]);
     A[i]=MathLog(High[i]);
   
   }
   return(0);
} 

  
//+------------------------------------------------------------------+
 
honest_knave:

The problem is that you have declared an array A[] but it has no size.

So when you access A[i] it is going to be out of range. Have you checked your Experts Tab?

Why don't you dispense with A altogether:

 

I believe you meant dispense with B altogether right ?
As you have done in the corrected code with //B's related commenting ?
 
honest_knave:

The problem is that you have declared an array A[] but it has no size.

So when you access A[i] it is going to be out of range. Have you checked your Experts Tab?

Why don't you dispense with A altogether:

 

Thank you very much.
Another question,
how many decimal places at most shows MQL4?

I have understood that 15, but with this instruction, I do not get more than 8:

int init()
  {
//---- indicators
//   
   SetIndexBuffer(0,A);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID);
 IndicatorDigits(Digits+10);
    return(0);
  }

 

 
Agent86:
I believe you meant dispense with B altogether right ?
As you have done in the corrected code with //B's related commenting ?



Dispense with either because only 1 is necessary.

But you are quite right though, because of my code I should have written B not A. 

 
justify:
Thank you very much.
Another question,
how many decimal places at most shows MQL4?

I have understood that 15, but with this instruction, I do not get more than 8:

 

The values of A are stored as doubles, so their representation accuracy is 15 significant digits. You can see this by printing the values of A.

int start()
  {
 int counted_bars=IndicatorCounted();


if (counted_bars>0)
counted_bars--;
int limit= Bars-counted_bars;


for (int i=0;i<limit;i++)
{
//----
  
  
//     A[i]=(High[i]);
//     B[i]=MathLog(A[i]);
     A[i]=MathLog(High[i]);
     printf("%.15f",A[i]);   
   }
   return(0);
} 

 

 

 

However, I'm not sure how many digits the data window is capable of showing.

 
honest_knave:

The values of A are stored as doubles, so their representation accuracy is 15 significant digits. You can see this by printing the values of A.

 

However, I'm not sure how many digits the data window is capable of showing.

If it is a question I had about the decimals it supports. Still, I do not show me more than 8 decimal places, and I instuccion the previous post:

int init()
  {
//---- indicators
//   
   SetIndexBuffer(0,A);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID);
 IndicatorDigits(Digits+10);
    return(0);
  }

int start()
  {
 int counted_bars=IndicatorCounted();


if (counted_bars>0)
counted_bars--;
int limit= Bars-counted_bars;


for (int i=0;i<limit;i++)
{
//----
  
  
//     A[i]=(High[i]);
//     B[i]=MathLog(A[i]);
     A[i]=MathLog(High[i]);
     printf("%.15f",A[i]);   
   }
   return(0);
} 

 

 
I know what I do not understand.
Decimal doing calculations with mt4?
That is, if I want to do calculations with decimals, I can take the number of decimal places to make those calculations ?.
For example, instead of using 18 decimal, I can use only 4?
 

Take a look at NormalizeDouble - it might be what you are after.

Reason: