Preguntas de los principiantes MQL5 MT5 MetaTrader 5 - página 1337

 
Hola, ¿podríais decirme cómo pasar una estructura a una función, para que los cambios en los elementos de la estructura en la función se almacenen en la estructura creada globalmente?
 
Кирилл Смирнов:
Hola, ¿podríais decirme cómo pasar una estructura a una función, para que los cambios en los elementos de la estructura en la función, se guarden en la estructura creada globalmente?

Pasar la estructura por referencia. Ejemplo:

//+------------------------------------------------------------------+
//|                                                     Expert 1.mq5 |
//|                              Copyright © 2021, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2021, Vladimir Karputov"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Structure Positions                                              |
//+------------------------------------------------------------------+
struct STRUCT_POSITION
  {
   ENUM_POSITION_TYPE pos_type;              // position type
   double            volume;                 // position volume (if "0.0" -> the lot is "Money management")
   double            lot_coefficient;        // lot coefficient
   bool              waiting_transaction;    // waiting transaction, "true" -> it's forbidden to trade, we expect a transaction
   ulong             waiting_order_ticket;   // waiting order ticket, ticket of the expected order
   bool              transaction_confirmed;  // transaction confirmed, "true" -> transaction confirmed
   //--- Constructor
                     STRUCT_POSITION()
     {
      pos_type                   = WRONG_VALUE;
      volume                     = 0.0;
      lot_coefficient            = 0.0;
      waiting_transaction        = false;
      waiting_order_ticket       = 0;
      transaction_confirmed      = false;
     }
  };
STRUCT_POSITION SPosition;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   Example(SPosition);
   Comment(EnumToString(SPosition.pos_type));
  }
//+------------------------------------------------------------------+
//| Example                                                          |
//+------------------------------------------------------------------+
void Example(STRUCT_POSITION &struct_position)
  {
   int res=MathRand();
   if(res<32767/2)
      struct_position.pos_type=POSITION_TYPE_BUY;
   else
      struct_position.pos_type=POSITION_TYPE_SELL;
  }
//+------------------------------------------------------------------+
Archivos adjuntos:
Expert_1.mq5  6 kb
 
Vladimir Karputov:

Pasar la estructura por referencia. Ejemplo:

Gracias, ¿de dónde salió este ejemplo? Estaba buscando algo sobre mi pregunta en la ayuda y no lo encontré(((

 
Kira27:

Gracias, ¿de dónde salió este ejemplo? Estaba buscando algo sobre mi pregunta en la ayuda y no lo encontré(((

Aplicación real + función inventada, por ejemplo.

 
Vladimir Karputov:

Aplicación real + función inventada por ejemplo.

Bien, ¡gracias de nuevo!

 
Vladimir Karputov:

Aplicación real + función inventada por ejemplo.

Un mal ejemplo. Una estructura, así como cualquier variable declarada globalmente, es accesible en cualquier parte del programa.

También funcionará así.

//+------------------------------------------------------------------+
//| Structure Positions                                              |
//+------------------------------------------------------------------+
struct STRUCT_POSITION
  {
   ENUM_POSITION_TYPE pos_type;              // position type
   double            volume;                 // position volume (if "0.0" -> the lot is "Money management")
   double            lot_coefficient;        // lot coefficient
   bool              waiting_transaction;    // waiting transaction, "true" -> it's forbidden to trade, we expect a transaction
   ulong             waiting_order_ticket;   // waiting order ticket, ticket of the expected order
   bool              transaction_confirmed;  // transaction confirmed, "true" -> transaction confirmed
   //--- Constructor
                     STRUCT_POSITION()
     {
      pos_type                   = WRONG_VALUE;
      volume                     = 0.0;
      lot_coefficient            = 0.0;
      waiting_transaction        = false;
      waiting_order_ticket       = 0;
      transaction_confirmed      = false;
     }
  };
STRUCT_POSITION SPosition;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   Example();
   Comment(EnumToString(SPosition.pos_type));
  }
//+------------------------------------------------------------------+
//| Example                                                          |
//+------------------------------------------------------------------+
void Example()
  {
   int res=MathRand();
   if(res<32767/2)
      SPosition.pos_type=POSITION_TYPE_BUY;
   else
      SPosition.pos_type=POSITION_TYPE_SELL;
  }
//+------------------------------------------------------------------+

Es diferente si una variable de estructura se declara localmente.

//+------------------------------------------------------------------+
//| Structure Positions                                              |
//+------------------------------------------------------------------+
struct STRUCT_POSITION
  {
   ENUM_POSITION_TYPE pos_type;              // position type
   double            volume;                 // position volume (if "0.0" -> the lot is "Money management")
   double            lot_coefficient;        // lot coefficient
   bool              waiting_transaction;    // waiting transaction, "true" -> it's forbidden to trade, we expect a transaction
   ulong             waiting_order_ticket;   // waiting order ticket, ticket of the expected order
   bool              transaction_confirmed;  // transaction confirmed, "true" -> transaction confirmed
   //--- Constructor
                     STRUCT_POSITION()
     {
      pos_type                   = WRONG_VALUE;
      volume                     = 0.0;
      lot_coefficient            = 0.0;
      waiting_transaction        = false;
      waiting_order_ticket       = 0;
      transaction_confirmed      = false;
     }
  };
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   STRUCT_POSITION SPosition;
   Example(SPosition);
   Comment(EnumToString(SPosition.pos_type));
  }
//+------------------------------------------------------------------+
//| Example                                                          |
//+------------------------------------------------------------------+
void Example(STRUCT_POSITION &struct_position)
  {
   int res=MathRand();
   if(res<32767/2)
      struct_position.pos_type=POSITION_TYPE_BUY;
   else
      struct_position.pos_type=POSITION_TYPE_SELL;
  }
//+------------------------------------------------------------------+
 
Hola a todos. MT5 está cargando gigabytes de datos al trabajar con múltiples herramientas. ¿Hay alguna forma de limitarlo en la configuración? Quiero que los gráficos mensuales, semanales y diarios se dibujen como en MT4, es decir, cada barra es (apertura, alta, baja, cierre) sin ningún historial interno adicional. Y ya los gráficos con barras intradía fueron construidos según el principio actual de MT5 (implementación).
 
Vladimir Makhnin:
¿Cómo aumentar/disminuir el plazo con mt5?

Foro sobre comercio, sistemas de comercio automatizados y pruebas de estrategias

Indicadores: Cambio de plazos con teclas de acceso rápido

SanAlex, 2020.08.07 11:59

creo que sería lo mismo para mt4 y mt5

//+------------------------------------------------------------------+
//|                                                      hotKeys.mq5 |
//|                                     Copyright 2015,Mohit Marwaha |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, Mohit Marwaha"
#property link      "marwaha1@gmail.com"
#property version   "1.20"
#property indicator_chart_window
#property indicator_plots 0
#property description "Keys 1 through 9 change timeframes from 1 minute to Monthly"
#define  KEY_MONTHLY 57
#define  KEY_WEEKLY 56
#define  KEY_DAILY 55
#define  KEY_4HOUR 54
#define  KEY_1HOUR 53
#define  KEY_30MIN 52
#define  KEY_15MIN 51
#define  KEY_5MIN 50
#define  KEY_1MIN 49
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   Comment("Copyright MohitMarwaha");
//---
   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[])
  {
//---
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   if(id==CHARTEVENT_KEYDOWN)
     {
      switch(int(lparam))
        {
         case KEY_WEEKLY:
            ChartSetSymbolPeriod(0,NULL,PERIOD_W1);
            break;
         case KEY_DAILY:
            ChartSetSymbolPeriod(0,NULL,PERIOD_D1);
            break;
         case KEY_4HOUR:
            ChartSetSymbolPeriod(0,NULL,PERIOD_H4);
            break;
         case KEY_1HOUR:
            ChartSetSymbolPeriod(0,NULL,PERIOD_H1);
            break;
         case KEY_5MIN:
            ChartSetSymbolPeriod(0,NULL,PERIOD_M5);
            break;
         case KEY_30MIN:
            ChartSetSymbolPeriod(0,NULL,PERIOD_M30);
            break;
         case KEY_15MIN:
            ChartSetSymbolPeriod(0,NULL,PERIOD_M15);
            break;
         case KEY_MONTHLY:
            ChartSetSymbolPeriod(0,NULL,PERIOD_MN1);
            break;
         case KEY_1MIN:
            ChartSetSymbolPeriod(0,NULL,PERIOD_M1);
            break;
        }
      ChartRedraw();
     }
  }
//+------------------------------------------------------------------+

 
Alexey Viktorov:

Un mal ejemplo. Una estructura, así como cualquier variable declarada globalmente, es accesible en cualquier parte del programa.

Esto también funcionará.

Es diferente si la variable de estructura se declara localmente.

¡¡¡Gracias!!!

 
Valeriy Yastremskiy:

Gracias
Razón de la queja: