stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Need help: data zeroes by Empty4
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=3326
Page 1 of 1
Author:  giailang [ Thu Dec 12, 2013 4:28 pm ]
Post subject:  Need help: data zeroes by Empty4

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,
Author:  phil_trade [ Thu Dec 12, 2013 4:34 pm ]
Post subject:  Re: Need help: data zeroes by Empty4

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
Author:  giailang [ Thu Dec 12, 2013 4:55 pm ]
Post subject:  Re: Need help: data zeroes by Empty4

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.
Author:  garyfritz [ Thu Dec 12, 2013 5:19 pm ]
Post subject:  Re: Need help: data zeroes by Empty4

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
Author:  phil_trade [ Thu Dec 12, 2013 5:22 pm ]
Post subject:  Re: Need help: data zeroes by Empty4

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 ?
Author:  milanese [ Thu Dec 12, 2013 5:31 pm ]
Post subject:  Re: Need help: data zeroes by Empty4

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
Author:  phil_trade [ Thu Dec 12, 2013 5:35 pm ]
Post subject:  Re: Need help: data zeroes by Empty4

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
Author:  giailang [ Fri Dec 13, 2013 3:34 am ]
Post subject:  Re: Need help: data zeroes by Empty4

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.
All times are UTC Page 1 of 1