【问题标题】:MetaTrader Terminal 4: debugging an expert advisor on historical dataMetaTrader 终端 4:根据历史数据调试智能交易系统
【发布时间】:2020-06-19 12:00:14
【问题描述】:

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

我编写并编译了我的第一个 EA 交易。

我可以在实时图表上调试它,所以工作正常。我想在历史数据上调试它。在 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;
  }

【问题讨论】:

  • 我们在哪里看到您所说的 MQL4 代码并要求我们帮助您?
  • @user3666197 我已经用大部分代码更新了帖子。抱歉,我认为不需要关于调试的一般性问题

标签: mql4 algorithmic-trading metatrader4 mt4 metatrader5


【解决方案1】:

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

没有。

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

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

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

【讨论】:

  • 感谢您的回答。我在哪里可以找到“调试”-inspector/monitor 代理工具?
  • :o) 通常用户自己设计一个这样的并自己编译。
  • 哦,我明白了。认为这可能超出了我的技能水平:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多