Useful features from KimIV - page 88

 

kombat 17.07.2009 22:13

Add up, get the buy-summer 3 lots and counter this volume with sell 3 lots.

.......................

As an option, yes. Although, if there are already open Sell orders, the EA may close not the ones you need. It should be solved.

kombat 17.07.2009 22:13.

But to answer your question: is it possible to close all positions?

yes it is, one by one, it's quick script, aap! and that's it...

.........................

That's what I'm using now. A set of scripts - you use this one and that one... But I tried to code my strategy into Expert Advisor and encountered a number of difficulties.

 
gince >> :
I have something that does not work here, it means it does not close no errors.

In the ClosePositions() function, its b-Positions.mqh library, KimIV uses a call to its other ClosePosBySelect() function which prohibits the use of ee in the tester :

if (!IsTesting() && (!IsExpertEnabled() || IsStopped())) break;


For history tests use his library b-ForTest.mqh

 
BoraBo писал(а) >>

In the ClosePositions() function, its b-Positions.mqh library, KimIV uses a call to its other function ClosePosBySelect() which prohibits using ee in the tester :

if (!IsTesting() && (!IsExpertEnabled() || IsStopped())) break;

For history tests, use his library b-ForTest.mqh

You misunderstood my code... Translating into Russian... If NOT test mode AND (EA is NOT allowed to trade OR the Expert Advisor is stopped), then abort loop. To put it simply, this line ensures exit from the loop, when you press button "Expert Advisors" on the toolbar.

 
Sorry for the confusion.
 

Good evening,

Unfortunately, there is a discrepancy with the array as well... When order counting is done with a condition if lots are bigger than 0 (Lts>0), lots are closed one by one (when profit has been reached), but total profit should be taken into account:

   int Tip;                               // Тип ордера
   int Ticket=0,                          // Номер ордера
   Tkt;                                   // Количество номеров ордеров
   double Lot=0,                          // Количество закр. лотов
   Lts;                                   // Количество лотов всего
   double Price_Cls;                      // Цена закрытия ордера
   int Errors;
//--------------------------------------------------------------- 3 --

      for(int mm=1; mm<= Mas_Ord_Tek[0][0]; mm++)// Цикл по живым ордерам
        {
         if( Mas_Ord_Tek[ mm][6]== Tip &&     // Среди ордеров нашего типа
            Mas_Ord_Tek[ mm][5]> Lot &&      // .. выбираем лоты
            Mas_Ord_Tek[ mm][4]> Ticket)     // .. выбираем номер ордера
           {                              // Этот больше ранее найден.
            Lts= Mas_Ord_Tek[ mm][5]+ Lot;
            Tkt= Mas_Ord_Tek[ mm][4]+ Ticket;
           }
        }
      if ( Tip==0) Price_Cls=Bid;          // Для ордеров Buy
      if ( Tip==1) Price_Cls=Ask;          // Для ордеров Sell
      if ( BuyOrdProf>100 && Lts>0)
      Ans=OrderClose( Tkt, Lts, Price_Cls,0,Red);// Закрыть все ордера

And at condition in orders count, if lots are more than 1 (Lts>1), lots are not closed:

   int Tip;                               // Тип ордера
   int Ticket=0,                          // Номер ордера
   Tkt;                                   // Количество номеров ордеров
   double Lot=0,                          // Количество закр. лотов
   Lts;                                   // Количество лотов всего
   double Price_Cls;                      // Цена закрытия ордера
   int Errors;
//--------------------------------------------------------------- 3 --

      for(int mm=1; mm<= Mas_Ord_Tek[0][0]; mm++)// Цикл по живым ордерам
        {
         if( Mas_Ord_Tek[ mm][6]== Tip &&     // Среди ордеров нашего типа
            Mas_Ord_Tek[ mm][5]> Lot &&      // .. выбираем лоты
            Mas_Ord_Tek[ mm][4]> Ticket)     // .. выбираем номер ордера
           {                              // Этот больше ранее найден.
            Lts= Mas_Ord_Tek[ mm][5]+ Lot;
            Tkt= Mas_Ord_Tek[ mm][4]+ Ticket;
           }
        }
      if ( Tip==0) Price_Cls=Bid;          // Для ордеров Buy
      if ( Tip==1) Price_Cls=Ask;          // Для ордеров Sell
      if ( BuyOrdProf>100 && Lts>1)
      Ans=OrderClose( Tkt, Lts, Price_Cls,0,Red);// Закрыть все ордера

Please advise who can see the error.

Or maybe, Igor, I have a misunderstanding of using your function?

BuyOrdProf=GetProfitOpenBuyPosInCurrency(OP_BUY); // Call function

//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 19.02.2008                                                     |
//|  Описание : Возвращает суммарный профит открытых позиций в валюте депозита |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
double GetProfitOpenPosInCurrency(string sy="", int op=-1, int mn=-1) {
  double p=0;
  int    i, k=OrdersTotal();

  if ( sy=="0") sy=Symbol();
  for ( i=0; i< k; i++) {
    if (OrderSelect( i, SELECT_BY_POS, MODE_TRADES)) {
      if ((OrderSymbol()== sy || sy=="") && ( op<0 || OrderType()== op)) {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if ( mn<0 || OrderMagicNumber()== mn) {
            p+=OrderProfit()+OrderCommission()+OrderSwap();
          }
        }
      }
    }
  }
  return( p);
}
 

Igor, please tell me how to insert your PriceOpenLastPos function into the indicator.

When compiling the editor gives error on the first bracket and subsequent variables.

double PriceOpenLastPos( string sy="", int op=-1, int mn=-1) {
datetime t;
double r=0;
int k=OrdersTotal();

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

'(' - function definition unexpected D:\MT4\experts\indicators\Astrea.mq4 (627, 24)
'sy' - variable not defined D:\MT4\experts\indicators\Astrea.mq4 (632, 7)
etc.

I implement the logic of trading signals formation in an indicator and send the generated signal to the Expert Advisor via a global variable to open/close orders.

I need the open price of an order to make some decisions, so I am trying to do that. My indicator is a dynamic one and it can be debugged only in demo mode or in Visualisation mode in a tester.

 

The picture is that it is not the total profit that is taken into account, but the profit of the individual orders.

 
Angela, the compiler did not expect to find my function definition in this particular place. It means that somewhere higher up in the code you have something missing. Maybe a semicolon or a closing curly brace. Anyway, check the code carefully.
 
hope писал(а) >>
BuyOrdProf=GetProfitOpenBuyPosInCurrency(OP_BUY); // Function call

Incorrect parameter passing into the function. It should be like this:

BuyOrdProf=GetProfitOpenBuyPosInCurrency("", OP_BUY);
 
KimIV >> :

Incorrect parameter passing into the function. It should be like this:

Adding a binding to the symbol in the function call doesn't help. Why does it count the profit for each position separately (picture above)?

Reason: