无锡路路发软件有限公司

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

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

[复制链接]

管理员

MT4软件工程师

Rank: 9Rank: 9Rank: 9

积分
6544
帖子
2772
主题
2762
QQ
发表于 2014-4-4 14:34:08 | 显示全部楼层 |阅读模式
MT4智能交易编程示例 ATR指标
  1. #property copyright   "路路发智能交易研发中心"
  2. #property link        "http://www.ea668.com"
  3. #property description "Average True Range"
  4. #property strict

  5. //--- indicator settings
  6. #property indicator_separate_window
  7. #property indicator_buffers 1
  8. #property indicator_color1  DodgerBlue
  9. //--- input parameter
  10. input int InpAtrPeriod=14; // ATR Period
  11. //--- buffers
  12. double ExtATRBuffer[];
  13. double ExtTRBuffer[];
  14. //+------------------------------------------------------------------+
  15. //| Custom indicator initialization function                         |
  16. //+------------------------------------------------------------------+
  17. int OnInit(void)
  18.   {
  19.    string short_name;
  20. //--- 1 additional buffer used for counting.
  21.    IndicatorBuffers(2);
  22.    IndicatorDigits(Digits);
  23. //--- indicator line
  24.    SetIndexStyle(0,DRAW_LINE);
  25.    SetIndexBuffer(0,ExtATRBuffer);
  26.    SetIndexBuffer(1,ExtTRBuffer);
  27. //--- name for DataWindow and indicator subwindow label
  28.    short_name="ATR("+IntegerToString(InpAtrPeriod)+")";
  29.    IndicatorShortName(short_name);
  30.    SetIndexLabel(0,short_name);
  31. //--- check for input parameter
  32.    if(InpAtrPeriod<=0)
  33.      {
  34.       Print("Wrong input parameter ATR Period=",InpAtrPeriod);
  35.       return(INIT_FAILED);
  36.      }
  37. //---
  38.    SetIndexDrawBegin(0,InpAtrPeriod);
  39. //---
  40.    return(INIT_SUCCEEDED);
  41.   }
  42. //+------------------------------------------------------------------+
  43. //| Average True Range                                               |
  44. //+------------------------------------------------------------------+
  45. int OnCalculate(const int rates_total,
  46.                 const int prev_calculated,
  47.                 const datetime &time[],
  48.                 const double &open[],
  49.                 const double &high[],
  50.                 const double &low[],
  51.                 const double &close[],
  52.                 const long &tick_volume[],
  53.                 const long &volume[],
  54.                 const int &spread[])
  55.   {
  56.    int i,limit;
  57. //--- check for bars count and input parameter
  58.    if(rates_total<=InpAtrPeriod || InpAtrPeriod<=0)
  59.       return(0);
  60. //--- counting from 0 to rates_total
  61.    ArraySetAsSeries(ExtATRBuffer,false);
  62.    ArraySetAsSeries(ExtTRBuffer,false);
  63.    ArraySetAsSeries(open,false);
  64.    ArraySetAsSeries(high,false);
  65.    ArraySetAsSeries(low,false);
  66.    ArraySetAsSeries(close,false);
  67. //--- preliminary calculations
  68.    if(prev_calculated==0)
  69.      {
  70.       ExtTRBuffer[0]=0.0;
  71.       ExtATRBuffer[0]=0.0;
  72.       //--- filling out the array of True Range values for each period
  73.       for(i=1; i<rates_total; i++)
  74.          ExtTRBuffer[i]=MathMax(high[i],close[i-1])-MathMin(low[i],close[i-1]);
  75.       //--- first AtrPeriod values of the indicator are not calculated
  76.       double firstValue=0.0;
  77.       for(i=1; i<=InpAtrPeriod; i++)
  78.         {
  79.          ExtATRBuffer[i]=0.0;
  80.          firstValue+=ExtTRBuffer[i];
  81.         }
  82.       //--- calculating the first value of the indicator
  83.       firstValue/=InpAtrPeriod;
  84.       ExtATRBuffer[InpAtrPeriod]=firstValue;
  85.       limit=InpAtrPeriod+1;
  86.      }
  87.    else
  88.       limit=prev_calculated-1;
  89. //--- the main loop of calculations
  90.    for(i=limit; i<rates_total; i++)
  91.      {
  92.       ExtTRBuffer[i]=MathMax(high[i],close[i-1])-MathMin(low[i],close[i-1]);
  93.       ExtATRBuffer[i]=ExtATRBuffer[i-1]+(ExtTRBuffer[i]-ExtTRBuffer[i-InpAtrPeriod])/InpAtrPeriod;
  94.      }
  95. //--- return value of prev_calculated for next call
  96.    return(rates_total);
  97.   }
  98. //+------------------------------------------------------------------+
复制代码




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

使用道具 举报

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

本版积分规则


MT4编程培训|EA定制开发|QQ在线咨询|无锡路路发软件有限公司 ( 沪ICP备13011711号-1 )

苏公网安备 32021102000151号

GMT+8, 2019-7-20 07:14 , Processed in 1.203125 second(s), 26 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

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