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

 
Mihail Matkovskij #:

Then you have to formulate the questions correctly. You only get 1 signal. And it is at the opening of a new bar. And if there is only 1 signal, how many orders will be closed? And only if the signal appears on a new bar. And, if there is no signal, the orders will be closed on the next bars. And again, if there is a signal.

In such cases a flow chart usually helps. Or we need to describe the algorithm in detail point by point on a piece of paper or in a text file. And only then you will understand how to write your code.

 

Need function to close 1 order opened first among all available orders. who can share.

 
GlaVredFX #:

So if you've noticed, the algorithm is written in the code.

1) New bar appears

2) If the MA is higher than the previous bar close price, go to step 3 if the MA is below the close price, go to step 4.

3)If we have a BUY position, we close one position with maximum profit in the currency pair.

3.1)If there is no open BUY order,open a SELL position.

4) If we have a BUY position, we close one position with a maximum profit in the currency pair.

4.1)If there is no open SELL order we open a BUY position.


Now again I have a question, what is wrong in this code of the above described algorithm:

This code contains a set of commands. The terminal does not know what you need, it only knows what is written in the code!

...

if(isNewBar()) {  
  if(Signal > clos ) {  //--------------------- МА выше цены закрытия предыдущего бара.
    if(ExistPositions( NULL, OP_BUY, Magic)) //---------------- Если есть позиция BUY то
      ClosePosWithMaxProfitInCurrency(); //--------  закрытие одной позиции с максимальным профитом в валюте депозита.
    if(!ExistPositions( NULL, OP_BUY, Magic)) //----------- если позиции BUY отсутствуют то
      sellSignal = true;
  } 

 // и аналогично для п.4.

} 
return; 

...

void OnTimer() {
  if (sellSignal) {
    ticket=OpenPosition(NULL,OP_SELL, NDLot(Lot),0,0, Magic, Com); //------------ открываем позицию SELL
    sellSignal = false;
  }
  // и аналогично для buy
}
Write the buySignal andsellSignal variables globally.
 
Mihail Matkovskij #:

A set of commands is written in the code. The terminal does not know what you need, it only knows what is written in the code!

I deleted this message because the problem is that orders should be closed not by profit but by time.

This is because we have profit not always and not always where it should be.

Therefore, we need the function"Close one position opened at the very beginning of all available positions".

 
GlaVredFX #:

I deleted this message, because the problem is that orders should be closed according to time rather than profit.

Because the profit is not always there and not always where it should be.

Therefore, we need the function"Close one position opened at the very beginning of all available positions".

I have added an example of position opening in the timer in the previous post. So that the position is guaranteed to be opened. You can substitute your own function there.

 
Mihail Matkovskij #:

Added an example of a timer position opening in the previous post. So that the position is guaranteed to open. You can substitute your own function there.

But not like this. You need to monitor the result of

ClosePosWithMaxProfitInCurrency

or a new function of yours in OnTradeTransaction. And if there are no positions, it will enter. Or write the signal in buySignal orsellSignal and process it in OnTimer as I have shown in the example.

 
Mihail Matkovskij #:

Not so, though. You need to keep track of the result of

or your new function in OnTradeTransaction. And if there are no positions, you should enter. Or write the signal in buySignal orsellSignal and process it in OnTimer as I have shown in the example.

ClosePosWithMaxProfitInCurrency

This function is no longer relevant. It is not suitable. Instead of it, we need to substitute the function that will close one order opened first, if it exists.

I do not understand the buySignal andsellSignal variables written on the global scope. But when I try to compile it, it gives me the following error

'sellSignal' - some operator expected   

expression has no effect        

'buySignal' - some operator expected    

expression has no effect        



 

Good afternoon.

I've certainly read all of this, and I can write out each order separately, but the question was to shorten the code.

 
Good afternoon, colleagues! Please advise a newbie, how to get the current value (at the moment) of the indicator, rather than from the previous bar? The Expert Advisor only triggers when the previous bar finishes, and I need it earlier.
 
makssub #:

Good afternoon.

I've certainly read all of this, and I can write out each order separately, but the question was to shorten the code.

What exactly is the code shortening not working out?

Reason: