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

 
Алексей КоКоКо:
请教mql5,我想收集12小时蜡烛的平均大小的统计数据,例如今天是星期五,我想从星期四、星期三、星期二和星期一的数据中进行计算。
我把时间[]转换成TimeToString,然后SplitString和TD。 有没有一种更快更省事的方法来引用一天或更久以前的同一时间蜡烛?当然,我可以用区间来运行当前时间框架在一天内的烛台数量,但我担心如果有一些空隙的报价,它们会被移位。

我认为我们不能没有循环。但这个循环可以用不同的方式建立。请注意CopyRates()

int  CopyRates(
   string           symbol_name,       // имя символа
   ENUM_TIMEFRAMES  timeframe,         // период
   datetime         start_time,        // с какой даты
   int              count,             // сколько копируем
   MqlRates         rates_array[]      // массив, куда будут скопированы данные
   );

我们设置PERIOD_H1,start_time - date 12:00,计数1。在下一次迭代中,我们在这个日期上做加法或减法(取决于循环的组织方式) PeriodSeconds(PERIOD_D1)

 
MAKSIM KASHFYLGAIANOV:
请告诉我如何在iCustom中列举指标参数。
你可以设置一个空字符串来代替字符串变量,但是枚举......我们需要看看它们在代码中是怎么写的。
 

我有一个带有过敏性的随机指标,它在移动线的交叉处给出信号。请告诉我如何使它在高于或低于超买或超卖区时才发出信号,越过滑点。

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 LightSeaGreen
#property indicator_color2 Red
#property indicator_color3 Blue
#property indicator_color4 Red
#property indicator_level1 80
#property indicator_level2 20
#property indicator_maximum 100
#property indicator_minimum 0

//---- input parameters
/*************************************************************************
PERIOD_M1   1
PERIOD_M5   5
PERIOD_M15  15
PERIOD_M30  30 
PERIOD_H1   60
PERIOD_H4   240
PERIOD_D1   1440
PERIOD_W1   10080
PERIOD_MN1  43200
You must use the numeric value of the timeframe that you want to use
when you set the TimeFrame' value with the indicator inputs.
---------------------------------------
MODE_SMA    0 Simple moving average, 
MODE_EMA    1 Exponential moving average, 
MODE_SMMA   2 Smoothed moving average, 
MODE_LWMA   3 Linear weighted moving average. 
You must use the numeric value of the MA Method that you want to use
when you set the 'ma_method' value with the indicator inputs.

**************************************************************************/
extern int TimeFrame=240;
extern int KPeriod=5;
extern int DPeriod=3;
extern int Slowing=3;
extern int MAMethod=0;
extern int PriceField=0;// PriceField:  0=Hi/Low   1=Close/Close

extern string note_TimeFrames = "M1;5,15,30,60H1;240H4;1440D1;10080W1;43200MN";
extern string __MA_Method = "SMA0 EMA1 SMMA2 LWMA3";
extern string __PriceField = "0=Hi/Low   1=Close/Close";
//extern string __Price = "0O,1C 2H3L,4Md 5Tp 6WghC: Md(HL/2)4,Tp(HLC/3)5,Wgh(HLCC/4)6";


double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];

datetime last_t=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicator line
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexStyle(1,DRAW_LINE,STYLE_DOT);
   SetIndexLabel(0,  "MTF_Stochastic("+KPeriod+","+DPeriod+","+Slowing+")TF"+TimeFrame+"");
   SetIndexLabel(1,"MTF_Stochastic("+KPeriod+","+DPeriod+","+Slowing+")TF"+TimeFrame+"");
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexArrow(2,233);
   SetIndexBuffer(3,ExtMapBuffer4);
   SetIndexStyle(3,DRAW_ARROW);
   SetIndexArrow(3,234);

//---- name for DataWindow and indicator subwindow label   
   switch(TimeFrame)
   {
      case 1 : string TimeFrameStr="Period_M1"; break;
      case 5 : TimeFrameStr="Period_M5"; break;
      case 15 : TimeFrameStr="Period_M15"; break;
      case 30 : TimeFrameStr="Period_M30"; break;
      case 60 : TimeFrameStr="Period_H1"; break;
      case 240 : TimeFrameStr="Period_H4"; break;
      case 1440 : TimeFrameStr="Period_D1"; break;
      case 10080 : TimeFrameStr="Period_W1"; break;
      case 43200 : TimeFrameStr="Period_MN1"; break;
      default : TimeFrameStr="Current Timeframe";
   } 
   IndicatorShortName("MTF_Stochastic("+KPeriod+","+DPeriod+","+Slowing+") "+TimeFrameStr);  
   start();
   return(0);
  }
//----
   
 
//+------------------------------------------------------------------+
//| MTF Stochastic                                                   |
//+------------------------------------------------------------------+
 int deinit()
  {
   for (int i=Bars;i>=0;i--){
      ObjectDelete("st"+Symbol()+Period()+DoubleToStr(KPeriod+DPeriod+Slowing,0)+DoubleToStr(i,0));
   }
   return(0);
  }

bool up_a=false;
bool dn_a=false;
int start()
  {
   datetime TimeArray[];
   ArrayResize(TimeArray,Bars);
   int    i,limit,y=0,counted_bars=IndicatorCounted();
    
// Plot defined timeframe on to current timeframe   
   ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),TimeFrame); 
   
 //  limit=Bars-counted_bars+TimeFrame/Period(); //igorad
limit=Bars-1;
limit=MathMax(limit,TimeFrame/Period());
//limit=MathMin(limit,BarsToCount);
   for(i=0,y=0;i<limit;i++)
   {
   if (Time[i]<TimeArray[y]) y++; 
   
 /***********************************************************   
   Add your main indicator loop below.  You can reference an existing
      indicator with its iName  or iCustom.
   Rule 1:  Add extern inputs above for all neccesary values   
   Rule 2:  Use 'TimeFrame' for the indicator timeframe
   Rule 3:  Use 'y' for the indicator's shift value
 **********************************************************/  
   ExtMapBuffer3[i]=EMPTY_VALUE;  
   ExtMapBuffer4[i]=EMPTY_VALUE;
   ExtMapBuffer1[i]=EMPTY_VALUE;  
   ExtMapBuffer2[i]=EMPTY_VALUE;

   ExtMapBuffer1[i]=iStochastic(NULL,TimeFrame,KPeriod,DPeriod,Slowing,MAMethod,PriceField,0,y);
   ExtMapBuffer2[i]=iStochastic(NULL,TimeFrame,KPeriod,DPeriod,Slowing,MAMethod,PriceField,1,y);
   ObjectDelete("st"+Symbol()+Period()+DoubleToStr(KPeriod+DPeriod+Slowing,0)+DoubleToStr(i,0));   

   if (NormalizeDouble(ExtMapBuffer1[i],Digits)>NormalizeDouble(ExtMapBuffer2[i],Digits) && NormalizeDouble(ExtMapBuffer1[i+1],Digits)<=NormalizeDouble(ExtMapBuffer2[i+1],Digits)  && NormalizeDouble(ExtMapBuffer1[i+1],Digits)!=NormalizeDouble(ExtMapBuffer1[i],Digits) ){
      ExtMapBuffer3[i]=ExtMapBuffer1[i];
      ObjectDelete("st"+Symbol()+Period()+DoubleToStr(KPeriod+DPeriod+Slowing,0)+DoubleToStr(i,0));
      ObjectCreate("st"+Symbol()+Period()+DoubleToStr(KPeriod+DPeriod+Slowing,0)+DoubleToStr(i,0),22,0,Time[i],Low[i]-5*Point);
      ObjectSet("st"+Symbol()+Period()+DoubleToStr(KPeriod+DPeriod+Slowing,0)+DoubleToStr(i,0),6,Blue);
      ObjectSet("st"+Symbol()+Period()+DoubleToStr(KPeriod+DPeriod+Slowing,0)+DoubleToStr(i,0),14,233);      
   }
   if (NormalizeDouble(ExtMapBuffer1[i],Digits)<NormalizeDouble(ExtMapBuffer2[i],Digits) && NormalizeDouble(ExtMapBuffer1[i+1],Digits)>=NormalizeDouble(ExtMapBuffer2[i+1],Digits)  && NormalizeDouble(ExtMapBuffer1[i+1],Digits)!=NormalizeDouble(ExtMapBuffer1[i],Digits)){
      ExtMapBuffer4[i]=ExtMapBuffer1[i];
      ObjectDelete("st"+Symbol()+Period()+DoubleToStr(KPeriod+DPeriod+Slowing,0)+DoubleToStr(i,0));
      ObjectCreate("st"+Symbol()+Period()+DoubleToStr(KPeriod+DPeriod+Slowing,0)+DoubleToStr(i,0),22,0,Time[i],High[i]+5*Point);
      ObjectSet("st"+Symbol()+Period()+DoubleToStr(KPeriod+DPeriod+Slowing,0)+DoubleToStr(i,0),6,Red);
      ObjectSet("st"+Symbol()+Period()+DoubleToStr(KPeriod+DPeriod+Slowing,0)+DoubleToStr(i,0),14,234);      
   }
   }  
     
//
   //----  Refresh buffers ++++++++++++++ 
   if (TimeFrame < Period()) TimeFrame = Period();
   if (TimeFrame>Period()) {
     int PerINT=TimeFrame/Period()+1;
     datetime TimeArr[]; ArrayResize(TimeArr,PerINT);
     ArrayCopySeries(TimeArr,MODE_TIME,Symbol(),Period()); 
     for(i=0;i<PerINT+1;i++) {if (TimeArr[i]>=TimeArray[0]) {
 /********************************************************     
    Refresh buffers:         buffer[i] = buffer[0];
 ************************************************************/  

   ExtMapBuffer1[i]=ExtMapBuffer1[0];
   ExtMapBuffer2[i]=ExtMapBuffer2[0];

   } } }
//+++++++++++++++++++++++++++++++++++++++++++++++++++++   Raff 

   if (ExtMapBuffer3[0]!=EMPTY_VALUE && !up_a){
      up_a=true;
      dn_a=false;    
      Alert("MTF Stochastic Long cross Signal on "+Symbol());
   }
   if (ExtMapBuffer4[0]!=EMPTY_VALUE && !dn_a){
      dn_a=true;
      up_a=false;    
      Alert("MTF Stochastic Short cross Signal on "+Symbol());
   }

   return(0);
  }
 

大家好。
请告诉我如何使用我不记得的数学函数正确编码下面的条件。

if (Bid - Low[1]>=0.0030 &&Bid - Low[1]<0.0035) {action;}
我知道有一个数学函数可以在上述条件中使用,没有&& 符号。但我不记得这个数学函数叫什么,如何应用它。
谢谢你的帮助。

 
这个问题似乎是来自MQL4的基础知识。假设一些变量X、Y、Z被声明在全局范围内。然后,程序调用F1()函数来获得X值。虽然这个函数只返回X,但Y和Z变量是在其运行过程中计算出来的,也就是说,它们被分配了一些以后不会改变的值。再往下看,Y和Z又被用在某个函数F2()中。为了计算这些变量,我们是否应该再次处理F1()函数,不仅提供X的返回,而且提供Y和Z的返回?或者Y和Z已经在全局范围内被覆盖,并将以改变的形式自动插入F2()函数中?
 
Oleksandr Nozemtsev:

如果你在一个函数中改变一个全局变量,它就会改变。但这是一种危险的编程方式,因为在代码中,随着代码的增长,在程序的不同功能中会有不明显的赋值。

程序中有一个主函数,那是你进行全局变量 赋值的地方。而在其他职能部门,要这样做。

int X, Y, Z;

void OnTick()
   {
   X=Sum(Y,Z);
   }

int Sum(int y, int z)
   {
   return(y+z);
   }

或者像这样。

int X, Y, Z;

void OnTick()
   {
   Replace(X,Y,Z);
   }

void Replace(int & x, int & y, int & z)
   {
   int a=x;
   x=y;
   y=z;
   z=a;
   }
 
ANDREY:

如何使用我不记得的数学函数正确编码以下条件?

我不知道有什么更好的方法来设置这个条件

 
ANDREY:

大家好。
请告诉我如何使用我不记得的数学函数正确编码下面的条件。

if (Bid - Low[1]>=0.0030 &&Bid - Low[1]<0.0035) {action;}
我知道有一个数学函数可以用在上述条件中,没有&& 符号,程序会在4点范围内检查价格是否一致但我不记得这个数学函数叫什么,如何应用它。
谢谢你的帮助。

if(Bid - Low[1] >= 0.0030))
  {
   if(Bid - Low[1] < 0.0035)
     {
      действие;
     }
  }
没有和
 
Александр:
没有和

非常感谢你的提示。

 
你能告诉我为什么程序(在mql4上,在分钟上,所有的ticks上)明确读出的数字和函数中计算的同一数字不同?
以下是代码
double Pr,Lt1;
int Tick,H;
void OnTick()
{
Tick++;
if (Tick>15240&&Tick<15821)
{
Pr=iLow( NULL ,PERIOD_H4,0)+0.0030;
Print("--------- 0 ---------=     ",DoubleToString(Pr,5) );
if ((H!=Hour()&&Bid - iLow( NULL ,PERIOD_H1,1)>=0.0030&&Lt1!=Pr )||Bid==1.60854)
{
OrderSend(Symbol(),OP_SELL,2,Bid, 3,Ask+300*Point,Ask-100*Point,"300",0);
Lt1=Bid;
H=Hour();
}
}
}

尽管Print()显示Pr 的值为1.608 54,但程序并没有将Pr读为1.60854结果,程序打开了第二个订单,但它不应该这样做


而如果我们设置数字1.60854 而不是Pr 变量 ,程序就会读取它而不打开 第二单。

double Pr,Lt1;
int Tick,H;
void OnTick()
{
Tick++;
if (Tick>15240&&Tick<15821)
{
Pr=iLow( NULL ,PERIOD_H4,0)+0.0030;
Print("--------- 0 ---------=     ",DoubleToString(Pr,5) );
if ((H!=Hour()&&Bid - iLow( NULL ,PERIOD_H1,1)>=0.0030&&Lt1!= 1.60854)||Bid==1.60854)
{
OrderSend(Symbol(),OP_SELL,2,Bid, 3,Ask+300*Point,Ask-100*Point,"300",0);
Lt1=Bid;
H=Hour();
}
}
}

问题 我们应该在代码中做什么修改,使程序读取Pr而不打开 第二单。
谢谢
你的帮助。

原因: