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

 
Falx:

Good day all!

Please give me a command for the robot not to open more than 1 order per 1 candle.

Even if he took his take on this candle - not to open the next order on the same candle.

Now it comes out that if the conditions of the indicators are fulfilled - open a trade.

When a deal on the take is closed, it immediately opens a new deal on the same candlestick, and here it is no longer needed and creates problems for me.

I feel it is important to give some feedback.

It needs a strictly so 1 candle - 1 deal.

Many thanks in advance!

Thetime of opening a position, the time to close the position - the candle at which this time and the opening time of this candle.
 
Falx:

Good day all!

Please give me a command for the robot not to open more than 1 order per 1 candle.

Even if he took his take on this candle - not to open the next order on the same candle.

Now it comes out that if the conditions of the indicators are fulfilled - open a trade.

When a deal on the take is closed, it immediately opens a new deal on the same candlestick, and here it is no longer needed and creates problems for me.

I feel it is important to give some feedback.

It needs a strictly so 1 candle - 1 deal.

Thanks in advance!

You can also use the flag.

Declare a static bool variable or a global variable, let it be flag. Order opened - flag = true, a new candlestick opened - flag = false and add this flag to the condition of order opening.

 
Alexey Viktorov:

You can also use a flag.

Declare static bool variable or variable of global level, let it be flag. Order opened - flag = true, a new candle opened - flag = false and add this flag to the order open condition.

The flag should be saved in global terminal and restored at its restart.
 
Artyom Trishkin:
The flag should be saved in global terminal and restored at its restart.

It depends on what period the trade is on and the ability to restart the terminal within one bar to open an order and restart the terminal.

I don't like GV and try to do without it. When you start an EA, you should gather all information about open orders anyway so why not restore the value of the flag at startup? This is one option. The second variant is that the initial value of the flag prohibits opening of orders and only the next bar will give permission. So, even if we opened and restarted the EA on the current bar, the flag will anyway prohibit us from opening orders until the next bar.

If you give such advice with such subtleties, you will not have to think about anything. So the number of questions will increase accordingly... Why decide something yourself... Ask on the forum and you'll get a working version. And the only thing left is to put the product on the market...

 
Alexey Viktorov:

It depends on what period the trade is on and the ability to restart the terminal within one bar to open an order and restart the terminal.

I don't like GV and try to do without it. When you start an EA, you should gather all information about open orders anyway so why not restore the value of the flag at startup? This is one option. The second variant is that the initial value of the flag prohibits opening of orders and only the next bar will give permission. So, even if we have a new order opening on the current bar and restart the EA, the flag will anyway prohibit opening of orders until the next bar.

Just by giving advice with such subtleties, we will not have to think about anything. Consequently, the number of questions may increase. Why decide something yourself... Ask on the forum and you'll get a working version. And the only thing left is to put the product on the market...

:)
I'm in the habit of looking straight ahead.
For me, precisely because flags still need to be restored, it's better to look at the position's opening bar, and dance around it, and not bother with flags and their storage.
 
Artyom Trishkin:
:)
I am in the habit of looking straight ahead.
I think it's better to look at the opening bar of a position, and then start dancing around it, without having to worry about flags and storing them.

It's the same reason why we like different women.

What difference does it make, the flag or the time of opening a position? No matter how you spin it, you still have to check something...
 

Hi! Can you tell me how to return the number of last losing orders, after profitable ones?

Here is my code, what is wrong?

int GetProfitLoss()// Loss on closed orders (number of last losing orders)

{

int Loss = 0;

datetime lastCloseTime = 0;

int cnt = OrdersHistoryTotal();

for(int i=0; i < cnt; i++)

{

if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))

{

if (OrderSymbol() == Symbol() && OrderMagicNumber() == OrderId && lastCloseTime < OrderCloseTime())

{

lastCloseTime = OrderCloseTime();

}

if (OrderProfit()<0)

{

Loss ++;

}

if(OrderProfit()>0)

{

break; // Exit the loop on the first profitable one encountered }

}

}

}

}

return(Loss);

}

 
Falx:

Good day all!

Please give me a command for the robot not to open more than 1 order per 1 candle.

Even if he took his take on this candle - not to open the next order on the same candle.

Now it comes out that if the conditions of the indicators are fulfilled - open a trade.

When a deal on the take is closed, it immediately opens a new deal on the same candlestick, and here it is no longer needed and creates problems for me.

I feel it is important to give some feedback.

It needs a strictly so 1 candle - 1 deal.

Thanks in advance!

Function NumberOfBarCloseLastPos().

This function returns the bar number of the last position to be closed, or-1. The selection of positions to be considered is set by external parameters:

  • sy- Name of market instrument. If this parameter is set, the function will only consider positions of this instrument. The default value -NULL denotes the current market instrument.
  • tf- Timeframe. The default value0 means the current timeframe.
  • op- Trade operation, position type. Valid values:OP_BUY,OP_SELL or-1. The default value-1 means any position.
  • mn- Position identifier, MagicNumber. The default value of-1 means any identifier.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 19.02.2008                                                     |
//|  Описание : Возвращает номер бара закрытия последней позиции или -1.       |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   ("" или NULL - текущий символ)          |
//|    tf - таймфрейм                  (    0       - текущий таймфрейм)       |
//|    op - операция                   (   -1       - любая позиция)           |
//|    mn - MagicNumber                (   -1       - любой магик)             |
//+----------------------------------------------------------------------------+
int NumberOfBarCloseLastPos(string sy="0", int tf=0, int op=-1, int mn=-1) {
  datetime t=0;
  int      i, k=OrdersHistoryTotal();

  if (sy=="" || sy=="0") sy=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) {
      if (OrderSymbol()==sy) {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (op<0 || OrderType()==op) {
            if (mn<0 || OrderMagicNumber()==mn) {
              if (t<OrderCloseTime()) t=OrderCloseTime();
            }
          }
        }
      }
    }
  }
  return(iBarShift(sy, tf, t, True));
}
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 19.02.2008                                                     |
//|  Описание : Возвращает номер бара открытия последней позиции или -1.       |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   ("" или NULL - текущий символ)          |
//|    tf - таймфрейм                  (    0       - текущий таймфрейм)       |
//|    op - операция                   (   -1       - любая позиция)           |
//|    mn - MagicNumber                (   -1       - любой магик)             |
//+----------------------------------------------------------------------------+
int NumberOfBarOpenLastPos(string sy="0", int tf=0, int op=-1, int mn=-1) {
  datetime t=0;
  int      i, k=OrdersTotal();

  if (sy=="" || sy=="0") sy=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if (OrderSymbol()==sy) {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (op<0 || OrderType()==op) {
            if (mn<0 || OrderMagicNumber()==mn) {
              if (t<OrderOpenTime()) t=OrderOpenTime();
            }
          }
        }
      }
    }
  }
  return(iBarShift(sy, tf, t, True));
}

The NumberOfBarOpenLastPos() function.

This function returns the bar number of the last position opened or-1. The selection of positions to be considered is defined by external parameters:

  • sy- Name of market instrument. If this parameter is set, the function will only consider positions of the specified instrument. The default value -NULL denotes the current market instrument.
  • tf- Timeframe. The default value0 means the current timeframe.
  • op- Trade operation, position type. Valid values:OP_BUY,OP_SELL or-1. The default value-1 means any position.
  • mn- Position identifier, MagicNumber. The default value of-1 means any identifier.

Then using these functions check the condition you need.

 

Please help.

I need to add the following function to my robot:

After opening a deal, in 40 seconds if the profit is more than 15 pips, the deal is closed instantly, if less, the takeprofit is moved to the profit level of 15 pips.

Thank you in advance.

 
Vladimir Zubov:

The NumberOfBarCloseLastPos() function.

This function returns the bar close number of the last position or-1. The selection of positions to be taken into account is defined by external parameters:

  • sy- Name of market instrument. If this parameter is set, the function will only consider positions of this instrument. The default value -NULL denotes the current market instrument.
  • tf- Timeframe. The default value -0 indicates the current timeframe.
  • op- Trade operation, position type. Valid values:OP_BUY,OP_SELL or-1. The default value-1 means any position.
  • mn- Position identifier, MagicNumber. The default value of-1 means any identifier.

The NumberOfBarOpenLastPos() function.

This function returns the bar number of the last position opened or-1. The selection of positions to be considered is defined by external parameters:

  • sy- Name of market instrument. If this parameter is set, the function will only consider positions of the specified instrument. The default value -NULL denotes the current market instrument.
  • tf- Timeframe. The default value0 means the current timeframe.
  • op- Trade operation, position type. Valid values:OP_BUY,OP_SELL or-1. The default value-1 means any position.
  • mn- Position identifier, MagicNumber. The default value of-1 means any identifier.

Then use these functions to check the condition you need.


Thank you very much!

I will look into it!!!

Reason: