Download MetaTrader 5
To add comments, please log in or register
Do you have a trading signal? Publish it in your blog as well!
Ernest Klokow
1069
Ernest Klokow 2015.03.05 10:46 

I have a little code snippet that determines two prices at different times and then I want to calculate the difference between the prices.

Why do I get a result of "0.0" when there should be a definite result with some real figures as can be seen from the two different prices?

(All the variables are doubles)

0    12:38:23    2015.01.06 00:10  Scalper2 EURUSD,M1: priceNow is 1.19354 priceLater is 1.19349 difference is 0.0

Here is the code:

RefreshRates();
   priceNow = Bid;
   datetime now = TimeCurrent();
   
   if( now > lastTime + pause)
   {
   lastTime = now;
   priceLater = Bid;
   difference = MathAbs(priceNow - priceLater);
Keith Watford
Moderator
7870
Keith Watford 2015.03.05 10:54  

You don't show the code for the print.

In the code that you do show, you give both variables the value Bid, so obviously the difference will be 0 

   priceNow = Bid;
   datetime now = TimeCurrent();
   
   if( now > lastTime + pause)
   {
   lastTime = now;
   priceLater = Bid;
   difference = MathAbs(priceNow - priceLater);
Ernest Klokow
1069
Ernest Klokow 2015.03.05 11:26  
GumRai:

You don't show the code for the print.

In the code that you do show, you give both variables the value Bid, so obviously the difference will be 0 

 

Here is the code for the print:

   Print( " priceNow is ", priceNow, " priceLater is ", priceLater,  " difference is ", difference);

Not sure why this should make a difference. The variables clearly show different Bid prices.

The reason for the different Bid prices is that they are read with an interval of 30 seconds between them.

Keith Watford
Moderator
7870
Keith Watford 2015.03.05 11:48  
ernest02:

Here is the code for the print:

   Print( " priceNow is ", priceNow, " priceLater is ", priceLater,  " difference is ", difference);

Not sure why this should make a difference. The variables clearly show different Bid prices.

The reason for the different Bid prices is that they are read with an interval of 30 seconds between them.


No, you need to show the code for the print with the other code.

We don't know where you have put the print in your code and that makes a difference.

 You may get over your problem by simply changing the order a bit.

   priceNow = Bid;
   datetime now = TimeCurrent();
   
   if( now > lastTime + pause)
   {
   lastTime = now;
   priceLater = Bid;
   difference = MathAbs(priceNow - priceLater);

 to

   priceNow = Bid;
   datetime now = TimeCurrent();
   
   if( now > lastTime + pause)
   {
   lastTime = now;
   difference = MathAbs(priceNow - priceLater);
   priceLater = Bid;
Ernest Klokow
1069
Ernest Klokow 2015.03.05 11:56  
GumRai:

No, you need to show the code for the print with the other code.

We don't know where you have put the print in your code and that makes a difference.

 You may get over your problem by simply changing the order a bit.

 to

 

 

Gumrai I appreciate your efforts to help me.

If I change the sequence of the code as you suggest, then priceLater has NO value in the line and will be zero, which will give a false result. Am I missing something?

difference = MathAbs(priceNow - priceLater);
Keith Watford
Moderator
7870
Keith Watford 2015.03.05 12:31  

Study this code

  static double priceNow=Bid;
  static double priceEarlier=Bid;
  static datetime lastTime=TimeCurrent();
  datetime pause=30;
  
  
  RefreshRates();
   priceNow = Bid;
   datetime now = TimeCurrent();
   
   if( now >= lastTime + pause)
   {
   string difference = DoubleToStr(MathAbs(priceNow - priceEarlier),Digits);
   Print( " priceNow is ", DoubleToStr(priceNow,Digits), " priceEarlier is ", DoubleToStr(priceEarlier,Digits),
          " difference is ", difference);
   lastTime = now;
   priceEarlier = Bid;
   }
Ernest Klokow
1069
Ernest Klokow 2015.03.05 14:03  

I have adjusted the code according to your recommendations, except I need the "difference" variable to be a double and not a string since I need to do some calculations with it.

Now I do get values for this variable, but they are completely random and unexplainable. Here are some examples (you can deduct the prices and you will see it does not match the difference)

0    15:51:15    2015.01.06 00:08  Scalper2 EURUSD,M1: priceNow is 1.19308 priceLater is 1.19311 difference is 0.0
0    15:51:15    2015.01.06 00:08  Scalper2 EURUSD,M1: priceNow is 1.19314 priceLater is 1.19314 difference is 3.0
0    15:51:14    2015.01.06 00:06  Scalper2 EURUSD,M1: priceNow is 1.19317 priceLater is 1.19318 difference is 0.0
0    15:51:12    2015.01.06 00:03  Scalper2 EURUSD,M1: priceNow is 1.19315 priceLater is 1.19315 difference is 21.0

And this is what the code looks like:

extern datetime   pause=30;

static double     priceNow;
static double     priceLater;
static datetime   lastTime=TimeCurrent();

RefreshRates();
   priceNow = Bid;
   datetime now = TimeCurrent();
   
   if( now > lastTime + pause)
   {
   difference = MathAbs(priceNow - priceLater);
   if (CalcPoint == 0.01)
   difference = NormalizeDouble(difference,3) * 1000;
   if (CalcPoint == 0.0001)
   difference = NormalizeDouble(difference,5) * 100000;
   lastTime = now;
   priceLater = Bid;
   }
Ernest Klokow
1069
Ernest Klokow 2015.03.05 15:49  
Is there anybody who can tell me how the same calculation can give such diverse and unexplainable results?
Keith Watford
Moderator
7870
Keith Watford 2015.03.05 19:46  
As I said before, you don't show the code that includes the print so we don't know what you do to the values before it prints.
Ernest Klokow
1069
Ernest Klokow 2015.03.06 06:44  
GumRai:
As I said before, you don't show the code that includes the print so we don't know what you do to the values before it prints.

I am embarrassed to say that once again you are right. I moved the Print statement to just after the "difference" calculation and now I get the correct prices and differences.

Thank you very much for your patience and the time you took to help me.

/
To add comments, please log in or register