Search code examples
algorithmic-tradingmql4forex

Blank Results while backtesting


I am new to the world of MQL4-code.

I come from a C++ background and I am trying to learn MQL4 language & conventions.

I am writing a simple Expert Advisor (my first ever).
It compiles but, when I am trying to test it, it ends with no results. I attach code to better illustrate what I am trying to do:

//+------------------------------------------------------------------+
//|                                                MyFirstExpert.mq4 |
//|                                                  Leonardo        |
//|                            http://investinmarkets.altervista.org |
//+------------------------------------------------------------------+
#property copyright "Leonardo "
#property link      "http://investinmarkets.altervista.org"
#property version   "1.00"
#property strict
input int BarCount = 3;
      int Ticket   = 0;
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick() { 
     int    BarCountTemp = BarCount + 1;
     double bars[];
     ArrayResize( bars, BarCountTemp );
     for ( int i = 0; i < BarCountTemp; i++ ) {
           bars[i] = Close[i + 1];
     }
     int   i = 0;
     bool  is_p;
     do
     {
        if ( bars[i] > bars[i+1] && i < BarCountTemp ) is_p = true;
        else is_p = false;
        i++;
     }
     while ( is_p );

     if ( is_p == true && Ticket == 0 ) {
          Ticket = OrderSend(_Symbol,OP_SELL,0.1,Bid,0,0,0,"Sell Order Custom",110);
          Alert("Sell order opened to match found.");
          Comment("Sell order opened #"+Ticket+".");
     }
     if ( Ticket != 0 ) {
          bool select = OrderSelect(Ticket,SELECT_BY_TICKET);
          if ( Close[1] > Close[2] ) {
               bool close = OrderClose(Ticket,OrderLots(),Ask,0,clrGreen);
               Alert("Sell order closed.");
               Comment("Sell order closed #"+Ticket+".");
               Ticket = 0;
          }
     }
  }
//+------------------------------------------------------------------+

I want to simply count bars (input by user) and then perform a check: if e.g. 3 bars are all positive then open a sell order (just this case for the moment). If opened, the next bar check if still positive, if not close the trade.

I am getting always blank results.

Thank you in advance!


Solution

  • Welcome to the MQL4-world, Leonardo

    let's review the syntax:

    for ( int i = 0; i < BarCountTemp; i++ ) {
          bars[i] = Close[i + 1];
         }
         int   i = 0;
         bool  is_p;
         do
         {
            if ( bars[i] > bars[i+1] && i < BarCountTemp ) is_p = true;
            else is_p = false;
            i++;
         }
         while ( is_p );
    

    could be merged / simplified into a single loop/break construct:

    bool is_p = True;                          // FYI: FALSE if not initialised
                                               // WARNING: "New"-MQL4 has changed variable visibility-scope to be limited just to the innermost syntax-construct and variables easily "cease" exist outside that syntax-construct boundary ... for(){bool is_p ...visible...} ...invisible...
    for ( int  i = 0;                          // .SET
               i < BarCountTemp;               // .TEST: [**]
               i++ ) {                         // .INC
               if (  Close[i+1] > Close[i+2]   // avoid TimeSeries' replica(s)
               // && i          < BarCountTemp // ALWAYS TRUE [^**]
                  ) continue; // ---------------------------- LOOP-^
               else {
                    is_p = False;
                    break;    // ---------------------------- EXIT-v
    }
    

    Next: got at least one Comment() remark on top of the chart window?

    int   Ticket =  EMPTY;                        // Rather initialise as = EMPTY;
    
    if (  is_p   == True
       && Ticket == EMPTY                         // un-ambiguous meaning
       ) {
          Ticket =  OrderSend( _Symbol,           // .SYM
                               OP_SELL,           // .OP
                               0.1,               // .LOTs check sizing, MarketInfo()
                               Bid,               // .PRICE
                               0,                 // .SLIPPAGE
                               0,                 // .SL
                               0,                 // .TP
                              "Sell Order Custom",// .COMMENT
                               110                // .MAGNUM
                               );
          if (  Ticket == EMPTY ){                // EXC. HANDLER
                      ...
          }
          else {
                Alert(   "Sell order opened to match found." );     // .NOP if isTesting()
                Comment( "Sell order opened #" + Ticket + "." );    // .GUI is visible????
          }
    }
    

    Finally: include Exception handlers for cases, where error may appear

     if (  Ticket   != EMPTY               // TEST 1st,
        && Close[1] >  Close[2]            // TEST 2nd, prevent dbPool-ops, if not True
        ) {
                 bool select = OrderSelect( Ticket, SELECT_BY_TICKET );
                 if (!select ){           // EXC. HANDLER
                      ...
                 }
                 bool close  = OrderClose(  Ticket,
                                            OrderLots(),
                                            Ask,
                                            0,
                                            clrGreen
                                            );
                 if (!close ){           // EXC. HANDLER
                      ...
                 }
                 Alert(   "Sell order closed." );
                 Comment( "Sell order closed #" + Ticket + "." );
                 Ticket = EMPTY;                            // .SET EMPTY
           }
     }