STRANGE STRANGE thing happens?

Back to topics list To post a new topic, please log in or register
avatar
16
tazosmr 2015.11.28 01:26
 
#property copyright "sample"
#property link      "http://sample.sample"
#property version   "1.00"
#property indicator_buffers 7
#property strict
#property indicator_chart_window                        // or indicator_separate_window
#property indicator_plots   3                           //How Many Plots

input int Periods=13;
double         MyBuffer[];
int OnInit()
{
   SetIndexBuffer(0,MyBuffer,INDICATOR_DATA);
   return(INIT_SUCCEEDED);
}

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 first=Periods+2;    if(prev_calculated > 0 ) { first = prev_calculated-1; } 
                
        for(int bar = 0; bar < rates_total-first; bar++){                                       // main loop of the calculation
                MyBuffer[bar]= (2 / Periods);
        }
        return(rates_total);
}


i use (on line 32)  :

2/1 (or 2/2 )

it works well, otherwise, if i put:

2/3  (or 2/4 or etc...)

plot always gets ZERO?


why????????????????

avatar
Moderator
3419
GumRai 2015.11.28 04:05 #
 
                MyBuffer[bar]= (2.0 / Periods);

Change the 2 to 2.0, otherwise it returns an integer.

In future, please just paste your code using the SRC button, instead of a link. I have done it for you this time. 

avatar
16
tazosmr 2015.11.28 08:00 #
 

GumRai:

Change the 2 to 2.0, otherwise it returns an integer.

In future, please just paste your code using the SRC button, instead of a link. I have done it for you this time. 

!!!!!!!!!!  THANKS !!!!!!!!!!  thanks being kind for helping users!!!!!!!
avatar
13006
avatar
126
Demos 2015.11.28 17:22 #
 

Hello,

while we are discussing floats, i may add somes points, regarding floating point arithmetic in MQL. If someone ever tries to declare a float with a fractional part which is not present in binary, such as 0.6, 1.8, will get a compiler warning "truncation of a constant value". After some searching, we can see that this happens in languages where floats are represented as doubles internally. So, if your number is not x.5,x.25,x.125,() the double will most likely have some trailing numbers, in the end (which are going to be truncated for the number to be represented in float); for example, 0.6 could be 0.5999999999999, or 0.600000000000001 or whatever ( :) ).

p.s. Interestingly enough, if you declare a float with more than 5 digits in fractional part, but that number is in binary "format", like 0.00390625, compiler is not going to complain about any truncation 

p.s. 2 Lesson is , there should be not any benefit when using floats in MQL , instead of doubles , quite the contrary probably

p.s. 3 If you would like to use floats for some reason , you can put a "f" after the number e.g. float a=0.6f (but we do not have to, for e.g. float a=0.5)

 best regards 

Back to topics list  

To add comments, please log in or register