how to implement CopyRate function

 

hello all,

I start working on CopyRates, I have some question about this function,

first, I try to run this function in a script but the value I get for each day(for example the last 7 days) are just the same for one symbol, can you help me with it?

and second I run it in a script, because there are several symbols in my market watch that I want to get their close price, can I do it in just one script?


this is my code, I really appreciate if you help

#property script_show_inputs
//--- input parameters
input int      NUMBER_OF_DAYS = 7;
int marketWatchCount;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   MqlRates rates[];
   MqlDateTime today; 
   ArraySetAsSeries(rates,true);
   //get number of 
   marketWatchCount = SymbolsTotal(true);
  
   datetime current_time=TimeCurrent();                          
   TimeToStruct(current_time,today);                             
   PrintFormat("current_time=%s",TimeToString(current_time));    
   today.hour=0; 
   today.min=0; 
   today.sec=0; 
   datetime startday=StructToTime(today); 
   datetime endday=startday+(NUMBER_OF_DAYS)*24*60*60; 
   for(int i = 0 ; i <marketWatchCount ; i++)
   for(int day = 0; day < NUMBER_OF_DAYS ; day++){
   CopyRates(SymbolName(i,true) ,0 , ((startday*1000 - (day + 1)*24*60*60)*1000), 1, rates );
      printf(rates[0].close);
   }
   
   }
 
bitamirshafiee:

hello all,

I start working on CopyRates, I have some question about this function,

first, I try to run this function in a script but the value I get for each day(for example the last 7 days) are just the same for one symbol, can you help me with it?

and second I run it in a script, because there are several symbols in my market watch that I want to get their close price, can I do it in just one script?


this is my code, I really appreciate if you help

Your main issue is the way you are trying to use time in this scenario. You are attempting to get rates from the beginning of today to seven days into the future. Even if you were to subtract the seconds this would still cause you errors due to the markets being closed over the weekends. You could use iTime to get the start and end times based on the number of bars, but a much better solution is to use the PERIOD_D1 timeframe and simply specify the number of bars. 

#property script_show_inputs
//--- input parameters
input int   inp_num_days = 7; //Number of days
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   MqlRates rates[];
   ArraySetAsSeries(rates, true);
   int total = CopyRates(_Symbol, PERIOD_D1, 1, inp_num_days, rates);
   for(int i=0; i<total; i++){
      printf("The close on %s was %s",
         TimeToString(rates[i].time, TIME_DATE),
         DoubleToString(rates[i].close, _Digits)
      );
   }
}
 
nicholi shen:

Your main issue is the way you are trying to use time in this scenario. You are attempting to get rates from the beginning of today to seven days into the future. Even if you were to subtract the seconds this would still cause you errors due to the markets being closed over the weekends. You could use iTime to get the start and end times based on the number of bars, but a much better solution is to use the PERIOD_D1 timeframe and simply specify the number of bars. 

Thanks a lot I completely understand what was my problem, thanks for your great code
Reason: