//+------------------------------------------------------------------+
//| AdvancedSignals |
//| 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 14
#property indicator_plots 11
//--- 输入参数
input int MA1_Period = 5; // 快速均线周期
input int MA2_Period = 20; // 中期均线周期
input int MA3_Period = 60; // 慢速均线周期
input int RSI_Period = 14; // RSI周期
input int BB_Period = 20; // 布林带周期
input double BB_Deviation = 2.0; // 布林带标准差
input int MACD_Fast = 12; // MACD快线
input int MACD_Slow = 26; // MACD慢线
input int MACD_Signal = 9; // MACD信号线
input int Box_Lookback = 50; // 箱体回溯周期
input bool Draw_Trendline = true;// 绘制趋势线
input bool Draw_Fibonacci = true;// 绘制黄金分割
//--- 指标缓冲区
double MA1Buffer[], MA2Buffer[], MA3Buffer[];
double RSIBuffer[], UpperBB[], LowerBB[], MiddleBB[];
double MACDBuffer[], SignalBuffer[], HistogramBuffer[];
double BuySignal[], SellSignal[];
//--- 全局变量
double boxHigh, boxLow;
datetime lastAlertTime;
int trendStart, trendEnd;
//+------------------------------------------------------------------+
//| 自定义指标初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
//--- 设置指标缓冲区
SetIndexBuffer(0, MA1Buffer);
SetIndexBuffer(1, MA2Buffer);
SetIndexBuffer(2, MA3Buffer);
SetIndexBuffer(3, RSIBuffer);
SetIndexBuffer(4, UpperBB);
SetIndexBuffer(5, LowerBB);
SetIndexBuffer(6, MiddleBB);
SetIndexBuffer(7, MACDBuffer);
SetIndexBuffer(8, SignalBuffer);
SetIndexBuffer(9, HistogramBuffer);
SetIndexBuffer(10, BuySignal);
SetIndexBuffer(11, SellSignal);
//--- 设置指标样式
SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 1, clrBlue);
SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 1, clrOrange);
SetIndexStyle(2, DRAW_LINE, STYLE_SOLID, 1, clrRed);
SetIndexStyle(3, DRAW_LINE, STYLE_DOT, 1, clrGold);
SetIndexStyle(4, DRAW_LINE, STYLE_DOT, 1, clrGreen);
SetIndexStyle(5, DRAW_LINE, STYLE_DOT, 1, clrGreen);
SetIndexStyle(6, DRAW_LINE, STYLE_SOLID, 1, clrGreen);
SetIndexStyle(7, DRAW_LINE, STYLE_SOLID, 1, clrBlue);
SetIndexStyle(8, DRAW_LINE, STYLE_SOLID, 1, clrRed);
SetIndexStyle(9, DRAW_HISTOGRAM, STYLE_SOLID, 1, clrSilver);
SetIndexStyle(10, DRAW_ARROW, 0, 1, clrYellow);
SetIndexStyle(11, DRAW_ARROW, 0, 1, clrGreen);
//--- 设置箭头代码
SetIndexArrow(10, 233); // 黄色向上箭头
SetIndexArrow(11, 234); // 绿色向下箭头
//--- 设置指标标签
IndicatorSetString(INDICATOR_SHORTNAME, "AdvancedSignals");
IndicatorSetInteger(INDICATOR_DIGITS, _Digits);
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[])
{
int limit = rates_total - prev_calculated;
if(prev_calculated > 0) limit++;
//--- 计算移动平均线
for(int i = limit - 1; i >= 0; i--)
{
MA1Buffer = iMA(NULL, 0, MA1_Period, 0, MODE_SMA, PRICE_CLOSE, i);
MA2Buffer = iMA(NULL, 0, MA2_Period, 0, MODE_SMA, PRICE_CLOSE, i);
MA3Buffer = iMA(NULL, 0, MA3_Period, 0, MODE_SMA, PRICE_CLOSE, i);
}
//--- 计算RSI
for(int i = limit - 1; i >= 0; i--)
{
RSIBuffer = iRSI(NULL, 0, RSI_Period, PRICE_CLOSE, i);
}
//--- 计算布林带
for(int i = limit - 1; i >= 0; i--)
{
MiddleBB = iMA(NULL, 0, BB_Period, 0, MODE_SMA, PRICE_CLOSE, i);
double deviation = iStdDev(NULL, 0, BB_Period, 0, MODE_SMA, PRICE_CLOSE, i);
UpperBB = MiddleBB + BB_Deviation * deviation;
LowerBB = MiddleBB - BB_Deviation * deviation;
}
//--- 计算MACD
for(int i = limit - 1; i >= 0; i--)
{
MACDBuffer = iMACD(NULL, 0, MACD_Fast, MACD_Slow, MACD_Signal, PRICE_CLOSE, MODE_MAIN, i);
SignalBuffer = iMACD(NULL, 0, MACD_Fast, MACD_Slow, MACD_Signal, PRICE_CLOSE, MODE_SIGNAL, i);
HistogramBuffer = MACDBuffer - SignalBuffer;
}
//--- 计算箱体
CalculateBox(Box_Lookback);
//--- 绘制趋势线
if(Draw_Trendline) DrawTrendLines();
//--- 绘制黄金分割
if(Draw_Fibonacci) DrawFibonacci();
//--- 交易信号判断
for(int i = limit - 1; i >= 1; i--) // 从第1根开始避免数组越界
{
BuySignal = EMPTY_VALUE;
SellSignal = EMPTY_VALUE;
// 组合条件示例:金叉+RSI超卖+价格突破布林下轨
bool buyCondition =
(MA1Buffer > MA2Buffer && MA1Buffer[i-1] <= MA2Buffer[i-1]) &&
(RSIBuffer < 30) &&
(close < LowerBB && close[i-1] >= LowerBB[i-1]);
// 组合条件示例:死叉+RSI超买+价格突破布林上轨
bool sellCondition =
(MA1Buffer < MA2Buffer && MA1Buffer[i-1] >= MA2Buffer[i-1]) &&
(RSIBuffer > 70) &&
(close > UpperBB && close[i-1] <= UpperBB[i-1]);
if(buyCondition)
{
BuySignal = low - 10 * _Point;
if(SellSignal[i-1] != EMPTY_VALUE || BuySignal[i-1] == EMPTY_VALUE)
TriggerAlert("BUY", i, time);
}
else if(sellCondition)
{
SellSignal = high + 10 * _Point;
if(BuySignal[i-1] != EMPTY_VALUE || SellSignal[i-1] == EMPTY_VALUE)
TriggerAlert("SELL", i, time);
}
}
return(rates_total);
}
//+------------------------------------------------------------------+
//| 计算箱体范围 |
//+------------------------------------------------------------------+
void CalculateBox(int lookback)
{
int highest = iHighest(NULL, 0, MODE_HIGH, lookback, 1);
int lowest = iLowest(NULL, 0, MODE_LOW, lookback, 1);
boxHigh = High[highest];
boxLow = Low[lowest];
// 绘制箱体
DrawHorizontalLine("BoxUpper", boxHigh, clrDodgerBlue);
DrawHorizontalLine("BoxLower", boxLow, clrDodgerBlue);
}
//+------------------------------------------------------------------+
//| 绘制趋势线 |
//+------------------------------------------------------------------+
void DrawTrendLines()
{
// 简化示例:连接最近两个低点
int low1 = iLowest(NULL, 0, MODE_LOW, 20, 1);
int low2 = iLowest(NULL, 0, MODE_LOW, 20, low1 + 5);
if(low1 != -1 && low2 != -1 && low1 != low2)
{
trendStart = MathMin(low1, low2);
trendEnd = MathMax(low1, low2);
DrawTrendLine("TrendLine", Time[trendStart], Low[trendStart], Time[trendEnd], Low[trendEnd], clrDeepPink);
}
}
//+------------------------------------------------------------------+
//| 绘制黄金分割线 |
//+------------------------------------------------------------------+
void DrawFibonacci()
{
int highest = iHighest(NULL, 0, MODE_HIGH, 50, 0);
int lowest = iLowest(NULL, 0, MODE_LOW, 50, 0);
if(highest != -1 && lowest != -1)
{
double fibLevels[] = {0.0, 0.236, 0.382, 0.5, 0.618, 1.0};
double priceRange = High[highest] - Low[lowest];
for(int i = 0; i < ArraySize(fibLevels); i++)
{
double levelPrice = Low[lowest] + priceRange * fibLevels;
string name = "FibLevel_" + DoubleToString(fibLevels, 3);
DrawHorizontalLine(name, levelPrice, clrGoldenrod);
}
}
}
//+------------------------------------------------------------------+
//| 触发交易警报 |
//+------------------------------------------------------------------+
void TriggerAlert(string type, int barIndex, datetime time)
{
if(time != lastAlertTime)
{
string symbol = Symbol();
string message = symbol + " " + TimeToString(time) + " " + type + " Signal";
Alert(message);
lastAlertTime = time;
}
}
//+------------------------------------------------------------------+
//| 绘制水平线辅助函数 |
//+------------------------------------------------------------------+
void DrawHorizontalLine(string name, double price, color clr)
{
if(ObjectFind(0, name) < 0)
ObjectCreate(0, name, OBJ_HLINE, 0, 0, price);
else
ObjectMove(0, name, 0, 0, price);
ObjectSetInteger(0, name, OBJPROP_COLOR, clr);
ObjectSetInteger(0, name, OBJPROP_STYLE, STYLE_DASHDOT);
ObjectSetInteger(0, name, OBJPROP_WIDTH, 1);
}
//+------------------------------------------------------------------+
//| 绘制趋势线辅助函数 |
//+------------------------------------------------------------------+
void DrawTrendLine(string name, datetime t1, double p1, datetime t2, double p2, color clr)
{
if(ObjectFind(0, name) < 0)
ObjectCreate(0, name, OBJ_TRENDLINE, 0, t1, p1, t2, p2);
else
{
ObjectMove(0, name, 0, t1, p1);
ObjectMove(0, name, 1, t2, p2);
}
ObjectSetInteger(0, name, OBJPROP_COLOR, clr);
ObjectSetInteger(0, name, OBJPROP_WIDTH, 2);
}
//+------------------------------------------------------------------+ |