// 定义指标参数和变量
extern int RSI_period = 14;
extern int RSI_buy_threshold = 30;
extern int RSI_sell_threshold = 70;
int last_alert = 0;
//+------------------------------------------------------------------+
//| expert初始化函数 |
//+------------------------------------------------------------------+
int init()
{
// 指示EA需要使用的技术指标
IndicatorAdd(0, IND_RSI, RSI_period);
IndicatorSetInteger(0, IND_RSI, MODE_APPLIED_PRICE, PRICE_CLOSE); // 应用价格
return(0);
}
//+------------------------------------------------------------------+
//| expert开始函数 |
//+------------------------------------------------------------------+
int start()
{
// 获取技术指标值
double rsi_value = iRSI(Symbol(), PERIOD_CURRENT, RSI_period, PRICE_CLOSE, 0);
double prev_rsi_value = iRSI(Symbol(), PERIOD_CURRENT, RSI_period, PRICE_CLOSE, 1);
// 检查是否穿越买入或卖出阈值
if(rsi_value > RSI_sell_threshold && prev_rsi_value <= RSI_sell_threshold)
{
PlaySound("alert.wav"); // 发出声音警报
Comment("Sell Alert"); // 在图表上显示警报
ObjectCreate("sell_arrow", OBJ_ARROW, 0, Time[0], High[0] + 5 * Point); // 创建箭头警报
ObjectSet("sell_arrow", OBJPROP_ARROWCODE, 2); // 设置向下箭头
last_alert = -1; // 记录最后一个警报的类型
}
else if(rsi_value < RSI_buy_threshold && prev_rsi_value >= RSI_buy_threshold)
{
PlaySound("alert.wav"); // 发出声音警报
Comment("Buy Alert"); // 在图表上显示警报
ObjectCreate("buy_arrow", OBJ_ARROW, 0, Time[0], Low[0] - 5 * Point); // 创建箭头警报
ObjectSet("buy_arrow", OBJPROP_ARROWCODE, 3); // 设置向上箭头
last_alert = 1; // 记录最后一个警报的类型
}
// 检查上一个警报是否已过期
if(last_alert != 0)
{
int max_bars_back = 100; // 检查向后的最大价格栏数
bool is_expired = true;
for(int i = 1; i <= max_bars_back; i++)
{
if(last_alert == -1 && High[i] >= ObjectGet("sell_arrow", OBJPROP_PRICE1) - 2*Point)
{
is_expired = false; // 卖出警报仍在图表上
break;
}
else if(last_alert == 1 && Low[i] <= ObjectGet("buy_arrow", OBJPROP_PRICE1) + 2*Point)
{
is_expired = false; // 买入警报仍在图表上
break;
}
}
if(is_expired)
{
// 移除过期的箭头和注释
if(last_alert == -1)
{
ObjectDelete("sell_arrow");
}
else
{
ObjectDelete("buy_arrow");
}
Comment("");
last_alert = 0;
}
}
return(0);
} |