问题描述
早上好,
我目前正在测试我的 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(),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;
}