Questions from Beginners MQL4 MT4 MetaTrader 4 - page 85

 
deathmond1990:
Hello. The question is: Can we load the values of ticks (the corresponding prices) for a certain trading period (i.e., for a single candle) into a one-dimensional array? And to do it in such a way that the array is formed during the formation of a candle. Is it realistic in MQL4? I would appreciate your feedback.


It is real. A dynamic array and go.

//-----------------------+
int k=0;       //на глобальном уровне
double тики[];
//----------------
void OnTick()
  {
   ArrayResize(тики,k+1);
   тики[k]=Bid;
   k++;
  }
//+------------------------
 
Alekseu Fedotov:


Realistically. A dynamic array and off you go.

Thank you very much =)
 
Greetings gentlemen! I don't want this to be a flood, but I cannot do without this function. So guys, can someone help me with this function which goes through all orders and deletes two orders at the same time, the one with the smallest lot and negative profit, and the one with the biggest lot and positive profit.
 
Arseniy Barudkin:
Greetings gentlemen! I don't want this to sound like flooding, but I cannot do without this function. So guys, can someone help me with this function which goes through all orders and deletes two orders at the same time, the one with the smallest lot and negative profit, and the one with the biggest lot and positive profit, also from existing orders.

Try searching forOrderCloseBy
 

How to make data of all global variables to be saved even after closing the terminal in normal and abnormal mode?

The question concerns variables declared at the beginning of the code like this:

#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern double  VAR1 = 1;

double VAR2[1000];

I need not to lose values of VAR1 and VAR2 (the array is filled with data during Expert Advisor's operation). So far, the only idea is to write data into a file (global.txt) at every tick and then, when the terminal is launched, read from the file (global.txt) and recreate the variables and then write them again at every tick. What other options are there?

 
smart_man:

How to make sure that data of all global variables are preserved even after the client terminal is closed in normal and abnormal modes?

The question concerns variables declared like this:

extern double  VAR1 = 1;

double VAR2[1000];

I need not to lose values of VAR1 and VAR2 (the array is filled with data during Expert Advisor's operation). So far, the only idea is to write data into a file (global.txt) at every tick and then, when the terminal is launched, read from the file (global.txt) and recreate the variables and then write them again at every tick. What other options are there?

Option

Global variables of the client terminal

//--------

Not to be confused with globally declared variables

 
fxtz:

Question Can I write a program in Metatrader 4 to open from 2 to 250 positions of my choice with a set stoploss and set profit in one click? I don't need to open one manually


Have you found a brokerage company where you can open so many orders for one instrument? Usually 100 orders is a limit for all instruments...

And yes, I could open 100.

 
Alekseu Fedotov:

Try searching forOrderCloseBy

Thank you! But I have to select tickets of these orders from at least 3 orders, often there are more from 5 to 10 orders, do I have to calculate and compare lots and profits of these orders separately?

 
Arseniy Barudkin: We have to select the order tickers, so why should we calculate and compare the lots and profits of these orders separately?

The situation is much worse than you imagine. Only one order, selected by OrderSelect(), is available at any given moment. And when the very first order is selected, what should we compare it with? The smart guys really do something like this

double МинПрофит=1000000, МаксПрофит=-1000000;
int МаксТикет=0, МинТикет=0;
for(int Номер=OrdersTotal-1; Номер>=0; Номер--)
{
   if(!OrderSelect(Номер,SELECT_BY_POS)) continue;
   if(OrderProfit()>МаксПрофит)
   {
      МаксТикет=OrderTicket();
      МаксПрофит=OrderProfit();
   }
   else if(OrderProfit()<МинПрофит)
   {
      МинТикет=OrderTicket();
      МинПрофит=OrderProfit();
   }
 }
Then look at the value of the tickets found - suddenly they are zero!!!
 
STARIJ:

The situation is much worse than you imagine. Only one order, selected by OrderSelect(), is available at any given moment. And when the very first order is selected, what should we compare it with? The smart guys really do something like this

Then look at the value of the found tickets - suddenly they are zero!!!

Thanks for the help! So far I left it like this, now I decided to change a bit the conditions under which the function is called. And in general, you have helped me a lot))
void MarginDefuse(double lot1=0,double lot2=0)
{
 int ticket1=0,ticket2=0,type=-1;
 double profit1=0,profit2=0;
 bool res1=false,res2=false;
 // Выделение
 for(int i=0; i<OrdersTotal(); i++)
 {
  if(!OrderSelect(i,SELECT_BY_POS)) continue; // Что-то помешало - идем к следующему
  if(OrderSymbol() != Symbol()) continue;          // Чужой график
  if(OrderType()<2 && OrderMagicNumber() == Magic)
  {
   if(OrderLots()>=lot1 && OrderProfit()<0)
   {
    ticket1=OrderTicket();
    lot1=OrderLots();
    type=OrderType();
    profit1=OrderProfit();
   }
  }
 }
 for(int i=OrdersTotal()-1; i>=0; i--)
 {
  if(!OrderSelect(i,SELECT_BY_POS)) continue; // Что-то помешало - идем к следующему
  if(OrderSymbol() != Symbol()) continue;          // Чужой график
  if(OrderType() != type && OrderType()<2 && OrderMagicNumber() == Magic)
  {
   if(OrderLots() >= lot2/Multipler && OrderProfit()>0 && OrderLots()>lot1)
   {
    ticket2 = OrderTicket();
    lot2 = OrderLots();
    profit2=OrderProfit();
   }
  }
 }
 if((profit1+profit2)>0 && lot1<lot2)
 {
  while (!RefreshRates());
  if(ticket1)
  {
   if(OrderSelect(ticket1,SELECT_BY_TICKET))
   res1=OrderCloseX(ticket1,lot1);
  }
  if(ticket2)
  {
   if(OrderSelect(ticket2,SELECT_BY_TICKET))
   res2=OrderCloseX(ticket2,lot2);
  }
  if(res1 && res2)
   DeletePos();
 }
}
Reason: