[ARCHIVE!] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Can't go anywhere without you - 4. - page 602

 
lottamer:

Please advise where to look to see how the condition "If the last take profit deal was > 0 then...." is coded.

i.e. accounting for trades already closed (automatically) .


https://www.mql5.com/ru/forum/131859

https://www.mql5.com/ru/forum/131859/page4#434230

Function isCloseLastPosByTake().

This function returns a flag to close the last position by Takei. Flag is raised - True - TakeProfit has triggered. Flag lowered - False - position was closed due to another reason. A more accurate selection of positions to be considered is specified using external parameters:

  • sy - Name of market instrument. If you specify this parameter, the function will consider only positions of the specified instrument. The default value "" means any market instrument. NULL means the current instrument.
  • op - Trade operation, position type. Valid values: OP_BUY, OP_SELL or -1. The default value -1 means any position.
  • mn - Position identifier, MagicNumber. Default value -1 means any identifier.
  • //+----------------------------------------------------------------------------+
    //|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
    //+----------------------------------------------------------------------------+
    //|  Версия   : 19.05.2008                                                     |
    //|  Описание : Возвращает флаг закрытия последней позиции по тейку.           |
    //+----------------------------------------------------------------------------+
    //|  Параметры:                                                                |
    //|    sy - наименование инструмента   (""   - любой символ,                   |
    //|                                     NULL - текущий символ)                 |
    //|    op - операция                   (-1   - любая позиция)                  |
    //|    mn - MagicNumber                (-1   - любой магик)                    |
    //+----------------------------------------------------------------------------+
    bool isCloseLastPosByTake(string sy="", int op=-1, int mn=-1) {
      datetime t;
      double   ocp, otp;
      int      dg, i, j=-1, k=OrdersHistoryTotal();
    
      if (sy=="0") sy=Symbol();
      for (i=0; i<k; i++) {
        if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) {
          if (OrderSymbol()==sy || sy=="") {
            if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
              if (op<0 || OrderType()==op) {
                if (mn<0 || OrderMagicNumber()==mn) {
                  if (t<OrderCloseTime()) {
                    t=OrderCloseTime();
                    j=i;
                  }
                }
              }
            }
          }
        }
      }
      if (OrderSelect(j, SELECT_BY_POS, MODE_HISTORY)) {
        dg=MarketInfo(sy, MODE_DIGITS);
        if (dg==0) if (StringFind(OrderSymbol(), "JPY")<0) dg=4; else dg=2;
        ocp=NormalizeDouble(OrderClosePrice(), dg);
        otp=NormalizeDouble(OrderTakeProfit(), dg);
        if (ocp==otp) return(True);
      }
      return(False);
    }

 
BeerGod:

https://www.mql5.com/ru/forum/131859

https://www.mql5.com/ru/forum/131859/page4#434230

The isCloseLastPosByTake() function.

This function returns a flag to close the last position by Take Profit. Flag is up - True - TakeProfit has triggered. Flag lowered - False - position was closed due to another reason. A more accurate selection of positions to be considered is specified using external parameters:

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




Thanks, I came across KIM's Libraries myself...(Comrade Kim apparently like MARX of the forex world - wrote CAPITAL...and we all quote it now (i.e. insert it into our EAs)) .... there are "flags" of recent trades in different variations...

except I have never used # includ-....

can you explain in a nutshell?

at the top I write # includ (file name mqh)

But in the code? just where do I put the function ?

GetTypeLastClosePos ();

and then what ?

WHAT of this will return the type of position to me ?

int GetTypeLastClosePos(string sy="", int mn=-1) {
  datetime t;
  int      i, k=OrdersHistoryTotal(), r=-1;

  if (sy=="0") sy=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) {
      if ((OrderSymbol()==sy || sy=="") && (mn<0 || OrderMagicNumber()==mn)) {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (t<OrderCloseTime()) {
            t=OrderCloseTime();
            r=OrderType();
          }
        }
      }
    }
  }
  return(r);
}

OrderType() ?

i.e. then if(OrderType() ==TRUE ) do this and that... right?

 
How are the functions for the tester different from those for online trading?
 
In theory, error handling in communication with the trade server.
 
lottamer:


Thanks, I myself came across KIM's Libraries ... (Comrade KIM probably wrote CAPITAL like MARKS of the Forex world ... and we all quote it now (i.e., insert it into our Expert Advisors) ...) there are "flags" of recent trades in different variations...

except I've never used # includ-....

can you explain in a nutshell?

at the top I write # include (file name mqh)

But in the code? just where do I put the function ?

GetTypeLastClosePos ();

and then what ?

WHAT of this will return the type of position to me ?

OrderType() ?

i.e. then if(OrderType() ==TRUE ) do this and that... right?

The functions should be inserted after return(0);

The GetTypeLastClosePos() function will return 0 if there was a buy, and 1 if there was a sell, or -1 if there was nothing in the history.

//+------------------------------------------------------------------+
//|                                                          123.mq4 |
//|                        Copyright 2012, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
if (isCloseLastPosByTake()==true) Comment("Профит"); else Comment("ХЗ");   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 19.05.2008                                                     |
//|  Описание : Возвращает флаг закрытия последней позиции по тейку.           |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
bool isCloseLastPosByTake(string sy="", int op=-1, int mn=-1) {
  datetime t;
  double   ocp, otp;
  int      dg, i, j=-1, k=OrdersHistoryTotal();

  if (sy=="0") sy=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) {
      if (OrderSymbol()==sy || sy=="") {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (op<0 || OrderType()==op) {
            if (mn<0 || OrderMagicNumber()==mn) {
              if (t<OrderCloseTime()) {
                t=OrderCloseTime();
                j=i;
              }
            }
          }
        }
      }
    }
  }
  if (OrderSelect(j, SELECT_BY_POS, MODE_HISTORY)) {
    dg=MarketInfo(sy, MODE_DIGITS);
    if (dg==0) if (StringFind(OrderSymbol(), "JPY")<0) dg=4; else dg=2;
    ocp=NormalizeDouble(OrderClosePrice(), dg);
    otp=NormalizeDouble(OrderTakeProfit(), dg);
    if (ocp==otp) return(True);
  }
  return(False);
}
 
BeerGod:

The function must be inserted after return(0);

The GetTypeLastClosePos() function will return 0 if there was a buy and 1 if there was a sell, or -1 if there is nothing in the history.



Got it, thanks.

And yet, if the function is in a #include file, then how should it be handled?

 
lottamer:


Got it, thanks.

and yet, if the function is in a #include file, then how do you dispose of it?


how do you understand #include?

Like in the doc or in your own way?

 

I need the owl to put a pending order exactly on the opening of a candle on D1.

I have written a function:

bool GetTimeToInput()
{
  if(TimeCurrent() == iTime(Symbol(),1440,0)
  {
    return(true);
  }
  else
    return(false);
}

The compiler frowns:

'\end_of_program' - unbalanced left parenthesis E:\Insall'd soft's\Forex\Alpari NZ MT4\experts\Gann_2Days.mq4 (228, 4)

Everyone has brackets. This is strange. If this function is commented out, the code compiles without errors. What may be wrong?

Am I correct in writing the function to open only at opening price of a daily candle?

 

hoz:

Everyone's brackets are there, it's strange.

no. you have the right dog missing in the if line


and maybe don't say the word "weird" when it comes to the compiler. Of course, it's hard to see the log in your own eye :)

And did I actually write the correct function to open only at the opening price of the day's candle?

no
 
lottamer:


The developers had nothing to do with it. :__

Instead of ( Hour()>=10 || Hour()<20 ) you should have made it simple Hour()>=10 && Hour()<20

and everything worked


The question is not that it worked, but that if the help is written correctly, it should not work.