Need help: data zeroes by Empty4

Post Reply
giailang
Trader
Posts: 18
Joined: Wed Oct 24, 2012 10:57 am

Need help: data zeroes by Empty4

Post by giailang »

When I try to calculate within my snippet, I found that Empty4 zeroes my fragment after decimal, though all variables are double. The same fragment runs ok within indicator. Please advise how to cope with this, without using NormalizeDouble?

TIA,
phil_trade

Re: Need help: data zeroes by Empty4

Post by phil_trade »

giailang wrote:When I try to calculate within my snippet, I found that Empty4 zeroes my fragment after decimal, though all variables are double. The same fragment runs ok within indicator. Please advise how to cope with this, without using NormalizeDouble?

TIA,
Hi giailang

you should publish this code snippet So that we can understand

cheers

Philippe
giailang
Trader
Posts: 18
Joined: Wed Oct 24, 2012 10:57 am

Re: Need help: data zeroes by Empty4

Post by giailang »

phil_trade wrote:
giailang wrote:When I try to calculate within my snippet, I found that Empty4 zeroes my fragment after decimal, though all variables are double. The same fragment runs ok within indicator. Please advise how to cope with this, without using NormalizeDouble?

TIA,
Hi giailang

you should publish this code snippet So that we can understand

cheers

Philippe
Thanks, it's so simple so I did not post. Here it goes

Code: Select all

void testdata(string sym, int TF, int period, int Fast, int Slow)
{
    double ma1,ma2,testbuf[];
    for(int i=0;i<period;i++)
    {
      ma1=iMA(symb,TF,Fast,0,MODE_EMA,PRICE_CLOSE,i);
      ma2=iMA(symb,TF,Slow,0,MODE_EMA,PRICE_CLOSE,i);
      testbuf[i]=ma1-ma2;
      Print("ma1= ",ma1,", ma2= ",ma2, ", testbuf[",i,"]= ",testbuf[i]);
     }
  
}

Run the code, you can see ma1!=ma2, but testbuf ==0. Later, compare testbuf and testbuf[i+1] you got testbuf=testbuf[i+1];
I did add IndicatorDigits(Digits+1) to the code, but nothing change. When put inside the indicator, it works normally, but the iCustom has drawback of consuming too much RAM, increase each time the indicator is called.

Thanks for your help.
garyfritz

Re: Need help: data zeroes by Empty4

Post by garyfritz »

testbuf[] is a POINTER to an array of doubles, not the array itself. MQL should have thrown an error when you accessed the un-initialized pointer, but, well, MQL.

If you just need an array of doubles, you would declare it as "double testbuf[100]". But you probably need an array that corresponds to your display buffers?

If you want to display the values in testbuf[], you should use SetIndexBuffer(). That will tell MQL to allocate space to the array, and grow it by one every time a new bar is added to the chart.

If you just want to use it for calculation, you have to allocate the space yourself. And I've forgotten the function for that and I just got an emergency call and have to leave. Someone will remember. :D
phil_trade

Re: Need help: data zeroes by Empty4

Post by phil_trade »

giailang wrote:
phil_trade wrote:
giailang wrote:When I try to calculate within my snippet, I found that Empty4 zeroes my fragment after decimal, though all variables are double. The same fragment runs ok within indicator. Please advise how to cope with this, without using NormalizeDouble?

TIA,
Hi giailang

you should publish this code snippet So that we can understand

cheers

Philippe
Thanks, it's so simple so I did not post. Here it goes

Code: Select all

void testdata(string sym, int TF, int period, int Fast, int Slow)
{
    double ma1,ma2,testbuf[];
    for(int i=0;i<period;i++)
    {
      ma1=iMA(symb,TF,Fast,0,MODE_EMA,PRICE_CLOSE,i);
      ma2=iMA(symb,TF,Slow,0,MODE_EMA,PRICE_CLOSE,i);
      testbuf[i]=ma1-ma2;
      Print("ma1= ",ma1,", ma2= ",ma2, ", testbuf[",i,"]= ",testbuf[i]);
     }
  
}

Run the code, you can see ma1!=ma2, but testbuf ==0. Later, compare testbuf and testbuf[i+1] you got testbuf=testbuf[i+1];
I did add IndicatorDigits(Digits+1) to the code, but nothing change. When put inside the indicator, it works normally, but the iCustom has drawback of consuming too much RAM, increase each time the indicator is called.

Thanks for your help.


I don't know if it's the bug with another varibale, but you Have "sym" param in TestData but use "symb" variable in it. Just a typo ?
User avatar
milanese
TechAdmin
Posts: 3293
Joined: Wed Jan 09, 2013 9:02 am
Location: btr rdx, r8 +

Re: Need help: data zeroes by Empty4

Post by milanese »

giailang wrote:
phil_trade wrote:
giailang wrote:When I try to calculate within my snippet, I found that Empty4 zeroes my fragment after decimal, though all variables are double. The same fragment runs ok within indicator. Please advise how to cope with this, without using NormalizeDouble?

TIA,
Hi giailang

you should publish this code snippet So that we can understand

cheers

Philippe
Thanks, it's so simple so I did not post. Here it goes

Code: Select all

void testdata(string sym, int TF, int period, int Fast, int Slow)
{
    double ma1,ma2,testbuf[];
    for(int i=0;i<period;i++)
    {
      ma1=iMA(symb,TF,Fast,0,MODE_EMA,PRICE_CLOSE,i);
      ma2=iMA(symb,TF,Slow,0,MODE_EMA,PRICE_CLOSE,i);
      testbuf[i]=ma1-ma2;
      Print("ma1= ",ma1,", ma2= ",ma2, ", testbuf[",i,"]= ",testbuf[i]);
     }
  
}

Run the code, you can see ma1!=ma2, but testbuf ==0. Later, compare testbuf and testbuf[i+1] you got testbuf=testbuf[i+1];
I did add IndicatorDigits(Digits+1) to the code, but nothing change. When put inside the indicator, it works normally, but the iCustom has drawback of consuming too much RAM, increase each time the indicator is called.

Thanks for your help.


you should add in init() this:

Code: Select all

if(ArraySize( testbuf) < Bars)
     {
       ArraySetAsSeries( testbuf, false);
       ArrayResize( testbuf, Bars); 
       ArraySetAsSeries (testbuf, true);
    
     } 
that should work if you will correct the typo that Phil has spotted..

Cheers :)

Tommaso
Global Prime is the official SHF broker :yahoo:
Searching for Servers and Workstations with individual configuration?
Just PM
:smile: Click here to go to the BoardKnowledgeBase
NOTE: Cookies and JavaScript are required for the using the board, with full functionality
phil_trade

Re: Need help: data zeroes by Empty4

Post by phil_trade »

You need to initialize array like this :

Code: Select all

   void testdata(string sym, int TF, int period, int Fast, int Slow)
{
    double ma1,ma2;
    double testbuf[10];
    
    for(int i=0;i<period;i++)
    {
      
      ma1=iMA(sym,TF,Fast,0,MODE_EMA,PRICE_CLOSE,i);
      ma2=iMA(sym,TF,Slow,0,MODE_EMA,PRICE_CLOSE,i);
      testbuf[i]=ma1-ma2;
      Print("ma1= ",ma1,", ma2= ",ma2, "ma1-ma2= ",(ma1-ma2), "testbuf[",i,"]= ",NormalizeDouble(testbuf[i],5));
     }
  
}
Unfortunately, you can pass period to init it like this

Code: Select all

double testbuf[period];
as mql need a literal integer

cheers

Philippe
giailang
Trader
Posts: 18
Joined: Wed Oct 24, 2012 10:57 am

Re: Need help: data zeroes by Empty4

Post by giailang »

phil_trade wrote:
giailang wrote:
phil_trade wrote:
giailang wrote:When I try to calculate within my snippet, I found that Empty4 zeroes my fragment after decimal, though all variables are double. The same fragment runs ok within indicator. Please advise how to cope with this, without using NormalizeDouble?

TIA,
Hi giailang

you should publish this code snippet So that we can understand

cheers

Philippe
Thanks, it's so simple so I did not post. Here it goes

Code: Select all

void testdata(string sym, int TF, int period, int Fast, int Slow)
{
    double ma1,ma2,testbuf[];
    for(int i=0;i<period;i++)
    {
      ma1=iMA(symb,TF,Fast,0,MODE_EMA,PRICE_CLOSE,i);
      ma2=iMA(symb,TF,Slow,0,MODE_EMA,PRICE_CLOSE,i);
      testbuf[i]=ma1-ma2;
      Print("ma1= ",ma1,", ma2= ",ma2, ", testbuf[",i,"]= ",testbuf[i]);
     }
  
}

Run the code, you can see ma1!=ma2, but testbuf ==0. Later, compare testbuf and testbuf[i+1] you got testbuf=testbuf[i+1];
I did add IndicatorDigits(Digits+1) to the code, but nothing change. When put inside the indicator, it works normally, but the iCustom has drawback of consuming too much RAM, increase each time the indicator is called.

Thanks for your help.


I don't know if it's the bug with another varibale, but you Have "sym" param in TestData but use "symb" variable in it. Just a typo ?


Thanks Phil, that's my typo, in the real code it is sym. Thanks Garry for pointing out my declaration is only pointer, not array.Thanks Tomasso for the init code.
Actually, I am trying to convert BB_MACD indicator into Signal detector. When finish my mod, I'll post it asap.
Post Reply

Return to “Coders Hangout”