Download MetaTrader 5

Store Bid and update

To add comments, please log in or register
Mrvla
177
Mrvla  
I want to store current bid price to use later and update stored every specified seconds, Now can anyone tell me why my code does not update the stored price?

  double time = 1000,price;
  int TimeBar;

//Fill price with Bid and record time
   if (price==0)
   {
       if(price=NormalizeDouble(Bid,Digits)) TimeBar=GetTickCount();
      
   }
    
//Now if 1 second pass, update price value with current Bid  
   if (price!=0)
   {
      if (TimeBar<GetTickCount()-time)
      {
        if(price=NormalizeDouble(Bid,Digits)) TimeBarB=GetTickCount();
        Print("Updated");
      }
   }
 
honest_knave
Moderator
2293
honest_knave  

There is no need to NormalizeDouble on the Bid.

Also be careful with the scope of your variables. Only static variables or variables with global scope will hold their value between calls. Local variables will not.

It would be a lot easier to use OnTimer(). 

double price;

int OnInit()
  {
   EventSetTimer(1);        // set the timer for 1 second
   return(INIT_SUCCEEDED);
  }

void OnTimer()
  {
   price=Bid;
   Print("Updated");
  }

As you're using the predefined variable Bid, I'm surmising this is MQL4? In which case I'll move to the MQL4 section.

Mrvla
177
Mrvla  
honest_knave:

There is no need to NormalizeDouble on the Bid.

Also be careful with the scope of your variables. Only static variables or variables with global scope will hold their value between calls. Local variables will not.

It would be a lot easier to use OnTimer(). 

double price;

int OnInit()
  {
   EventSetTimer(1);        // set the timer for 1 second
   return(INIT_SUCCEEDED);
  }

void OnTimer()
  {
   price=Bid;
   Print("Updated");
  }

As you're using the predefined variable Bid, I'm surmising this is MQL4? In which case I'll move to the MQL4 section.

My variables  are on Global scope.

Yes OnTimer() is easier, only if it worked on my build 1045 but it does not. Tested like below

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetTimer(60);
      
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
      
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
  
  Print("A minute passed");
  }
honest_knave
Moderator
2293
honest_knave  
Mrvla:
My variables  are on Global scope.

Yes OnTimer() is easier, only if it worked on my build 1045 but it does not. Tested like below

Are you testing it through strategy tester?

OnTimer() doesn't work in the strategy tester.

Add your EA to a live chart (it doesn't matter that the markets are closed - you don't need a tick) 

whroeder1
16572
whroeder1  
To add comments, please log in or register