新人对MQL4和MQL5的任何问题,对算法和代码的帮助和讨论 - 页 848

 
Artyom Trishkin:

什么计算方法?

Last_Arrow_Buy_volume = Buf_Arrow_Buy[n];
Last_Arrow_Buy_index  = n;

明白了,问题删除了,我听了你的话,意识到在OnTick()区域声明一个变量 是不好的,计算也可以在循环内进行。

完成了,一切正常,谢谢,现在我只需要得到价格。非常感谢你。

void OnTick()
  {
   if (CopyBuffer(CrossAD, 1, 0, period_find, Buf_Arrow_Buy) != period_find)
      {  
         Print("НЕ удалось правильно скопировать данные из 1-го буфера индикатора iCrossAD, error code %d",GetLastError());
         return;
      }
         for(int n=0; n<(int)period_find; n++)
            {
               if(Buf_Arrow_Buy[n]!=EMPTY_VALUE)
               {
                  Last_Arrow_Buy_volume = Buf_Arrow_Buy[n];
                  Last_Arrow_Buy_index  = n;
                  Print("Last_Arrow_Buy_volume = ",Last_Arrow_Buy_volume,", Last_Arrow_Buy_index = ",Last_Arrow_Buy_index);
                  break;
               }   
            }
         
   if (CopyBuffer(CrossAD, 2, 0, period_find, Buf_Arrow_Sell) != period_find)
      {  
         Print("НЕ удалось правильно скопировать данные из 2-го буфера индикатора iCrossAD, error code %d",GetLastError());
         return;
      }
         for(int n=0; n<(int)period_find; n++)
            {
               if(Buf_Arrow_Sell[n]!=EMPTY_VALUE)
               {
                  Last_Arrow_Sell_volume = Buf_Arrow_Sell[n];
                  Last_Arrow_Sell_index  = n;
                  Print("Last_Arrow_Sell_volume = ",Last_Arrow_Sell_volume,", Last_Arrow_Sell_index = ",Last_Arrow_Sell_index);
                  break;
               }
            }
      
Comment("-------------------------", 
         "\n Last_Arrow_Buy_volume     = ",Last_Arrow_Buy_volume,
         "\n Last_Arrow_Buy_index        = ",Last_Arrow_Buy_index,
         "\n ---------------------- ",
         "\n Last_Arrow_Sell_volume     = ",Last_Arrow_Sell_volume,
         "\n Last_Arrow_Sell_index        = ",Last_Arrow_Sell_index
         ); 
  }
 
Sergey Voytsekhovsky:

明白了,问题删除了,我听了你的话,意识到在OnTick()区域声明一个变量 是不好的,计算也可以在循环内进行。

完成了,一切正常,谢谢,现在我只需要获得价格。非常感谢你。

这不是循环内部的计算,而是获取必要的数据,这也是这个循环的设计目的。

但应避免在循环内进行计算。

事实上,你需要立即重新考虑你的方法。你在循环中获得数据。你在第一次遇到数据后中断了循环。所有这些完全是在函数的概念之内--返回请求的结果。

把整个循环变成一个函数,如果找到了,就返回酒吧的号码,如果没有找到,就返回WRONG_VALUE-。

那么:n=Func()

并在不等于WRONG_VALUE的情况下,将n 用于其预期目的。用一个 "会说话 "的名字来命名这个函数。而且OnTick()中的代码会更短,逻辑也会更清晰--它不会被不必要的东西所重载。

 
Artyom Trishkin:

将整个循环形成一个函数,如果找到了,就从中返回酒吧号码,如果没有找到,就返回WRONG_VALUE。

然后:n=Func()

然后按照预期使用n ,如果它不等于WRONG_VALUE。用一个 "告诉 "的名字来命名这个函数。而且OnTick()中的代码会更短,逻辑会更清晰--它不会被不必要的东西所重载。

你要活到老,学到老。非常有趣,我晚上会试试。同时,作为一份报告并非常感谢您,我得到了来自..... 的价格。这是个好日子 :-))

void OnTick()
  {
   if (CopyBuffer(CrossAD, 1, 0, period_find, Buf_Arrow_Buy) != period_find)
      {  
         Print("НЕ удалось правильно скопировать данные из 1-го буфера индикатора iCrossAD, error code %d",GetLastError());
         return;
      }
         for(int n=0; n<(int)period_find; n++)
            {
               if(Buf_Arrow_Buy[n]!=EMPTY_VALUE)
               {
                  Last_Arrow_Buy_volume = iOpen(_Symbol,_Period,n);
                  Last_Arrow_Buy_index  = n;
                  Print("Last_Arrow_Buy_volume = ",Last_Arrow_Buy_volume,", Last_Arrow_Buy_index = ",Last_Arrow_Buy_index);
                  break;
               }   
            }
         
   if (CopyBuffer(CrossAD, 2, 0, period_find, Buf_Arrow_Sell) != period_find)
      {  
         Print("НЕ удалось правильно скопировать данные из 2-го буфера индикатора iCrossAD, error code %d",GetLastError());
         return;
      }
         for(int n=0; n<(int)period_find; n++)
            {
               if(Buf_Arrow_Sell[n]!=EMPTY_VALUE)
               {
                  Last_Arrow_Sell_volume = iOpen(_Symbol,_Period,n);
                  Last_Arrow_Sell_index  = n;
                  Print("Last_Arrow_Sell_volume = ",Last_Arrow_Sell_volume,", Last_Arrow_Sell_index = ",Last_Arrow_Sell_index);
                  break;
               }
            }
      
Comment("-------------------------", 
         "\n Last_Arrow_Buy_volume     = ",Last_Arrow_Buy_volume,
         "\n Last_Arrow_Buy_index        = ",Last_Arrow_Buy_index,
         "\n ---------------------- ",
         "\n Last_Arrow_Sell_volume     = ",Last_Arrow_Sell_volume,
         "\n Last_Arrow_Sell_index        = ",Last_Arrow_Sell_index
         ); 
  }
 

能帮助解决这个问题的人https://www.mql5.com/ru/forum/160683/page845#comment_11741857--评论已经走远了吗?还有一个问题,如何创建一个可以双向工作的循环,如果你需要运行增量,如果你需要运行减量?下面是一个关于如何工作的示例方案。

如果(有事发生){给变量赋值},那么循环将被调用,它将包含一个主体,其计算结果将取决于循环的值

for( 根据上面的变量增加或减少)

{正文}

没有办法重复这个条件,必须在循环中指定一次。是否可以这样做。

 

又是晚上好。我还没有把它变成一个函数,但我有一个新问题。在附图中,你可以看到有一个新的信号(箭头),但在评论中,你可以看到EA不接受它,它仍然认为11个蜡烛前的信号是终极上升箭头。

https://www.mql5.com/ru/charts/10181812/eurusd-h1-alpari-international

График EURUSD, H1, 2019.05.20 18:00 UTC, Alpari International, MetaTrader 5, Real
График EURUSD, H1, 2019.05.20 18:00 UTC, Alpari International, MetaTrader 5, Real
  • www.mql5.com
Символ: EURUSD. Период графика: H1. Брокер: Alpari International. Торговая платформа: MetaTrader 5. Режим торговли: Real. Дата: 2019.05.20 18:00 UTC.
 
Sergey Voytsekhovsky:

11支蜡烛前。

事实证明,该指标已经给出了一个新的信号(它在图表上与EA平行运行),除了OnTick之外,EA中没有其他事件,而且EA没有看到这个信号。

 
Sergey Voytsekhovsky:

又是晚上好。我还没有把它变成一个函数,但我有一个新问题。在附图中,你可以看到有一个新的信号(箭头),但在评论中,你可以看到EA不接受它,它仍然认为11个蜡烛前的信号是终极上升箭头。

https://www.mql5.com/ru/charts/10181812/eurusd-h1-alpari-international

当找到一个非空的缓冲区值时,在日志中打印出n条对应的时间。你会看到它找到的是哪个酒吧。这很可能是你把缓冲区读反了。

 

请告知为什么EA看不到箭头,它已经在指标缓冲区,从数据窗口来看,EA询问了OnTick,它还需要什么?上面有完整的代码。


 
Artyom Trishkin:

当找到一个非空的缓冲区值时,在日志中打印出n条对应的时间。它将显示它找到的是哪一个酒吧。这很可能是你把缓冲区读反了。

好了,完成了,时间显示的是正确的时间,即箭头所在的蜡烛的开启时间。代码附后,图片附后。

void OnTick()
  {
   if (CopyBuffer(CrossAD, 1, 0, period_find, Buf_Arrow_Buy) != period_find)
      {  
         Print("НЕ удалось правильно скопировать данные из 1-го буфера индикатора iCrossAD, error code %d",GetLastError());
         return;
      }
         for(int n=0; n<(int)period_find; n++)
            {
               if(Buf_Arrow_Buy[n]!=EMPTY_VALUE)
               {
                  Last_Arrow_Buy_volume = iOpen(_Symbol,_Period,n);
                  Last_Arrow_Buy_time   = iTime(_Symbol,0,n);
                  Last_Arrow_Buy_index  = n;
                  Print("Last_Arrow_Buy_volume = ",Last_Arrow_Buy_volume,", Last_Arrow_Buy_index = ",Last_Arrow_Buy_index,", Last_Arrow_Buy_time = ",Last_Arrow_Buy_time);
                  break;
               }   
            }
         
   if (CopyBuffer(CrossAD, 2, 0, period_find, Buf_Arrow_Sell) != period_find)
      {  
         Print("НЕ удалось правильно скопировать данные из 2-го буфера индикатора iCrossAD, error code %d",GetLastError());
         return;
      }
         for(int n=0; n<(int)period_find; n++)
            {
               if(Buf_Arrow_Sell[n]!=EMPTY_VALUE)
               {
                  Last_Arrow_Sell_volume = iOpen(_Symbol,_Period,n);
                  Last_Arrow_Sell_time   = iTime(_Symbol,0,n);
                  Last_Arrow_Sell_index  = n;
                  Print("Last_Arrow_Sell_volume = ",Last_Arrow_Sell_volume,", Last_Arrow_Sell_index = ",Last_Arrow_Sell_index,", Last_Arrow_Buy_time = ",Last_Arrow_Buy_time);
                  break;
               }
            }
      
Comment("-------------------------", 
         "\n Last_Arrow_Buy_volume     = ",Last_Arrow_Buy_volume,
         "\n Last_Arrow_Buy_index        = ",Last_Arrow_Buy_index,
         "\n ---------------------- ",
         "\n Last_Arrow_Sell_volume     = ",Last_Arrow_Sell_volume,
         "\n Last_Arrow_Sell_index        = ",Last_Arrow_Sell_index
         ); 
  }


 

如果你看不到图片中的日志,这里有一些极端的条目


2019.05.20 21:42:52.601 Test_iCustom (EURUSD,H1) Last_Arrow_Sell_volume = 1.12112, Last_Arrow_Sell_index = 56.0, Last_Arrow_Buy_time = 2019.05.20 10:00:00

2019.05.20 21:42:53.824 Test_iCustom (EURUSD,H1) Last_Arrow_Buy_volume = 1.11544, Last_Arrow_Buy_index = 11.0, Last_Arrow_Buy_time = 2019.05.20 10:00:00

2019.05.20 21:42:53.824 Test_iCustom (EURUSD,H1) Last_Arrow_Sell_volume = 1.12112, Last_Arrow_Sell_index = 56.0, Last_Arrow_Buy_time = 2019.05.20 10:00:00

2019.05.20 21:43:00.819 Test_iCustom (EURUSD,H1) Last_Arrow_Buy_volume = 1.11544, Last_Arrow_Buy_index = 11.0, Last_Arrow_Buy_time = 2019.05.20 10:00:00

2019.05.20 21:43:00.819 Test_iCustom (EURUSD,H1) Last_Arrow_Sell_volume = 1.12112, Last_Arrow_Sell_index = 56.0, Last_Arrow_Buy_time = 2019.05.20 10:00:00

2019.05.20 21:43:03.021 Test_iCustom (EURUSD,H1) Last_Arrow_Buy_volume = 1.11544, Last_Arrow_Buy_index = 11.0, Last_Arrow_Buy_time = 2019.05.20 10:00:00

2019.05.20 21:43:03.021 Test_iCustom (EURUSD,H1) Last_Arrow_Sell_volume = 1.12112, Last_Arrow_Sell_index = 56.0, Last_Arrow_Buy_time = 2019.05.20 10:00:00

2019.05.20 21:43:03.621 Test_iCustom (EURUSD,H1) Last_Arrow_Buy_volume = 1.11544, Last_Arrow_Buy_index = 11.0, Last_Arrow_Buy_time = 2019.05.20 10:00:00

2019.05.20 21:43:03.622 Test_iCustom (EURUSD,H1) Last_Arrow_Sell_volume = 1.12112, Last_Arrow_Sell_index = 56.0, Last_Arrow_Buy_time = 2019.05.20 10:00:00

2019.05.20 21:43:10.696 Test_iCustom (EURUSD,H1) Last_Arrow_Buy_volume = 1.11544, Last_Arrow_Buy_index = 11.0, Last_Arrow_Buy_time = 2019.05.20 10:00:00

2019.05.20 21:43:10.696 Test_iCustom (EURUSD,H1) Last_Arrow_Sell_volume = 1.12112, Last_Arrow_Sell_index = 56.0, Last_Arrow_Buy_time = 2019.05.20 10:00:00


原因: