下载MetaTrader 5

请观看如何免费下载自动交易

有趣的脚本?
因此发布一个链接 -
让其他人评价

喜欢这个脚本? 在MetaTrader 5客户端尝试它

2015.04.17 09:02
脚本

KeyFinder 2.0 - MetaTrader 5脚本

| Chinese English Русский Español Deutsch 日本語 Português

显示:
578
等级:
投票: 30

这款脚本搜索 DeMark 的轴点, 并在图表上显示它们, 同时指示它们的尺度。

此脚本几乎与 KeyFinder 有相同的功能。它在视觉上没有任何不同,除了拥有从图表上删除脚本和关联对象的按钮。

图例. 1. 加载 KeyFinder 2.0 脚本之后的 RTS 指数期货合约图表

图例. 1. 加载 KeyFinder 2.0 脚本之后的 RTS 指数期货合约图表

虽然脚本并未改变视觉和功能,但其代码已被明显修改。

为此脚本创建了 CKeyFinder 类。

图例. 2. CKeyFinder 类

图例. 2. CKeyFinder 类

类源码提供如下。因为我已经尽可能的添加了详尽注解, 您将不会面对任何问题。

//+------------------------------------------------------------------+
//|                                                    KeyFinder.mqh |
//|                                                   Trofimov Pavel |
//|                                               trofimovpp@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Trofimov Pavel"
#property link      "trofimovpp@mail.ru"
//+------------------------------------------------------------------+
//| Class for KeyFinder 2.0                                          |
//+------------------------------------------------------------------+
class CKeyFinder
  {
private:
   // Class variables
   int               MinDimension;  //Minimum dimension of points
   int               MaxBars;       //The number of bars to process
protected:
   // Class methods for internal use
   // Determining the upper points dimensions
   int               getHighDimension(MqlRates &tmpRates[],int tmp_i,int tmp_iCod);
   // Determining the lower points dimensions
   int               getLowDimension(MqlRates &tmpRates[],int tmp_i,int tmp_iCod);
public:
   // Class constructor
                     CKeyFinder();
   // Class destructor
                    ~CKeyFinder();
   // Property method for getting the dimension   
   int               GetMinDimension();
   // Property method for setting the dimension
   void              SetMinDimension(int temp_Val);
   // Property method for getting the number of bars to process
   int               GetMaxBars();
   // Property method for setting the number of bars to process
   void              SetMaxBars(int temp_Val);
   // Method for finding upper pivot points
   void              FindUpKeyPoints(int temp_iCod,MqlRates &temp_rates[]);
   // Method for finding lower pivot points
   void              FindLowKeyPoints(int temp_iCod,MqlRates &temp_rates[]);
   // Clearing the chart from the script objects.
   int               CleanChart();
  };
//+------------------------------------------------------------------+
//|                Determining the upper point dimension             |
//+------------------------------------------------------------------+
int CKeyFinder::getHighDimension(MqlRates &tmpRates[],int tmp_i,int tmp_iCod)// The method receives an array of MqlRates type data, a number of a processed bar in the array and a maximum index of the array
  {
   int k=1;
   while((tmpRates[tmp_i].high>tmpRates[tmp_i+k].high) && (tmpRates[tmp_i].high>tmpRates[tmp_i-k].high) && ((tmp_i+k)<(tmp_iCod)) && ((tmp_i-k)>0)) k++;
   if(((tmp_i+k)==tmp_iCod) || ((tmp_i-k)==0)) k=-1;
   return(k);
  };
//+------------------------------------------------------------------+
//|                Determining the lower point dimension             |
//+------------------------------------------------------------------+
int CKeyFinder::getLowDimension(MqlRates &tmpRates[],int tmp_i,int tmp_iCod)// The method receives an array of MqlRates type data, a number of a processed bar in the array and a maximum index of the array
  {
   int k=1;
   while((tmpRates[tmp_i].low<tmpRates[tmp_i+k].low) && (tmpRates[tmp_i].low<tmpRates[tmp_i-k].low) && ((tmp_i+k)<(tmp_iCod)) && ((tmp_i-k)>0)) k++;
   if(((tmp_i+k)==tmp_iCod) || ((tmp_i-k)==0)) k=-1;
   return(k);
  };
//+------------------------------------------------------------------+
//| Method GetMinDimension                                           |
//+------------------------------------------------------------------+
int CKeyFinder::GetMinDimension(){return(MinDimension);};
//+------------------------------------------------------------------+
//| Method SetMinDimension                                           |
//+------------------------------------------------------------------+
void CKeyFinder::SetMinDimension(int temp_Val)
  {
   if(temp_Val>1) MinDimension=temp_Val;
   else
     {
      MessageBox("The set dimension is too low."+"\n"+"The default dimension of 5 will be used","Warning!",MB_OK+MB_ICONWARNING);
      MinDimension=5;
     };
  };
//+------------------------------------------------------------------+
//| Method GetMaxBars                                                |
//+------------------------------------------------------------------+
int CKeyFinder::GetMaxBars() {return(MaxBars);};
//+------------------------------------------------------------------+
//| Method SetMaxBars                                                |
//+------------------------------------------------------------------+
void CKeyFinder::SetMaxBars(int temp_Val)
  {
// Checking the availability of history for the set number of bars
   int SMaxBars=Bars(Symbol(),0);
   if(SMaxBars<temp_Val)
     {
      temp_Val=SMaxBars;// Setting the maximum available number of bars
      MessageBox("The MaxBars parameter is too high."+"\n"+"Only "+IntegerToString(temp_Val)+" bars will be used for calculation.","Warning!",MB_OK+MB_ICONWARNING);
     };

// Checking if the number of bars is not too low
   if(temp_Val<(2*MinDimension))
     {
      MessageBox("The set number of bars is too low."+"\n"+"The default number of 300 will be used","Warning!",MB_OK+MB_ICONWARNING);
      temp_Val=300;
     };
   MaxBars=temp_Val;
  };
//+------------------------------------------------------------------+
//| Method FindKeyPoints - Finding upper key points                  |
//+------------------------------------------------------------------+
void CKeyFinder::FindUpKeyPoints(int temp_iCod,MqlRates &temp_rates[])// The method receives an index of the maximum array element and the MqlRates array
  {
   int HD=1;   //initializing points dimensions
   for(int i=temp_iCod-MinDimension; i>(MinDimension-1); i--)// A loop from the last bar - MinDimension to the zero bar + MinDimension
     {
      HD=getHighDimension(temp_rates,i,temp_iCod);//Getting points dimensions
      if((HD>=MinDimension) || (HD==-1))
        {//Creating a mark if MinDimension condition is met
         string Ob_Name="KF_Label"+IntegerToString(i);
         if(HD!=-1)
           {
            ObjectCreate(0,Ob_Name,OBJ_TEXT,0,temp_rates[i].time,temp_rates[i].high);
            ObjectSetInteger(0,Ob_Name,OBJPROP_ANCHOR,0,ANCHOR_LOWER);
            ObjectSetString(0,Ob_Name,OBJPROP_TEXT,0,IntegerToString(HD));
            ObjectSetInteger(0,Ob_Name,OBJPROP_COLOR,clrRed);
           }
         else
           { //If the dimension can't be determined, use a ball mark
            ObjectCreate(0,Ob_Name,OBJ_ARROW,0,temp_rates[i].time,temp_rates[i].high);
            ObjectSetInteger(0,Ob_Name,OBJPROP_ARROWCODE,0,159);
            ObjectSetInteger(0,Ob_Name,OBJPROP_ANCHOR,0,ANCHOR_BOTTOM);
            ObjectSetInteger(0,Ob_Name,OBJPROP_COLOR,clrRed);
           };
        };
     };
  };
//+------------------------------------------------------------------+
//| Method FindLowKeyPoints - Finding lower key points            |
//+------------------------------------------------------------------+   
void CKeyFinder::FindLowKeyPoints(int temp_iCod,MqlRates &temp_rates[])// The method receives an index of the maximum array element and the MqlRates array
  {
   int LD=1;//initializing points dimensions
   bool iCreate;
   for(int i=temp_iCod-MinDimension; i>(MinDimension-1); i--)// A loop from the last bar - MinDimension to the zero bar + MinDimension
     {
      LD=getLowDimension(temp_rates,i,temp_iCod);
      if((LD>=MinDimension) || (LD==-1))
        {
         string Ob_Name="KF_Label"+IntegerToString(i)+"_1";//Avoiding the bars there Low and High can be key points
         if(LD!=-1)
           {
            iCreate=ObjectCreate(0,Ob_Name,OBJ_TEXT,0,temp_rates[i].time,temp_rates[i].low);
            if(iCreate)
              {
               ObjectSetInteger(0,Ob_Name,OBJPROP_ANCHOR,0,ANCHOR_UPPER);
               ObjectSetString(0,Ob_Name,OBJPROP_TEXT,0,IntegerToString(LD));
               ObjectSetInteger(0,Ob_Name,OBJPROP_COLOR,clrGreen);
              }
            else Comment("Cannot create the object");
           }
         else
           {
            iCreate=ObjectCreate(0,Ob_Name,OBJ_ARROW,0,temp_rates[i].time,temp_rates[i].low);
            if(iCreate)
              {
               ObjectSetInteger(0,Ob_Name,OBJPROP_ARROWCODE,0,159);
               ObjectSetInteger(0,Ob_Name,OBJPROP_ANCHOR,0,ANCHOR_TOP);
               ObjectSetInteger(0,Ob_Name,OBJPROP_COLOR,clrGreen);
              }
            else Comment("Cannot create the object");
           };
        };
     };
  };
//+------------------------------------------------------------------+
//| Method CleanChart - Clearing the chart from objects              |
//+------------------------------------------------------------------+   
int CKeyFinder::CleanChart(void)
  {
   string Label="KF_Label";
   int obj_total=ObjectsTotal(0,0,-1),n=0;
   for(int obj=obj_total-1; obj>=0; obj--) //Clearing the objects created by the script by the name mask
     {
      string objname=ObjectName(0,obj,0,-1);
      if(StringFind(objname,Label)>=0) ObjectDelete(0,objname);
      n++;
     }
   return(n);
  }
//+------------------------------------------------------------------+
//| Class constructor                                                |
//+------------------------------------------------------------------+
CKeyFinder::CKeyFinder(){};
//+------------------------------------------------------------------+
//| Class destructor                                                 |
//+------------------------------------------------------------------+
CKeyFinder::~CKeyFinder(){};
//+---------------------------------------------------------------------+

因为大部分的代码已被移至类中,主要脚本代码是非常紧凑的。

//+------------------------------------------------------------------+
//|                                                   KeyFinder2.mq5 |
//|                                                   Trofimov Pavel |
//|                                               trofimovpp@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Trofimov Pavel"
#property link      "trofimovpp@mail.ru"
#property version   "2.00"
#property description "Warning! This algorithm uses loops for calculations!"
#property description "It is recommended so set not more than 1000 to process!"
#property script_show_inputs
//--- input parameters
input int      MinDimesion=5; //Minimum dimension of points
input int      MaxBars=300;   //The number of bars to process
#include       "KeyFinder.mqh"
#include       <ChartObjects\ChartObjectsTxtControls.mqh>
//+------------------------------------------------------------------+
//| Script variables                                                 |
//+------------------------------------------------------------------+
CKeyFinder           m_keyfinder;      //Declaring СKeyFinder class variable
CChartObjectButton   m_button_close;   //Declaring CChartObjectButton class variable
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//-Initializing variables
   MqlRates  rates_array[];
   string Com="\n\n\n";
   int iCod=CopyRates(Symbol(),Period(),0,MaxBars,rates_array);// The number of elements in MqlRates array that we were able to copy
   int sy=10;//Button position
   if(ChartGetInteger(0,CHART_SHOW_OHLC))
      sy+=16;
//-Setting the necessary parameters   
   m_keyfinder.SetMaxBars(MaxBars);
   m_keyfinder.SetMinDimension(MinDimesion);
   m_keyfinder.CleanChart();//Clearing the chart from objects in case of a second use 
   iCod=iCod-1;//An index of the maximum element in rates_array[]
   Com+="Working...Please, wait!";
//-Creating the button and setting its properties
   m_button_close.Create(0,"ButtonClose",0,10,sy,100,20);
   m_button_close.Description("Close Script");
   m_button_close.Color(Blue);
   m_button_close.FontSize(8);
   Comment(Com);
//-Processing the bars using the CKeyFinder class methods   
   if(iCod>0)
     {
      m_keyfinder.FindUpKeyPoints(iCod,rates_array);//Searching for upper key points
      Com+="\nUpper points have been processed.\n";
      Comment(Com);
      m_keyfinder.FindLowKeyPoints(iCod,rates_array);//Searching for lower key points
      Comment("\n\n\nProcessing is complete");
     }
   else Comment("\n\n\nNo bars to process!!!");
   m_button_close.State(false);
//-Looping the script until the button is pressed
   while(!(m_button_close.State()))
     {
      //--- redraw chart
      ChartRedraw();
      Sleep(250);
     };
//-Clearing the chart by a press of the button
   m_keyfinder.CleanChart();
   Comment("");
  }

最后, 我提供了一些有用的链接和解释:

  • 如何从 MetaEditor 直接加载代码
  • 为了在 MetaTrader 4 中使用此脚本,您需要将文件 KeyFinder2.mq5 的扩展名改为 mq4,并在 MetaEditor 里重新编译它。而 KeyFinder.mqh 文件含有类代码,应与主脚本文件置于同一文件夹里 (例如, MQL5\Scripts)。

我期待收到您的评论, 反馈和建议。祝交易好运!


本文译自 MetaQuotes Software Corp. 撰写的俄文原文
官方代码: https://www.mql5.com/ru/code/12439

KeyLevels KeyLevels

带有整数位 00, 20, 50, 80 的价格级别指标。

KeyFinder KeyFinder

这款脚本搜索 DeMark 的轴点, 并在图表上显示它们, 同时指示它们的尺度。

回到未来 回到未来

使用监管分析基本面。

趋势振荡器 趋势振荡器

一款使用 Hodrick-Prescott 滤波器的趋势振荡器。