初学者的问题 MQL5 MT5 MetaTrader 5 - 页 575

 

下面是我解决重复搜索问题的变种。

//+------------------------------------------------------------------+
//|                                              FindRetryPrices.mq5 |
//|                                 Copyright 2016, Vasiliy Sokolov. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, Vasiliy Sokolov."
#property link      "http://www.mql5.com"
#property version   "1.00"
#include <Dictionary.mqh>

input int RatesPeriod = 10;                        // Период за который ищутся повторения
input int RoundPoints = 10;                        // Загрубление цены в прайсстепах
class CBar : public CObject
{
private:
   MqlRates m_bar;
public:
            CBar(MqlRates& bar){m_bar = bar;}
   datetime Date(){return m_bar.time;}
   double Open(){return m_bar.open;}
   double High(){return m_bar.high;}
   double Low(){return m_bar.low;}
   double Close(){return m_bar.close;}
};
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   // Нахождение повторяющихся баров
   MqlRates rates[];
   int total = CopyRates(Symbol(), Period(), 0, RoundPoints, rates);
   CDictionary* BarsCollections = new CDictionary(total);
   for(int i = 0; i < total; i++)
   {
      double price = PriceToLevel(rates[i].high);
      CBar* bar = new CBar(rates[i]);
      if(!BarsCollections.ContainsKey(price))
         BarsCollections.AddObject(price, new CList());
      CList* equal_bars = BarsCollections.GetObjectByKey(price);
      equal_bars.Add(bar);
   }
   // Вывод повторов
   FOREACH_DICT(BarsCollections)
   {
      CList* list = node;
      if(list.Total() < 2)continue;
      string retry_bars = "";
      for(CBar* bar = list.GetFirstNode(); bar != NULL; bar = list.GetNextNode())
         retry_bars += " [" + TimeToString(bar.Date()) + ", " + DoubleToString(bar.High(), Digits()) + "]";
      printf("Обнаружено совпадение баров:" + retry_bars);
   }
   delete BarsCollections;
}
//+------------------------------------------------------------------+
//| Округляет цену в соответствии с параметром RoundPoints           |
//+------------------------------------------------------------------+
double PriceToLevel(double price)
{
   double price_step = RoundPoints*Point();
   double mn = MathFloor(price/price_step);
   return mn * price_step;
}

//+------------------------------------------------------------------+

为了使脚本发挥作用,必须将字典文件复制到MQL5/Include中。

搜索是在黄色显示的单程for循环中进行的。产生的列表不包含重复的内容(如果A重复B,B重复A,那么将输出一个{A,B}集合,而不是两个{A,B}和{B,A})。).

这个脚本的输出显示如下。

2016.05.10 11:09:06.730 FindRetryPrices (Si Splice,M1)  Обнаружено совпадение баров: [2016.05.10 11:02, 67149] [2016.05.10 11:03, 67147] [2016.05.10 11:04, 67144]
2016.05.10 11:09:06.730 FindRetryPrices (Si Splice,M1)  Обнаружено совпадение баров: [2016.05.10 11:01, 67160] [2016.05.10 11:05, 67161]
2016.05.10 11:09:06.730 FindRetryPrices (Si Splice,M1)  Обнаружено совпадение баров: [2016.05.10 11:00, 67132] [2016.05.10 11:06, 67139]
附加的文件:
Dictionary.mqh  18 kb
 
Artyom Trishkin:

从参考资料来看。

瓦西里-索科洛夫

以下是我对重复搜索问题的解决版本。


非常感谢你们我今天会试着把它全部弄清楚。重要的是,不要只是把一段代码复制到Expert Advisor中,而是要理解为什么这样写,所以也许我会问更多问题。
 

请帮助我找到一个计算标准RSI的函数,要求很简单。

1.返回给定条形图上的RSI值

2.只对那些被要求的条形图(如有必要)计算指数(理想的)。

3.能够在指定的TF上进行计算

4.以牺牲第2点为代价的工作比指标更快。

我想把这个功能整合到专家顾问中,如果有人有现成的,请与我分享。

我这样问是因为这个指标非常普遍,并不神秘。

 
-Aleks-:

请帮助我找到一个计算标准RSI的函数,要求很简单。

1.返回给定条形图上的RSI值

2.只对那些被要求的条形图(如有必要)计算指数(理想的)。

3.能够在指定的TF上进行计算

4.以牺牲第2点为代价的工作比指标更快。

我想把这个功能整合到EA中,如果有人有现成的,请与我分享。

我这样问是因为这个指标非常普遍,并不神秘。

为什么你不喜欢标准的iRSI?不够秘密?
 
Vasiliy Sokolov:
标准iRSI有什么问题?不够秘密?
我需要对该功能进行一些修改...
 
-Aleks-:
我需要对该功能进行一些修改...

那就拿一个现成的,并根据你的要求进行修改。

//+------------------------------------------------------------------+
//|                                                          RSI.mq5 |
//|                        Copyright 2009, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright   "2009, MetaQuotes Software Corp."
#property link        "http://www.mql5.com"
#property description "Relative Strength Index"
//--- indicator settings
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property  indicator_level1 30
#property  indicator_level2 70
#property indicator_buffers 3
#property indicator_plots   1
#property  indicator_type1   DRAW_LINE
#property  indicator_color1  DodgerBlue
//--- input parameters
input int InpPeriodRSI=14; // Period
//--- indicator buffers
double    ExtRSIBuffer[];
double    ExtPosBuffer[];
double    ExtNegBuffer[];
//--- global variable
int       ExtPeriodRSI;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- check for input
   if(InpPeriodRSI<1)
     {
      ExtPeriodRSI=12;
      Print("Incorrect value for input variable InpPeriodRSI =",InpPeriodRSI,
            "Indicator will use value =",ExtPeriodRSI,"for calculations.");
     }
   else ExtPeriodRSI=InpPeriodRSI;
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtRSIBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,ExtPosBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(2,ExtNegBuffer,INDICATOR_CALCULATIONS);
//--- set accuracy
   IndicatorSetInteger(INDICATOR_DIGITS,2);
//--- sets first bar from what index will be drawn
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtPeriodRSI);
//--- name for DataWindow and indicator subwindow label
   IndicatorSetString(INDICATOR_SHORTNAME,"RSI("+string(ExtPeriodRSI)+")");
//--- initialization done
  }
//+------------------------------------------------------------------+
//| Relative Strength Index                                          |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
   int    i;
   double diff;
//--- check for rates count
   if(rates_total<=ExtPeriodRSI)
      return(0);
//--- preliminary calculations
   int pos=prev_calculated-1;
   if(pos<=ExtPeriodRSI)
     {
      //--- 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<=ExtPeriodRSI;i++)
        {
         ExtRSIBuffer[i]=0.0;
         ExtPosBuffer[i]=0.0;
         ExtNegBuffer[i]=0.0;
         diff=price[i]-price[i-1];
         SumP+=(diff>0?diff:0);
         SumN+=(diff<0?-diff:0);
        }
      //--- calculate first visible value
      ExtPosBuffer[ExtPeriodRSI]=SumP/ExtPeriodRSI;
      ExtNegBuffer[ExtPeriodRSI]=SumN/ExtPeriodRSI;
      if(ExtNegBuffer[ExtPeriodRSI]!=0.0)
         ExtRSIBuffer[ExtPeriodRSI]=100.0-(100.0/(1.0+ExtPosBuffer[ExtPeriodRSI]/ExtNegBuffer[ExtPeriodRSI]));
      else
        {
         if(ExtPosBuffer[ExtPeriodRSI]!=0.0)
            ExtRSIBuffer[ExtPeriodRSI]=100.0;
         else
            ExtRSIBuffer[ExtPeriodRSI]=50.0;
        }
      //--- prepare the position value for main calculation
      pos=ExtPeriodRSI+1;
     }
//--- the main loop of calculations
   for(i=pos;i<rates_total && !IsStopped();i++)
     {
      diff=price[i]-price[i-1];
      ExtPosBuffer[i]=(ExtPosBuffer[i-1]*(ExtPeriodRSI-1)+(diff>0.0?diff:0.0))/ExtPeriodRSI;
      ExtNegBuffer[i]=(ExtNegBuffer[i-1]*(ExtPeriodRSI-1)+(diff<0.0?-diff:0.0))/ExtPeriodRSI;
      if(ExtNegBuffer[i]!=0.0)
         ExtRSIBuffer[i]=100.0-100.0/(1+ExtPosBuffer[i]/ExtNegBuffer[i]);
      else
        {
         if(ExtPosBuffer[i]!=0.0)
            ExtRSIBuffer[i]=100.0;
         else
            ExtRSIBuffer[i]=50.0;
        }
     }
//--- OnCalculate done. Return new prev_calculated.
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
Vasiliy Sokolov:

那就拿一个现成的,并根据你的要求进行修改。

我知道代码里有一个指标...问题是谁可能已经改变了它......
 
Vasiliy Sokolov:

这里是我解决搜索重复问题的变体。

Vasily,当我打开#include <Dictionary.mqh>时,它的打开方式与EA正文中的一样它应该是这样的,或者里面应该有一些其他的代码?

因为在我看到的代码中,并不清楚它是如何搜索重复的((

 
Artyom Trishkin:

// строку int searchPeriod=(Search_Period<1)?1:Search_Period;
// можно расписать так:

input int Search_Period=10; // Количество копируемых свечей ... эту строку вы видите в настройках
int searchPeriod;           // Сюда будем записывать входной параметр
if(Search_Period<1) searchPeriod=1; // Если во входном параметре ввели ноль или меньше нуля, то параметр будет равен нулю
else searchPeriod=Search_Period;    // иначе примем входной параметр

// соответственно и строку int delta=(Delta<0)?0:Delta;
// можно расписать точно так же

我认为这是个错字,应该是0而不是1。

像这样。int searchPeriod=(Search_Period<1)?0:Search_Period。

并进一步在可执行脚本中。

int copy_bars=(int)fmin(Search_Period,Bars(Symbol(),Period()); // 要复制的蜡烛数

在这种情况下,我们也许应该已经使用了一个变量:searchPeriod。 对吗?

------

另一个问题,这条线路是做什么的?我发现,结构中的每个元素都会重置为零。而且我不明白,如果我们似乎刚刚写好这个数据,应该进一步使用它,那么我们在这里归零的是什么。

ZeroMemory(dataCandle); // 将结构中的数据归零。

 
Andrey Koldorkin:

我认为这是一个错字,应该是0而不是1,这样说对吗?

像这样。int searchPeriod=(Search_Period<1)?0:Search_Period。

并进一步在可执行脚本中。

int copy_bars=(int)fmin(Search_Period,Bars(Symbol(),Period()); //要复制的蜡烛数

在这种情况下,我们也许应该已经使用了一个变量:searchPeriod。对吗?

------

另一个问题,这条线路是做什么的?我发现,结构中的每个元素都会重置为零。而且我不明白,如果我们似乎刚刚写好这个数据,应该进一步使用它,那么我们在这里归零的是什么。

ZeroMemory(dataCandle); // 清除结构中的数据。


"像这样。int searchPeriod=(Search_Period<1)?0:Search_Period;"

不,不是的。这就是你的方式(字面意思):如果用户在设置中设置的Search_Period 小于1,那么searchPeriod 将等于0;否则,searchPeriod 将等于用户在设置中设置的Search_Period 的值。这是不正确的。我们不需要一个等于零的搜索范围。所以,如果这个范围被用户设置为0或小于0(小于1),那么我们将设置这个范围等于最小值--1。


"int copy_bars=(int)fmin(Search_Period,Bars(Symbol(),Period()); //要复制的烛台数
这里我们应该已经使用了变量:searchPeriod,对吗?"

是的,没错,有一个错字。


"另一个问题,这条线路是做什么的? 它的作用是什么?我发现,它把结构中的每个元素都重置为零。在这里我不明白,如果我们刚刚写了这个数据,并应该进一步使用它,那么我们为什么要归零呢?ZeroMemory(dataCandle); // 将结构中的数据归零"。

用数据 填充结构 之前,我们要做这件事。首先,我们把它归零,然后我们把它填满。看--我们在循环之前将其归零。然后在循环中,我们用数据填充该结构。