MetaTrader Terminal 4:根据历史数据调试智能交易系统

MetaTrader Terminal 4: debugging an expert advisor on historical data

我有一个 MetaTrader Terminal 4 登录模拟账户。

我已经编写并编译了我的第一个 Expert Advisor。

我可以在实时图表上调试它,所以工作正常。我想根据历史数据对其进行调试。在 MetaEditor 中,Debug>Start on history data 是灰色的。

此外,当 运行 策略测试器我的断点从未被击中时,我将断点放在 OnTick() 函数的第一行,因此在我的理解中它应该被击中。

这是因为我有模拟账户吗?如果不是,我如何根据历史数据调试我的脚本?

更新下面的代码

#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;
  }

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

没有

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

在 MT4 (as-is-2020/06) 你仍然可以实现,如果需要的话,你自己的“调试”-Inspector/Monitor-agent 工具在 MQL4 编译 EA 的 运行 的 StrategyTester 模式期间执行。

MT4 的 StrategyTester 是一个 BackTesting 工具,不是(模拟的)实时市场模拟器和调试器,它只在合成(加速|减速)模式下运行 EA )-time,使用受控的合成-QUOTE-消息流,针对给定的交易工具和 TimeFrame 进行仿真。