Download MetaTrader 5
To add comments, please log in or register
All Code Base apps are available from MetaTrader 5. You can publish your own one!
strontiumDog
263
strontiumDog 2014.07.25 23:29 

OK, I've been on it now for ages, time to get a sanity check.

The code below is only the important parts of my EA, which operates fine, but in order to get a more automated way of calculating the best stoploss without trawling back through visual charts, I write open close and peak pull back for each trade.  You'll note that after a filewrite I fileflush and remembered to fileclose on deinit.  I have not tested this in forward, only in the mt4 back tester.  I'm checking the file in the tester\files folder and the file is created ok and the header is written in the file ok too.

I'm in debt to anyone who can suggest why my EA is not writing to the file in start() after it successful creates and writes to it in the init fn.  Cheers

//global

int filehandle;

string buySell;
string openDate;
string closeDate;
string peakDate;
double openPrice;
double closePrice;
double peakPrice;
string logFile;

// THIS FILEWRITE *DOES* WORK
//
int init() {
   logFile=StringConcatenate("log-", Symbol(), ".csv");
   filehandle=FileOpen(logFile,FILE_CSV|FILE_READ|FILE_WRITE,',');
   if(filehandle!=INVALID_HANDLE) {
      FileWrite(filehandle,"symbol","buysell","open price","open date","peak price","peak date","close price","close date");
      FileClose(filehandle);
      Print("FileOpen OK");
   }
   else Print("Operation FileOpen failed, error ",GetLastError());
}

int deinit()
{
    // when deleting the indicator delete all objects
    ObjectsDeleteAll();
    FileClose(filehandle);
    return(0);
}

int start() {
    // phase a: look for entry
    // phase b: open one trade and await tp/sl
    // phase c: watch for dirtyCondition reset
    static datetime btime;
    if(btime != Time[0]) {
        btime = Time[0];
        newBar = true;
        //Print("NEW BAR");
    } else {
        newBar = false;
    }

    // THIS FILEWRITE *DOES NOT* WORK
    if(newBar) {
FileWrite(filehandle, sym, "BAR");
    }

// peak price and date is updated

// THIS FOLLOWING SNIP IS WHERE I CHECK THE TRADE HAS CLOSED AND THEN GET THE CLOSE DATA AND WRITE THE LINE TO THE LOG.  THIS WRITE ALSO *DOES NOT* WORK
//
   int last_trade=HistoryTotal();
   if(last_trade > 0) {
        if(OrderSelect(last_trade-1,SELECT_BY_POS,MODE_HISTORY)==true) {
            closePrice = OrderClosePrice();
            closeDate  = TimeToStr(OrderCloseTime(),TIME_DATE|TIME_MINUTES);
        } else {
            Print("Trouble selecting previous order!!!!!!!!!");
        }
        if(newBar) {
            Print("peakPrice: ", peakPrice);
        }
        if(peakPrice && peakPrice != 0.0) {
            int bytes = FileWrite(filehandle, sym, buySell, openPrice, openDate, peakPrice, peakDate, closePrice, closeDate);
            if(bytes == 0) {
                int error=GetLastError();
                Print("ERROR CODE ",error," REPORTED!");
            }
            FileFlush(filehandle);

 // NOTE THE PRINT STATEMENT BELOW WRITES SUCCESSFULLY TO THE JOURNAL
//
            Print("Logged FileWrite to ", logFile, ": sym=", sym, " bs=",buySell," open=",openPrice," ",openDate," close=",closePrice," ",closeDate," peak=",peakPrice," ",peakDate);
            // now reset peakPrice as after we close I don't want duplicate entries in the log
            //
            buySell    = "";
            peakPrice  = 0.0;  
            openPrice  = 0.0;
            closePrice = 0.0;
            peakDate   = "";
            openDate   = "";
            closeDate  = "";
        }
   }

// INITIAL order opened based on conditions

            buySell   = "SELL";
            openPrice = Bid;
            peakPrice = Open[0];
            openDate  = TimeToStr(TimeCurrent(),TIME_DATE|TIME_MINUTES);
            peakDate  = openDate;
            Print("Set peakPrice to Open[0]: ", Open[0], " and openDate to ", openDate);

// and equivalent long code
whroeder1
13656
whroeder1 2014.07.26 00:38  
strontiumDog: OK, I've been on it now for ages, time to get a sanity check.
    // THIS FILEWRITE *DOES NOT* WORK
    if(newBar) {
FileWrite(filehandle, sym, "BAR");
  1. What is the value of filehandle? You closed it in init!
  2. Insane checked. Using invalid filehandle confirmed.
strontiumDog
263
strontiumDog 2014.07.26 08:06  

Good grief.  Thanks.

Now, the other good thing to come out of this is a timely reminder to all yo mql4 devs to remember to get some rest!

I saw Carpe Noctem somewhere, perhaps I shouldve ignored it.

/
To add comments, please log in or register