Erreurs, bugs, questions - page 3143

 
Alexey Viktorov #:

Sans vouloir être impoli... La documentation ne précise-t-elle pas clairement que

Si la position est déjà sélectionnée, pourquoi la sélectionner à nouveau avec la fonction PositionSelectByTicket ?

Merci pour votre réponse ! Je me suis posé la question, car après avoir regardé dans la bibliothèque <Trade/Trade.mqh>, la fonction PositionClose(const ulong ticket,const ulong deviation) utilise également PositionSelectByTicket. Et la fonction PositionClose( const ulong ticket,const ulong deviation ) elle-même est souvent utilisée par les codeurs en combinaison avec la boucle for(int i = PositionsTotal()-1 ; i >= 0 ; i--) en passant en boucle par toutes les positions. Et je me demande si je n'en utilise pas un de trop quelque part.
 
Wizard #:
Bonjour, la ligne PositionSelectByTicket est-elle nécessaire dans le code... Si oui, comment écrire correctement un ticket à l'intérieur, par PositionGetTicket(i) ou laisserPositionGetInteger(POSITION_TICKET) ? Merci d'avance !
int IsPositions()
{
   int pos = 0;     
   
   int total = PositionsTotal();
   for(int i = total-1; i >= 0; i--)
   {
      if(!PositionGetTicket(i))
         continue;        
          
      if(PositionGetString(POSITION_SYMBOL) == Symbol_T && PositionGetInteger(POSITION_MAGIC) == EXPERT_MAGIC)
         pos = (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) ? 1 : (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) ? -1 : 0;
   }
   
   return(pos);
}
 
Roman #:
Merci ! Ainsi, PositionGetTicket(i) sera suffisant etPositionSelectByTicket ne sera pas nécessaire.
 
Wizard #:
Merci pour votre réponse ! J'ai commencé à me poser la question, car après avoir regardé dans la bibliothèque <Trade/Trade.mqh>, la fonction PositionClose(const ulong ticket,const ulong deviation) utilise également PositionSelectByTicket. Et la fonction PositionClose( const ulong ticket,const ulong deviation ) elle-même est souvent utilisée par les codeurs en combinaison avec la boucle for(int i = PositionsTotal()-1 ; i >= 0 ; i--) en passant en boucle par toutes les positions. Et je me demande si je n'en utilise pas un de trop quelque part.

La fonction PositionClose(const ulong ticket,const ulong deviation) de la bibliothèque reçoit un ticket de la position à fermer, mais personne ne sait comment le ticket a été reçu et si la position existe.

Par conséquent, PositionSelectByTicket vérifie surtout s'il y a quelque chose à fermer. Et pourquoi avez-vous décidé que tous les postes sont souvent fermés dans la boucle ? Pas nécessairement...

 
Alexey Viktorov #:

La fonction PositionClose(const ulong ticket,const ulong deviation) de la bibliothèque reçoit un ticket de la position à fermer, mais personne ne sait comment le ticket a été reçu et si la position existe.

Par conséquent, PositionSelectByTicket vérifie surtout s'il y a quelque chose à fermer. Et pourquoi avez-vous décidé que tous les postes sont souvent fermés dans la boucle ? Pas nécessairement...

Oui, je comprends tout maintenant. J'ai récemment eu un EA, je l'ai écrit en suivant votre exemple (avec des moyennes) d'un forum, en le combinant avec le code de Timur Mashnin. Maintenant j'ai décidé de changer les conditions là et de les réécrire un peu, en remplaçant tout par des fonctions système, sans bibliothèques. Si avec un simple PositionSelect tout était très simple, une position - un choix, alors avec la boucle ici bien sûr j'ai besoin de plus de considérations et de logique.
 

Bonjour

Pouvez-vous m'aider ?

avec le code.

Faire en sorte qu'un indicateur dans le testeur fonctionne correctement

Lorsque je le place sur le graphique, il ne s'affiche pas correctement.

Je n'arrive pas à comprendre pourquoi c'est mauvais.

//+------------------------------------------------------------------+
//|                                                        Oscil.mq5 |
//|                                  Copyright 2021, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property  indicator_buffers 5
#property indicator_plots   5

#property  indicator_color1  clrNONE
#property  indicator_color2  clrRoyalBlue
#property  indicator_color3  clrPink
#property  indicator_color4  clrAqua
#property  indicator_color5  clrYellow

#property  indicator_width1 1
#property  indicator_width2 5
#property  indicator_width3 5
#property  indicator_width4 5
#property  indicator_width5 5

double MainLine[];
double UpLine[];
double DnLine[];
double muls[];
double x,y,z;
double price;
double mulSum=0;
double Pi   = 3.1415926535;
bool LastUp = false;
bool GoUp   = false;
input bool otl    = false;
/***********Range***************/
int    Length             = 3;
int    MajorRangeStrength = 4;


double MajorRangeBuy[];
double MajorRangeSell[];


double RangePrice  = 0.0,
       SweepB      = 0.0;
int    Switch2     = 0,
         SwitchB     = 0;
double Price2BuyA  = 0.0;
int    Price2BuyB  = 1.0;
double Price2SellA = 0.0;
int    Price2SellB = 0.0;
bool   BuySwitchB  = false,
       SellSwitchB = false;
       
int hendlMA_1;
double MA_1[];

int hendlMA_2;
double MA_2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,MainLine,INDICATOR_DATA);
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, 0);
   PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_HISTOGRAM); 
   ArraySetAsSeries(MainLine, true);
   
   SetIndexBuffer(1,UpLine,INDICATOR_DATA);
   PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, 0);
   PlotIndexSetInteger(1,PLOT_DRAW_TYPE,DRAW_HISTOGRAM); 
   ArraySetAsSeries(UpLine, true);
   
   SetIndexBuffer(2,DnLine,INDICATOR_DATA);
   PlotIndexSetDouble(2, PLOT_EMPTY_VALUE, 0);
   PlotIndexSetInteger(2,PLOT_DRAW_TYPE,DRAW_HISTOGRAM); 
   ArraySetAsSeries(DnLine, true);
   
   SetIndexBuffer(3,MajorRangeBuy,INDICATOR_DATA);
   PlotIndexSetDouble(3, PLOT_EMPTY_VALUE, 0);
   PlotIndexSetInteger(3,PLOT_DRAW_TYPE,DRAW_HISTOGRAM); 
   ArraySetAsSeries(MajorRangeBuy, true);
   
   SetIndexBuffer(4,MajorRangeSell,INDICATOR_DATA);
   PlotIndexSetDouble(4, PLOT_EMPTY_VALUE, 0);
   PlotIndexSetInteger(4,PLOT_DRAW_TYPE,DRAW_HISTOGRAM); 
   ArraySetAsSeries(MajorRangeSell, true);
   
   hendlMA_1=iMA(Symbol(),0,1,0,MODE_LWMA,PRICE_CLOSE);
   ArraySetAsSeries(MA_1,true);
   
   hendlMA_2=iMA(Symbol(),0,1,0,MODE_SMMA,PRICE_CLOSE);
   ArraySetAsSeries(MA_2,true);
   
   ArrayResize(muls, 99);
   
   mulSum = 0;
   
   for (int i0 = 0; i0 < 98; i0++) {//повторяем в цикле 98 раз
      if (i0 <= 18) y = 1.0 * i0 / 18; //если это первые 18 повторений
      else y = (i0 - 18) * 7.0 / 79.0 + 1.0; //иначе
      
      x = MathCos(Pi * y);
      z = 1.0 / (3.0 * Pi * y + 1.0);
      if (y <= 0.5) z = 1;
      
      muls[i0] = z * x;
      mulSum += muls[i0];
   }
   if(otl)Print(" Распределение создано muls[20]=",muls[20]);
//---
   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[])
  {
//---
  if (PeriodSeconds() <60*60 || PeriodSeconds() >10080*60) return(0);
   int depth=0;

   int l_ind_counted_8 = prev_calculated; //Возвращает количество баров, не измененных после последнего вызова индикатора.
   
   int bars=Bars(Symbol(),PERIOD_CURRENT);
   if (l_ind_counted_8 < 0) return (0);
   if (l_ind_counted_8 == 0)
      {
         depth = bars - 98;
         for(int a=0;a<bars;a++)
            {
               MainLine[a] = 0;
               UpLine[a] = 0;
               DnLine[a] = 0;
               MajorRangeBuy[a]=0;
               MajorRangeSell[a]=0;
            }
      }
   if (l_ind_counted_8 > 0)  depth = bars - l_ind_counted_8;
   if(otl)Print(" количество баров, не измененных после последнего вызова индикатора= ",l_ind_counted_8,"  Количество баров на текущем графике Bars=",bars,"  depth= ",depth);
   if (l_ind_counted_8 < 1) {
      for (int i2 = 1; i2 < 100; i2++) {
         MainLine[bars - i2] = 0;
         UpLine[bars - i2] = 0;
         DnLine[bars - i2] = 0;
      }
   }
   
   for (int i1 = depth; i1 >= 0; i1--) 
   {
      price = 0;
          
          CopyBuffer(hendlMA_1,0,0,bars,MA_1);
          
      for (int i2 = 0; i2 <= 98; i2++) 
         {
            if(i2 + i1>=bars)break;
            price += muls[i2] * MA_1[i2 + i1];
         }
          
      if (mulSum > 0.0) MainLine[i1] = price / mulSum;

     GoUp=MainLine[i1 + 1] > MainLine[i1] ;
     
      if (GoUp) 
      {
         if (!LastUp) DnLine[i1+1] = MainLine[i1+1];
         DnLine[i1] = MainLine[i1];
         UpLine[i1] = 0;
      }
         else
      {
         if (LastUp) UpLine[i1+1] = MainLine[i1+1];
         UpLine[i1] = MainLine[i1];
         DnLine[i1] = 0;
      }
      LastUp=GoUp; 

   }//  for (int i1

 //  return (0);

/***************** Range **********************/

  int counted_bars=prev_calculated;
  if(otl)Print(" Range counted_bars = ", counted_bars);
   if(counted_bars<0) return(-1);
   int position=bars-counted_bars;
   if (position<0) position=0;
   if (position==0) position=1;
   int rnglength = 250;
   double range = 0.0, srange = 0.0;
   if(otl) Print(" position=",position);
   
   for (int pos = position; pos >=0; pos--)
   {/***************** MAIN Range **********************/
      srange = 0.0;
      int j = 0;
      for (int i=0;i<rnglength;i++)
      {
         j++;
         int posr = pos + i;
         if (posr >= bars) break; 
         srange = srange + (High(posr) - Low(posr));
      }
      range = srange / j * Length;
      int BarNumber = bars-pos; //??????????
      if (BarNumber < 0)  BarNumber = 0;
          
          CopyBuffer(hendlMA_2,0,0,bars,MA_2);
          //Print(bars," - ",pos);
      if(pos<bars)RangePrice = MA_2[pos];  //Moving Average MODE_SMMA
      else RangePrice = MA_2[pos-1];

      if (BarNumber == 1)
      {
         SweepB  = range *  MajorRangeStrength;
         Price2BuyA = RangePrice;
         Price2SellA = RangePrice;
      }     

      if (BarNumber > 1)
      {

         if (Switch2  >  - 1)//проверка цикла на покупку
         {
            if (RangePrice < Price2BuyA) //если средняя цена ниже
            {
if (BuySwitchB ) MajorRangeBuy [pos +BarNumber - Price2BuyB] = 0;                                                                //OUT
                           Price2BuyA = RangePrice;
               Price2BuyB = BarNumber;
               BuySwitchB = true;
            } 
            else if (RangePrice > Price2BuyA)
            {
                            SwitchB = BarNumber - Price2BuyB;
MajorRangeBuy [pos +SwitchB] = MainLine[pos + SwitchB]*1.0005;                                                                                                                          //OUT
                BuySwitchB = true;

                              if (RangePrice - MA_2[pos + SwitchB] >= SweepB && SwitchB >= 1)
                              {
                     Switch2 =  - 1;
                     Price2SellA = RangePrice;
                     Price2SellB = BarNumber;
                     SellSwitchB = false;
                     BuySwitchB = false;
               }
            }
         }
         if (Switch2  < 1)//проверка цикла на продажу
         {
            if (RangePrice  > Price2SellA )
            {
if (pos +BarNumber - Price2SellB<bars&&SellSwitchB ) MajorRangeSell [pos +BarNumber - Price2SellB] = 0;                                                         //OUT
                           Price2SellA = RangePrice;
               Price2SellB = BarNumber;
               SellSwitchB = true;
                    }
                       else if (RangePrice < Price2SellA)
                    {
               SwitchB = BarNumber - Price2SellB ;

         if(pos+ SwitchB<bars)MajorRangeSell[pos + SwitchB] =MainLine[pos + SwitchB]*1.0005;                                                                                                                             //OUT
                SellSwitchB = true;             
        
                              if (pos + SwitchB<bars&&MA_2[pos + SwitchB] - RangePrice >= SweepB && SwitchB >= 1)
                              {
                                     Switch2 = 1;
                     Price2BuyA = RangePrice;
                     Price2BuyB = BarNumber;
                     SellSwitchB = false;
                     BuySwitchB = false;
                                  }
            }
         }
      }

   //   MajorRangeSell[pos] = 0;
    //  MajorRangeBuy[pos]  = 0;  
    }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//========================================================================================
double High(int index)
{   
   if(index < 0) return(-1);
   double Arr[];
   ENUM_TIMEFRAMES timeframe=PERIOD_CURRENT;
   if(CopyHigh(Symbol(),timeframe, index, 1, Arr)>0) 
        return(Arr[0]);
   else return(-1);
}
//========================================================================================
double Low(int index)
{   
   if(index < 0) return(-1);
   double Arr[];
   ENUM_TIMEFRAMES timeframe=PERIOD_CURRENT;
   if(CopyLow(Symbol(),timeframe, index, 1, Arr)>0) 
        return(Arr[0]);
   else return(-1);
}
 

Qu'est-ce qui est renvoyé à la structure MqlTradeCheckResult ?

Il est écrit dans la documentation que"Le montant de la marge requise pour l'opération de transaction requise".

Champ

Description

retcode

Code de retour

équilibre

Valeur du solde qui sera après l'exécution de la transaction

actions

Valeur de l'équité, qui sera après l'exécution de la transaction.

profit

Valeur du profit flottant qui sera après l'exécution de la transaction.

marge

Le montant de la marge nécessaire pour l'opération requise.

marge_libre

Le montant de l'équité libre qui restera après l'exécution de la transaction requise.

niveau de marge

Le niveau de marge à fixer après l'exécution de la transaction requise.

commentaire

Commentaire sur le code de réponse, description de l'erreur


Mais ce qui est obtenu en réalité, c'est la taille de la marge totale, actuelle et plus celle qui sera prise après l'exécution de cette opération.

Voici le script

#include <Trade\Trade.mqh>
CTrade trade;
  MqlTradeRequest           my_request;
  MqlTradeCheckResult       my_check_result;
  MqlTick                   mqlTick;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
 {
  SymbolInfoTick(_Symbol, mqlTick);
  CheckOrder(0.01, ORDER_TYPE_SELL);
  Print(my_check_result.margin);
//---
  trade.PositionOpen(_Symbol, ORDER_TYPE_SELL, 0.01, mqlTick.bid, 0.0, 0.0);
//---
  CheckOrder(0.01, ORDER_TYPE_SELL);
  Print(my_check_result.margin);
 }/******************************************************************/

/********************************************************************\
||
\********************************************************************/
bool CheckOrder(double volume, ENUM_ORDER_TYPE order_type)
 {
  if(order_type != ORDER_TYPE_BUY && order_type != ORDER_TYPE_SELL)
    return false;
  ZeroMemory(my_request);
  ZeroMemory(my_check_result);
  bool checkOrder = false;
//--- setting my_request
  my_request.action = TRADE_ACTION_DEAL;
  my_request.symbol = _Symbol;
  my_request.volume = volume;
  my_request.type   = order_type;
  my_request.price  = order_type == ORDER_TYPE_BUY ? mqlTick.ask : mqlTick.bid;
  checkOrder = ::OrderCheck(my_request, my_check_result);
  if(my_check_result.retcode != 0)
    return(false);
//---
  return(true);
 }/******************************************************************/

et le résultat de l'exécution

2022.02.01 10:11:28.002 Test bag (EURUSD,H1)    2.25
2022.02.01 10:11:28.108 Test bag (EURUSD,H1)    4.5

Tant qu'il n'y a pas de positions ouvertes sur le compte, my_check_result.margin est égal à la marge pour la position ouverte avec le lot 0.01, et lorsqu'il y a déjà 0.01 sur le compte, la marge pour le lot 0.02.

Dossiers :
Test_bag.mq5  5 kb
 
Alexey Viktorov #:

Mais ce que vous obtenez réellement, c'est la taille de la marge totale, actuelle et plus celle qui sera prise après la réalisation de cette opération.

Bien.

MarginOpen = CheckResult.margin - ::AccountInfoDouble(ACCOUNT_MARGIN); // Маржа для открытия.
 
fxsaber #:

Bien.

Qu'il en soit ainsi, mais alors il devrait en être de même dans la documentation.

Si balans est la valeur du solde qui sera après que la transaction soit faite

alors la marge - devrait être. La valeur de la marge, qui sera après l'exécution de l'opération commerciale.

 

J'appelle le code suivant dans MT5 dans le testeur de stratégie

Print("!!!  ",AccountInfoString(ACCOUNT_NAME));

Il est indiqué que le nom du compte est Tester

C'est censé être comme ça ?