Questions des débutants MQL4 MT4 MetaTrader 4 - page 223

 

J'essaie de remplir le tampon de l'indicateur depuis l'autre extrémité (ArraySetAsSeries (..., false)), tout fonctionne bien pendant un moment, mais ensuite quelque chose se passe dans le tampon et le tout dernier index du tableau est rempli, alors qu'il ne devrait pas l'être.
Je suppose que le terminal coupe le tampon à un moment donné et que le poids du tampon est rempli même avec le dernier index du tableau. Peut-être que la mémoire tampon du tableau doit être libérée à un moment donné, mais quand ? Pouvez-vous corriger l'exemple de test ?


#property indicator_buffers 1

double ExtMapBuffer[]; // AQUA   "Line"
int      LastData;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping

   SetIndexBuffer(0,ExtMapBuffer);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,7,clrAqua);
   SetIndexEmptyValue(0,0.0);
   SetIndexLabel(0,"Line");

   LastData=0;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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;
   bool Series=false;
   ArraySetAsSeries(ExtMapBuffer,Series);
   ArraySetAsSeries(high,Series);

   i=prev_calculated-1;

   if(i<1) 
     {
      LastData=0;
      i=1;
      while(i<rates_total-1)
        {
         ExtMapBuffer[i]=high[i];
         i++; 
         LastData=i;
        }
     }
   else
     {
      i=LastData;
     }

   while(i<rates_total-1)
     {
      ExtMapBuffer[i]=high[i];
      i++;
      LastData=i;
     }

   return(rates_total);
  }
//+------------------------------------------------------------------+
 

Bonjour.

Pouvez-vous me dire pourquoi cet indicateur dessine rarement des flèches sur l'historique, mais quand vous commencez à trader, il commence à en dessiner souvent.

Ensuite, si vous fermez MT4 et que vous l'ouvrez à nouveau, à l'endroit même où, en temps réel, l'indicateur, comme une mitraillette, tirait des transactions, il y a à nouveau 1-2 transactions.

Lorsque je regarde l'historique, il semble que l'indicateur devrait bien fonctionner, mais dès que je passe au trading en temps réel, j'obtiens toutes sortes de choses bizarres.

Veuillez expliquer pourquoi ? Voici le code de l'indicateur :

//---- indicator settings
#property  indicator_chart_window
#property  indicator_buffers 2
#property  indicator_color1  Blue
#property  indicator_color2  Red
//---- indicator parameters
extern int  period = 4; //12
extern int  shift  = 0; //сдвиг по бару
//---- indicator buffers
double BufferUp[],BufferDn[];
int q,st=5;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorBuffers(2);
//---- drawing settings
   SetIndexStyle(0,DRAW_ARROW,2);
   SetIndexArrow(0,233);
   SetIndexStyle(1,DRAW_ARROW,2);
   SetIndexArrow(1,234);

   SetIndexBuffer(0,BufferUp);//стрелка синяя верх
   SetIndexBuffer(1,BufferDn);//стрелка красная вниз
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("T3MA-ALARM ("+period+")");
//---- initialization done
   if(Digits==3 || Digits==5) q=10;
   st=st*q;
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit()
  {
   ObjectDelete("low");
   ObjectDelete("high");
  }
//+----------------------------------------------------------------------+
//| Moving Average of Oscillator                                         |
//+----------------------------------------------------------------------+
int start()
  {
//---- ArraySetAsSeries --------------------------------------------------  
   double Ma[],MaOn[];
   double y0[],y1[],y2[];
//   int i;
   int    i;
   ArraySetAsSeries(Ma,true);
//---- IndicatorCounted --------------------------------------------------
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   int limit1=Bars-counted_bars;
   if(counted_bars==0) limit1-=1+MathMax(period,shift)+2;

   int buffer_size=ArraySize(BufferUp);
   ArrayResize(Ma,buffer_size);
   ArrayResize(MaOn,buffer_size);

   ArrayResize(y0,buffer_size+shift+2);
   ArrayResize(y1,buffer_size+shift+2);
   ArrayResize(y2,buffer_size+shift+2);

   int limit=ArraySize(Ma);
//---- EMA --------------------------------------------------------------- 
   for(i=limit1; i>=0; i--)  Ma[i]  =iMA(NULL,0,period,0,MODE_EMA,PRICE_CLOSE,i);
   for(i=limit1; i>=0; i--)  MaOn[i]=iMAOnArray(Ma,limit,period,0,MODE_EMA,i);

   for(i=limit1; i>=0; i--)
     {
      y0[i+shift]=MaOn[i+shift];
      y1[i+1+shift]=MaOn[i+1+shift];
      y2[i+2+shift]=MaOn[i+2+shift];

      if(y0[i+shift]-y1[i+1+shift]<0 && y1[i+1+shift]-y2[i+2+shift]>0){BufferDn[i+1]=High[i+1]+st*Point;}
      if(y0[i+shift]-y1[i+1+shift]>0 && y1[i+1+shift]-y2[i+2+shift]<0){BufferUp[i+1]=Low[i+1]-st*Point;}
      //---- Signal Trend Up || Dn ---------------------------------------------   
      if(y0[i]-y1[i+1]>0) Comment("\n SWAPLONG = ",MarketInfo(Symbol(),MODE_SWAPLONG),
         "   SWAPSHORT = ",MarketInfo(Symbol(),MODE_SWAPSHORT),"\n BUY TREND ",DoubleToStr(Close[i],Digits));

      else if(y0[i]-y1[i+1]<0) Comment("\n SWAPLONG = ",MarketInfo(Symbol(),MODE_SWAPLONG),
         "   SWAPSHORT = ",MarketInfo(Symbol(),MODE_SWAPSHORT),"\n SELL TREND ",DoubleToStr(Close[i],Digits));
     }

//---- done
   return(0);
  }
//+---------------------------------------------------------------------+
Документация по MQL5: Константы, перечисления и структуры / Константы индикаторов / Стили рисования
Документация по MQL5: Константы, перечисления и структуры / Константы индикаторов / Стили рисования
  • www.mql5.com
При создании пользовательского индикатора можно указать один из 18 типов графического построения (способа отображения на главном окне графика или в подокне графика), значения которых указаны в перечислении ENUM_DRAW_TYPE. В зависимости от стиля рисования, может потребоваться от одного до четырех буферов значений (отмеченных как INDICATOR_DATA...
 

essayez de remplacer le 0 par le 1 à trois endroits

   int limit=ArraySize(Ma);
//---- EMA --------------------------------------------------------------- 
   for(i=limit1; i>=1; i--)  Ma[i]  =iMA(NULL,0,period,0,MODE_EMA,PRICE_CLOSE,i);
   for(i=limit1; i>=1; i--)  MaOn[i]=iMAOnArray(Ma,limit,period,0,MODE_EMA,i);

   for(i=limit1; i>=1; i--)
 
Aleksei Stepanenko:

essayez de remplacer le 0 par le 1 à trois endroits

   int limit=ArraySize(Ma);
//---- EMA --------------------------------------------------------------- 
   for(i=limit1; i>=1; i--)  Ma[i]  =iMA(NULL,0,period,0,MODE_EMA,PRICE_CLOSE,i);
        // вот сюда
   for(i=limit1; i>=1; i--)  MaOn[i]=iMAOnArray(Ma,limit,period,0,MODE_EMA,i);

   for(i=limit1; i>=1; i--)

Je déplacerais aussi ceci

 
Vitaly, j'ai remarqué que les gens écrivent une question et vaquent à leurs occupations, tandis que nous nous amusons entre nous. Il y a parfois plusieurs pages d'extraits.
 
Aleksei Stepanenko:

essayez de remplacer le 0 par le 1 à trois endroits

Merci beaucoup. Maintenant, comment puis-je faire en sorte qu'il n'ouvre des transactions qu'à la fin de la bougie actuelle sur l'échelle de temps actuelle ? :)) Je ne sais pas quand ça ouvre les marchés maintenant... Les flèches semblent être correctes, et regardez les chandeliers, tout est ok, mais les transactions semblent être ouvertes au milieu ou vers la fin de la bougie, en général, partout, mais pas là où c'est nécessaire. :(

//---- indicator settings
#property  indicator_chart_window
#property  indicator_buffers 2
#property  indicator_color1  Blue
#property  indicator_color2  Red
//---- indicator parameters
extern int  period = 4; //12
extern int  shift  = 0; //сдвиг по бару
//---- indicator buffers
double BufferUp[],BufferDn[];
int q,st=5;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorBuffers(2);
//---- drawing settings
   SetIndexStyle(0,DRAW_ARROW,2);
   SetIndexArrow(0,233);
   SetIndexStyle(1,DRAW_ARROW,2);
   SetIndexArrow(1,234);

   SetIndexBuffer(0,BufferUp);//стрелка синяя верх
   SetIndexBuffer(1,BufferDn);//стрелка красная вниз
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("T3MA-ALARM ("+period+")");
//---- initialization done
   if(Digits==3 || Digits==5) q=10;
   st=st*q;
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit()
  {
   ObjectDelete("low");
   ObjectDelete("high");
  }
//+----------------------------------------------------------------------+
//| Moving Average of Oscillator                                         |
//+----------------------------------------------------------------------+
int start()
  {
//---- ArraySetAsSeries --------------------------------------------------  
   double Ma[],MaOn[];
   double y0[],y1[],y2[];
//   int i;
   int    i;
   ArraySetAsSeries(Ma,true);
//---- IndicatorCounted --------------------------------------------------
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   int limit1=Bars-counted_bars;
   if(counted_bars==0) limit1-=1+MathMax(period,shift)+2;

   int buffer_size=ArraySize(BufferUp);
   ArrayResize(Ma,buffer_size);
   ArrayResize(MaOn,buffer_size);

   ArrayResize(y0,buffer_size+shift+2);
   ArrayResize(y1,buffer_size+shift+2);
   ArrayResize(y2,buffer_size+shift+2);

   int limit=ArraySize(Ma);
//---- EMA --------------------------------------------------------------- 
   for(i=limit1; i>=1; i--)  Ma[i]  =iMA(NULL,0,period,0,MODE_EMA,PRICE_CLOSE,i);
   for(i=limit1; i>=1; i--)  MaOn[i]=iMAOnArray(Ma,limit,period,0,MODE_EMA,i);

   for(i=limit1; i>=1; i--)
     {
      y0[i+shift]=MaOn[i+shift];
      y1[i+1+shift]=MaOn[i+1+shift];
      y2[i+2+shift]=MaOn[i+2+shift];

      if(y0[i+shift]-y1[i+1+shift]<0 && y1[i+1+shift]-y2[i+2+shift]>0){BufferDn[i+1]=High[i+1]+st*Point;}
      if(y0[i+shift]-y1[i+1+shift]>0 && y1[i+1+shift]-y2[i+2+shift]<0){BufferUp[i+1]=Low[i+1]-st*Point;}
      //---- Signal Trend Up || Dn ---------------------------------------------   
      if(y0[i]-y1[i+1]>0) Comment("\n SWAPLONG = ",MarketInfo(Symbol(),MODE_SWAPLONG),
         "   SWAPSHORT = ",MarketInfo(Symbol(),MODE_SWAPSHORT),"\n BUY TREND ",DoubleToStr(Close[i],Digits));

      else if(y0[i]-y1[i+1]<0) Comment("\n SWAPLONG = ",MarketInfo(Symbol(),MODE_SWAPLONG),
         "   SWAPSHORT = ",MarketInfo(Symbol(),MODE_SWAPSHORT),"\n SELL TREND ",DoubleToStr(Close[i],Digits));
     }

//---- done
   return(0);
  }
//+---------------------------------------------------------------------+
 

En général, il est d'usage d'ouvrir des transactions au moment où une nouvelle bougie se forme, alors que l'on dispose déjà de toutes les informations sur la bougie qui vient d'être fermée

datetime LastTime=0;


void OnTick()
   {
   if(LastTime==iTime(symbol,frame,0)) return;
   LastTime=iTime(symbol,frame,0);
 
Vitaly Muzichenko:

Je déplacerais aussi ceci.

Vous êtes sûr que c'est là qu'on vous a montré ?

 int limit=ArraySize(Ma);
//---- EMA --------------------------------------------------------------- 
// а не вот сюда?
   for(i=limit1; i>=1; i--)  Ma[i]  =iMA(NULL,0,period,0,MODE_EMA,PRICE_CLOSE,i);
        
   for(i=limit1; i>=1; i--)  MaOn[i]=iMAOnArray(Ma,limit,period,0,MODE_EMA,i);

   for(i=limit1; i>=1; i--)
 
Aleksei Stepanenko:

En général, il est d'usage d'ouvrir des transactions au moment où une nouvelle bougie se forme, alors que toutes les informations sur la bougie qui vient de se fermer sont déjà en place

Où dois-je insérer ce code pour qu'il fonctionne correctement ? Je ne le comprends pas très bien, je suis juste au début de mon voyage...
 
Aleksei Stepanenko:
Vitaly, j'ai remarqué que les gens écrivent une question et vaquent à leurs occupations, tandis que nous nous amusons entre nous. Il y a parfois plusieurs pages d'extraits.
Comme vous pouvez le constater, ce n'est pas toujours le cas. :))
Raison: