#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots 2
//--- 输入函数
input int RSIperiod=14; // 计算RSI的周期
input int Smooth=8; // 平滑RSI周期
input ENUM_MA_METHOD meth=MODE_SMMA; // 修匀法
//---- 图 RSI
#property indicator_label1 "RSI"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//---- 图 RSI_Smoothed
#property indicator_label2 "RSI_Smoothed"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrNavy
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//--- 指标缓冲区
double RSIBuffer[]; // 这里存储 RSI值
double RSI_SmoothedBuffer[]; // RSI平滑值
int RSIhandle; // 处理 RSI 指标
//+------------------------------------------------------------------+
//| 自定义指标初始化函数 |
//+------------------------------------------------------------------+
void OnInit()
{
//--- 指标缓冲区绘图indicator buffers mapping
SetIndexBuffer(0,RSIBuffer,INDICATOR_DATA);
SetIndexBuffer(1,RSI_SmoothedBuffer,INDICATOR_DATA);
IndicatorSetString(INDICATOR_SHORTNAME,"iRSI");
IndicatorSetInteger(INDICATOR_DIGITS,2);
//---
RSIhandle=iRSI(NULL,0,RSIperiod,PRICE_CLOSE);
//---
}
//+------------------------------------------------------------------+
//| 自定义指标重复函数 |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const int begin,
const double &price[]
)
{
//--- 重置上一个错误值
ResetLastError();
//--- 数组RSIBuffer []中获得 RSI 指标数据
int copied=CopyBuffer(RSIhandle,0,0,rates_total,RSIBuffer);
if(copied<=0)
{
Print("Unable to copy the values of the indicator RSI. Error = ",
GetLastError(),", copied =",copied);
return(0);
}
//--- 创建使用RSI值的平均值的指标
int RSI_MA_handle=iMA(NULL,0,Smooth,0,meth,RSIhandle);
copied=CopyBuffer(RSI_MA_handle,0,0,rates_total,RSI_SmoothedBuffer);
if(copied<=0)
{
Print("Unable to copy the smoothed indicator of RSI. Error = ",
GetLastError(),", copied =",copied);
return(0);
}
//--- 为下次调用返回prev_calculated值
return(rates_total);
}
|