An advisor that would follow the rate on a five-minute chart with conditions after launch: - page 12

 

Insert a function at the end of the code -

//+----------------------------------------------------------------------------+
//|  Возвращает номер бара открытия последней позиции или -1.                  |
//|  Параметры:                                                                |
//|    sym - наименование инструмента  ("" - текущий символ)                   |
//|    tf  - таймфрейм                 ( 0 - текущий таймфрейм)                |
//|    op  - операция                  (-1 - любая позиция)                    |
//|    mn  - MagicNumber               (-1 - любой магик)                      |
//+----------------------------------------------------------------------------+
int NumberOfBarOpenLastPos(string sym="", int tf=0, int op=-1, int mn=-1) {
  datetime oot;
  int      i, k=OrdersTotal();
 
  if (sym=="") sym=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if (OrderSymbol()==sym) {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (op<0 || OrderType()==op) {
            if (mn<0 || OrderMagicNumber()==mn) {
              if (oot<OrderOpenTime()) oot=OrderOpenTime();
            }
          }
        }
      }
    }
  }
  return(iBarShift(sym, tf, oot, True));
}

And to make it work, insert the line before the buy/sell :

if (NumberOfBarOpenLastPos(здесь сам всё расставь)>0) {//если на текущ. баре не было сделок 
//ххххххххххххххххххххххххххххххххххххххххххххххххххххххххххххххххххххххххххххххххх
if (Bid - iOpen(NULL,0,0)>=Delta*Point) //Цена выросла на больше или = Delta пунктов
//продаем-
{
ticket=OrderSend(Symbol(),1,Lots,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,
                 "продал",MagicNumber,22222,Red);
 
if(ticket<0){Print("Ошибка открытия ордера SELL #",GetLastError());return(0);}
}
//-------------------------------------------------------------------------
if (iOpen(NULL,0,0)-Bid  >Delta*Point) //цена упала более дельты - покупаем
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Bid-StopLoss*Point,Ask+TakeProfit*Point,
            "Купил",MagicNumber,11111,Green);
 
if(ticket<0){Print("Ошибка открытия ордера BUY #",GetLastError());return(0);}
}
//ххххххххххххххххххххххххххххххххххххххххххххххххххххххххххххххххххххххххххххххх
}
and see what happens.
 
--
NumberOfBarOpenLastPos( "", 0,  -1, MagicNumber)
 
What a brainteaser.....
 
I'll do it tomorrow, thanks...
Still mistakes... mistakes...
 
What kind of errors are you getting? Copy it here.
 
That's how I did it, it compiled fine. There won't be any pitfalls?

//------------------------------
 //+----------------------------------------------------------------------------+
//|  Возвращает номер бара открытия последней позиции или -1.                  |
//|  Параметры:                                                                |
//|    sym - наименование инструмента  ("" - текущий символ)                   |
//|    tf  - таймфрейм                 ( 0 - текущий таймфрейм)                |
//|    op  - операция                  (-1 - любая позиция)                    |
//|    mn  - MagicNumber               (-1 - любой магик)                      |
//+----------------------------------------------------------------------------+
int NumberOfBarOpenLastPos(string sym="", int tf=0, int op=-1, int mn=-1) {
  datetime oot;
  int      i, k=OrdersTotal();
 
  if (sym=="") sym=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if (OrderSymbol()==sym) {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (op<0 || OrderType()==op) {
            if (mn<0 || OrderMagicNumber()==mn) {
              if (oot<OrderOpenTime()) oot=OrderOpenTime();
            }
          }
        }
      }
    }
  }
  return(iBarShift(sym, tf, oot, True));
}

int start()
{
if (NumberOfBarOpenLastPos()>0)
if(OrdersTotal()!=0)  return; //Выполнять только одну сделку.
//---

if (Ask - iOpen(NULL,0,0)>=Delta*Point) //Цена выросла  больше Delta пунктов - продаем!!!

{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"Продал");
 }
//-------------------------------------------------------------------------

if (iOpen(NULL,0,0)-Bid  >=Delta*Point) //цена упала больше Delta пунктов - покупаем!!!
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Bid-StopLoss*Point,Ask+TakeProfit*Point,"Купил");
 }
//----
return(0);
}
And also, will this line in the code be redundant? :
if(OrdersTotal()!=0)  return; //Выполнять только одну сделку.
 
Another mishap.
Trying to make the above code work. An error appears in the log with the text:
18:38:23 '153085': order sell 1.00 GBPUSD opening at 2.0006 sl: 2.0019 tp: 2.0003 failed [Invalid S/L or T/P]
What is the meaning of this?
 
int start()
{
int total=OrdersTotal();
   if(total<1)                           {// если нет открытых позиций
if (NumberOfBarOpenLastPos("", 0,  -1, MagicNumber)>0){
 
//---
 
if (Ask - iOpen(NULL,0,0)>=Delta*Point) //Цена выросла  больше Delta пунктов - продаем!!!
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"Продал");
if(ticket<0){Print("Ошибка открытия ордера SELL #",GetLastError());return(0);}
 
 }
//-------------------------------------------------------------------------
 
if (iOpen(NULL,0,0)-Bid  >=Delta*Point) //цена упала больше Delta пунктов - покупаем!!!
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Bid-StopLoss*Point,Ask+TakeProfit*Point,"Купил");
if(ticket<0){Print("Ошибка открытия ордера BUY #",GetLastError());return(0);}
 }
//----
} //if (NumberOfBarOpenLastPos
}//if(OrdersTotal
return(0);
}
//------------------------------
 //+----------------------------------------------------------------------------+
//|  Возвращает номер бара открытия последней позиции или -1.                  |
//|  Параметры:                                                                |
//|    sym - наименование инструмента  ("" - текущий символ)                   |
//|    tf  - таймфрейм                 ( 0 - текущий таймфрейм)                |
//|    op  - операция                  (-1 - любая позиция)                    |
//|    mn  - MagicNumber               (-1 - любой магик)                      |
//+----------------------------------------------------------------------------+
int NumberOfBarOpenLastPos(string sym="", int tf=0, int op=-1, int mn=-1) {
  datetime oot;
  int      i, k=OrdersTotal();
 
  if (sym=="") sym=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if (OrderSymbol()==sym) {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (op<0 || OrderType()==op) {
            if (mn<0 || OrderMagicNumber()==mn) {
              if (oot<OrderOpenTime()) oot=OrderOpenTime();
            }
          }
        }
      }
    }
  }
  return(iBarShift(sym, tf, oot, True));
}
That's what I'd do...
 
Slightly. wrong. Corrected above...
 
salesman77:
Again a problem.
I am trying the above described code. An error appears in the log with the text:
18:38:23 '153085': order sell 1.00 GBPUSD opening at 2.0006 sl: 2.0019 tp: 2.0003 failed [Invalid S/L or T/P]
What is the meaning of this?


1) And what DT do you use for MT4?

2) What is the allowable distance of stoploss and takeprofit in this brokerage house?

Reason: