诚心请教

 
各位老师,知道了一根K线的时间,怎么获得这根K线布林带值?
 
mt5平台
 

在MT5中调用指标需要预先创建指标句柄,不能等到要用的时候再创建.

//+------------------------------------------------------------------+
//|                                                 test_GetBoll.mq5 |
//|                                           Copyright 2022,fxMeter |
//|                            https://www.mql5.com/en/users/fxmeter |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022,fxMeter"
#property link      "https://www.mql5.com/en/users/fxmeter"
#property version   "1.00"

int handle = -1;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
//---
   handle = iBands(Symbol(),PERIOD_CURRENT,10,0,2.0,PRICE_CLOSE);
   if(handle==-1)return(INIT_FAILED);
//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
//---
   double boll = 0.0;
   datetime t = iTime(Symbol(),PERIOD_CURRENT,0);
   if(!GetBoll(handle,t,UPPER_BAND,boll))//获取上轨数据失败
   {
      return;
   }
   if(boll>0.0)
   {
      Comment(boll);
   }
}
//获取布林线 buffer: 0 - BASE_LINE, 1 - UPPER_BAND, 2 - LOWER_BAND
bool GetBoll(int handlex,datetime t,int buffer,double &boll)
{
   int shift = iBarShift(Symbol(),PERIOD_CURRENT,t);
   if(shift==-1)return(false);
   double tmp[1];
   if(CopyBuffer(handlex,0,shift,buffer,tmp)!=1)return(false);
   boll = tmp[0];
   return(true);
}
//+------------------------------------------------------------------+
 
非常感谢您对解答。如果我想获得开仓那根K线对应的布林带上轨值,该怎么编写呢?
 
yuhuochongsheng #:
如果我想开仓那根K线对应的布林带上轨值,该怎么编写呢?

先找到单子的开仓时间, 把时间传入GetBoll(...)

 
Ziheng Zhuang #:

先找到单子的开仓时间, 把时间传入GetBoll(...)

好的,已解决。非常感谢。
原因: