大家早,
我目前正在测试我的 EA 的一部分,它应该只在烛台开盘时开仓交易(如果满足其他条件),在 MQL4 语言中是LastActiontime=Time[0];
.
它工作得非常好:它只LastActiontime=Time[0];
按时开仓交易,如果 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];
}
}
//+------------------------------------------------------------------+
一切顺利,
你的代码并不是你想要实现的真正最佳实践。仅在柱形开始时执行操作的最佳方法如下:
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;
}
谢谢你,非常感谢。