在指标代码结尾处添加三个新函数: BuySignal(), SellSignal() 和 GetStringTimeframe()
void BuySignal(string SignalSirname,
double &ColorArray[],
int ColorIndex,
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(ColorArray);
int index,index1;
if(SeriesTest)
{
index=int(NumberofBar);
index1=index+1;
}
else
{
index=Rates_total-int(NumberofBar)-1;
index1=index-1;
}
if(SignMode==MODE_SIGNAL)
{
if(ColorArray[index1]!=ColorIndex && ColorArray[index]==ColorIndex) BuySignal=true;
}
else
{
if(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+=_Point*Spread[index];
string sAsk=DoubleToString(Ask,_Digits);
string sBid=DoubleToString(Bid,_Digits);
string sPeriod=GetStringTimeframe(ChartPeriod());
if(SignMode==MODE_SIGNAL || (SignMode==MODE_TREND && ColorArray[index1]!=ColorIndex))
{
if(SoundON) Alert("买入信号 \n 采购价=",采购价,"\n 供给价=",Bid,"\n 当前时间=",text,"\n 品种=",Symbol()," 周期=",sPeriod);
if(EMailON) SendMail(SignalSirname+": 买入信号警报","买入信号采购价="+sAsk+", 供给价="+sBid+", 日期="+text+" 品种="+Symbol()+" 周期="+sPeriod);
if(PushON) SendNotification(SignalSirname+": 买入信号采购价="+sAsk+", 供给价="+sBid+", 日期="+text+" 品种="+Symbol()+" 周期="+sPeriod);
}
else
{
if(SoundON) Alert("上行趋势信号 \n 采购价=",Ask,"\n 供给价=",Bid,"\n 当前时间=",text,"\n 品种=",Symbol()," 周期=",sPeriod);
if(EMailON) SendMail(SignalSirname+": 上行趋势信号警报","买入信号采购价="+sAsk+", 供给价="+sBid+", 日期="+text+" 品种="+Symbol()+" 周期="+sPeriod);
if(PushON) SendNotification(SignalSirname+": 上行趋势信号采购价="+sAsk+", 供给价="+sBid+", 日期="+text+" 品种="+Symbol()+" 周期="+sPeriod);
}
}
}
void SellSignal(string SignalSirname,
double &ColorArray[],
int ColorIndex,
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(ColorArray);
int index,index1;
if(SeriesTest)
{
index=int(NumberofBar);
index1=index+1;
}
else
{
index=Rates_total-int(NumberofBar)-1;
index1=index-1;
}
if(SignMode==MODE_SIGNAL)
{
if(ColorArray[index1]!=ColorIndex && ColorArray[index]==ColorIndex) SellSignal=true;
}
else
{
if(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+=_Point*Spread[index];
string sAsk=DoubleToString(Ask,_Digits);
string sBid=DoubleToString(Bid,_Digits);
string sPeriod=GetStringTimeframe(ChartPeriod());
if(SignMode==MODE_SIGNAL || (SignMode==MODE_TREND && ColorArray[index1]!=ColorIndex))
{
if(SoundON) Alert("卖出信号 \n 采购价=",Ask,"\n 供给价=",Bid,"\n 当前时间=",text,"\n 品种=",Symbol()," 周期=",sPeriod);
if(EMailON) SendMail(SignalSirname+": 卖出信号警报","卖出信号采购价="+sAsk+", 供给价="+sBid+", 日期="+text+" 品种="+Symbol()+" 周期="+sPeriod);
if(PushON) SendNotification(SignalSirname+": 卖出信号采购价="+sAsk+", 供给价="+sBid+", 日期="+text+" 品种="+Symbol()+" 周期="+sPeriod);
}
else
{
if(SoundON) Alert("下行趋势信号 \n 采购价=",Ask,"\n 供给价=",Bid,"\n 当前时间=",text,"\n 品种=",Symbol()," 周期=",sPeriod);
if(EMailON) SendMail(SignalSirname+": 下行趋势信号警报","卖出信号采购价="+sAsk+", 供给价="+sBid+", 日期="+text+" 品种="+Symbol()+" 周期="+sPeriod);
if(PushON) SendNotification(SignalSirname+": 下行趋势采购价="+sAsk+", 供给价="+sBid+", 日期="+text+" 品种="+Symbol()+" 周期="+sPeriod);
}
}
}
string GetStringTimeframe(ENUM_TIMEFRAMES timeframe)
{
return(StringSubstr(EnumToString(timeframe),7,-1));
}