求助大神,自定义指标取值与重新赋值的问题

要添加评论,请登录注册
charfanta
30
charfanta  
想从指标Fractals.实验(2)中取正直赋给取值EA中的数组,可是在第一次复制后就不能再次重新赋值。数组内一直是相同的值是怎么回事?
附加的文件:
Ziheng Zhuang
30816
Ziheng Zhuang  

你应该先运行下指标,看看指标对不对。

Ziheng Zhuang
30816
Ziheng Zhuang  

你的指标有明显的问题,我帮你修改了下,见黄色部分。

//+------------------------------------------------------------------+
//|                                                  Fractals.实验.mq4 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot Up
#property indicator_label1  "Fractal Up"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot Down
#property indicator_label2  "Fractal Down"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrAqua
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- indicator buffers
extern int leftbars      = 5;
extern int rightbars     = 5;
double       Fractal_UpBuffer[];    //Fractals数组
double       Fractal_DownBuffer[];  //Fractals数组

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,Fractal_UpBuffer);        //Fractals数组编号绑定
   SetIndexBuffer(1,Fractal_DownBuffer);      //Fractals数组编号绑定
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
   SetIndexArrow(0,225);    //箭头形状设置
   SetIndexArrow(1,226);    //箭头形状设置
   
   SetIndexEmptyValue(0,0.0);
   SetIndexEmptyValue(1,0.0);
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   int i,j;
   int limit;
   int countup=0;
   int countdown=0;
//----计算范围限制,每次运算时limit都会重新取值大部分时候都会取值leftbars,从而实现从图标右边开始计算。      
   if(rates_total<leftbars+rightbars+1)   //K线总数小于11,此句决定了当总K数量达到11时才会启动计算
      return(0);
  // ArrayInitialize(Fractal_UpBuffer,0);
 //  ArrayInitialize(Fractal_DownBuffer,0);
   if(prev_calculated<leftbars+rightbars+1)  //计算过的K线总数小于11,此时的的K线数已经最少是11根。因为上一句IF
     {
      limit=leftbars;
     }
    else
     {
      limit=rates_total-(leftbars+rightbars+1);  //最少K线等于总K线数减11
     }
//---条件计算
    for(i=limit;i<=rates_total-leftbars-1;i++) 
     {
       for(j=1;j<=leftbars;j++)
        {   
          if(High[i]>High[i+j]) countup=countup+1;       //5K高点大于5+J K高点是计算高点之前5根K的高点
          if(Low[i]<Low[i+j]) countdown=countdown+1;     //5K低点小于5+J K低点是计算低点之前5根K的低点
        }
       for(j=1;j<=rightbars;j++)
        {
          if(High[i]>High[i-j]) countup=countup+1;       //5K高点大于5-J K高点是计算高点之后5根K的高点
          if(Low[i]<Low[i-j]) countdown=countdown+1;     //5K低点小于5+J K低点是计算低点之后5根K的低点
        }
//---Fractals数组赋值        
       if(countup==leftbars+rightbars) Fractal_UpBuffer[i]=High[i];  //如果CUP等于左右参数相加则,说明两个小循环都运行到了最高值,并且都加在了Cup上面,形成高点。
       else Fractal_UpBuffer[i]=0;                                   //在不满足分型成立条件时,Fractals值为0
       if(countdown==leftbars+rightbars) Fractal_DownBuffer[i]=Low[i];  //如果Cdown等于左右参数相加则,说明两个小循环都运行到了最高值,并且都加在了Cdown上面,形成低点。          
       else Fractal_DownBuffer[i]=0;                                    //在不满足分型成立条件时,Fractals值为0
       countup=0;
       countdown=0;          
     }
    printf("低点="+Fractal_UpBuffer[0]);
    printf("高点="+Fractal_DownBuffer[0]);  
     return(rates_total);
  }

charfanta
30
charfanta  
Ziheng Zhuang:

你的指标有明显的问题,我帮你修改了下,见黄色部分。

万分感谢。这样编写后的确更为合理。可是取值时还时未能在新的正值出现时,付给数组。是我的取值循编写有问题吧?
Ziheng Zhuang
30816
Ziheng Zhuang  
//+------------------------------------------------------------------+
//|                                                         取值.mq4 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

input int leftbars= 5;
input int rightbars= 5;
input int 计算范围=100;
input int 数组限制范围=1;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double Fractalsup(int shift)
{
   return(iCustom(NULL,PERIOD_M5,"Fractals.实验(2)",leftbars,rightbars,0,shift));
}
double Fractalsdown(int shift)
{
   return(iCustom(NULL,PERIOD_M5,"Fractals.实验(2)",leftbars,rightbars,1,shift));
}


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
//--- create timer
   EventSetTimer(60);
//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{

}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
//---
   double   Frac_up[],Frac_down[];
   int      upzhu[],downzhu[];

   int upjs = 0;
   for(int i=0; i<计算范围 && upjs < 数组限制范围; i++)
   {
      if(Fractalsup(i)>0)
      {
         upjs++;
         ArrayResize(Frac_up,upjs);
         ArrayResize(upzhu,upjs);
         Frac_up[upjs-1]=Fractalsup(i);
         upzhu[upjs-1]=i;
      }
   }

   int downjs = 0;
   for(int i=0; i<计算范围 && downjs<数组限制范围; i++)
   {
      if(Fractalsdown(i)>0)
      {
         downjs++;
         ArrayResize(Frac_down,downjs);
         ArrayResize(downzhu,downjs);
         Frac_down[downjs-1]=Fractalsdown(i);
         downzhu[downjs-1]=i;
      }
   }

   if(downjs>0)printf("低点="+Frac_down[0]+"  "+"up柱数="+downzhu[0]);
   if(upjs>0)printf("高点="+Frac_up[0]+"  "+"down柱数="+upzhu[0]);


}
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
{
//---

}
//+------------------------------------------------------------------+
//| Tester function                                                  |
//+------------------------------------------------------------------+
double OnTester()
{
//---
   double ret=0.0;
//---

//---
   return(ret);
}
//+------------------------------------------------------------------+
通过MQL5社区和服务探索MetaTrader 5的新机遇
通过MQL5社区和服务探索MetaTrader 5的新机遇
  • www.mql5.com
The provided robot is a result of several years of trading and research on thousands of strategies, various indicators of forecasting, aimed at creating the science of online trading engineering. By combining several strategies and algorithms in this robot, at changing each tick price, with the utmost precision and speed,  whatever a trader...
charfanta
30
charfanta  
Ziheng Zhuang:
感谢大神指点,问题已经解决了。还想在问下,取正值的循环如您这样写的优点是什么呢?我的编写是哪里的问题呀?
Ziheng Zhuang
30816
Ziheng Zhuang  

这么简单的问题,还谈不上什么写法优点缺点的。

你的问题是,要找的fractal的个数随行情变化,个数是变化的,因此要用动态数组,不能用静态数组。

charfanta
30
charfanta  
感谢大神指点,终于解了。搞了好几天没找出原因。
charfanta
30
charfanta  
Ziheng Zhuang:

这么简单的问题,还谈不上什么写法优点缺点的。

你的问题是,要找的fractal的个数随行情变化,个数是变化的,因此要用动态数组,不能用静态数组。

还想再追问下,我发现指标有时会发生不显示的情况,重新加载后才会在新的K线上显示。想问下这是什么情况呢。

Ziheng Zhuang
30816
Ziheng Zhuang  
charfanta:

还想再追问下,我发现指标有时会发生不显示的情况,重新加载后才会在新的K线上显示。想问下这是什么情况呢。

指标加载完成一次计算后,limit的值是多少?

应该是没有计算新的K线,你检查limit的值,看看每次循环for到底计算了哪些K线,是不是符合你的设计逻辑。

charfanta
30
charfanta  
Ziheng Zhuang:

指标加载完成一次计算后,limit的值是多少?

应该是没有计算新的K线,你检查limit的值,看看每次循环for到底计算了哪些K线,是不是符合你的设计逻辑。

谢谢指点。。我在研究研究。

要添加评论,请登录注册