Download MetaTrader 5

Help with EA to do logging

To add comments, please log in or register
raviragas
32
raviragas  

Hi Guys

I wrote an EA to just do logging of signals into a .csv file for the 1M timeframe.

I tried running it by attaching the EA to a live chart, but the code doesn't seem to update the .csv file.

My "my_data.csv" file is located in <C:\MetaTrader - Alpari UK\tester\files>

I would really appreciate if anyone can tell what is wrong.

Thanks!

//+------------------------------------------------------------------+
//|                                               Moving Average.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#define MAGICMA  20110715

extern double Lots               = 0.1;
extern double MaximumRisk        = 0.01;
extern double DecreaseFactor     = 3;
extern double MovingPeriod       = 12;
extern double MovingShift        = 6;

extern double Fast_EMA_Period    = 10;
extern double Slow_EMA_Period    = 20;

extern double TPpip   = 100;
extern double SLpip   = 23;

extern double TP2pip   = 7;
extern double SL2pip   = 25;

double second_order_placed = 0;
int order_ticket = 0, current_order_ticket = 0; 
  
  
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
   double fast_ema_1, slow_ema_1, fast_ema_2, slow_ema_2, fast_ema_3, slow_ema_3, ema_diff;
   double ema_diff3, ema_diff2, ema_diff1;
   double MacdCurrent, SignalCurrent;
   double adx_1, adx_2, adx_3, adx_rising;
   double rsi_1, rsi_2, rsi_3;
   int    res;
   string order_type;
   
   double pips2dbl, pips2point, minDistance,
       slPoints, tpPoints,
       itotal,
       sl, tp;
       
   int crossover_up, crossover_down;
   bool order_send;
          
//---- go trading only for first ticks of new bar
   //if(Volume[0]>1) return;
   
   static datetime Time0; 
   
   if (Time0 == Time[0]) return; 
   
   Time0 = Time[0];

//---- Time Info 

//---- Opening File for logging
  int handle;
  
  handle=FileOpen("my_data.csv",FILE_CSV|FILE_READ|FILE_WRITE,';');
  if(handle<1)
    {
     Print("File my_data.csv not found, the last error is ", GetLastError());
     return(false);
    }
//----  

//---- get Moving Average 
   fast_ema_1=iMA(NULL,0,Fast_EMA_Period,0,MODE_EMA,PRICE_CLOSE,1);
   slow_ema_1=iMA(NULL,0,Slow_EMA_Period,0,MODE_EMA,PRICE_CLOSE,1);   
   fast_ema_2=iMA(NULL,0,Fast_EMA_Period,0,MODE_EMA,PRICE_CLOSE,2);
   slow_ema_2=iMA(NULL,0,Slow_EMA_Period,0,MODE_EMA,PRICE_CLOSE,2); 
   fast_ema_3=iMA(NULL,0,Fast_EMA_Period,0,MODE_EMA,PRICE_CLOSE,3);
   slow_ema_3=iMA(NULL,0,Slow_EMA_Period,0,MODE_EMA,PRICE_CLOSE,3); 

   ema_diff3 = (fast_ema_3 - slow_ema_3)*1000;     
   ema_diff2 = (fast_ema_2 - slow_ema_2)*1000;  
   ema_diff1 = (fast_ema_1 - slow_ema_1)*1000;  
                
//---- get MACD values   
   MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
   SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);  
   
//---- get ADX values

   adx_1 = iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,1);   
   adx_2 = iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,2);  
   adx_3 = iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,3); 
   
   rsi_1 = iRSI(NULL,0,14,PRICE_CLOSE,1);
   rsi_2 = iRSI(NULL,0,14,PRICE_CLOSE,2);
   rsi_3 = iRSI(NULL,0,14,PRICE_CLOSE,3);
   
   if((adx_1 >= adx_2) && (adx_2 >= adx_3) && (adx_1 > 30))
       adx_rising = 1;
   else
       adx_rising = 0;
       
   minDistance=MarketInfo(Symbol(),MODE_STOPLEVEL)*Point;
   
   if (Digits == 5 || Digits == 3)    // Adjust for five (5) digit brokers.
   {            
      pips2dbl = Point*10; pips2point = 10;
   } 
   else 
   {    
      pips2dbl = Point;   pips2point = 1;
   }    
   
   slPoints=SLpip*pips2dbl;
   tpPoints=TPpip*pips2dbl;
   order_send = false;
   
//-- Buy

   if((fast_ema_1 > slow_ema_1) && ((fast_ema_2 <= slow_ema_2) ||(fast_ema_3 <= slow_ema_3)))
   {
         crossover_up = 1;
         ema_diff = (fast_ema_1 - slow_ema_1)*1000;
   }
      else
         crossover_up = 0;

  

//-- Sell

   if((fast_ema_1 < slow_ema_1) && ((fast_ema_2 >= slow_ema_2) ||(fast_ema_3 >= slow_ema_3)))
   {
      crossover_down = 1;
      ema_diff = (slow_ema_1 - fast_ema_1)*1000;
   }   
   else
      crossover_down = 0;
   
    
   FileSeek(handle, 0, SEEK_END);   

   FileWrite(handle, TimeToStr(Time0), "NONE", fast_ema_1, slow_ema_1, fast_ema_2, slow_ema_2, fast_ema_3, slow_ema_3,
                                              MacdCurrent, SignalCurrent, Close[1], Open[1], High[1], Low[1],
                                          adx_3, adx_2, adx_1, rsi_3, rsi_2, rsi_1, ema_diff3, ema_diff2, ema_diff1, crossover_up, crossover_down);     

   FileClose(handle); 

}


//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()
  {
//---- check for history and trading
   //if(Bars<100 || IsTradeAllowed()==false) return;
    
//---- calculate open orders by current symbol

   CheckForOpen();



//----
  }
//+------------------------------------------------------------------+
Ubzen
5394
Ubzen  
raviragas
32
raviragas  
//+------------------------------------------------------------------+
//|                                               Moving Average.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#define MAGICMA  20110715

extern double Lots               = 0.1;
extern double MaximumRisk        = 0.01;
extern double DecreaseFactor     = 3;
extern double MovingPeriod       = 12;
extern double MovingShift        = 6;

extern double Fast_EMA_Period    = 10;
extern double Slow_EMA_Period    = 20;

extern double TPpip   = 100;
extern double SLpip   = 23;

extern double TP2pip   = 7;
extern double SL2pip   = 25;

double second_order_placed = 0;
int order_ticket = 0, current_order_ticket = 0; 
  
  
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
   double fast_ema_1, slow_ema_1, fast_ema_2, slow_ema_2, fast_ema_3, slow_ema_3, ema_diff;
   double ema_diff3, ema_diff2, ema_diff1;
   double MacdCurrent, SignalCurrent;
   double adx_1, adx_2, adx_3, adx_rising;
   double rsi_1, rsi_2, rsi_3;
   int    res;
   string order_type;
   
   double pips2dbl, pips2point, minDistance,
       slPoints, tpPoints,
       itotal,
       sl, tp;
       
   int crossover_up, crossover_down;
   bool order_send;
          
//---- go trading only for first ticks of new bar
   //if(Volume[0]>1) return;
   
   static datetime Time0; 
   
   if (Time0 == Time[0]) return; 
   
   Time0 = Time[0];

//---- Time Info 

//---- Opening File for logging
  int handle;
  
  handle=FileOpen("my_data.csv",FILE_CSV|FILE_READ|FILE_WRITE,';');
  if(handle<1)
    {
     Print("File my_data.csv not found, the last error is ", GetLastError());
     return(false);
    }
//----  

//---- get Moving Average 
   fast_ema_1=iMA(NULL,0,Fast_EMA_Period,0,MODE_EMA,PRICE_CLOSE,1);
   slow_ema_1=iMA(NULL,0,Slow_EMA_Period,0,MODE_EMA,PRICE_CLOSE,1);   
   fast_ema_2=iMA(NULL,0,Fast_EMA_Period,0,MODE_EMA,PRICE_CLOSE,2);
   slow_ema_2=iMA(NULL,0,Slow_EMA_Period,0,MODE_EMA,PRICE_CLOSE,2); 
   fast_ema_3=iMA(NULL,0,Fast_EMA_Period,0,MODE_EMA,PRICE_CLOSE,3);
   slow_ema_3=iMA(NULL,0,Slow_EMA_Period,0,MODE_EMA,PRICE_CLOSE,3); 

   ema_diff3 = (fast_ema_3 - slow_ema_3)*1000;     
   ema_diff2 = (fast_ema_2 - slow_ema_2)*1000;  
   ema_diff1 = (fast_ema_1 - slow_ema_1)*1000;  
                
//---- get MACD values   
   MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
   SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);  
   
//---- get ADX values

   adx_1 = iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,1);   
   adx_2 = iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,2);  
   adx_3 = iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,3); 
   
   rsi_1 = iRSI(NULL,0,14,PRICE_CLOSE,1);
   rsi_2 = iRSI(NULL,0,14,PRICE_CLOSE,2);
   rsi_3 = iRSI(NULL,0,14,PRICE_CLOSE,3);
   
   if((adx_1 >= adx_2) && (adx_2 >= adx_3) && (adx_1 > 30))
       adx_rising = 1;
   else
       adx_rising = 0;
       
   minDistance=MarketInfo(Symbol(),MODE_STOPLEVEL)*Point;
   
   if (Digits == 5 || Digits == 3)    // Adjust for five (5) digit brokers.
   {            
      pips2dbl = Point*10; pips2point = 10;
   } 
   else 
   {    
      pips2dbl = Point;   pips2point = 1;
   }    
   
   slPoints=SLpip*pips2dbl;
   tpPoints=TPpip*pips2dbl;
   order_send = false;
   
//-- Buy

   if((fast_ema_1 > slow_ema_1) && ((fast_ema_2 <= slow_ema_2) ||(fast_ema_3 <= slow_ema_3)))
   {
         crossover_up = 1;
         ema_diff = (fast_ema_1 - slow_ema_1)*1000;
   }
      else
         crossover_up = 0;

  

//-- Sell

   if((fast_ema_1 < slow_ema_1) && ((fast_ema_2 >= slow_ema_2) ||(fast_ema_3 >= slow_ema_3)))
   {
      crossover_down = 1;
      ema_diff = (slow_ema_1 - fast_ema_1)*1000;
   }   
   else
      crossover_down = 0;
   
    
   FileSeek(handle, 0, SEEK_END);   

   FileWrite(handle, TimeToStr(Time0), "NONE", fast_ema_1, slow_ema_1, fast_ema_2, slow_ema_2, fast_ema_3, slow_ema_3,
                                              MacdCurrent, SignalCurrent, Close[1], Open[1], High[1], Low[1],
                                          adx_3, adx_2, adx_1, rsi_3, rsi_2, rsi_1, ema_diff3, ema_diff2, ema_diff1, crossover_up, crossover_down);     

   FileClose(handle); 

}


//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()
  {
//---- check for history and trading
   //if(Bars<100 || IsTradeAllowed()==false) return;
    
//---- calculate open orders by current symbol

   CheckForOpen();



//----
  }
//+------------------------------------------------------------------+
qjol
3243
qjol  

raviragas:

[...]My "my_data.csv" file is located in <C:\MetaTrader - Alpari UK\tester\files>[...]


who told u that

read again

int FileOpen( string filename, int mode, int delimiter=';')

Opens file for input and/or output. Returns a file handle for the opened file or -1 (if the function fails). To get the detailed error information, call GetLastError() function.

Notes: Files can only be opened in the terminal_directory\experts\files folder (terminal_directory\tester\files if for expert testing) or in its subfolders.
FILE_BIN and FILE_CSV modes cannot be used simultaneously.
If FILE_WRITE does not combine with FILE_READ, a zero-length file will be opened. If even the file containd some data, they will be deleted. If there is a need to add data to an existing file, it must be opened using combination of FILE_READ | FILE_WRITE.
If FILE_READ does not combine with FILE_WRITE, the file will be opened only if it already exists. If the file does not exist, it can be created using the FILE_WRITE mode.
No more than 32 files can be opened within an executable module simultaneously. Handles of files opened in the same module cannot be passed to other modules (libraries).
raviragas
32
raviragas  

Hi qjol

Thanks! I guess i was looking in the wrong place...

Great!

To add comments, please log in or register