Requesting help: Reading dynamic CSV file into EA

 

Hello, 

I am in the process of developing an Excel spreadsheet that analyzes 17 currency pairs and outputs "buy" or "sell" signals. The signals are based on multiple conventional indicators (MACD, RSI, CCI, MA's, BANDS etc) that I have hard coded into the spreadsheet and use the Solver Add-in for parameter optimization. The main sheet is the Summary sheet in which each currency pair occupies a single row and a CSV file is generated whenever a signal (1 for buy, -1 for sell) occurs. The CSV file is populated dynamically as the signals occur and will therefore only contain data on the pertinent currency pair(s) at any given time, or else blank when no signals have been generated. The CSV file contains 7 variables (columns), namely, Symbol, Command, Price, Slippage, Stoploss, Takeprofit and Magic Number that should be passed into the OrderSend() function.

This is as far as I have come. I am currently stuck at trying to read the CSV file and executing the trades using an EA. In the process of my research I came across an EA (link below) that I modified to suit my needs. However for reasons unknown to me, I cannot get the EA to read each row independently whenever a signal occurs but will execute trades when I manually populate all the rows with signal values. I also want the EA to indicate the open and close time and price of each trade using vertical lines so that I can monitor the accuracy of the signals but sadly the EA does not run the ObjectCreate() function as intended.  

#property strict

/**
  Variables
**/ 
// External
extern double Lots      = 0.01;//Lots size
// Internal
double glbOrderProfit;
double glbOrderOpen;
double glbOrderStop;
double glbOrderType;
double glbOrderTicket;
double mOpen, mClose, mStopLoss, mTakeProfit; 
double ask, bid, point;

int mNumber, mSlippage, file;
int    Ticket = 0;
string mSymbol, mType;


//opendate, closedate;
//datetime date1, date2;  

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
   {
//---Set symbol digits
   if((Digits==5)||(Digits==3))
      point=Point*10;
   else
      point=Point;
//---
   if(Lots==0)
     {
      MessageBox("Specify lots");
      return(INIT_FAILED);
     }
   else
      Print("Trading allowed");
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {								//<----- function for reading the csv file test.csv
file = FileOpen("test.csv",FILE_READ|FILE_CSV,',');
   while (!FileIsEnding(file))
     {
  
       
      // mClose = StrToDouble(FileReadString(file));    
           
       mSymbol = FileReadString(file);
       mType = FileReadString(file); 
       mOpen = StrToDouble(FileReadString(file));   
       mSlippage = StrToInteger(FileReadString(file));  
       mStopLoss = StrToDouble(FileReadString(file));
       mTakeProfit = StrToDouble(FileReadString(file));   
       mNumber = StrToInteger(FileReadString(file));   

//     date1=StrToTime(opendate);
//     date2=StrToTime(closedate);

       ask     = MarketInfo(mSymbol,MODE_ASK);
       bid     = MarketInfo(mSymbol,MODE_BID);   
//     spread  = MarketInfo(mSymbol,MODE_SPREAD);   
//     point   = MarketInfo(mSymbol,MODE_POINT);

       if (file>0)
//       if (TimeCurrent() == date1) 
       {
         if(mType == "SELL" && OrderFind(mNumber, mSymbol) == false)
         {   
       Ticket =   OrderSend(mSymbol, OP_SELL, Lots, bid, mSlippage, mStopLoss, mTakeProfit, "Sell", mNumber, 0, clrRed);
       if(Ticket<1)
     {
      Print("Order send failed, OrderType : ", OP_SELL,", errcode : ",GetLastError());
       return;
   }  
   else
      Print(mSymbol,  mType, "executed successfully @: ", mOpen);
         
         
         if(mType == "BUY" && OrderFind(mNumber, mSymbol) == false)
         {
       Ticket =    OrderSend(mSymbol, OP_BUY, Lots, ask, mSlippage, mStopLoss, mTakeProfit, "Buy", mNumber, 0, clrGreen);
       if(Ticket<1)
     {
      Print("Order send failed, OrderType : ",OP_BUY,", errcode : ",GetLastError());
      return;
      }
       else
      Print(mSymbol,  mType, "executed successfully @: ",mOpen);
         
       }
   }    
       else 
       {
         continue;
       }  
}
              
  }     
         
       
      
   
   FileClose(file);  
   return;
}
 							//<----- function for drawing lines on chart on entry						
void drawVerticalLine(int barsBack) {
   
   string lineName = "Line"+string(MathRand());
   
	
   if (mType == "BUY") {
      ObjectCreate(lineName,OBJ_VLINE,0,Time[barsBack],0);
      ObjectSet(lineName,OBJPROP_COLOR, clrBlue);
      ObjectSet(lineName,OBJPROP_WIDTH,1);
      ObjectSet(lineName,OBJPROP_STYLE,STYLE_DOT);
   }
   
   if (mType == "SELL") {
      ObjectCreate(lineName,OBJ_VLINE,0,Time[barsBack],0);
      ObjectSet(lineName,OBJPROP_COLOR, clrRed);
      ObjectSet(lineName,OBJPROP_WIDTH,1);
      ObjectSet(lineName,OBJPROP_STYLE,STYLE_DOT);
   
   }
   //if (bid >= mTakeProfit && mType == "BUY") {
   //   ObjectCreate(lineName,OBJ_VLINE,0,Time[barsBack],0);
   //   ObjectSet(lineName,OBJPROP_COLOR, clrBlue);
   //   ObjectSet(lineName,OBJPROP_WIDTH,1);
   //   ObjectSet(lineName,OBJPROP_STYLE,STYLE_DOT);
   //}
   //if (bid <= mTakeProfit && mType == "SELL") {
   //   ObjectCreate(lineName,OBJ_VLINE,0,Time[barsBack],0);
   //   ObjectSet(lineName,OBJPROP_COLOR, clrRed);
   //   ObjectSet(lineName,OBJPROP_WIDTH,1);
   //   ObjectSet(lineName,OBJPROP_STYLE,STYLE_DOT);
   //}
   //if (mStopLoss == bid && mType == "BUY") {
   //   ObjectCreate(lineName,OBJ_VLINE,0,Time[barsBack],0);
   //   ObjectSet(lineName,OBJPROP_COLOR, clrBlue);
   //   ObjectSet(lineName,OBJPROP_WIDTH,1);
   //   ObjectSet(lineName,OBJPROP_STYLE,STYLE_DASH);
   //}
   //if (mStopLoss == bid && mType == "SELL") {
   //   ObjectCreate(lineName,OBJ_VLINE,0,Time[barsBack],0);
   //   ObjectSet(lineName,OBJPROP_COLOR, clrRed);
   //   ObjectSet(lineName,OBJPROP_WIDTH,1);
   //   ObjectSet(lineName,OBJPROP_STYLE,STYLE_DASH);
   //}   
}
 
/**
  Order Find Function
**/  

bool OrderFind(int Magic, string symbol) 
{

   glbOrderType = -1;
   glbOrderTicket = -1;
   glbOrderProfit = 0;
   glbOrderOpen = -1;
   glbOrderStop = -1;

   int total = OrdersTotal();

   bool res = false;

   for(int cnt = 0 ; cnt < total ; cnt++)

     {

bool ordrfnd =       OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

       if(OrderMagicNumber() == Magic && OrderSymbol() == symbol)

         {

           glbOrderType = OrderType();
           glbOrderTicket = OrderTicket();
           glbOrderProfit = OrderProfit();
           glbOrderOpen = OrderOpenPrice();
           glbOrderStop = OrderStopLoss();

           res = true;

         }

     }

 return(res);

}
 


 
 


My questions are:

1. How can I modify the code so that the EA reads each row in the CSV file independently? By this I mean that if for instance there is a buy signal in row 5 and rows 1 to 4 are empty, the EA will still read the values in row 5.

2. Is it also possible to modify the code so that the EA draws vertical lines when a trade is opened and closed? Alternatively, how can I implement OBJ_TREND in the code to mark open and close time/price using a trend line?


I will appreciate any guidance you can offer since I am totally stymied at this point despite it being the final leg of the project. Please note that I am fairly new to MQL4 programming so please be gentle.


Attached are:

1. Link to original EA that I modified

2. Image of Summary sheet

3. Image of CSV file layout







https://www.mql5.com/en/code/11108

CSV signals to Expert
CSV signals to Expert
  • www.mql5.com
How to load csv signals into an expert advisor. Here is some sample csv data: Magic, opendate, closedate,openprice,closeprice,stoplevel,profitlevel,type,symbol 1,2014-01-23 14:43:00,2014-01-23 15:43:00,1.36440,0.00000,0.00000,0.00000,Buy,EURUSD 2,2014-01-23 14:43:00,2014-01-23 16:43:00,1.36440,0.00000,0.00000,0.00000,Buy,EURUSD Important...
Files:
Reason: