路路发智能交易研发中心

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

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

[复制链接]

管理员

MT4软件工程师

Rank: 9Rank: 9Rank: 9

积分
6521
帖子
2771
主题
2761
QQ
发表于 2014-4-4 14:41:21 | 显示全部楼层 |阅读模式
MT4智能交易编程示例 KDJ指标
  1. #property copyright   "路路发智能交易研发中心"
  2. #property link        "http://www.ea668.com"
  3. #property description "Stochastic Oscillator"
  4. #property strict

  5. #property indicator_separate_window
  6. #property indicator_minimum    0
  7. #property indicator_maximum    100
  8. #property indicator_buffers    2
  9. #property indicator_color1     LightSeaGreen
  10. #property indicator_color2     Red
  11. #property indicator_level1     20.0
  12. #property indicator_level2     80.0
  13. #property indicator_levelcolor clrSilver
  14. #property indicator_levelstyle STYLE_DOT
  15. //--- input parameters
  16. input int InpKPeriod=5; // K Period
  17. input int InpDPeriod=3; // D Period
  18. input int InpSlowing=3; // Slowing
  19. //--- buffers
  20. double ExtMainBuffer[];
  21. double ExtSignalBuffer[];
  22. double ExtHighesBuffer[];
  23. double ExtLowesBuffer[];
  24. //---
  25. int draw_begin1=0;
  26. int draw_begin2=0;
  27. //+------------------------------------------------------------------+
  28. //| Custom indicator initialization function                         |
  29. //+------------------------------------------------------------------+
  30. int OnInit(void)
  31.   {
  32.    string short_name;
  33. //--- 2 additional buffers are used for counting.
  34.    IndicatorBuffers(4);
  35.    SetIndexBuffer(2, ExtHighesBuffer);
  36.    SetIndexBuffer(3, ExtLowesBuffer);
  37. //--- indicator lines
  38.    SetIndexStyle(0,DRAW_LINE);
  39.    SetIndexBuffer(0, ExtMainBuffer);
  40.    SetIndexStyle(1,DRAW_LINE);
  41.    SetIndexBuffer(1, ExtSignalBuffer);
  42. //--- name for DataWindow and indicator subwindow label
  43.    short_name="Sto("+IntegerToString(InpKPeriod)+","+IntegerToString(InpDPeriod)+","+IntegerToString(InpSlowing)+")";
  44.    IndicatorShortName(short_name);
  45.    SetIndexLabel(0,short_name);
  46.    SetIndexLabel(1,"Signal");
  47. //---
  48.    draw_begin1=InpKPeriod+InpSlowing;
  49.    draw_begin2=draw_begin1+InpDPeriod;
  50.    SetIndexDrawBegin(0,draw_begin1);
  51.    SetIndexDrawBegin(1,draw_begin2);
  52. //--- initialization done
  53.    return(INIT_SUCCEEDED);
  54.   }
  55. //+------------------------------------------------------------------+
  56. //| Stochastic oscillator                                            |
  57. //+------------------------------------------------------------------+
  58. int OnCalculate(const int rates_total,
  59.                 const int prev_calculated,
  60.                 const datetime &time[],
  61.                 const double &open[],
  62.                 const double &high[],
  63.                 const double &low[],
  64.                 const double &close[],
  65.                 const long &tick_volume[],
  66.                 const long &volume[],
  67.                 const int &spread[])
  68.   {
  69.    int    i,k,pos;
  70. //--- check for bars count
  71.    if(rates_total<=InpKPeriod+InpDPeriod+InpSlowing)
  72.       return(0);
  73. //--- counting from 0 to rates_total
  74.    ArraySetAsSeries(ExtMainBuffer,false);
  75.    ArraySetAsSeries(ExtSignalBuffer,false);
  76.    ArraySetAsSeries(ExtHighesBuffer,false);
  77.    ArraySetAsSeries(ExtLowesBuffer,false);
  78.    ArraySetAsSeries(low,false);
  79.    ArraySetAsSeries(high,false);
  80.    ArraySetAsSeries(close,false);
  81. //---
  82.    pos=InpKPeriod-1;
  83.    if(pos+1<prev_calculated)
  84.       pos=prev_calculated-2;
  85.    else
  86.      {
  87.       for(i=0; i<pos; i++)
  88.         {
  89.          ExtLowesBuffer[i]=0.0;
  90.          ExtHighesBuffer[i]=0.0;
  91.         }
  92.      }
  93. //--- calculate HighesBuffer[] and ExtHighesBuffer[]
  94.    for(i=pos; i<rates_total && !IsStopped(); i++)
  95.      {
  96.       double dmin=1000000.0;
  97.       double dmax=-1000000.0;
  98.       for(k=i-InpKPeriod+1; k<=i; k++)
  99.         {
  100.          if(dmin>low[k])
  101.             dmin=low[k];
  102.          if(dmax<high[k])
  103.             dmax=high[k];
  104.         }
  105.       ExtLowesBuffer[i]=dmin;
  106.       ExtHighesBuffer[i]=dmax;
  107.      }
  108. //--- %K line
  109.    pos=InpKPeriod-1+InpSlowing-1;
  110.    if(pos+1<prev_calculated)
  111.       pos=prev_calculated-2;
  112.    else
  113.      {
  114.       for(i=0; i<pos; i++)
  115.          ExtMainBuffer[i]=0.0;
  116.      }
  117. //--- main cycle
  118.    for(i=pos; i<rates_total && !IsStopped(); i++)
  119.      {
  120.       double sumlow=0.0;
  121.       double sumhigh=0.0;
  122.       for(k=(i-InpSlowing+1); k<=i; k++)
  123.         {
  124.          sumlow +=(close[k]-ExtLowesBuffer[k]);
  125.          sumhigh+=(ExtHighesBuffer[k]-ExtLowesBuffer[k]);
  126.         }
  127.       if(sumhigh==0.0)
  128.          ExtMainBuffer[i]=100.0;
  129.       else
  130.          ExtMainBuffer[i]=sumlow/sumhigh*100.0;
  131.      }
  132. //--- signal
  133.    pos=InpDPeriod-1;
  134.    if(pos+1<prev_calculated)
  135.       pos=prev_calculated-2;
  136.    else
  137.      {
  138.       for(i=0; i<pos; i++)
  139.          ExtSignalBuffer[i]=0.0;
  140.      }
  141.    for(i=pos; i<rates_total && !IsStopped(); i++)
  142.      {
  143.       double sum=0.0;
  144.       for(k=0; k<InpDPeriod; k++)
  145.          sum+=ExtMainBuffer[i-k];
  146.       ExtSignalBuffer[i]=sum/InpDPeriod;
  147.      }
  148. //--- OnCalculate done. Return new prev_calculated.
  149.    return(rates_total);
  150.   }
  151. //+------------------------------------------------------------------+
复制代码




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

使用道具 举报

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

本版积分规则


MT4编程培训|EA定制开发|QQ在线咨询|路路发智能交易研发中心

GMT+8, 2024-12-22 10:53 , Processed in 0.161168 second(s), 26 queries .

© 2009-2022 520EA.com EA668.com

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