|
RSI是交易中非常常见的一个技术指标,去经典书籍《期货市场技术分析》中,约翰墨菲对相对力度指数(RSI)做了比较详细的介绍,也对该指标比较推崇。RSI是由传奇的投资大师韦尔斯.王尔德首创的,发表在他的《技术交易系统的新思路》一书中。韦尔斯.王尔德是投资者界的大神,他发现的技术指标除了这个RSI,还有抛物线系统(SAR),方向性运动指数(DMI),商晶选择指数(CSI)(大神请收下我的膝盖)。
今天我们来讲一下RSI指标的相关状况。
首先要计算:RS=[A÷B]×100%
公式中,A——N日内收盘涨幅之和 B——N日内收盘跌幅之和(取正值)
然后求RSI=100-(100/(1+RS))公式中,当RSI超过70时,显示超买状态,当RSI低于30时,则是超卖状态。
下面我们用代码把这个指标完整演绎一下:
- #property copyright "2018-2019, geekquan number Corp."
- #property link "http://www.geekquant.com"
- #property strict
-
- #property indicator_separate_window //主图指标
- #property indicator_minimum 0 //指标的下限
- #property indicator_maximum 100 //指标的上限
- #property indicator_buffers 1 //指标的数量为1
- #property indicator_color1 DodgerBlue //指标线的颜色
- #property indicator_level1 30.0 //需进一步研究
- #property indicator_level2 70.0 //需进一步研究
- #property indicator_levelcolor clrSilver //需进一步研究
- #property indicator_levelstyle STYLE_DOT //需进一步研究
- //--- input parameters
- input int InpRSIPeriod=14; // RSI Period 设置一个输入变量
- //--- buffers
- double ExtRSIBuffer[]; //设置指标数组,
- double ExtPosBuffer[]; //用于计算RSI的中间数组,用来计算上涨
- double ExtNegBuffer[]; //用于计算RSI的中间数组,用来计算下跌
- //+------------------------------------------------------------------+
- //| Custom indicator initialization function |
- //+------------------------------------------------------------------+
- int OnInit(void)
- {
- string short_name;
- //--- 初始化3个指标缓冲区 2 additional buffers are used for counting.
- IndicatorBuffers(3);
- SetIndexBuffer(1,ExtPosBuffer); //将过程数组与缓冲区联系起来
- SetIndexBuffer(2,ExtNegBuffer);//将过程数组与缓冲区联系起来
- //--- indicator line
- SetIndexStyle(0,DRAW_LINE); //设置划线的类型
- SetIndexBuffer(0,ExtRSIBuffer);//将指标数组与指标缓冲区联系
- //--- name for DataWindow and indicator subwindow label
- short_name="RSI("+string(InpRSIPeriod)+")";
- IndicatorShortName(short_name);
- SetIndexLabel(0,short_name);//设置RSI的label
- //--- check for input
- if(InpRSIPeriod<2)
- {
- Print("Incorrect value for input variable InpRSIPeriod = ",InpRSIPeriod);
- return(INIT_FAILED);
- }
- //---
- SetIndexDrawBegin(0,InpRSIPeriod);//设置一个可输入参数的变量,需进一步研究这个函数
- //--- initialization done
- return(INIT_SUCCEEDED);
- }
- //+------------------------------------------------------------------+
- //| Relative Strength Index |
- //+------------------------------------------------------------------+
- 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 i,pos;
- double diff;
- // Print(rates_total);
- // Print(Bars);
- //---做一些初始化的判断
- if(Bars<=InpRSIPeriod || InpRSIPeriod<2)
- return(0);
- //--- counting from 0 to rates_total 设置数组索引的顺序
- ArraySetAsSeries(ExtRSIBuffer,false);
- ArraySetAsSeries(ExtPosBuffer,false);
- ArraySetAsSeries(ExtNegBuffer,false);
- ArraySetAsSeries(close,false);
- //--- preliminary calculations
- // Print(prev_calculated);
- pos=prev_calculated-1;
- // Print(rates_total);
- if(pos<=InpRSIPeriod)//用于首次加载指标时计算
- {
- //--- first RSIPeriod values of the indicator are not calculated
- ExtRSIBuffer[0]=0.0;
- ExtPosBuffer[0]=0.0;
- ExtNegBuffer[0]=0.0;
- double sump=0.0;
- double sumn=0.0;
- for(i=1; i<=InpRSIPeriod; i++)//注意i从1开始
- {
- ExtRSIBuffer[i]=0.0;
- ExtPosBuffer[i]=0.0;
- ExtNegBuffer[i]=0.0;
- diff=close[i]-close[i-1];
- if(diff>0)
- sump+=diff;
- else
- sumn-=diff;
- }
- //--- calculate first visible value
- ExtPosBuffer[InpRSIPeriod]=sump/InpRSIPeriod;//计算上涨
- ExtNegBuffer[InpRSIPeriod]=sumn/InpRSIPeriod;//计算下跌
- if(ExtNegBuffer[InpRSIPeriod]!=0.0)//计算RS 和 RSI
- ExtRSIBuffer[InpRSIPeriod]=100.0-(100.0/(1.0+ExtPosBuffer[InpRSIPeriod]/ExtNegBuffer[InpRSIPeriod]));
- else
- {
- if(ExtPosBuffer[InpRSIPeriod]!=0.0)
- ExtRSIBuffer[InpRSIPeriod]=100.0;
- else
- ExtRSIBuffer[InpRSIPeriod]=50.0;
- }
- //--- prepare the position value for main calculation
- pos=InpRSIPeriod+1;
- }
- //--- the main loop of calculations 主循环
- for(i=pos; i<rates_total && !IsStopped(); i++)
- {
- diff=close[i]-close[i-1];
- ExtPosBuffer[i]=(ExtPosBuffer[i-1]*(InpRSIPeriod-1)+(diff>0.0?diff:0.0))/InpRSIPeriod;
- ExtNegBuffer[i]=(ExtNegBuffer[i-1]*(InpRSIPeriod-1)+(diff<0.0?-diff:0.0))/InpRSIPeriod;
- if(ExtNegBuffer[i]!=0.0)
- ExtRSIBuffer[i]=100.0-100.0/(1+ExtPosBuffer[i]/ExtNegBuffer[i]);
- else
- {
- if(ExtPosBuffer[i]!=0.0)
- ExtRSIBuffer[i]=100.0;
- else
- ExtRSIBuffer[i]=50.0;
- }
- }
- //---
- return(rates_total);
- }
复制代码
|
|