Previous tick's value - page 4

 
Thanks Nicholi for that more streamlined code. After some experimenting, I found that declaring ask_diff as float produced differences with the correct number of digits but a compile error about possible data loss from type conversion. It looks like I have to choose between have very small inaccuracies in my differences or having a compile warning. The latter would be better for trading decisions, but does anyone else have a solution to get at most five decimal places in ask_diff?
 
rdone:
Thanks Nicholi for that more streamlined code. After some experimenting, I found that declaring ask_diff as float produced differences with the correct number of digits but a compile error about possible data loss from type conversion. It looks like I have to choose between have very small inaccuracies in my differences or having a compile warning. The latter would be better for trading decisions, but does anyone else have a solution to get at most five decimal places in ask_diff?

your ask_diff should be type int and look like this instead. 

int diff = int(round((current.ask - last.ask) / _Point));
 
I just discovered NormalizeDouble and DoubleToString. Hey, it's my first day at this...
 
rdone:
I just discovered NormalizeDouble and DoubleToString. Hey, it's my first day at this...
My goal is to determine the difference between the current Ask and the previous Ask (Part 1) and then keep a running total of those differences (Part 2). Part 1 works but Part 2 just returns the previous Ask amount. Can anyone see where I am going wrong and provide some course correction?
double ask_curr;
double ask_prev;
double ask_diff;
double diff_ask;
MqlTick tick_curr, tick_prev;
void OnTick()
  {
   if(SymbolInfoTick(Symbol(),tick_curr))
     {
      /* Part 1 */
      ask_curr = tick_curr.ask;
      ask_prev = tick_prev.ask;
      ask_diff = ask_curr - ask_prev;
      Print(DoubleToString(ask_diff,5));
      /* Part 2 */
      diff_ask = diff_ask + ask_diff;
      Print(DoubleToString(diff_ask,5));     
      tick_prev = tick_curr;
     }
   else Print("SymbolInfoTick() failed, error = ",GetLastError());
  }
 
As often happens, the code was doing what it was supposed to instead of what I wanted it to do. The logic requires tick_prev to always be populated but is not true on the first tick. I am interested in a logic that does not require tick_prev to be populated on the first tick, but otherwise this works.
 
rdone:
As often happens, the code was doing what it was supposed to instead of what I wanted it to do. The logic requires tick_prev to always be populated but is not true on the first tick. I am interested in a logic that does not require tick_prev to be populated on the first tick, but otherwise this works.
And now with code.
double ask_curr;
double ask_prev;
double ask_diff;
double diff_ask;
int notfirst;
MqlTick tick_curr, tick_prev;
void OnTick()
  {
   if(SymbolInfoTick(Symbol(),tick_curr))
     {
      if (notfirst > 0)
      {
      /* Part 1 */
      ask_curr = tick_curr.ask;
      ask_prev = tick_prev.ask;
      ask_diff = ask_curr - ask_prev;
      Print(DoubleToString(ask_diff,5));
      /* Part 2 */
      diff_ask = diff_ask + ask_diff;
      Print(DoubleToString(diff_ask,5));
      }
      tick_prev = tick_curr;
      notfirst = 1;  
     }
   else Print("SymbolInfoTick() failed, error = ",GetLastError());
  }
 
rdone:
As often happens, the code was doing what it was supposed to instead of what I wanted it to do. The logic requires tick_prev to always be populated but is not true on the first tick. I am interested in a logic that does not require tick_prev to be populated on the first tick, but otherwise this works.
double diff_ask=0;
MqlTick tick_curr, tick_prev={0};
void OnTick()
  {
   if(SymbolInfoTick(Symbol(),tick_curr))
     {
      /* Part 1 */
      diff_ask += tick_prev.ask!=0.0?(tick_curr.ask-tick_prev.ask):0.0;
      Print(DoubleToString(diff_ask,5));
      /* Part 2 */
      //diff_ask = diff_ask + ask_diff;
      //Print(DoubleToString(diff_ask,5));     
      tick_prev = tick_curr;
     }
   else Print("SymbolInfoTick() failed, error = ",GetLastError());
  }
 
Ernst Van Der Merwe:
This is a level of coding that I aspire to achieve. Thank you Ernst!
diff_ask += tick_prev.ask!=0.0?(tick_curr.ask-tick_prev.ask):0.0;
 
rdone:
This is a level of coding that I aspire to achieve. Thank you Ernst!

You don't want to store the point difference as a double since doubles are rarely equal. You want to store it as an integer value. For example (1.00001 - 1.00000) / 0.00001 = 1 point. So if you only were comparing two ticks then you can make your code more compact and just subclass MqlTick and add an int attribute for the point diff. 

struct MyTick : public MqlTick{int point_diff;} tick_curr={0}, tick_prev={0};
void OnStart()
{
   tick_prev = tick_curr;
   SymbolInfoTick(_Symbol, tick_curr);
   if(tick_prev.ask != 0.0){
      tick_curr.point_diff = int(round(
         (tick_curr.ask - tick_prev.ask) / _Point
      ));
      printf("%.5f -> %.5f: %d point diff",
         tick_prev.ask,
         tick_curr.ask,
         tick_curr.point_diff
      );
   }
}
Reason: