相对力度指数(RSI)及MQL4代码实现

0
回复
6600
查看
[复制链接]

53

主题

8

回帖

836

积分

管理员

积分
836
来源: 2019-11-22 17:29:33 显示全部楼层 |阅读模式
       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时,则是超卖状态。
下面我们用代码把这个指标完整演绎一下:
rsi20191122172854.png

  1. #property copyright   "2018-2019, geekquan number Corp."
  2. #property link        "http://www.geekquant.com"
  3. #property strict

  4. #property indicator_separate_window  //主图指标
  5. #property indicator_minimum    0      //指标的下限
  6. #property indicator_maximum    100    //指标的上限
  7. #property indicator_buffers    1      //指标的数量为1
  8. #property indicator_color1     DodgerBlue   //指标线的颜色
  9. #property indicator_level1     30.0     //需进一步研究
  10. #property indicator_level2     70.0       //需进一步研究
  11. #property indicator_levelcolor clrSilver  //需进一步研究
  12. #property indicator_levelstyle STYLE_DOT  //需进一步研究
  13. //--- input parameters
  14. input int InpRSIPeriod=14; // RSI Period  设置一个输入变量
  15. //--- buffers
  16. double ExtRSIBuffer[];      //设置指标数组,
  17. double ExtPosBuffer[];        //用于计算RSI的中间数组,用来计算上涨
  18. double ExtNegBuffer[];        //用于计算RSI的中间数组,用来计算下跌
  19. //+------------------------------------------------------------------+
  20. //| Custom indicator initialization function                         |
  21. //+------------------------------------------------------------------+
  22. int OnInit(void)
  23.   {
  24.    string short_name;
  25. //--- 初始化3个指标缓冲区   2 additional buffers are used for counting.
  26.    IndicatorBuffers(3);
  27.    SetIndexBuffer(1,ExtPosBuffer); //将过程数组与缓冲区联系起来
  28.    SetIndexBuffer(2,ExtNegBuffer);//将过程数组与缓冲区联系起来
  29. //--- indicator line
  30.    SetIndexStyle(0,DRAW_LINE); //设置划线的类型
  31.    SetIndexBuffer(0,ExtRSIBuffer);//将指标数组与指标缓冲区联系
  32. //--- name for DataWindow and indicator subwindow label
  33.    short_name="RSI("+string(InpRSIPeriod)+")";
  34.    IndicatorShortName(short_name);
  35.    SetIndexLabel(0,short_name);//设置RSI的label
  36. //--- check for input
  37.    if(InpRSIPeriod<2)
  38.      {
  39.       Print("Incorrect value for input variable InpRSIPeriod = ",InpRSIPeriod);
  40.       return(INIT_FAILED);
  41.      }
  42. //---
  43.    SetIndexDrawBegin(0,InpRSIPeriod);//设置一个可输入参数的变量,需进一步研究这个函数
  44. //--- initialization done
  45.    return(INIT_SUCCEEDED);
  46.   }
  47. //+------------------------------------------------------------------+
  48. //| Relative Strength Index                                          |
  49. //+------------------------------------------------------------------+
  50. int OnCalculate(const int rates_total,
  51.                 const int prev_calculated,
  52.                 const datetime &time[],
  53.                 const double &open[],
  54.                 const double &high[],
  55.                 const double &low[],
  56.                 const double &close[],
  57.                 const long &tick_volume[],
  58.                 const long &volume[],
  59.                 const int &spread[])
  60.   {
  61.    int    i,pos;
  62.    double diff;
  63. //  Print(rates_total);
  64. //  Print(Bars);
  65. //---做一些初始化的判断
  66.    if(Bars<=InpRSIPeriod || InpRSIPeriod<2)
  67.       return(0);
  68. //--- counting from 0 to rates_total 设置数组索引的顺序
  69.    ArraySetAsSeries(ExtRSIBuffer,false);
  70.    ArraySetAsSeries(ExtPosBuffer,false);
  71.    ArraySetAsSeries(ExtNegBuffer,false);
  72.    ArraySetAsSeries(close,false);
  73. //--- preliminary calculations
  74. //   Print(prev_calculated);
  75.    pos=prev_calculated-1;
  76. //   Print(rates_total);
  77.    if(pos<=InpRSIPeriod)//用于首次加载指标时计算
  78.      {
  79.       //--- first RSIPeriod values of the indicator are not calculated
  80.       ExtRSIBuffer[0]=0.0;
  81.       ExtPosBuffer[0]=0.0;
  82.       ExtNegBuffer[0]=0.0;
  83.       double sump=0.0;
  84.       double sumn=0.0;
  85.       for(i=1; i<=InpRSIPeriod; i++)//注意i从1开始
  86.         {
  87.          ExtRSIBuffer[i]=0.0;
  88.          ExtPosBuffer[i]=0.0;
  89.          ExtNegBuffer[i]=0.0;
  90.          diff=close[i]-close[i-1];
  91.          if(diff>0)
  92.             sump+=diff;
  93.          else
  94.             sumn-=diff;
  95.         }
  96.       //--- calculate first visible value
  97.       ExtPosBuffer[InpRSIPeriod]=sump/InpRSIPeriod;//计算上涨
  98.       ExtNegBuffer[InpRSIPeriod]=sumn/InpRSIPeriod;//计算下跌
  99.       if(ExtNegBuffer[InpRSIPeriod]!=0.0)//计算RS  和  RSI
  100.          ExtRSIBuffer[InpRSIPeriod]=100.0-(100.0/(1.0+ExtPosBuffer[InpRSIPeriod]/ExtNegBuffer[InpRSIPeriod]));
  101.       else
  102.         {
  103.          if(ExtPosBuffer[InpRSIPeriod]!=0.0)
  104.             ExtRSIBuffer[InpRSIPeriod]=100.0;
  105.          else
  106.             ExtRSIBuffer[InpRSIPeriod]=50.0;
  107.         }
  108.       //--- prepare the position value for main calculation
  109.       pos=InpRSIPeriod+1;
  110.      }
  111. //--- the main loop of calculations  主循环
  112.    for(i=pos; i<rates_total && !IsStopped(); i++)
  113.      {
  114.       diff=close[i]-close[i-1];
  115.       ExtPosBuffer[i]=(ExtPosBuffer[i-1]*(InpRSIPeriod-1)+(diff>0.0?diff:0.0))/InpRSIPeriod;
  116.       ExtNegBuffer[i]=(ExtNegBuffer[i-1]*(InpRSIPeriod-1)+(diff<0.0?-diff:0.0))/InpRSIPeriod;
  117.       if(ExtNegBuffer[i]!=0.0)
  118.          ExtRSIBuffer[i]=100.0-100.0/(1+ExtPosBuffer[i]/ExtNegBuffer[i]);
  119.       else
  120.         {
  121.          if(ExtPosBuffer[i]!=0.0)
  122.             ExtRSIBuffer[i]=100.0;
  123.          else
  124.             ExtRSIBuffer[i]=50.0;
  125.         }
  126.      }
  127. //---
  128.    return(rates_total);
  129.   }

复制代码




回复

使用道具 举报

您需要登录后才可以回帖 登录 | 免费注册
关注微信