Get Close Price of each day in a script

To add comments, please log in or register
bitamirshafiee
17
bitamirshafiee  

Hi all,

I searched a lot to get the close price of each day for the specified symbol, this is what I've done:


for(int i = 0 ; i <marketWatchCount ; i++)
   for(int day = 0; day < NUMBER_OF_DAYS ; day++){
      ticks = CopyTicksRange(SymbolName(i,true),tick_array_day, COPY_TICKS_ALL, (startday*1000 + 24*60*60*day), ((startday + (day + 1)*24*60*60)*1000));
      
      OnTick(SymbolName(i,true), tick_array_day[0]);
      sleep(10);
   }
}

void OnTick(string symbolName, MqlTick& last_tick) 
  {  

   if(SymbolInfoTick(symbolName,last_tick)) 
     { 
     printf(symbolName,"mahdi: ",last_tick.ask);
      Print(last_tick.time,": Bid = ",DoubleToString(last_tick.bid), 
            " Ask = ",DoubleToString(last_tick.last),"  Volume = ",DoubleToString(last_tick.volume)); 
     } 
   else Print("SymbolInfoTick() failed, error = ",GetLastError()); 
//--- 
  } 

but I get same integer value for every bid or last value of last tick

Can you tell me what I'm doing wrong?

nicholi shen
2155
nicholi shen  
Can you tell me what I'm doing wrong?

  1. This is an incomplete code snippet. 
  2. Don't overload the OnTick function.
  3. Use CopyRates if all you need is the close price

bitamirshafiee
17
bitamirshafiee  
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property script_show_inputs
//--- input parameters
input int      NUMBER_OF_DAYS = 7;
//--- Requesting 100 million ticks to be sure we receive the entire tick history 
input int      getticks=100000000; // The number of required ticks
int marketWatchCount;

struct Str1
  { 
   double               count[]; 
   double               day[]; 
  }; 
  Str1 str1;
  int ticks;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   MqlTick last_tick;
         int price_count = 0;
   int     attempts=0;     // Count of attempts 
   bool    success=false;  // The flag of a successful copying of ticks 
   MqlTick tick_array[];   // Tick receiving array 
   MqlTick tick_array_day[];
   MqlTick tick_array_day_after[];
   MqlDateTime today;
   
   
   marketWatchCount = SymbolsTotal(true);
  //printf(marketWatchCount);
   for(int i = 0; i < marketWatchCount; i++){
   //printf(SymbolName(i,true));
   double ask=SymbolInfoDouble(SymbolName(i,true),SYMBOL_ASK); 
   }
   
   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++){
      ticks = CopyTicksRange(SymbolName(i,true),tick_array_day, COPY_TICKS_ALL, (startday*1000 + 24*60*60*day), ((startday + (day + 1)*24*60*60)*1000));
      
      OnTick(SymbolName(i,true), tick_array_day[0]);
   }
   }
   
//+------------------------------------------------------------------+

 void OnTick(string symbolName, MqlTick& last_tick) 
  { 
   if(SymbolInfoTick(symbolName,last_tick)) 
     { 
      Print(last_tick.time,": Bid = ",DoubleToString(last_tick.bid), 
            " Ask = ",DoubleToString(last_tick.last),"  Volume = ",DoubleToString(last_tick.volume)); 
     } 
   else Print("SymbolInfoTick() failed, error = ",GetLastError()); 
//--- 
  }  

this is the full code, 

Ok I will use CopyRate, but the problem with it I don't know if the code is ok for getting the last price of each day

nicholi shen
2155
nicholi shen  
bitamirshafiee:

this is the full code, 

Ok I will use CopyRate, but the problem with it I don't know if the code is ok for getting the last price of each day

It will give you the last bid price of each day. I'm not sure there's a value in knowing the exact tick which happened to be the very last tick of the day unless you are just doing it for fun.

bitamirshafiee
17
bitamirshafiee  

thanks nicholi for your help

I start working on CopyRates as you said, and yes it is a better choice for implementing this script, 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);
   }
   
   }
   
To add comments, please log in or register