prev_calculated - page 10

 
Dmitry Fedoseev:
It doesn't. There will be a connection break of several bars, several bars will be uninitialized with rubbish.
Alexey Kozitsyn:
Either, as Dmitry said above, there was a connection break of several bars... By the way, will prev_calculated return 0 too in case of a connection failure?

Apparently there was indeed a break in communication at this time.

2016.10.19 04:46:13.770 Network '4092672': scanning network finished
2016.10.19 04:45:37.260 Network '4092672': scanning network for access points
2016.10.19 04:45:36.630 Network '4092672': trading has been enabled - hedging mode
2016.10.19 04:45:36.630 Network '4092672': terminal synchronized with MetaQuotes Software Corp.
2016.10.19 04:45:36.000 Network '4092672': previous successful authorization performed from 31.173.80.3 on 2016.10.18 17:51:14
2016.10.19 04:45:36.000 Network '4092672': authorized on MetaQuotes-Demo through Access Point SG Singapore (ping: 583.86 ms)
2016.10.19 04:42:57.680 Network '4092672': connection to MetaQuotes-Demo lost

And it is most likely that when there is a break, prev_calculated returns 0.

OK, it was a long break, but what caused the other prev_calculated nulls?

From these lines.

2016.10.18 23:51:34.895 Network '4092672': scanning network finished
2016.10.18 23:51:20.865 Network '4092672': scanning network for access points

Up to those above there are no other records other than trade records like this one.

2016.10.19 00:00:36.066 Trades  '4092672': cancel order #103987819 buy stop 0.10 USDJPY at 103.977 sl: 103.377 tp: 104.077

And you can see how many zeroing of prev_calculated was in my previous post.

 
Alexey Kozitsyn:

Potentially, the fault could be here:

if(rates_total > prev_calculated)
   {
    minEquity = 0;
    maxEquity = 0;
   }
   minEquity = NormalizeDouble(fmin((minEquity == 0 ? bal : minEquity), equity), 2);
    maxEquity = NormalizeDouble(fmax(maxEquity, equity), 2);

When a new bar arrives, you reset the values to 0 - fine. But then you check minEquity and 0 for equality, which is not recommended.

To prove my words, here is your image. You can see that the "rubbish" values, as you said, are about zero.

Of course, you'd better add a data window in the picture with the "trash" value.

Can you elaborate on why this method is not recommended?

This (minEquity == 0? bal : minEquity) part of the code, if minEquity == 0, will return the value of bal which was obtained earlier; the value of minEquity will not change until the function fmin() terminates.

Although I agree that such writing is a bit risky... but that's not the problem.

 
Alexey Viktorov:

Can you explain in more detail why this method is not recommended?

this (minEquity == 0? bal : minEquity) part of the code, if minEquity == 0, will return the value of bal obtained earlier; the value of minEquity will not change until the function fmin() terminates

Although I agree that this way of writing the code is a bit risky... but that's not the problem.

I was speaking specifically about this: minEquity ==0. You're comparing to equal numbers of doubles. This might explain the drop in values to 0.
 
Alexey Kozitsyn:
I was talking specifically about this: minEquity ==0. You're comparing on the equality of the dabble numbers. That might explain the drop in values to 0.

I see, the correct answer is minEquity ==0.0 ... I can't get used to it.

And the last sentence I don't understand at all. What values to 0?

 
Alexey Viktorov:

I see, the correct answer is minEquity ==0.0 ... I can't get used to it.

And the last sentence I don't understand at all. What values to 0?

But I don't know if an implicit type conversion might cause an error. I was speaking generally about comparing real types. I.e. here, to be safe, I would write it this way:

CompareDoubles( minEquity, 0.0 );

bool CompareDoubles(double number1,double number2)
  {
   if(NormalizeDouble(number1-number2,8)==0) return(true);
   else return(false);
  }
 
Alexey Kozitsyn:

However, I do not know if implicit type conversion may cause an error, I was speaking, in principle, about comparing real types. I.e. here, for reliability, I would write it like this:

CompareDoubles( minEquity, 0.0 );

bool CompareDoubles(double number1,double number2)
  {
   if(NormalizeDouble(number1-number2,8)==0) return(true);
   else return(false);
  }


Yes, my version counts money with two decimal places. This is not a quotation to be so meticulous about it.
 
Alexey Viktorov:
Yes, my version counts money with two decimal places. This is not a quotation, so you can't be so precise about it.

It's not a matter of meticulousness, it's a matter of accuracy. In your case, it could result in a zero value being written to the buffer. If you don't want such precision, do it this way:

if(rates_total > prev_calculated)
   {
    minEquity = -1.0;
    maxEquity = 0.0;
   }
   minEquity = NormalizeDouble(fmin((minEquity < 0 ? bal : minEquity), equity), 2);
    maxEquity = NormalizeDouble(fmax(maxEquity, equity), 2);

Then there will be no error.

 
Alexey Kozitsyn:

It's not a matter of meticulousness, it's a matter of accuracy. In your case, it could result in a zero value being written to the buffer. If you don't want that precision, do it this way:

if(rates_total > prev_calculated)
   {
    minEquity = -1.0;
    maxEquity = 0.0;
   }
   minEquity = NormalizeDouble(fmin((minEquity < 0 ? bal : minEquity), equity), 2);
    maxEquity = NormalizeDouble(fmax(maxEquity, equity), 2);

There will be no error in that case.

Why am I so stupid? How could I have not guessed that it's better to initialize minEquity with a non-zero value? Then everything will be easier...

if(rates_total > prev_calculated)
   {
    minEquity = DBL_MAX;
    maxEquity = 0.0;
   }
   minEquity = NormalizeDouble(fmin(minEquity, equity), 2);
    maxEquity = NormalizeDouble(fmax(maxEquity, equity), 2);

You may also use maxEquity = DBL_MIN;

 
Alexey Viktorov:

Why am I so stupid? How could I have not guessed that it's better to initialize minEquity with a non-zero value? Then everything would be easier...

if(rates_total > prev_calculated)
   {
    minEquity = DBL_MAX;
    maxEquity = 0.0;
   }
   minEquity = NormalizeDouble(fmin(minEquity, equity), 2);
    maxEquity = NormalizeDouble(fmax(maxEquity, equity), 2);


Well... or so...
 
Alexey Kozitsyn:
Well... Or so...
That's not really the problem. Although it has somehow been circumvented, the rubbish in the indicator buffer when the indicator starts is not right.