[WARNING CLOSED!] Any newbie question, so as not to clutter up the forum. Professionals, don't go by. Can't go anywhere without you. - page 285

 
rid >> :

Perhaps the function https://www.mql5.com/ru/forum/107476/page21 can help.

Function isCloseLastPosByTake().
This function returns a flag to close the last position on the take.

Flag up - True - TakeProfit was triggered.

Flag lowered - False - position was closed due to another reason.


The function itself should be placed at the very end of the code. I also add to the condition to sell:


Good day, evening or morning to all of YOU.

I did everything as I wrote it. I put the function at the very end of code and added it to my conditions for sale.

if( isCloseLastPosByTake( NULL, OP_BUY, MagicNumber) ==true) {//продаем

I just thought about it and added it for buying. Orders will be closed not only by BUY, but also by SHELL.

I have inserted the isCloseLasPosByTake() function at the end.

2;116;C:\Program Files\MetaTrader - Alpari\experts\SimpleMA22333.mq4;104:33;'(' - function definition unexpected
2;75;C:\Program Files\MetaTrader - Alpari\experts\SimpleMA22333.mq4;109:7;'sy' - variable not defined
2;75;C:\Program Files\MetaTrader - Alpari\experts\SimpleMA22333.mq4;109:14;'sy' - variable not defined
2;75;C:\Program Files\MetaTrader - Alpari\experts\SimpleMA22333.mq4;112:27;'sy' - variable not defined
2;75;C:\Program Files\MetaTrader - Alpari\experts\SimpleMA22333.mq4;112:33;'sy' - variable not defined
2;75;C:\Program Files\MetaTrader - Alpari\experts\SimpleMA22333.mq4;114:15;'op' - variable not defined
2;75;C:\Program Files\MetaTrader - Alpari\experts\SimpleMA22333.mq4;114:37;'op' - variable not defined
2;75;C:\Program Files\MetaTrader - Alpari\experts\SimpleMA22333.mq4;115:17;'mn' - variable not defined
2;75;C:\Program Files\MetaTrader - Alpari\experts\SimpleMA22333.mq4;115:46;'mn' - variable not defined
2;75;C:\Program Files\MetaTrader - Alpari\experts\SimpleMA22333.mq4;116:19;'t' - variable not defined
2;75;C:\Program Files\MetaTrader - Alpari\experts\SimpleMA22333.mq4;117:17;'t' - variable not defined
2;75;C:\Program Files\MetaTrader - Alpari\experts\SimpleMA22333.mq4;127:19;'sy' - variable not defined

Here's such a pie, or as was saying pfc Gotsman oil painting. help the dummy out.... don't know anything.......

the code itself (i.e. just the start)

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
    // Узнаем уровень стопов и спрэд
    int Spread = MarketInfo(Symbol(), MODE_SPREAD);
    int StopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL);

    // Рассчитываем значения средних на 0-ом и 1-ом барах
    double MAF_0 = iMA(NULL, 0, MAFP, 0, 1, 1, 0);
    double MAF_1 = iMA(NULL, 0, MAFP, 0, 1, 1, 1);
    double MAS_0 = iMA(NULL, 0, MASP, 0, 1, 1, 0);
    double MAS_1 = iMA(NULL, 0, MASP, 0, 1, 1, 1);
    
    // открытие позиции Buy покупка
    if( MAF_1 < MAS_1 && MAF_0 > MAS_0)
    if( isCloseLastPosByTake( NULL, OP_SELL, MagicNumber) ==true) {//покупаем
     if( CheckOrders(OP_SELL))
      {
       if( StopLoss <= StopLevel+ Spread)
         double SL = 0;
        else
         SL = Ask - StopLoss*Point;
       if( TakeProfit <= StopLevel- Spread)
         double TP = 0;
        else
         TP = Ask + TakeProfit*Point;
       if(!OrderSend(Symbol(), OP_BUY, Lots, Ask, 5, SL, TP, NULL, MagicNumber))
         Print("Не открыт ордер Buy. Ошибка №", GetLastError()); 
       }
    
    // Открытие позиции Sell продажа  
    if( MAF_1 > MAS_1 && MAF_0 < MAS_0)
    if( isCloseLastPosByTake( NULL, OP_BUY, MagicNumber) ==true) {//продаем
     if( CheckOrders(OP_BUY))
      {
       if( StopLoss <= StopLevel+ Spread)
         SL = 0;
        else
         SL = Bid + StopLoss*Point;
       if( TakeProfit <= StopLevel- Spread)
         TP = 0;
        else
         TP = Bid - TakeProfit*Point;
       if(!OrderSend(Symbol(), OP_SELL, Lots, Bid, 5, SL, TP, NULL, MagicNumber))
         Print("Не открыт ордер Sell. Ошибка №", GetLastError()); 
       }
       bool isCloseLastPosByTake(string sy=NULL, 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==NULL) {
        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);
}
//----
   return(0);
  }//+------------------------------------------------------------------+
 
igrok2008 >> :


I specifically wrote in my post. That this function isCloseLastPosByTake() - must be placed at the very end of code, BEFORE the START FUNCTION!

And where did you put it?

And be careful with curly brackets in buying and selling conditions. You are clearly missing them there (check - that the number of open brackets equals the number of closed ones). Here corrected the sale :

    //  ----- Открытие позиции Sell продажа------------  
    if( MAF_1 > MAS_1 && MAF_0 < MAS_0)                         {
    if( isCloseLastPosByTake( NULL, OP_BUY, MagicNumber) ==true) {
     if( CheckOrders(OP_BUY))                                     {
       if( StopLoss <= StopLevel+ Spread)
         SL = 0;        else         SL = Bid + StopLoss*Point;
       if( TakeProfit <= StopLevel- Spread)
         TP = 0;        else         TP = Bid - TakeProfit*Point;
       if(!OrderSend(Symbol(), OP_SELL, Lots, Bid, 5, SL, TP, NULL, MagicNumber))
         Print("Не открыт ордер Sell. Ошибка №", GetLastError()); 
                 }
                }
               }
      //---конец блока продажи ------------------------------
 

Good day!!!


I need some help! I apologize for not being modest.

But here is my problem, I have an EA which is based on a Martin, it places the first order SELL or BUY, whatever I want, but I want to be able to choose BUY or SELL in EA properties. I.e. I put e.g. BUY in its properties and it opens an BUY only order, or the opposite.

I'm afraid I haven't learned how to program in MQL4 yet.


I am attaching the advisor itself.


Thanks in advance to those who would like to help!

 
rid >> :

I specifically wrote in my post. That this function isCloseLastPosByTake() - must be placed at the very end of code, BEFORE the START FUNCTION!

And where did you put it?

And be careful with curly brackets in buying and selling conditions. You are clearly missing them there (check - that the number of open brackets equals the number of closed ones). Here's the fix for the sale :


I put it in after the start fi, I understand.....

I've got it wrong then, thanks for the poke :-))

If I got it right, it should be put after } at the end of expert deinitialization function block ??????? or after return(0); at the very end of start-???????

Thanks in advance for your patience!!!!!!!!!

 
igrok2008 >> :

I put it in after the start function, so I understand.....

I've got it wrong, thanks for the poke :-))

If I understood correctly, it should be put after } at the end of expert deinitialization function block ??????? or after return(0); at the very end of start-???????

Thanks in advance for your patience!!!!!!!!!

It must be placed outside. Not inside any function, - but as a separate independent function.

Like this:

int start()
  {
//----
    // Узнаем уровень стопов и спрэд
    int Spread = MarketInfo(Symbol(), MODE_SPREAD);
    int StopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL);

    // Рассчитываем значения средних на 0-ом и 1-ом барах
    double MAF_0 = iMA(NULL, 0, MAFP, 0, 1, 1, 0);
    double MAF_1 = iMA(NULL, 0, MAFP, 0, 1, 1, 1);
    double MAS_0 = iMA(NULL, 0, MASP, 0, 1, 1, 0);
    double MAS_1 = iMA(NULL, 0, MASP, 0, 1, 1, 1);
    
    // открытие позиции Buy покупка
    if( MAF_1 < MAS_1 && MAF_0 > MAS_0)                         {
    if( isCloseLastPosByTake( NULL, OP_SELL, MagicNumber) ==true) {
     if( CheckOrders(OP_SELL))                                      {
       if( StopLoss <= StopLevel+ Spread)
         double SL = 0;        else         SL = Ask - StopLoss*Point;
       if( TakeProfit <= StopLevel- Spread)
         double TP = 0;        else         TP = Ask + TakeProfit*Point;
       if(!OrderSend(Symbol(), OP_BUY, Lots, Ask, 5, SL, TP, NULL, MagicNumber))
         Print("Не открыт ордер Buy. Ошибка №", GetLastError()); 
                }
               }
              }
//-------------------Конец покупки -----------------------

    //  ----- Открытие позиции Sell продажа------------  
    if( MAF_1 > MAS_1 && MAF_0 < MAS_0)                         {
    if( isCloseLastPosByTake( NULL, OP_BUY, MagicNumber) ==true) {
     if( CheckOrders(OP_BUY))                                     {
       if( StopLoss <= StopLevel+ Spread)
         SL = 0;        else         SL = Bid + StopLoss*Point;
       if( TakeProfit <= StopLevel- Spread)
         TP = 0;        else         TP = Bid - TakeProfit*Point;
       if(!OrderSend(Symbol(), OP_SELL, Lots, Bid, 5, SL, TP, NULL, MagicNumber))
         Print("Не открыт ордер Sell. Ошибка №", GetLastError()); 
                 }
                }
               }
      //---конец блока продажи ------------------------------
    
//----
   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);
}


I'm not sure that under these conditions, the Expert Advisor will work. Because the very first position will not be able to open...

The function isCloseLastPosByTake() should be removed from a Buy or Sell condition.

Then the advisor will work.

 
Rover82 >> :

Good Day!!!

But here is my problem, I have an EA which is based on a Martin, it places the first order SELL or BUY, whatever I want, but I want to be able to choose BUY or SELL in EA properties. I.e. I put for example BUY in its properties and it opens an BUY only order, or vice versa.

I am attaching the EA itself.

Thanks in advance to everyone who responds!


It is unlikely that anyone will respond. The EA is decompiled (hacked) and such files are not welcome here...

 
WinExec("C:\\Program Files\\Opera\\opera.exe http://www.mail.ru",2);
Hi all, this is launching the link with a specific browser, but how do I launch the same link by default?
 
I.e. with the default browser
 
Dimoncheg >> :
I.e. the default browser

ShellExecute, the "open" command, as it were.

 

I have decided to study MQL 4 since everything I have found does not meet my aspirations. At the moment I am studying in order to understand how professionals work and improve it for my idea. There is a (simple and small, one page) custom function, the code is attached. Due to my amateurishness, I can't for two days understand - what this function returns as a result, and which nested condition is the Else?

Would anyone care to plant a spark of knowledge in my dullness?

What I get is this:

The purpose of the function is to determine if there is already an order placed for this pair and if there is, determine its type.

The function that called this function waits for the presence of one of the orders selected in the procedure switch, The function should return "1", in the presence of two orders - "2", and in the absence of orders "-1".

The cycle is started if l_pos_16= 0. Let us assume we have four pending orders in total for some pairs, including two at the pair where the EA is working and they are not the first.

However, at the first iteration of the loop, the function if(OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES)) check the position with the number "0" in the list of orders, but there is no such position (because I understand the numbering is starting from position 1) and then it will pass to the next one. else with complete exit from fCountOrders(int &a_count_0, int &a_count_4, int &a_count_8, int &a_count_12) with return of the value Return(-1).

The sense of the function as a whole is lost, since regardless of , the result should be the same regardless of presence or absence of orders for the given pair.

However, when the EA works, this function defines and gives you what the called function expects from it, which then works depending on the obtained result .

I also do not understand what else Return(-1) will work in

if (OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES)) {

Or same in if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic_N) {

The code of function:

int fCountOrders(int &a_count_0, int &a_count_4, int &a_count_8, int &a_count_12)

a_count_0= 0;

a_count_4 = 0;

a_count_8 = 0;

a_count_12 = 0;

gi_252 = 0;

for (int l_pos_16 = 0; l_pos_16 < OrdersTotal(); l_pos_16++) {

if (OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES)) {

if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic_N) {

switch (OrderType()) case OP_BUY:

a_count_0++;

gi_252 = 1;

g_ticket_268 = OrderTicket();

gs_272 = OrderComment();

g_ord_open_price_256 = OrderOpenPrice();

g_ord_stoploss_288 = OrderStopLoss();

g_ord_takeprofit_296 = OrderTakeProfit();

break;

case OP_SELL:

a_count_4++;

gi_252 = -1;

g_ticket_268 = OrderTicket();

gs_272 = OrderComment();

g_ord_open_price_256 = OrderOpenPrice();

g_ord_stoploss_288 = OrderStopLoss();

g_ord_takeprofit_296 = OrderTakeProfit();

break;

case OP_BUYSTOP:

a_count_8++;

break;

case OP_SELLSTOP:

a_count_12++;

}

}

} else return (-1);

}

return (a_count_0 + a_count_4 + a_count_8 + a_count_12);

/* normal execution of the f-i will return one in the variable in one of case and "0" in all others, which indicates that there is one of

of the order types, and the number of this order is available to the calling function,

comment, open price, stop loss and take profit */

}

Reason: