JBrainTrend1Stop 是一款功能强大的趋势指标,支持警报、邮件及推送通知。
为了实现这些功能,我们对指标代码进行了如下修改:
- 引入了新的输入参数:
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 &BuyArrow[], // 向上趋势的信号缓冲区
double &SellArrow[], // 向下趋势的信号缓冲区
const int Rates_total, // 当前柱数
const int Prev_calculated, // 上一刻的柱数
const double &Close[], // 收盘价
const int &Spread[]) // 点差
{
//---
static uint counter=0;
if(Rates_total!=Prev_calculated) counter=0;
bool BuySignal=false;
bool SeriesTest=ArrayGetAsSeries(BuyArrow);
int index,index1;
if(SeriesTest)
{
index=int(NumberofBar);
index1=index+1;
}
else
{
index=Rates_total-int(NumberofBar)-1;
index1=index-1;
}
if(SellArrow[index1] && SellArrow[index1]!=EMPTY_VALUE && BuyArrow[index] && BuyArrow[index]!=EMPTY_VALUE) 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+=_Point*Spread[index];
string sAsk=DoubleToString(Ask,_Digits);
string sBid=DoubleToString(Bid,_Digits);
string sPeriod=GetStringTimeframe(ChartPeriod());
if(SoundON) Alert("买入信号 Ask=",Ask," Bid=",Bid," 当前时间=",text," Symbol=",Symbol()," Period=",sPeriod);
if(EMailON) SendMail(SignalSirname+": 买入信号警报","买入信号在 Ask="+sAsk+", Bid="+sBid+", 日期="+text+" Symbol="+Symbol()+" Period="+sPeriod);
if(PushON) SendNotification(SignalSirname+": 买入信号在 Ask="+sAsk+", Bid="+sBid+", 日期="+text+" Symbol="+Symbol()+" Period="+sPeriod);
}
//---
}
//+------------------------------------------------------------------+
//| 卖出信号函数 |
//+------------------------------------------------------------------+
void SellSignal(string SignalSirname, // 邮件和推送消息中的指标名称文本
double &SellArrow[], // 向下趋势的信号缓冲区
double &BuyArrow[], // 向上趋势的信号缓冲区
const int Rates_total, // 当前柱数
const int Prev_calculated, // 上一刻的柱数
const double &Close[], // 收盘价
const int &Spread[]) // 点差
{
//---
static uint counter=0;
if(Rates_total!=Prev_calculated) counter=0;
bool SellSignal=false;
bool SeriesTest=ArrayGetAsSeries(SellArrow);
int index,index1;
if(SeriesTest)
{
index=int(NumberofBar);
index1=index+1;
}
else
{
index=Rates_total-int(NumberofBar)-1;
index1=index-1;
}
if(BuyArrow[index1] && BuyArrow[index1]!=EMPTY_VALUE && SellArrow[index] && SellArrow[index]!=EMPTY_VALUE) 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+=_Point*Spread[index];
string sAsk=DoubleToString(Ask,_Digits);
string sBid=DoubleToString(Bid,_Digits);
string sPeriod=GetStringTimeframe(ChartPeriod());
if(SoundON) Alert("卖出信号 Ask=",Ask," Bid=",Bid," 当前时间=",text," Symbol=",Symbol()," Period=",sPeriod);
if(EMailON) SendMail(SignalSirname+": 卖出信号警报","卖出信号在 Ask="+sAsk+", Bid="+sBid+", 日期="+text+" Symbol="+Symbol()+" Period="+sPeriod);
if(PushON) SendNotification(SignalSirname+": 卖出信号在 Ask="+sAsk+", Bid="+sBid+", 日期="+text+" Symbol="+Symbol()+" Period="+sPeriod);
}
//---
}
//+------------------------------------------------------------------+
//| 获取时间框架的字符串 |
//+------------------------------------------------------------------+
string GetStringTimeframe(ENUM_TIMEFRAMES timeframe)
{
//----
return(StringSubstr(EnumToString(timeframe),7,-1));
//----
} - 在 OnCalculate() 块的指标计算周期后添加了 BuySignal() 和 SellSignal() 函数的调用:
//---
BuySignal("JBrainTrend1Stop_Alert",BuyStopBuffer,SellStopBuffer,rates_total,prev_calculated,close,spread);
SellSignal("JBrainTrend1Stop_Alert",SellStopBuffer,BuyStopBuffer,rates_total,prev_calculated,close,spread);
//---
其中 BuyStopBuffer 和 SellStopBuffer 是用于存储上涨和下跌趋势信号的指标缓冲区(多头和空头的止损线)。如果没有相应的趋势,指标缓冲区应显示零值或 EMPTY_VALUE。
假设在指标代码的 OnCalculate() 块中仅使用一次 BuySignal() 和 SellSignal() 函数的调用。
将 JMA.mq5 指标的编译文件放置到 MQL5\Indicators 目录中。

图1. JBrainTrend1Stop_Alert指示器在图表上

图2. JBrainTrend1Stop_Alert指示器. 生成警报
评论 0