学习了,不错
谢谢楼主分享
{:1_186:}
谢谢
顶下
{:1_179:}
{:1_179:}
支持下
{:1_189:}
//+------------------------------------------------------------------+
//| PriceAlert.mq4 |
//| Copyright 2023, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 0
// 全局变量
datetime lastAlertTime;
bool alertTriggered = false;
//+------------------------------------------------------------------+
//| 自定义指标初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
lastAlertTime = Time;
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 自定义指标迭代函数 |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
// 仅在新K线或价格变动时检查
if(Time != lastAlertTime || !alertTriggered)
{
// 获取前两根K线的最高价和最低价
double prevHigh1 = iHigh(Symbol(), Period(), 1);
double prevHigh2 = iHigh(Symbol(), Period(), 2);
double prevLow1= iLow(Symbol(), Period(), 1);
double prevLow2= iLow(Symbol(), Period(), 2);
// 计算上下轨
double upperBand = MathMax(prevHigh1, prevHigh2) * 1.001;
double lowerBand = MathMin(prevLow1, prevLow2) * 0.999;
// 获取当前价格(使用最新报价)
double currentPrice = SymbolInfoDouble(Symbol(), SYMBOL_BID);
// 检查突破条件
if(currentPrice > upperBand)
{
TriggerAlert("UP", upperBand, currentPrice);
alertTriggered = true;
lastAlertTime = Time;
}
else if(currentPrice < lowerBand)
{
TriggerAlert("DOWN", lowerBand, currentPrice);
alertTriggered = true;
lastAlertTime = Time;
}
else
{
alertTriggered = false;
}
}
return(rates_total);
}
//+------------------------------------------------------------------+
//| 触发报警函数 |
//+------------------------------------------------------------------+
void TriggerAlert(string direction, double bandPrice, double currentPrice)
{
string message = StringFormat("%s 突破警报! %s 当前价: %.5f, 触发价: %.5f",
Symbol(),
(direction == "UP") ? "向上" : "向下",
currentPrice,
bandPrice);
// 弹出窗口报警
Alert(message);
// 播放声音报警(需将sound.wav放入MT4的sounds目录)
PlaySound("alert.wav");
// 发送通知到手机/邮箱(需在MT4设置中启用通知)
SendNotification(message);
}
//+------------------------------------------------------------------+