首页 > 解决方案 > MetaTrader 终端 4:根据历史数据调试智能交易系统

问题描述

我有一个 MetaTrader 终端 4 登录到一个模拟账户。

我编写并编译了我的第一个智能交易系统。

我可以在实时图表上调试它,所以工作正常。我想在历史数据上调试它。在 MetaEditor 中,历史数据上的调试>开始是灰色的。

此外,在运行策略测试器时,我的断点永远不会被击中,我将断点放在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;
  }

标签: mql4algorithmic-tradingmetatrader4mt4metatrader5

解决方案


“这是因为我有一个模拟账户吗?”

不。

“如果不是,我如何根据历史数据调试我的脚本?”

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

MT4StrategyTester是一种回溯测试工具,而不是(模拟的)实时市场模拟器和调试器,它仅在合成(加速|减速)时间内运行 EA,使用受控的合成QUOTE消息流),针对给定的交易工具和时间框架进行模拟。


推荐阅读