作者: RoboFx
Balance of Power (BOP) 指标以彩色直方图显示当前趋势的强度和方向,支持警报功能,包括邮件和手机推送通知。这个直方图的颜色根据超买和超卖水平的输入参数以及直方图的移动方向进行调整。
为了实现警报功能、邮件通知和推送通知,指标代码进行了以下修改:
- 新增输入参数:
//---- 警报的输入变量 input uint NumberofBar=1; //信号的柱数 input bool SoundON=true; //启用声音警报 input uint NumberofAlerts=2; //警报数量 input bool EMailON=false; //启用邮件信号 input bool PushON=false; //启用推送通知
- 在指标代码末尾添加了三个新功能:BuySignal()、SellSignal()和GetStringTimeframe()
//+------------------------------------------------------------------+ //| 买入信号函数 | //+------------------------------------------------------------------+ void BuySignal(string SignalSirname,// 邮件和推送消息的指标名称 double &ColorArray[],// 颜色索引缓冲区 int ColorIndex,// 生成信号的颜色索引 const int Rates_total, // 当前柱数 const int Prev_calculated, // 上一个Tick的柱数 const double &Close[], // 收盘价 const int &Spread[]) // 点差 { //--- static uint counter=0; if(Rates_total!=Prev_calculated) counter=0; bool BuySignal=false; bool SeriesTest=ArrayGetAsSeries(ColorArray); int index,index1; if(SeriesTest) { index=int(NumberofBar); index1=index+1; } else { index=Rates_total-int(NumberofBar)-1; index1=index-1; } if(ColorArray[index1]!=ColorIndex && ColorArray[index]==ColorIndex) BuySignal=true; if(BuySignal && counter<=NumberofAlerts) { counter++; MqlDateTime tm; TimeToStruct(TimeCurrent(),tm); string text=TimeToString(TimeCurrent(),TIME_DATE)+" "+string(tm.hour)+":"+string(tm.min); SeriesTest=ArrayGetAsSeries(Close); if(SeriesTest) index=int(NumberofBar); else index=Rates_total-int(NumberofBar)-1; double Ask=Close[index]; double Bid=Close[index]; SeriesTest=ArrayGetAsSeries(Spread); if(SeriesTest) index=int(NumberofBar); else index=Rates_total-int(NumberofBar)-1; Bid+=Spread[index]; string sAsk=DoubleToString(Ask,_Digits); string sBid=DoubleToString(Bid,_Digits); string sPeriod=GetStringTimeframe(ChartPeriod()); if(SoundON) Alert("BUY signal \n Ask=",Ask,"\n Bid=",Bid,"\n currtime=",text,"\n Symbol=",Symbol()," Period=",sPeriod); if(EMailON) SendMail(SignalSirname+": BUY signal alert","BUY signal at Ask="+sAsk+", Bid="+sBid+", Date="+text+" Symbol="+Symbol()+" Period="+sPeriod); if(PushON) SendNotification(SignalSirname+": BUY signal at Ask="+sAsk+", Bid="+sBid+", Date="+text+" Symbol="+Symbol()+" Period="+sPeriod); } //--- } //+------------------------------------------------------------------+ //| 卖出信号函数 | //+------------------------------------------------------------------+ void SellSignal(string SignalSirname, // 邮件和推送消息的指标名称 double &ColorArray[], // 颜色索引缓冲区 int ColorIndex, // 生成信号的颜色索引 const int Rates_total, // 当前柱数 const int Prev_calculated, // 上一个Tick的柱数 const double &Close[], // 收盘价 const int &Spread[]) // 点差 { //--- static uint counter=0; if(Rates_total!=Prev_calculated) counter=0; bool SellSignal=false; bool SeriesTest=ArrayGetAsSeries(ColorArray); int index,index1; if(SeriesTest) { index=int(NumberofBar); index1=index+1; } else { index=Rates_total-int(NumberofBar)-1; index1=index-1; } if(ColorArray[index1]!=ColorIndex && ColorArray[index]==ColorIndex) SellSignal=true; if(SellSignal && counter<=NumberofAlerts) { counter++; MqlDateTime tm; TimeToStruct(TimeCurrent(),tm); string text=TimeToString(TimeCurrent(),TIME_DATE)+" "+string(tm.hour)+":"+string(tm.min); SeriesTest=ArrayGetAsSeries(Close); if(SeriesTest) index=int(NumberofBar); else index=Rates_total-int(NumberofBar)-1; double Ask=Close[index]; double Bid=Close[index]; SeriesTest=ArrayGetAsSeries(Spread); if(SeriesTest) index=int(NumberofBar); else index=Rates_total-int(NumberofBar)-1; Bid+=Spread[index]; string sAsk=DoubleToString(Ask,_Digits); string sBid=DoubleToString(Bid,_Digits); string sPeriod=GetStringTimeframe(ChartPeriod()); if(SoundON) Alert("SELL signal \n Ask=",Ask,"\n Bid=",Bid,"\n currtime=",text,"\n Symbol=",Symbol()," Period=",sPeriod); if(EMailON) SendMail(SignalSirname+": SELL signal alert","SELL signal at Ask="+sAsk+", Bid="+sBid+", Date="+text+" Symbol="+Symbol()+" Period="+sPeriod); if(PushON) SendNotification(SignalSirname+": SELL signal at Ask="+sAsk+", Bid="+sBid+", Date="+text+" Symbol="+Symbol()+" Period="+sPeriod); } //--- } //+------------------------------------------------------------------+ //| 获取时间框架的字符串 | //+------------------------------------------------------------------+ string GetStringTimeframe(ENUM_TIMEFRAMES timeframe) { //---- return(StringSubstr(EnumToString(timeframe),7,-1)); //---- }
- 在OnCalculate()块的指标计算周期后添加BuySignal()和SellSignal()函数的调用:
//--- BuySignal("BalanceOfPower_Histogram_Alert",ColorIndBuffer,0,rates_total,prev_calculated,close,spread); SellSignal("BalanceOfPower_Histogram_Alert",ColorIndBuffer,7,rates_total,prev_calculated,close,spread); //---
其中 ColorIndBuffer 是存储蜡烛颜色索引的颜色索引缓冲区的名称。
假设在指标代码的 OnCalculate() 块中只会使用一次 BuySignal() 和 SellSignal() 函数的调用。
该指标使用了 SmoothAlgorithms.mqh 库的类(需复制到 <terminal_data_folder>\MQL5\Include)。类的使用已在文章中详细描述,详见 "不使用额外缓冲区进行中间计算的价格序列平均"。
此指标最初是用MQL5编写的,并于2013年2月7日首次发布在 代码库。

图1. BalanceOfPower_Histogram_Alert 指标在图表上的显示
图2. BalanceOfPower_Histogram_Alert 指标生成警报
