MetaTrader5专家顾问可以使用当前蜡烛进行交易

问题描述

让我们说我想写一个EA以将SMA 21作为日线图上的基线进行交易,只要价格超过基线,我都想开仓。我可以轻松创建一个布尔条件并根据该条件建​​立头寸:

   bool buyCondition = (PriceInfo[1].close > BufferMA[1]) && (PriceInfo[2].close < BufferMA[2]);
   bool sellCondition = (PriceInfo[1].close < BufferMA[1]) && (PriceInfo[2].close > BufferMA[2]);

但是,据此,对于今天发生的交易,我要决定昨天和前一天发生的事情,但是我真正想要的是如果今天发生交叉交易,就进行交易,今天的问题是或当前的蜡烛,因为市场尚未关闭,SMA的价值尚未最终确定,并且会不断更新。可能有解决方法,因为我们的本地市场在9:00 AM开放,在12:30 PM(每天只有3个半小时!)关闭,如果我在市场关闭前5分钟读取SMA值,相当接近当前蜡烛的实际收盘SMA。为了做到这一点,我有一个简单的代码段,它也确保了每个蜡烛我只能交易一次:

bool NewBar() {
   
   int Minutes_to_Close = 5

   static datetime prevTime = 0;
   datetime currentTime = iTime(_Symbol,_Period,0) + (750 - Minutes_to_Close)*60;
   if (currentTime != prevTime) {
      prevTime = currentTime;
      return(true);
   }

iTime的注册时间为00:00:00,因此我向市场收盘价添加了时差,并将其作为布尔值传递给OnTick()函数,并且还更改了使用的基准条件当前的蜡烛值,仅将其与昨天的值进行比较:

void OnTick() {

   if(!NewBar()) return;

   ArraySetAsSeries(BufferMA,true);
   ArraySetAsSeries(PriceInfo,true);
   
   
   CopyBuffer(Handle_cma,sma_Index,3,BufferMA) ;
   CopyRates(_Symbol,PriceInfo);

   bool buyCondition = (PriceInfo[0].close > BufferMA[0]) && (PriceInfo[1].close < BufferMA[1]);
   bool sellCondition = (PriceInfo[0].close < BufferMA[0]) && (PriceInfo[1].close > BufferMA[1]);
   
   
   if (buyCondition) {
      OrderOpen(ORDER_TYPE_BUY);
   }
   
   else if (sellCondition) {
      OrderOpen(ORDER_TYPE_SELL);
   }
   return;
}

和我的OrderOpen()函数如下:

bool OrderOpen(ENUM_ORDER_TYPE orderType) {

   double price;
   double stopLossPrice;
   double takeProfitPrice;
   
   int volume = 10000;
   StopLoss = ATR_Multiplier * BufferATR[0];
   TakeProfit =  ATR_Multiplier * BufferATR[0]; 
   
   
   if (orderType == ORDER_TYPE_BUY) {
      price = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
      stopLossPrice = NormalizeDouble(price - StopLoss,_Digits);
      takeProfitPrice = NormalizeDouble(price + TakeProfit,_Digits);
   }
   else if (orderType == ORDER_TYPE_SELL){
      price = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
      stopLossPrice = NormalizeDouble(price + StopLoss,Digits());
      takeProfitPrice = NormalizeDouble(price - TakeProfit,_Digits);
   }
   else return(false);
   
   
   Trade.PositionOpen(_Symbol,orderType,volume,price,stopLossPrice,takeProfitPrice,InpTradeComment);
   return(true);

}

我在这里的方法有几个问题。

  1. 从逻辑角度看,比较当前的蜡烛价格作为交易信号是否明智?

  2. 我的NewBar()函数是否确保在12:25:00用当前蜡烛的SMA填充BufferMA [0]?

  3. 在OnThick()函数中,如果满足我的NewBar()条件,为什么在回测历史记录中,我的所有交易都在当天进行,但是在市场开盘时是9:00:00,我的交易不应该在12:25:00进行吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...