Metatrader: The Search for Day or Time Patterns

2019-06-16 09:23:10+00

Often trader ask themselves if there is a pattern in price development based on days of week or daytime.

The following article discusses how to identify or visualize price developments of forex trades.

Here is a first result of the visualization. In the horizonal you see the hours (0 to 23) whereas in the verticals you see the days. This chart includes 365 days.

Step to produce the above Chart

First lets write data from metatrader to a file.

Here is the Metatrader code for this program:


#property copyright "AVA"
//  3 Minutes Bars

datetime lastbar_timeopen;  // for isNewBar()
int          EXPERT_MAGIC=123456;   // EA Magic Number

string             InpFileName="dtasci3min.csv";   // file name
string             InpDirectoryName="Data"; // directory name
int file_handle = -1;
int i;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit() {
  ResetLastError();
  PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
  file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_WRITE|FILE_ANSI|FILE_CSV,";");
  
  if(file_handle!=INVALID_HANDLE) {      
    Print("File opened correctly");
  }
  else Print("Error in opening file,",GetLastError());
   
  string str; 
  str = "GBPCHF" ;
  FileWrite(file_handle,str);
  return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
   FileFlush(file_handle);
    FileClose(file_handle);
}

void OnTick() {
     
  if(!isNewBar(PERIOD_M3)) {  return; }
      
  MqlRates M3[];    // PriceinfoArray for Candles
  ArraySetAsSeries(M3,true);   // sort array downwards
  int M3RC=CopyRates(Symbol(),PERIOD_M3,0,2,M3);  // fill array with 2 Candles
  
  MqlDateTime dt;
  TimeToStruct(M3[1].time,dt);
    
  string str="";
  str =   (string) M3[1].time +",";  
  str += (string)dt.day_of_week+",";  
  str += DoubleToString(M3[1].open,5)+",";
  str += DoubleToString(M3[1].close,5);

  FileWrite(file_handle,str);
}

//+-------------------------------------------------------------+
//|  Return 'true' when a new bar appears                       |
//+-------------------------------------------------------------+
bool isNewBar(ENUM_TIMEFRAMES periode) {
   bool print_log=false;
   static datetime bartime=0; // store open time of the current bar
   //--- get open time of the zero bar
   datetime currbar_time=iTime(_Symbol,periode,0);
   //--- if open time changes, a new bar has arrived
   if(bartime!=currbar_time) {
      bartime=currbar_time;
      lastbar_timeopen=bartime;
      //--- display data on open time of a new bar in the log      
      if(print_log && !(MQLInfoInteger(MQL_OPTIMIZATION) || MQLInfoInteger(MQL_TESTER))) {
         //--- display a message with a new bar open time
         PrintFormat("%s: new bar on %s %s opened at %s",__FUNCTION__,_Symbol,
      //               StringSubstr(EnumToString(_Period),7),
                     TimeToString(TimeCurrent(),TIME_SECONDS));
         //--- get data on the last tick
         MqlTick last_tick;
         if(!SymbolInfoTick(Symbol(),last_tick))
            Print("SymbolInfoTick() failed, error = ",GetLastError());
         //--- display the last tick time up to milliseconds
         PrintFormat("Last tick was at %s.%03d",
                     TimeToString(last_tick.time,TIME_SECONDS),last_tick.time_msc%1000);
        }
      return (true);
     }
   return (false);
}

Next we use a PHP script to produce an image.

<?php
if (($handle = fopen("Downloads/dtasci3min.csv","r")) === FALSE) exit;

$img = imagecreatetruecolor(600,600);  //width/height
$color = imagecolorallocate($img, 255, 255, 0);  //  yellow as background

$prevdata[1] = 'x';
$y = 1;

while (($data = fgetcsv($handle)) !== FALSE) {
  if ($prevdata[1] != $data[1]) {
          // new day starts
          $y++;  // increment day
          $x=0;
  }
  $data[2] = $data[2] * 100000;  // we translate to integer
  $data[3] = $data[3] * 100000;
  $diff = $data[2] - $data[3];
  if     ($diff > 0)   $color = imagecolorallocate($img,255,51,51);    // red
  elseif ($diff < 0)   $color = imagecolorallocate($img,51,255,51);    // green
  else                 $color = imagecolorallocate($img,102,255,255);  // blue

  imagesetpixel ( $img ,  $x ,  $y ,  $color ) ;
  $x++;
  $prevdata = $data;
}
fclose($handle);

imagepng($img,'datsci3min-chfgbp' . gmdate("YmdHis") . ".png");
imagedestroy($img);
?>

Conclusion

The only pattern to be identified is that during the night there are smaller movement in prices than at business hours.

Tag: metatrader