mql4algorithmic-tradingmetatrader4mt4metatrader5

MetaTrader Terminal 4: debugging an expert advisor on historical data


I have a MetaTrader Terminal 4 logged into a demo account.

I have written and compiled my first Expert Advisor.

I am able to debug it on a live chart, so working fine. I would like to debug it though on historical data. In the MetaEditor the Debug>Start on history data is grey out though.

Also when running the strategy tester my break points never get hit, I put the break point on the first line in the OnTick() function, so it should get hit in my understanding.

Is this because I have a demo account? If not how can I debug my script on historical data?

update code below

#property strict


//+------------------------------------------------------------------+
//| Includes and object initialization                               |
//+------------------------------------------------------------------+

#include <book_examples\Trade.mqh>
CTrade Trade;
CCount Count;

#include <book_examples\Timer.mqh>
CTimer Timer;                             
CNewBar NewBar;   // keeps track of timestamp of current bar & allows us to 
determine when a new bar has opened so prevents orders being opened intrabar

#include <book_examples\TrailingStop.mqh>
#include <book_examples\MoneyManagement.mqh>
#include <book_examples\Indicators.mqh>


//+------------------------------------------------------------------+
//| Input variables                                                  |
//+------------------------------------------------------------------+

// left out to keep the post short

//+------------------------------------------------------------------+
//| Enum                                                             |
//+------------------------------------------------------------------+

enum trade_action
{
    NO_ACTION = 0,
    LONG = 1,
    SHORT = 2,
    EXIT_TRADE = 3
 };

 //+------------------------------------------------------------------+
 //| Global variable and indicators                                   |
 //+------------------------------------------------------------------+

 int gBuyTicket, gSellTicket;

 //+------------------------------------------------------------------+
 //| Expert initialization function                                   |
 //+------------------------------------------------------------------+

 int OnInit()
 {
    // Set magic number
    Trade.SetMagicNumber(101);
    Trade.SetSlippage(10);

    return(INIT_SUCCEEDED);
  }


//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+

void OnTick()
{
     
   // Check for bar open 
   bool newBar = true;        // I put a breakpoint on this line
   int barShift = 0;

   if(TradeOnBarOpen == true)
   {
       newBar = NewBar.CheckNewBar(_Symbol,_Period);
       barShift = 1;               
   }

   // Trading
   if(newBar == true)
   {
       // Money management
       double lotSize = FixedLotSize;
      if(UseMoneyManagement == true)
      {
          lotSize = MoneyManagement(_Symbol,FixedLotSize,RiskPercent,StopLoss); 
      }
  
      trade_action action = calculate_signal();
  
      // Open buy order
      if(action == LONG)
      {
         // close sell orders
         Trade.CloseAllSellOrders();
     
         // check if we already have an open buy trade
         int open_buy = Count.Buy();
     
         if(open_buy == 0)
         {
            // no current existing buy position so enter
            gBuyTicket = Trade.OpenBuyOrder(_Symbol,lotSize);
            ModifyStopsByPoints(gBuyTicket,StopLoss,TakeProfit);
          }
                       
       }
  
       // Open sell order
      else if(action == SHORT)
      {
          Trade.CloseAllBuyOrders();
     
         // check if we already have an open sell trade
         int open_sell = Count.Sell();
     
         if(open_sell == 0)
         {
            // no current existing sell position so enter
            gSellTicket = Trade.OpenSellOrder(_Symbol,lotSize);
            ModifyStopsByPoints(gSellTicket,StopLoss,TakeProfit);
         }
     
       }
   } 

}


trade_action calculate_signal()
{
   trade_action action = NO_ACTION;   
  
    // now calculate trade logic
  
     if (some_value > some_threshold)
     {
        action = LONG;
     }
     else if (some_value < some_threshold)
     {
        // enter short position
        action = SHORT;
     }
  
     return action;
  }

Solution

  • Q : "Is this because I have a demo account?"

    No.

    Q : "If not how can I debug my script on historical data?"

    In the MT4 (as-is-2020/06) you still can implement, if in a need to have such, your own "debugging"-Inspector/Monitor-agent tool to get executed during the StrategyTester-mode of the run of the MQL4 compiled-EA.

    The MT4's StrategyTester is a BackTesting tool, not an (emulated)live-market Simulator-and-Debugger, it only operates the EA in synthetic(accelerated|slowed-down)-time, using a controlled-flow of synthetic-QUOTE-message(s), emulated for a given trading instrument and TimeFrame.