在 EA 应该阻止这种情况时偶尔打开交易

Sporadically opens trades when EA should be preventing this

大家早上好,

我目前正在测试我的 EA 的一部分,它应该只在蜡烛条开盘时开仓(前提是满足其他条件),在 MQL4 语言中是 LastActiontime=Time[0];

它工作得非常好:它只在 LastActiontime=Time[0]; 时间开仓,如果 EA 需要重新初始化,它不会在烛台条的中途开仓任何交易。

然而,在某些情况下(虽然不是每次),当我通过当前烛台条关闭交易方时,它偶尔会打开另一笔交易,从而违背了“仅在烛台开盘时间 " 规则。

我有下面的代码片段。有谁知道我哪里出错了?

备注:

建议/思考点

//+------------------------------------------------------------------+
//|                                          initialization_test.mq4 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

datetime LastActiontime;
bool totalOrders = OrdersTotal();
double currencyConversion;

int OnInit()
  {
//---

  LastActiontime=Time[0];
  
  if (totalOrders == 0){
      totalOrders = true;
  }
   

//---
   return(INIT_SUCCEEDED);
  }

void OnTick()
  {
//---
   int LotSize = 30;
   int RewardFactor = 3;
   int stopLossPoints = 200;
   double entryPriceBid = MarketInfo(Symbol(),MODE_BID);
   double spread = MarketInfo(Symbol(), MODE_SPREAD);
   double tickvalue = MarketInfo(Symbol(), MODE_TICKVALUE);
   color sellcolor = clrGreen;
   bool Newbar = true;
   
   if(LastActiontime!=Time[0])
   if(OrdersTotal() == 0)
   if(totalOrders == true){
   
      bool OpenShort = OrderSend(Symbol(),OP_SELL,LotSize,MarketInfo(Symbol(),MODE_BID),100,((entryPriceBid/Point)+(stopLossPoints))*Point,((entryPriceBid/Point)-(stopLossPoints*RewardFactor))*Point,"Spread Charge £"+DoubleToStr((spread * tickvalue)*LotSize,2),Period(),0,sellcolor);
LastActiontime=Time[0];    
       
   }
  }
//+------------------------------------------------------------------+

祝一切顺利,

您的代码并不是您要实现的目标的最佳实践。仅在柱开始时执行操作的最佳方法如下:

void OnTick()
{
    if(TimeBar==Time[0])
    {
        //Carry out any operations on each tick here
    return;
    }

   if(TimeBar==0)
   {
       // Operations carried out on First Run only here
       TimeBar=Time[0];
   return;
   }

   // Operations at the start of a new bar here

   TimeBar=Time[0];
return;
}