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

 
solnce600:

Gentlemen, please tell me how to code (probably in a conditional statement) a simple idea

-if a trade was closed at a stop loss

-open another trade at the stop loss price

Thank you

Have a look around.
 
Roger:

1. You cannot open orders in the init() function. (In the Strategy Tester, this will still work, but it will not work on the demo).

2. When you close orders, you should count from the end to the beginning, and not vice versa, like in your case.

Thank you for your reply.
In this case the loop operator was investigated in the tester, that is why orders are opened in init() for simplification, this does not happen in real EAs.
A change in the order of order selection does indeed lead to the closure of orders within one tick (I give the code of the program and report from the tester log below).
It seems strange that MQL4 does not allow me to change the order of working with orders. If you know why, please advise, since according to the work logic of an EA I am going to close orders exactly in the order they are opened.

Sincerely. Shurkin.

Код программы.

//+------------------------------------------------------------------+
//|                                                     Poligon1.mq4 |
//|                                            21 февраля 2013 года. |
//|Тестирование оператора цикла for.                                 |
//+------------------------------------------------------------------+
#property copyright "21 февраля 2013 года."
#property link      ""
//+------------------------------------------------------------------+
//|Объявленные переменные                                            |
//+------------------------------------------------------------------+
int Mn=357;//Magic number
int i;//Показатель счётчика итераций
double PriTP;//TP серии
int Tick;//Счётчик тиков
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----Установка серии ордеров
OrderSend(Symbol(),OP_BUY,0.5,Ask,0,NormalizeDouble(Bid-35*Point,Digits),
NormalizeDouble(Ask+35*Point,Digits),NULL,Mn,0,CLR_NONE);//OP_BUY
OrderSend(Symbol(),OP_BUY,1,Ask,0,NormalizeDouble(Bid-25*Point,Digits),
NormalizeDouble(Ask+25*Point,Digits),NULL,Mn,0,CLR_NONE);//OP_BUY
OrderSend(Symbol(),OP_BUY,1.5,Ask,0,NormalizeDouble(Bid-15*Point,Digits),
NormalizeDouble(Ask+15*Point,Digits),NULL,Mn,0,CLR_NONE);//OP_BUY
OrderSend(Symbol(),OP_BUY,2.5,Ask,0,NormalizeDouble(Bid-15*Point,Digits),
NormalizeDouble(Ask+15*Point,Digits),NULL,Mn,0,CLR_NONE);//OP_BUY
OrderSend(Symbol(),OP_BUY,3.5,Ask,0,NormalizeDouble(Bid-15*Point,Digits),
NormalizeDouble(Ask+15*Point,Digits),NULL,Mn,0,CLR_NONE);//OP_BUY
OrderSend(Symbol(),OP_BUYLIMIT,1,NormalizeDouble(Ask-15*Point,Digits),0,
NormalizeDouble(Bid-40*Point,Digits),NormalizeDouble(Ask+25*Point,Digits),
NULL,Mn,0,CLR_NONE);//OP_BUYLIMIT
OrderSend(Symbol(),OP_SELLSTOP,3,NormalizeDouble(Ask-30*Point,Digits),0,
NormalizeDouble(Bid+15*Point,Digits),NormalizeDouble(Ask-75*Point,Digits),
NULL,Mn,0,CLR_NONE);//OP_SELLSTOP
//----
   PriTP=Bid;//PriTP
   Print("Кс0:"," OrdersTotal=",OrdersTotal());//Сообщение
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   Tick++;//Tick
//   Print("Кс1:"," Tick=",Tick);//Сообщение
//----
//   for(i=0;i<OrdersTotal();i++)//Инициализация цикла
   for(i=OrdersTotal()-1; i>=0; i--)//
   {
   Print("Кс2:"," Tick=",Tick," i=",i);//Сообщение
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)//Выбор ордера i
    {
   if(OrderSymbol()==Symbol())//Символ
     {
   if(OrderMagicNumber()==Mn)//Идентификационное число
      {
   if(OrderType()==OP_BUY && OrderTakeProfit()>PriTP)//Тип операции текущего выбранного ордера
       {
   OrderClose(OrderTicket(),OrderLots(),Bid,0,CLR_NONE);//Закрытие позиции
       }//Закрыто if(OrderType()==OP_BUY && OrderTakeProfit()>PriTP)
      }//Закрыто if(OrderMagicNumber()==Mn)
     }//Закрыто if(OrderSymbol()==Symbol()
    }//Закрыто if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
   }//Закрыто for(i=0;i<OrdersTotal();i++)
//----
   return(0);
  }
//+------------------------------------------------------------------+
Записи в журнале тестера.

2013.03.08 13:28:19     Poligon1 EURUSD,M15: removed
2013.03.08 10:14:14     2013.02.11 12:15  Poligon1 EURUSD,M15: Кс2: Tick=3 i=0
2013.03.08 10:14:14     2013.02.11 12:15  Poligon1 EURUSD,M15: Кс2: Tick=3 i=1
2013.03.08 10:13:50     2013.02.11 12:15  Poligon1 EURUSD,M15: Кс2: Tick=2 i=0
2013.03.08 10:13:50     2013.02.11 12:15  Poligon1 EURUSD,M15: Кс2: Tick=2 i=1
2013.03.08 10:13:26     2013.02.11 12:15  Poligon1 EURUSD,M15: close #1 buy 0.50 EURUSD at 1.3397 sl: 1.3360 tp: 1.3432 at price 1.3395
2013.03.08 10:13:26     2013.02.11 12:15  Poligon1 EURUSD,M15: Кс2: Tick=1 i=0
2013.03.08 10:13:26     2013.02.11 12:15  Poligon1 EURUSD,M15: close #2 buy 1.00 EURUSD at 1.3397 sl: 1.3370 tp: 1.3422 at price 1.3395
2013.03.08 10:13:26     2013.02.11 12:15  Poligon1 EURUSD,M15: Кс2: Tick=1 i=1
2013.03.08 10:13:26     2013.02.11 12:15  Poligon1 EURUSD,M15: close #3 buy 1.50 EURUSD at 1.3397 sl: 1.3380 tp: 1.3412 at price 1.3395
2013.03.08 10:13:26     2013.02.11 12:15  Poligon1 EURUSD,M15: Кс2: Tick=1 i=2
2013.03.08 10:13:26     2013.02.11 12:15  Poligon1 EURUSD,M15: close #4 buy 2.50 EURUSD at 1.3397 sl: 1.3380 tp: 1.3412 at price 1.3395
2013.03.08 10:13:26     2013.02.11 12:15  Poligon1 EURUSD,M15: Кс2: Tick=1 i=3
2013.03.08 10:13:26     2013.02.11 12:15  Poligon1 EURUSD,M15: close #5 buy 3.50 EURUSD at 1.3397 sl: 1.3380 tp: 1.3412 at price 1.3395
2013.03.08 10:13:26     2013.02.11 12:15  Poligon1 EURUSD,M15: Кс2: Tick=1 i=4
2013.03.08 10:13:26     2013.02.11 12:15  Poligon1 EURUSD,M15: Кс2: Tick=1 i=5
2013.03.08 10:13:26     2013.02.11 12:15  Poligon1 EURUSD,M15: Кс2: Tick=1 i=6
2013.03.08 10:13:26     2013.02.11 12:15  Poligon1 EURUSD,M15: Кс0: OrdersTotal=7
2013.03.08 10:13:26     2013.02.11 12:15  Poligon1 EURUSD,M15: open #7 sell stop 3.00 EURUSD at 1.3367 sl: 1.3410 tp: 1.3322 ok
2013.03.08 10:13:26     2013.02.11 12:15  Poligon1 EURUSD,M15: open #6 buy limit 1.00 EURUSD at 1.3382 sl: 1.3355 tp: 1.3422 ok
2013.03.08 10:13:26     2013.02.11 12:15  Poligon1 EURUSD,M15: open #5 buy 3.50 EURUSD at 1.3397 sl: 1.3380 tp: 1.3412 ok
2013.03.08 10:13:26     2013.02.11 12:15  Poligon1 EURUSD,M15: open #4 buy 2.50 EURUSD at 1.3397 sl: 1.3380 tp: 1.3412 ok
2013.03.08 10:13:26     2013.02.11 12:15  Poligon1 EURUSD,M15: open #3 buy 1.50 EURUSD at 1.3397 sl: 1.3380 tp: 1.3412 ok
2013.03.08 10:13:26     2013.02.11 12:15  Poligon1 EURUSD,M15: open #2 buy 1.00 EURUSD at 1.3397 sl: 1.3370 tp: 1.3422 ok
2013.03.08 10:13:26     2013.02.11 12:15  Poligon1 EURUSD,M15: open #1 buy 0.50 EURUSD at 1.3397 sl: 1.3360 tp: 1.3432 ok
2013.03.08 10:13:26     Poligon1 test started
2013.03.08 10:13:24     Poligon1 EURUSD,M15: loaded successfully

 
Thank you very much for the valuable information, I will definitely try to take into account all you have told me in the future.
 
borilunad:

You can't open exactly at the stop loss price, but you can condition to open in the same or opposite direction!

Here is a function call from Kim's "Useful Functions", which you can put in a condition:

isCloseLastPosByStop - Returns flag to close last position by stop

if(isCloseLastPosByStop() == 0) or ... == 1) and whatever you want ......

You can find the function here: https://www.mql5.com/ru/forum/131859

And put it outside the start!

And I beg you! You may do all this by yourself, with diligence, accuracy and patience, otherwise you will not learn anything from hints!

Knowledge is gained only with adequate effort on your part! Get into daily inquisitive work! Good luck!


Thank you very much for the valuable information, I will definitely try to take into account all you have told me in the future.
 

Good day.

Please tell me if there are situations where TCs can change or delete comments to an order?

Thank you very much.

 
Gek34:

Please tell me if there are situations where TCs can change or delete comments to an order...?

As an example.
Native comment
e121220_3in1 01

With added DC after rollover

e121220_3in1 01[swap]

That is, if we search for an order with a comment, we should preliminarily determine the length of the native comment line and during the search we only check the first part of the changed comment in the corresponding length. Or check if the native string is in the comment being checked.

 

Shurkin:

Thanks for the reply.
In this case the work of loop operator was investigated exactly in the tester, that is why orders are opened in init() for simplification, it does not happen in real EAs.
Changing the order of order selection does lead to closure of orders within one tick (I give the code of the program and report from the tester log below).
It seems strange that MQL4 does not allow me to select an order. If you know why, please advise, as the work logic of the Expert Advisor suggests that orders should be closed exactly in the order they are opened.

Sincerely. Shurkin.



The logic is correct - the order numbers of the orders should be the same as the order numbers. Once one order is closed, all of the others will be recalculated. Try this construction

 for(i=0, int k=0; i<OrdersTotal();i++,k++)//
   {
   Print("Кс2:"," Tick=",Tick," i=",i);//Сообщение
   if(OrderSelect(k,SELECT_BY_POS,MODE_TRADES)==true)//Выбор ордера i
    {
   if(OrderSymbol()==Symbol())//Символ
     {
   if(OrderMagicNumber()==Mn)//Идентификационное число
      {
   if(OrderType()==OP_BUY && OrderTakeProfit()>PriTP)//Тип операции текущего выбранного ордера
       {
   OrderClose(OrderTicket(),OrderLots(),Bid,0,CLR_NONE);//Закрытие позиции
   k--;
       }//Закрыто if(OrderType()==OP_BUY && OrderTakeProfit()>PriTP)
      }//Закрыто if(OrderMagicNumber()==Mn)
     }//Закрыто if(OrderSymbol()==Symbol()
    }//Закрыто if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
   }//Закрыто for(i=0;i<OrdersTotal();i++)
 
Gek34:

Good day.

Please tell me if there are situations where TCs can change or delete comments to an order?

Thank you very much.


We have experienced a complete change of comment on an order. Also, magic is sometimes zeroed out during partial closures (partial closures are initiated by the server). Now, there is a separate, looped advisor to control the latter, which monitors "alien" orders and other inconsistencies, notifying by Push messages in a timely manner.
 

Please advise how to write the condition correctly:

1.if order symbol = symbol and order magik = 135

2.if there are no pending BAYSTOP orders

3.if there are no open orders BAY OR (if there are) stoploss>open price

4.if price is between MA and fractal up

PLACE PENDING BUYSTOP ORDER

WRITTEN LIKE THIS:

 static bool flag ;

   if(NewBar()==true)

     flag = true; 

     for(int i=0;i<total-1;i++) 

         {

         OrderSelect(i,SELECT_BY_POS); 

          if(OrderSymbol()==Symbol() && OrderMagicNumber()==135) 

          { 

          if(OrderType()<=OP_BUYSTOP) 

           {

           if((OrderType()<=OP_BUY) || (OrderType()==OP_BUY && OrderStopLoss()>OrderOpenPrice()))  

            { 

            if(Ask<m && Ask>ma && frUP>0 && flag)

            {

              double price = NormalizeDouble(frUP+30*Point+(Ask-Bid)*Point,Digits);

             double stoploss = NormalizeDouble(price-sl*Point-30*Point,Digits);

             double takeprofit = NormalizeDouble(price+tp*Point,Digits);

             int ticket = OrderSend(Symbol(),OP_BUYSTOP,Lots,price,5,stoploss,takeprofit,"R",135,TimeCurrent()+55000,Blue);

             if(ticket>0)

               {

               Alert("R!Отложенный ордер BUYSTOP!!! ",Symbol());

               flag = false;

               }

               else

               {

               Print("Lots= ", Lots,"price= ", price,"stoploss= ", stoploss,"takeprofit= ", takeprofit,"Ошибка № ",GetLastError());      

               return;

               }

             }

           }

         }

       }

     } 

DOES NOT WORK! TELL ME WHAT THE MISTAKE IS!

 

Please advise what could be causing the following problem.

There is an indicator which uses external functions. The compiled library code is located in the libraries folder, descriptors - in include.

Connection #include <lib_Zemckikh.mqh>. During the first run of the indicator, all library functions work normally, the indicator is displayed on the chart. After a change of parameters (after a new calculation of the indicator) the library functions are not called, the indicator is not displayed, there are no entries in the "Experts" tab after the change of parameters.

If you exclude the connection of the library and place the required functions in the indicator file (compiled directly with the functions), everything works fine.

I can't understand what's wrong. I don't want to pollute the forum, I'm not attaching the codes. If you need it, I'll send it to you in person.