Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 938

 
Seric29:

I want to create a one-dimensional array where 0 to 4 elements will store double, 5 to 9 will store datetame, 10 to 14 will store int. Is it possible to organize it in mql4? If so, could you show me how, I've seen an example with structures in C++, but I've lost the link to the site?

If you have specific array cells allocated for each type, make a structure instead of an array.

 
Koldun Zloy:

If you have specific array cells allocated for each type, then make a structure instead of an array.

Unfortunately, he can't even formalize the task

on the one hand it looks like he wants to get away from strict typing in C++ / MQL , here's a good discussion thread, although I doubt it will help himhttps://www.mql5.com/ru/forum/294094

In the course of this discussion (I think it's been going on for 3 months already?) it turns out that@Seric29 needs to get away from description of function signatures and get a "phantom"function that accepts an arbitrary number of parameters

Then it turns out during the discussion that multidimensional dynamic arrays should be involved in all this, and these arrays should be passed into the function as parameters

I.e. this is an unformalized task, solution of which constantly ends up in "shouts from the field", the developers reduced the functionality of something and give me the source code of loop operators, I will redo them, then the latter:

Seric29:
How to write a letter to the mql4 developers how to see the compiler code, is there any way to add more features to it so it's not so static?

... that's about it... the phrase "mind blowing" is the least you can say ))))

 

That's why I don't give any details.

There is no getting away from learning the basics.

 
Koldun Zloy:

There is no getting away from learning the basics.

It's interesting that a man doesn't learn the basics, but tries to show that he knows it all already, and you're all metaquotes preventing him from doing his job)

 
Taras Slobodyanik:

it's interesting that a man doesn't learn the basics, but tries to show that he knows it all already, and you're all metaquotes preventing him from doing his job)

Greetings. I have watched the Expert Advisor Video "From MQL4 to MQL5 - How to Rewrite an Expert Advisor for Metatrader 5".
Many thanks to the author. I have decided to try it myself. I decided to try it myself. The idea is as follows:
1. I set dtriger = 1 in the inputs - Buy opens.
2. I set dtriger = -1 - Sell opens. 3.
3. I set dtriger = 0 in the multiplugs - all open ones are closed.
I read in the FAQ that in MT5 it is not possible to hold opposite positions,
and I have them.
Question: How do I correctly prescribe the closing of an open position
The question is: How to correctly register the closure of an existing position at the opening of an arrow (reversal)?
Thank you very much.

#include <Trade\PositionInfo.mqh>.
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Trade\OrderInfo.mqh>

CPositionInfo o_position;
CTrade o_trade;
CSymbolInfo o_symbol;
COrderInfo o_order;

input int triger = 0;
input double StartLot = 0.01;
input double lpos_volume = 1.0;
input int Step = 10;
input int MagicNumber = 12345; // Magic nuaber
input int Slippage = 30; // slippage

int dtriger;
int dStep;

//+------------------------------------------------------------------+
//| Expert initialization function|
//+------------------------------------------------------------------+
int OnInit()
{
dStep = Step ;
dtriger = triger ;

if (!o_symbol.Name(Symbol()))
return(INIT_FAILED);

RefreshRates();

o_trade.SetExpertMagicNumber(MagicNumber) ;

if (IsFillingTypeAllowed(o_symbol.Name(), SYMBOL_FILLING_FOK))
{
o_trade.SetTypeFilling(ORDER_FILLING_FOK);
}
else if (IsFillingTypeAllowed(o_symbol.Name(), SYMBOL_FILLING_IOC))
{
o_trade.SetTypeFilling(ORDER_FILLING_IOC);
}
else
{
o_trade.SetTypeFilling(ORDER_FILLING_RETURN);
}
o_trade.SetDeviationInPoints(Slippage);

if (o_symbol.Digits() == 3 || o_symbol.Digits() == 5 )
{
dStep = 10 ;
}

return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function|
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| expert tick function|
//+------------------------------------------------------------------+
void OnTick()
{
datetimelpos_time = 0;
double lpos_price_open = 0.0;
ENUM_POSITION_TYPE lpos_type = -1;
intpos_count = 0;
double sum_profit = 0;

for (int i = PositionsTotal() - 1; i>=0; i--)
{
if (o_position.SelectByIndex(i))
{
if (o_position.Symbol() == o_symbol.Name() && o_position.Magic() == MagicNumber)
{
if (o_position.Time() > lpos_time)
{
lpos_time = o_position.Time(); //OrderOpenTime();
lpos_price_open = o_position.PriceOpen(); //OrderOpenPrice();
lpos_type = o_position.PositionType() ; //OrderTipe();
}

pos_count++;
sum_profit = sum_profit + o_position.Commission() + o_position.Swap() + o_position.Profit() ;
}
}
}

// Count the number of pending orders
int stop_count=0;

for (int i=OrdersTotal()-1; i >=0; i--)
{
if (o_order.SelectByIndex(i))
{
if (o_order.Symbol() == o_symbol.Name() && o_order.Magic() == MagicNumber)
stop_count++;
}
}

if (!RefreshRates())
return ;

if(dtriger == 0 )
{
CloseAll();
return;
}

// + ----- Open first order ++++++++++
if (pos_count == 0 && stop_count == 0 )
{
if ( dtriger == -1 && lpos_type != POSITION_TYPE_SELL)
{
o_trade.Sell(StartLot * lpos_volume , o_symbol.Name()); // S E L L 11111
}

if ( dtriger == 1 && lpos_type != POSITION_TYPE_BUY )
{
o_trade.Buy(StartLot * lpos_volume , o_symbol.Name()); // B U Y 11111
}
}


// + ----- Reverse ++++++++++++++++++++++++++++

if (pos_count>0)
{
if(lpos_type == POSITION_TYPE_BUY )
{
if ( dtriger == -1 )
{
o_trade.Sell(StartLot * lpos_volume , o_symbol.Name()); // S E L L +++++
}
}

if (lpos_type==POSITION_TYPE_SELL )
{
if ( dtriger == 1 )
{
o_trade.Buy(StartLot * lpos_volume , o_symbol.Name()); // B U Y +++++
}
}
}


if(pos_count>0 && stop_count>0)
DeleteStopOrders() ;

}
//-----------------------------------------------------------
bool RefreshRates()
{
if (!o_symbol.RefreshRates())
return(false) ;

if (o_symbol.Ask() == 0 || o_symbol.Bid() == 0)
return(false);

return(true);
}
//--- ---------------------------------------------------------
bool IsFillingTypeAllowed (string symbol, int fill_type)
{
int filling = (int)SymbolInfoInteger(symbol, SYMBOL_FILLING_MODE);

return((filling && fill_type) == fill_type) ;
}

// --------------------------------------------------
void CloseAll()
{
for (int index = PositionsTotal()-1; index >=0; index--)
{
if (o_position.SelectByIndex(index))
{
if (o_position.Symbol() == o_symbol.Name() && o_position.Magic() == MagicNumber)
{
o_trade.PositionClose(o_position.Ticket())
}
}
}
}

//-----------------------------------------------------------
// Delete all pending orders
//-------------------------------------
void DeleteStopOrders()
{
for (int i = OrdersTotal() - 1; i >= 0; i-- )
{
if (o_order.SelectByIndex(i))
if(o_order.Symbol() == o_symbol.Name() && o_order.Magic() == MagicNumber)
o_trade.OrderDelete(o_order.Ticket())
}
}

//+------------------------------------------------------------------+

 
procom:

Greetings....

Please insert the code correctly:


 
Artyom Trishkin:

Please insert the code correctly:


ok
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Trade\OrderInfo.mqh>

CPositionInfo   o_position;
CTrade        o_trade;
CSymbolInfo        o_symbol;
COrderInfo         o_order;

input int          triger            = 0;
input double    StartLot             = 0.01;
input double    lpos_volume       = 1.0;
input int          Step         = 10;
input int          MagicNumber    = 12345;      //      Magic   nuaber
input int          Slippage          = 30;         //   slippage

int dtriger;
int dStep;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   dStep = Step ;
   dtriger = triger ;

   if (!o_symbol.Name(Symbol()))
     return(INIT_FAILED);
   
   RefreshRates();
   
   o_trade.SetExpertMagicNumber(MagicNumber) ;

   if (IsFillingTypeAllowed(o_symbol.Name(), SYMBOL_FILLING_FOK))
   { 
      o_trade.SetTypeFilling(ORDER_FILLING_FOK);
   }
   else if (IsFillingTypeAllowed(o_symbol.Name(), SYMBOL_FILLING_IOC))
   { 
      o_trade.SetTypeFilling(ORDER_FILLING_IOC);
   }
   else 
   {
      o_trade.SetTypeFilling(ORDER_FILLING_RETURN);
   }
      o_trade.SetDeviationInPoints(Slippage);
   
   if (o_symbol.Digits() == 3 || o_symbol.Digits() == 5 )
   {
      dStep = 10 ;
   }
   
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
      datetime              lpos_time          =        0;
      double                lpos_price_open    =        0.0;
      ENUM_POSITION_TYPE   lpos_type           =        -1;
      int                      pos_count               =        0;
      double                sum_profit         = 0;
 
   for (int i = PositionsTotal() - 1; i>=0; i--)
   {
      if (o_position.SelectByIndex(i))
      {
         if (o_position.Symbol() == o_symbol.Name() && o_position.Magic() == MagicNumber)
         {
            if (o_position.Time() > lpos_time)
            {  
               lpos_time       = o_position.Time();            //OrderOpenTime();
               lpos_price_open = o_position.PriceOpen();       //OrderOpenPrice();
               lpos_type       = o_position.PositionType() ;   //OrderTipe();
             }  
            
            pos_count++;
            sum_profit = sum_profit + o_position.Commission() + o_position.Swap() + o_position.Profit() ;
          }     
       }     
    }          

   // Считаем кол-во отложенных ордеров
  int stop_count=0;

   for (int i=OrdersTotal()-1; i >=0; i--) 
   {
      if (o_order.SelectByIndex(i)) 
      {
         if (o_order.Symbol() == o_symbol.Name() && o_order.Magic() == MagicNumber) 
           stop_count++;
      }
   }

   if (!RefreshRates())
     return ;
     
   if(dtriger == 0 )
   {
      CloseAll();
      return;               
   } 
   
  // + -----    Откраваем Первый ордер   ++++++++++
 if (pos_count == 0  && stop_count == 0    )
   {
      if ( dtriger == -1 &&  lpos_type != POSITION_TYPE_SELL)
      {
         o_trade.Sell(StartLot * lpos_volume , o_symbol.Name());  //   S E L L   11111
      }
      
      if ( dtriger == 1 &&  lpos_type != POSITION_TYPE_BUY )
      {
         o_trade.Buy(StartLot * lpos_volume , o_symbol.Name());   //   B U Y    11111
      }
   }
                          

// +  -----   Переворот    ++++++++++++++++++++++++++++   

if (pos_count>0)
   {
      if(lpos_type == POSITION_TYPE_BUY )
      {
         if ( dtriger == -1 )
         {
         o_trade.Sell(StartLot * lpos_volume , o_symbol.Name());   //   S E L L   +++++
         }
      }

      if (lpos_type==POSITION_TYPE_SELL )
      {
         if ( dtriger == 1 )
         {
         o_trade.Buy(StartLot * lpos_volume , o_symbol.Name());       //   B U Y    +++++
         }
      }
   }


   if(pos_count>0 && stop_count>0) 
     DeleteStopOrders() ;
  
} 
//-----------------------------------------------------------
bool RefreshRates()
{
   if (!o_symbol.RefreshRates())
     return(false) ;
     
    if (o_symbol.Ask() == 0 || o_symbol.Bid() == 0)
      return(false);
      
    return(true);
}  
//---  --------------------------------------------------------- 
 bool IsFillingTypeAllowed (string symbol, int fill_type)
{ 
   int filling = (int)SymbolInfoInteger(symbol, SYMBOL_FILLING_MODE); 
 
   return((filling && fill_type) == fill_type) ;
} 
 
 //  -------------------------------------------------- 
   void CloseAll()
{
   for (int index = PositionsTotal()-1; index >=0; index--)
   {
      if (o_position.SelectByIndex(index))
      {
         if (o_position.Symbol() == o_symbol.Name() && o_position.Magic() == MagicNumber)
         {
            o_trade.PositionClose(o_position.Ticket());
         }
      }  
    } 
 } 
  
 //----------------------------------------------------------- 
 // Delete all pending orders
 //-------------------------------------
 void DeleteStopOrders()
 {
    for (int i = OrdersTotal() - 1; i >= 0; i-- ) 
   {
      if (o_order.SelectByIndex(i))
         if(o_order.Symbol() == o_symbol.Name() && o_order.Magic() == MagicNumber)
            o_trade.OrderDelete(o_order.Ticket());
   }
 } 
 
//+------------------------------------------------------------------+
Files:
Stend_t1.mq5  12 kb
 
Igor Makanu:

start reading at least a book on C++, your questions are a blast, you write technical terms, at first glance it seems that you are asking a specific question, but in fact you just operate in terms without understanding the essence .... what will the macro return? where will it return? in the body of the macro is a reloadable function.... I can't even begin to explain what your question looks like.

To understand what a macro is, spell the term correctly, macroSETTING , maybe it will be easier for you to understand what it is

In mql4 the capabilities and C++ are reduced, in other words, it's too static language. I can say my idea failed, I wrote a simple overload without classes, I added arguments and gave them 0, I tried to call it in a macro, I got an error saying there are 3 functions, so it turns out I need to describe yet which one to call and how, and I'm not saying anything about what kind of stumbling blocks I'll have to deal with later. Probably better not to do anything, then you won't make a mistake.

 
Seric29:

mql4 has cut features and C++, in other words too static language.

What has been cut, exactly?

In other words, you don't have to, especially not even google knows"too static language", C++ is a statically typed language and thinking about it is meaningless - it just is.

Seric29:

Probably better not to do anything, then you won't make a mistake.

I'll be brief RTFM ( lurkmore )

 

The question is purely one of aesthetics.

How do I make it so that there is a picture of its own right here? MT4