17评论

0收藏

想把RSI变成双线的,麻烦大神帮解决!谢谢!

avatar 公孙晚城 | 7563 人阅读 | 17 人评论 | 2016-10-16

//+------------------------------------------------------------------+
//|                                                          RSI.mq4 |
//|                   Copyright 2005-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "2005-2014, MetaQuotes Software Corp."
#property link        "http://www.mql4.com"
#property description "Relative Strength Index"
#property strict
#property indicator_separate_window
#property indicator_minimum    0
#property indicator_maximum    100
#property indicator_buffers    2
#property indicator_color1     DodgerBlue
#property indicator_color2     DarkOrange
#property indicator_level1     5.0
#property indicator_level2     95.0
#property indicator_level3     10.0
#property indicator_level4     90.0
#property indicator_levelcolor clrDimGray
#property indicator_levelstyle STYLE_DOT
//--- input parameters
input int InpRSIPeriod=4; // RSI Period
//--- buffers
double ExtRSIBuffer[];
double ExtPosBuffer[];
double ExtNegBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
   {
    string short_name;
//--- 2 additional buffers are used for counting.
    IndicatorBuffers(3);
    SetIndexBuffer(0,ExtRSIBuffer);
    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);
//--- 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;
//---
    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
    pos=prev_calculated-1;
    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++)
         {
          ExtRSIBuffer=0.0;
          ExtPosBuffer=0.0;
          ExtNegBuffer=0.0;
          diff=close-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)
          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-close[i-1];
       ExtPosBuffer=(ExtPosBuffer[i-1]*(InpRSIPeriod-1)+(diff>0.0?diff:0.0))/InpRSIPeriod;
       ExtNegBuffer=(ExtNegBuffer[i-1]*(InpRSIPeriod-1)+(diff<0.0?-diff:0.0))/InpRSIPeriod;
       if(ExtNegBuffer!=0.0)
          ExtRSIBuffer=100.0-100.0/(1+ExtPosBuffer/ExtNegBuffer);
       else
         {
          if(ExtPosBuffer!=0.0)
             ExtRSIBuffer=100.0;
          else
             ExtRSIBuffer=50.0;
         }
      }
//---
    return(rates_total);
   }
//+------------------------------------------------------------------+

QQ图片20161016152457.png
""
还没有人打赏,支持一下

评论|共 17 个

音乐谷主

发表于 2016-10-17 12:30:52 | 显示全部楼层

直接拖进去就行了,然后你把模板保存下来,就行了,很简单

爱不爱都没错

发表于 2020-3-21 12:35:48 | 显示全部楼层

路过,支持一下啦

小新比我乖

发表于 2020-6-12 10:52:08 | 显示全部楼层

难得一见的好帖

我是大哥

发表于 2020-6-17 10:47:49 来自手机 | 显示全部楼层

学习技术交流

小燕子

发表于 2020-7-13 11:44:16 | 显示全部楼层

学习了,不错

花生牛奶

发表于 2020-7-22 22:22:35 | 显示全部楼层

帮你顶下哈!!

小西点金

发表于 2020-8-30 15:59:46 | 显示全部楼层

谢谢楼主分享

zzetcw

发表于 2020-9-2 21:21:11 | 显示全部楼层

学习了,不错

dayaqwba

发表于 2021-7-5 13:26:02 | 显示全部楼层

12下一页
您需要登录后才可以回帖 登录 | 注册 微信登录

EA之家评论守则