Warm tip: This article is reproduced from serverfault.com, please click

mql4-当 EA 应该阻止这种情况时,偶尔会打开交易

(mql4 - Sporadically opens trades when EA should be preventing this)

发布于 2021-03-01 09:13:12

大家早,

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

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

但是,在某些情况下(尽管不是所有情况),当我通过当前烛台柱关闭交易方方式时,它会偶尔开启另一笔交易,从而违反“仅在烛台柱线打开时间开启交易”规则。

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

笔记:

  • 最好的方法是在 1M 图表上进行测试,这样你就不必再等待确认 EA 工作了。
  • EA 只允许开启一笔交易,如果在重新初始化 EA 时有任何交易开启,则不会开启新交易 - 这是为了避免过度交易而设计的。

建议/思考点

  • EA 的初始化速度可能不够快以符合 oninit参数,因此无法识别另一笔交易初始化之前的条件。

//+------------------------------------------------------------------+
//|                                          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];    
       
   }
  }
//+------------------------------------------------------------------+

一切顺利,

Questioner
Johannes Schulz-Bauer
Viewed
0
PaulB 2021-03-03 22:59:47

你的代码并不是你想要实现的真正最佳实践。仅在柱形开始时执行操作的最佳方法如下:

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