Useful features from KimIV - page 113

 
togomsk:


Hi!

For some reason, when your library is connected, extern operator stops working, i.e. variables are defined, everything compiles normally, and in principle, the EA works, but at the same time, the EA does not have any parameters when starting the type, even though there are quite a lot of parameters. What may be the reason for this?


Find in the code

#property library

And delete.

 

I have added a variable j to this function, for the penultimate transaction.

double GetProfitLastClosePos(string sy="", int op=-1, int mn=-1, int j=0) {
datetime o;
double p=-1;
int i, k=OrdersHistoryTotal();

if (sy=="0") sy=Symbol();
for (i=0; i<k; i++) {
if(OrderSelect(i-j, 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 (o<OrderCloseTime()) {
o=OrderCloseTime();
p=OrderProfit();
}
}
}
}
}
}
}
return(p);
}
 
abdul1:

I've added a variable j to this function for penultimate trades.

If we consider that the orders which have been closed are considered in OrdersHistoryTotal() according to the time of closing from right -> left (the last one to the right):

double GetProfitLastClosePos (string sy="", int op=-1, int mn=-1,
                              int j=0)          // Искомый (по номеру) ордер от последнего закрытого 
{
    int k = OrdersHistoryTotal(), li_cnt = 0;
//----
    if (sy == "0" || sy == "") sy = Symbol();
    for (int i = k - 1; i >= 0; i--)
    {
        if (!OrderSelect (i, SELECT_BY_POS, MODE_HISTORY)) continue;
        if (OrderSymbol() != sy) continue;
        if (OrderType() > 1) continue;
        if (op >= 0) if (OrderType() != op) continue;
        if (mn >= 0) if (OrderMagicNumber() != mn) continue;
        if (j == li_cnt) return (OrderProfit());
        li_cnt++;
    }
//----
    return (-1.0);
}

The function returns the profit j-th of the last closed order, "filtered" by symbol, magik, type. And the numbers are counted from 0th.

 
TarasBY:

If we take into account that closed orders are counted in OrdersHistoryTotal() by time of closing from right -> left (the latest extreme right):

The function returns the profit j-th of the last closed order, "filtered" by instrument, Magik, type. And the numbers are counted from 0th.

That's what I need!
 

Hi all.

Needed a function to close Buy and Sell orders of equal lots by counter orders, when their profit goes to +. I tried to do this function, but 4108 error comes out (wrong tickets)

Can you tell me what the problem is?

//+------------------------------------------------------------------+
int fCloseBu(){
        // Закрываем встречные
  double LotB,LotS,PrB=0,PrS=0;
  int TicketB,TicketS,i, k=OrdersTotal();

  for (i=k-1; i>=0; i--) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if ((OrderSymbol()==Symbol() || Symbol()=="")) {
        if (Magic_N<0 || OrderMagicNumber()==Magic_N) {
        //----------
        if(OrderType()==OP_BUY){
          if (PrB<OrderProfit()+OrderSwap()+OrderCommission()) {
            PrB=OrderProfit()+OrderSwap()+OrderCommission();
            LotB=OrderLots();
            TicketB=OrderTicket();
          }
        }
        if(OrderType()==OP_SELL){
          if (PrS<OrderProfit()+OrderSwap()+OrderCommission()) {
            PrS=OrderProfit()+OrderSwap()+OrderCommission();
            LotS=OrderLots();
            TicketS=OrderTicket();
          }
        }
       if (PrB>0 && PrS>0 && LotB==LotS) {
          OrderCloseBy(TicketB,TicketS,CLR_NONE);
          Print("Закрываем пару встречных ордеров c одинаковыми лотами в плюсе");
          }  
        }
      }
    }
  }
//-------------
   return(0);
}
//+------------------------------------------------------------------+
 
Orders should be closed when the whole cycle of checks is done, i.e. after the for statement, not inside.
 
Can you please advise me where I can find an EA that works on crossing a moving average with the ability to shift the moving average in the settings?
 
Roger:
Orders should be closed when the whole cycle of checks is done, i.e. after the for statement, not inside.


Thank you! If anyone else needs such a function, it will be like this:

//+------------------------------------------------------------------+
//|    Закрываем встречные                                           |
//+------------------------------------------------------------------+
int fCloseBu(){
  double LotB,LotS,PrB=0,PrS=0;
  int TicketB,TicketS,i, k=OrdersTotal();

  for (i=k-1; i>=0; i--) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if ((OrderSymbol()==Symbol() || Symbol()=="")) {
        if (Magic_N<0 || OrderMagicNumber()==Magic_N) {
        //----------
        if(OrderType()==OP_BUY){
          if (PrB<OrderProfit()+OrderSwap()+OrderCommission()) {
            PrB=OrderProfit()+OrderSwap()+OrderCommission();
            LotB=OrderLots();
            TicketB=OrderTicket();
          }
        }
        if(OrderType()==OP_SELL){
          if (PrS<OrderProfit()+OrderSwap()+OrderCommission()) {
            PrS=OrderProfit()+OrderSwap()+OrderCommission();
            LotS=OrderLots();
            TicketS=OrderTicket();
             }
           }
         }
       }
     }
   }//for
   if(PrB>0 && PrS>0 && LotB==LotS) {
     OrderCloseBy(TicketB,TicketS,CLR_NONE);
     Print("Закрываем пару встречных ордеров c одинаковыми лотами в плюсе");
  }
//-------------
   return(0);
}
//+------------------------------------------------------------------+
 
Are there any video tutorials on this language?
 
Andrei5:
Are there any video tutorials on this language?
There is a book. You can read it online or download it to your computer. Look above -> "Tutorial".