CSV Signals how to read

 

Hi say for example I have file with these signals.

Open Date,Close Date,Symbol,Action,Lots,SL,TP,Open Price,Close Price,Commission,Swap,Pips,Profit,Magic Number

01/28/2014 15:00,01/28/2014 15:28,USDJPY,Buy,0.18,101.80900,103.65900,103.00900,103.07500,0.0000,0.0000,6.6,11.53,469222

01/28/2014 11:29,01/28/2014 15:00,GBPUSD,Buy,0.30,1.65334,1.66684,1.65932,1.65707,0.0000,0.0000,-22.5,-67.50,4698523

How do scan this file to find the current datetime and feed back the open price to me?

Also how can I scan this file with a magic number and feed back that lines variables?

Thanks in advance

 
  1. Open the file, read the values into variables. If it's the one you want do it, otherwise loop and read another line.
  2. learn to code it, or pay someone. We're not going to code it FOR you. We are willing to HELP you when you post your attempt (using SRC) and the nature of your problem.
 

I know how to code @ https://www.mql5.com/en/users/bonechair, the book just dont show me enough with csv and I want to do it properly not messy

This doesn't tell me how to put it into an array @ https://docs.mql4.com/files/fileopen ?? Wheres the else { Part which I can look at

  int handle;
  handle=FileOpen("my_data.csv",FILE_CSV|FILE_READ,';');
  if(handle<1)
    {
     Print("File my_data.dat not found, the last error is ", GetLastError());
     return(false);
    }
 
bonechair:

I know how to code . . .

You need to figure it out . . . it's just working with strings, ints and doubles . . . you can read all these easily enough with the standard File Functions, FileOpen() with FILE_CSV|FILE_READ then FileReadNumber() and FileReadString(). You might have some problems with the dates, I'm not sure if they will convert to a datetime or not, you will need to test, if not you will have to manipulate the string.

Read all the fields then work with them as you need to . . . then read the next line, process . . . repeat.
 

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

I found this useful but I dunno how that works. Im just looking to learn some examples.

void DownloadCal_prior_to_event()
{
  int file1 = FileOpen("\Html\Calendar.csv",FILE_READ|FILE_CSV,',');
  while (!FileIsEnding(file1))
    {
      string stDate1="";
      while (!FileIsEnding(file1) && stDate1=="")
        
      stDate1 = FileReadString(file1);       
      string stTime1 = FileReadString(file1);
      string stTimeZone1 = FileReadString(file1);
      string stCurrency1 = FileReadString(file1);
      string stDescription1 = FileReadString(file1);
      string stImportance1 = FileReadString(file1);
      string stActual1 = FileReadString(file1);
      string stForecast1 = FileReadString(file1);
      string stPrevious1 = FileReadString(file1);
      Alert(stDate1,stTime1,stTimeZone1,stCurrency1,stDescription1,stImportance1,stActual1,stForecast1,stPrevious1);

 FileClose(file1);  
}
 

How does that call the columns, you would normally do something like this with other programming languages.

  stDate1 = FileReadString(file1,1);       
  string stTime1 = FileReadString(file1,2);

Thanks Raptor, date I can use this

string date = TimeToStr(TimeCurrent(),TIME_DATE|TIME_MINUTES);
 
bonechair:

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

I found this useful but I dunno how that works. Im just looking to learn some examples.

How does that work that you dont have to do this where you would call the first column in csv and then second. I dont know about that.

Thanks Raptor, date I can use this


But aren't you looking to change a string to a datetime ? that string looks to be the wrong format to read as a datetime directly.
 
I think its ok because Im recreating csv myself but this can help I think if I need to https://docs.mql4.com/convert/StrToTime
 
bonechair:
I think its ok because Im recreating csv myself but this can help I think if I need to https://docs.mql4.com/convert/StrToTime


News events and market times on your chart you will see data uploading

in this way

Date,Time,Time Zone,Currency,Event,Importance,Actual,Forecast,Previous
Sun Jan 26,21:45,GMT,nzd,NZD Performance Services Index,Medium,57.5,,56.4
Sun Jan 26,23:50,GMT,jpy,JPY Adjusted Merchandise Trade Balance (Yen),Low,-Â¥1148.6B,-Â¥1335.1B,-Â¥1293.8B
Sun Jan 26,23:50,GMT,jpy,JPY Merchandise Trade Exports (YoY),Low,15.3,18.0,18.4
Sun Jan 26,23:50,GMT,jpy,JPY Merchandise Trade Imports (YoY),Low,24.7,26.2,21.1
Sun Jan 26,23:50,GMT,JPY,JPY Bank of Japan December 19-20 meeting minutes,Medium,,,
Sun Jan 26,23:50,GMT,jpy,JPY Merchandise Trade Balance Total (Yen),Medium,-Â¥1302.1B,-Â¥1239.9B,-Â¥1294.1B
Mon Jan 27,09:00,GMT,eur,EUR German IFO - Business Climate,Medium,110.6,110.0,109.5
Mon Jan 27,09:00,GMT,eur,EUR German IFO - Current Assessment,Medium,112.4,112.4,111.6
Mon Jan 27,09:00,GMT,eur,EUR German IFO - Expectations,Medium,108.9,108.0,107.4
Mon Jan 27,14:00,GMT,eur,EUR Euro-zone Finance Ministers Meet,Medium,,,
Mon Jan 27,15:00,GMT,usd,USD New Home Sales (MoM),Medium,-7.0%,-1.9%,-3.9%
Mon Jan 27,15:00,GMT,usd,USD New Home Sales,Low,414K,455K,445K
Mon Jan 27,15:30,GMT,usd,USD Dallas Fed Manufacturing Activity,Low,3.8,3.5,3.7
Mon Jan 27,18:00,GMT,eur,EUR ECB's Weidmann Speaks in Stuttgart,Medium,,,
 

Thanks I have coded a little something but backtesting with a csv that 500kb takes a long time, I was hoping its quicker.

Heres my coding but how do I speed this up with much bigger files, its slow on backtesting. Enjoy

csv data:

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


/**
  Variables
**/  

double glbOrderProfit;
double glbOrderOpen;
double glbOrderStop;
double glbOrderType;
double glbOrderTicket;
double mOpen, mClose, mStopLoss, mTakeProfit; 
double ask, bid, point;

int mNumber, spread, file;
string mSymbol, mType, opendate, closedate;

datetime date1, date2;  

double Lots = 5;

int init()
{
return(0);
}

int deinit()
{
return(0);
}

int start()
{

   file = FileOpen("signals.csv",FILE_READ|FILE_CSV,',');
   while (!FileIsEnding(file))
     {
  
       mNumber = StrToInteger(FileReadString(file));
       opendate = FileReadString(file);       
       closedate = FileReadString(file);
       mOpen = StrToDouble(FileReadString(file));    
       mClose = StrToDouble(FileReadString(file));       
       mTakeProfit = StrToDouble(FileReadString(file));
       mStopLoss = StrToDouble(FileReadString(file));
       mType = FileReadString(file);   
       mSymbol = 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 (TimeCurrent() == date1) 
       {
         if(mType == "Sell" && OrderFind(mNumber, mSymbol) == false)
         {   
           OrderSend(mSymbol, OP_SELL, Lots, bid, 3, mStopLoss, mTakeProfit, "Sell Order", mNumber, 0, Blue);
         }
         if(mType == "Buy" && OrderFind(mNumber, mSymbol) == false)
         {
           OrderSend(mSymbol, OP_BUY, Lots, ask, 3, mStopLoss, mTakeProfit, "Buy Order", mNumber, 0, Blue);
         }
       }
       
       else if (TimeCurrent() >= date2 && OrderFind(mNumber, mSymbol) == true) 
       {
           CloseOrder(glbOrderTicket);
       }
       
       else 
       {
         continue;
       }  
                
       Print("Magic=", mNumber, ",Symbol=", mSymbol, ",Type=", mType, ",Open=", mOpen, ",Close=", mClose, ",SL=", mStopLoss, ",TakeProfit=", mTakeProfit);   
               
      }
   
   FileClose(file);  
   return(0);
}
 

/**
  Close Order Function
**/  

void CloseOrder( int Ticket )
{

  OrderSelect(Ticket, SELECT_BY_TICKET);

  if (OrderType() == OP_BUY) {
    OrderClose(Ticket, OrderLots(), Bid, 0);     
  }  

  else {
    OrderClose(Ticket, OrderLots(), Ask, 0);      
  }  

 return;  

}   
 
/**
  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++)

     {

       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);

}
 

/**
  Scan History
**/  

bool InHistory(int Magic) 
{
  int i,hstTotal=OrdersHistoryTotal();
  for(i=0;i<hstTotal;i++)
    {
     
     if(OrderMagicNumber() == Magic) {
        return(TRUE);
        break;
     }
     
    }
}
 
 

Thanks guys I coded this EA for us.

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