新手有偿求教!请大佬帮忙看看怎么回事

 

代码如下

很简单的基于WPR超买超卖的一段代码,代码不报错,但EA回测时无法按照代码规则开单,而是每次价格变动就开单,实在不知道哪里出了问题。

若有能指点的大佬,红包感谢!


//+------------------------------------------------------------------+
//|                                                     ProjectName   |
//|                                      Copyright 2023, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright "2023, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

input int WPR21Period = 21;
input int WPR42Period = 42;

// 全局变量
int MAGIC = 100;
double Slippage = 3;  // 滑点
double LotSize = 0.02;  // 下单量
double StopLossPoints = 30;  // 止损点数
double TakeProfitPoints = 60;  // 止盈点数

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }

//+------------------------------------------------------------------+
//| 主交易函数                                                        |
//+------------------------------------------------------------------+
bool isLongOpen = false;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   
   double wpr21 = iWPR(Symbol(), PERIOD_M5, WPR21Period);
   double wpr42 = iWPR(Symbol(), PERIOD_M5, WPR42Period);

// 开多函数
   if(!isLongOpen && (wpr21 >= -100 && wpr21 <= -80) && (wpr42 >= -100 && wpr42 <= -80))
     {
      OpenLong();
     }

// 平多函数
   if(isLongOpen && (wpr21 >= -50 && wpr42 >= -50))
     {
      CloseLong();
     }

// 如果持有多单且两条周期线回到了-80以下,也平多
   if(isLongOpen && (wpr21 <= -80 && wpr42 <= -80))
     {
      CloseLong();
     }
  }

//+------------------------------------------------------------------+
//| 辅助函数                                                          |
//+------------------------------------------------------------------+
bool CanOpenLong()
  {
   return !isLongOpen;  // 如果未持有多单,则可以开多
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool CanCloseLong()
  {
   return isLongOpen;  // 如果持有多单,则可以平多
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OpenLong()
  {
   MqlTradeRequest request = {};
   MqlTradeResult result = {};
   request.action = TRADE_ACTION_DEAL;
   request.symbol = Symbol();
   request.volume = LotSize;
   request.type = ORDER_TYPE_BUY;
   request.price = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
   request.deviation = Slippage;

   int ticket = OrderSend(request, result);
   if(ticket > 0)
     {
      Print("Opened long position: Ticket =", ticket);
      isLongOpen = true;
     }
   else
     {
      Print("Error opening long position: Error =", GetLastError());
     }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CloseLong()
  {
   MqlTradeRequest request = {};
   MqlTradeResult result = {};
   request.action = TRADE_ACTION_DEAL;
   request.symbol = Symbol();
   request.volume = LotSize;
   request.type = ORDER_TYPE_SELL;
   request.price = SymbolInfoDouble(Symbol(), SYMBOL_BID);
   request.deviation = Slippage;

   int ticket = OrderSend(request, result);
   if(ticket > 0)
     {
      Print("Closed long position: Ticket =", ticket);
      isLongOpen = false;
     }
   else
     {
      Print("Error closing long position: Error =", GetLastError());
     }
  }
//+------------------------------------------------------------------+
 

你這個代碼MT5用法不對 MT4函數使用也不對

MT5不會抱錯 但是應該不會開單

MT4應該會抱錯 不能執行

 
在没有弄懂弄明白MT5背后的规范和运行机理之前,想要上手写EA难度是有点大的。