路路发智能交易

 找回密码
 立即注册
查看: 3391|回复: 0

MT4智能交易编程示例 RSI指标

  [复制链接]

管理员

MT4软件工程师

Rank: 9Rank: 9Rank: 9

积分
6546
帖子
2772
主题
2762
QQ
发表于 2014-4-4 14:40:17 | 显示全部楼层 |阅读模式
MT4智能交易编程示例 RSI指标

  1. #property copyright   "路路发智能交易研发中心"
  2. #property link        "http://www.ea668.com"
  3. #property description "Relative Strength Index"
  4. #property strict

  5. #property indicator_separate_window
  6. #property indicator_minimum    0
  7. #property indicator_maximum    100
  8. #property indicator_buffers    1
  9. #property indicator_color1     DodgerBlue
  10. #property indicator_level1     30.0
  11. #property indicator_level2     70.0
  12. #property indicator_levelcolor clrSilver
  13. #property indicator_levelstyle STYLE_DOT
  14. //--- input parameters
  15. input int InpRSIPeriod=14; // RSI Period
  16. //--- buffers
  17. double ExtRSIBuffer[];
  18. double ExtPosBuffer[];
  19. double ExtNegBuffer[];
  20. //+------------------------------------------------------------------+
  21. //| Custom indicator initialization function                         |
  22. //+------------------------------------------------------------------+
  23. int OnInit(void)
  24.   {
  25.    string short_name;
  26. //--- 2 additional buffers are used for counting.
  27.    IndicatorBuffers(3);
  28.    SetIndexBuffer(0,ExtRSIBuffer);
  29.    SetIndexBuffer(1,ExtPosBuffer);
  30.    SetIndexBuffer(2,ExtNegBuffer);
  31. //--- indicator line
  32.    SetIndexStyle(0,DRAW_LINE);
  33.    SetIndexBuffer(0,ExtRSIBuffer);
  34. //--- name for DataWindow and indicator subwindow label
  35.    short_name="RSI("+string(InpRSIPeriod)+")";
  36.    IndicatorShortName(short_name);
  37.    SetIndexLabel(0,short_name);
  38. //--- check for input
  39.    if(InpRSIPeriod<2)
  40.      {
  41.       Print("Incorrect value for input variable InpRSIPeriod = ",InpRSIPeriod);
  42.       return(INIT_FAILED);
  43.      }
  44. //---
  45.    SetIndexDrawBegin(0,InpRSIPeriod);
  46. //--- initialization done
  47.    return(INIT_SUCCEEDED);
  48.   }
  49. //+------------------------------------------------------------------+
  50. //| Relative Strength Index                                          |
  51. //+------------------------------------------------------------------+
  52. int OnCalculate(const int rates_total,
  53.                 const int prev_calculated,
  54.                 const datetime &time[],
  55.                 const double &open[],
  56.                 const double &high[],
  57.                 const double &low[],
  58.                 const double &close[],
  59.                 const long &tick_volume[],
  60.                 const long &volume[],
  61.                 const int &spread[])
  62.   {
  63.    int    i,pos;
  64.    double diff;
  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.    pos=prev_calculated-1;
  75.    if(pos<=InpRSIPeriod)
  76.      {
  77.       //--- first RSIPeriod values of the indicator are not calculated
  78.       ExtRSIBuffer[0]=0.0;
  79.       ExtPosBuffer[0]=0.0;
  80.       ExtNegBuffer[0]=0.0;
  81.       double sump=0.0;
  82.       double sumn=0.0;
  83.       for(i=1; i<=InpRSIPeriod; i++)
  84.         {
  85.          ExtRSIBuffer[i]=0.0;
  86.          ExtPosBuffer[i]=0.0;
  87.          ExtNegBuffer[i]=0.0;
  88.          diff=close[i]-close[i-1];
  89.          if(diff>0)
  90.             sump+=diff;
  91.          else
  92.             sumn-=diff;
  93.         }
  94.       //--- calculate first visible value
  95.       ExtPosBuffer[InpRSIPeriod]=sump/InpRSIPeriod;
  96.       ExtNegBuffer[InpRSIPeriod]=sumn/InpRSIPeriod;
  97.       if(ExtNegBuffer[InpRSIPeriod]!=0.0)
  98.          ExtRSIBuffer[InpRSIPeriod]=100.0-(100.0/(1.0+ExtPosBuffer[InpRSIPeriod]/ExtNegBuffer[InpRSIPeriod]));
  99.       else
  100.         {
  101.          if(ExtPosBuffer[InpRSIPeriod]!=0.0)
  102.             ExtRSIBuffer[InpRSIPeriod]=100.0;
  103.          else
  104.             ExtRSIBuffer[InpRSIPeriod]=50.0;
  105.         }
  106.       //--- prepare the position value for main calculation
  107.       pos=InpRSIPeriod+1;
  108.      }
  109. //--- the main loop of calculations
  110.    for(i=pos; i<rates_total && !IsStopped(); i++)
  111.      {
  112.       diff=close[i]-close[i-1];
  113.       ExtPosBuffer[i]=(ExtPosBuffer[i-1]*(InpRSIPeriod-1)+(diff>0.0?diff:0.0))/InpRSIPeriod;
  114.       ExtNegBuffer[i]=(ExtNegBuffer[i-1]*(InpRSIPeriod-1)+(diff<0.0?-diff:0.0))/InpRSIPeriod;
  115.       if(ExtNegBuffer[i]!=0.0)
  116.          ExtRSIBuffer[i]=100.0-100.0/(1+ExtPosBuffer[i]/ExtNegBuffer[i]);
  117.       else
  118.         {
  119.          if(ExtPosBuffer[i]!=0.0)
  120.             ExtRSIBuffer[i]=100.0;
  121.          else
  122.             ExtRSIBuffer[i]=50.0;
  123.         }
  124.      }
  125. //---
  126.    return(rates_total);
  127.   }
  128. //+------------------------------------------------------------------+
复制代码



外汇智能交易,成就财富梦想!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则


MT4编程培训|EA定制开发|QQ在线咨询|路路发智能交易 ( 沪ICP备13011711号-1 )

苏公网安备 32021102000151号

GMT+8, 2019-10-23 18:16 , Processed in 1.171875 second(s), 26 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表