[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 192

 
artmedia70:
Igor Kim has an advisor that does the following at a set interval


Thank you!
 

Hello.

Problem with pending orders, specifically the expiry date.

   ticket=OrderSend(symb, OP_SELLLIMIT, Lots, price, Slippage, 0, 0, lsComm, mn, TimeCurrent() + 60*60, op_color);

This is how a new order is opened, i.e. if it has not been triggered in one hour, the server should close it.

Then I check what is the expiry date:

   if (ticket>0)
   {
    OrderSelect(ticket, SELECT_BY_TICKET);
    Alert("OrderExpiration = ", TimeToStr(OrderExpiration(), TIME_DATE),":",TimeToStr(OrderExpiration(), TIME_MINUTES)); 
.......
   } 

This is what I get when the EA works in the tester:

2013.03.01 12:45:58 2012.01.11 11:00 #advisor# EURUSD,H1: open #1 sell limit 2.00 EURUSD at 1.27972 ok
2013.03.01 12:45:58 2012.01.11 11:00 #advisor# EURUSD,H1: Alert: OrderExpiration = 2012.01.11:12:00
2013.03.01 12:45:58 2012.01.12 16:29 Tester: order #1, sell 2.00 EURUSD is opened at 1.27972

That is, date and time of expiration are set correctly, as I want, but there is no close at the time of expiration - a position is opened after 24 hours.

I searched a few forums and the problem occurs. The usual answers are "it works for me" or "trace the pending orders yourself". I do not want to track them myself and it is not working for me.

 
artmedia70:
Igor Kim has an advisor that does the following at a set interval

It works great.

Thanks for the tip!

 
DhP:

It works great.

Thanks for the tip!


It's not me, it's Google.

I think you can pull the save screen function from there yourself and set your own parameters to call it - I was too lazy...

 
borilunad:
Sergey! I am leaving the question about the function open for now, as tomorrow I will insert some more functions into your test EA to conduct a clean experiment. I have watched it in visual mode and almost all double closes are the last, the earlier ones are closed by SL and TP, i.e. there is nothing to choose from. I have removed Stops and Takes in order to have something to choose from. Tomorrow I will show your Expert Advisor with all additional functions and comments! You will be able to see for yourself how the function works. I really want it to show that I was wrong! If I am right, I will try to do my best! See you tomorrow!

Sergey, Dubakin, good morning (to me) and good afternoon (to you)! Did a visual check and made sure that this function selects the maximums, which is what I needed! I'm glad I no longer have any doubts, and my apologies to you! But in our business "it's better to measure seven times..." Showing your tester EA for function checking, with added features and comments for a clearer visual check! Thank you!

extern int EA_Magic=135; // внешняя переменная

int TimeNow, TimePrev, PrevType; // глобальные переменные

int start()
{
  double Price,SL,TP;
     int Ticket;

  TimeNow=iTime(NULL,240,0);
  if(TimePrev==TimeNow) return(0);

  if(PrevType!=1) {
   Price=NormalizeDouble(Ask,Digits);    
//   SL=NormalizeDouble(Price-300*Point,Digits);    
//   TP=NormalizeDouble(Price+300*Point,Digits);
   Ticket=OrderSend(Symbol(),OP_BUY,0.1,Price,3,0,0,"",EA_Magic);
   if(Ticket!=-1) { TimePrev=TimeNow; PrevType=1; } }

  else if(PrevType!=-1) {
   Price=NormalizeDouble(Bid,Digits);    
//   SL=NormalizeDouble(Price+300*Point,Digits);    
//   TP=NormalizeDouble(Price-300*Point,Digits);
   Ticket=OrderSend(Symbol(),OP_SELL,0.1,Price,3,0,0,"",EA_Magic);
   if(Ticket!=-1) { TimePrev=TimeNow; PrevType=-1; } }

  Comment("BuyPos: ",NumberOfBuyPositions(),"; SellPos: ",NumberOfSellPositions(),
  "; LotPos: ",GetAmountLotFromOpenPos(),
  "\nMaxLoss: ",DoubleToStr(GetMinProfit(),2),"; MaxProf: ",DoubleToStr(GetMaxProfit(),2),
  "\nLossDiff: ",DoubleToStr(GetMinProfit()+GetMaxProfit(),2));

  if(Hour()==0 && TimePrev==TimeNow) LockOFF(EA_Magic);

  return(0);
}

bool LockOFF(int EA_Magic) {
  double Result, PrevLoss, PrevProfit;
     int pos, orders_total, order_type, MaxProfitTicket, MaxLossTicket;
    bool Ans;

  MaxProfitTicket=-1; MaxLossTicket=-1;

  orders_total=OrdersTotal();
  for(pos=orders_total-1; pos>=0; pos--) {
    if(!OrderSelect(pos, SELECT_BY_POS, MODE_TRADES)) continue;
    if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=EA_Magic) continue; // не наш ордер
    if(OrderType()>1) continue;
    Result=OrderProfit()+OrderSwap()+OrderCommission();
    if(Result<0.0 && (PrevLoss==0.0 || Result<PrevLoss)) {
      PrevLoss=Result; MaxLossTicket=OrderTicket(); order_type=OrderType();  // end of for
  } }
  if(MaxLossTicket==-1) return(false); // нет убыточной позиции
  if(order_type==OP_BUY) order_type=OP_SELL; else order_type=OP_BUY; 

  orders_total=OrdersTotal();
  for(pos=orders_total-1; pos>=0; pos--) {
    if(!OrderSelect(pos, SELECT_BY_POS, MODE_TRADES)) continue;
    if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=EA_Magic) continue; // не наш ордер
    if(order_type!=OrderType()) continue;
    Result=OrderProfit()+OrderSwap()+OrderCommission();
    if(Result>0.0 && (PrevProfit==0.0 || Result>PrevProfit)) {
      PrevProfit=Result; MaxProfitTicket=OrderTicket();  // end of for
  } }
  if(MaxProfitTicket==-1) return(false); // нет противоположной прибыльной позиции

  Ans=OrderCloseBy(MaxLossTicket, MaxProfitTicket);
  if(!Ans) { 
    Print("Ошибка при встречном закрытие!"); return(false); 
  }
  return(true); 
}
//+----------------------------------------------------------------------------+
int NumberOfBuyPositions(string sy="", int op=-1, int mn=-1) {
  int i, k=OrdersTotal(), kp=0;
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if (OrderType()==OP_BUY) {
        if (op<0 || OrderType()==op) {
          kp++;
  } } } }
  return(kp);
}
//+----------------------------------------------------------------------------+
int NumberOfSellPositions(string sy="", int op=-1, int mn=-1) {
  int i, k=OrdersTotal(), kp=0;
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if (OrderType()==OP_SELL) {
        if (op<0 || OrderType()==op) {
          kp++;
  } } } }
  return(kp);
}
//+----------------------------------------------------------------------------+
double GetAmountLotFromOpenPos(string sy="", int op=-1, int mn=-1) {
  double l=0;
  int    i, k=OrdersTotal();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
        if (op<0 || OrderType()==op) {
          l+=OrderLots();
  } } } }
  return(l);
}
//+----------------------------------------------------------------------------+
double GetMinProfit(string sy="", int op=-1, int mn=-1) {
  double p;
  int    i, k=OrdersTotal();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
        if (op<0 || OrderType()==op) {
          if (p>OrderProfit()+OrderCommission()+OrderSwap())
          p=OrderProfit()+OrderCommission()+OrderSwap();
  } } } }
  return(p);
}
//+----------------------------------------------------------------------------+
double GetMaxProfit(string sy="", int op=-1, int mn=-1) {
  double p;
  int    i, k=OrdersTotal();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
        if (op<0 || OrderType()==op) {
          if (p<OrderProfit()+OrderCommission()+OrderSwap())
          p=OrderProfit()+OrderCommission()+OrderSwap();
  } } } }
  return(p);
}
//+----------------------------------------------------------------------------+
 
borilunad:

Sergey, Dubakin, good morning (for me) and good afternoon (for you)! Did a visual check and made sure that this function selects maximums, which is what I needed! I'm glad I no longer have any doubts, and my apologies to you! But in our business "it's better to measure seven times..." Showing your tester EA for function checking, with added features and comments for a clearer visual check! Thank you!

How so... Based on Igor Kim made something... Only one of your features... the rest are the same...

//+----------------------------------------------------------------------------+
int NumberOfBuyPositions(string sy="", int op=-1, int mn=-1) { // для чего присвоены значения по-умолчанию, если они нигде не используются? Кроме op (и то неверно)
  int i, k=OrdersTotal(), kp=0;
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if (OrderType()==OP_BUY) {                               // тут жесткая проверка на тип Buy
        if (op<0 || OrderType()==op) {                         // тут лишняя проверка на значение op, используемое по умолчанию и ещё одна лишняя проверка на ==op
          kp++;
  } } } }
  return(kp);
}
//+----------------------------------------------------------------------------+

something like this, I guess:

//+----------------------------------------------------------------------------+
int NumberOfPositions(string sy, int op, int mn) {
  int i, k=OrdersTotal()-1, kp=0;
  for (i=k; i>=0; i--) {
   if (OrderSelect(i,SELECT_BY_POS)) {           // если ордер выбран
      if (OrderMagicNumber()!=mn)   continue;    // если не наш магик - смотрим следующий ордер
      if (OrderSymbol()!=sy)        continue;    // если не наш символ - смотрим следующий ордер
      if (OrderType()!=op)          continue;    // если не соответствует тип - смотрим следующий
      kp++;                                      // тут искомый ордер - увеличим счётчик
      }
   }
  return(kp);
}
//+----------------------------------------------------------------------------+

And call her out:

for counting Buy and counting Sell by the current symbol, with Magic

//+----------------------------------------------------------------------------+
   int BuyPos= NumberOfPositions(Symbol(), OP_BUY,  Magic);
   int SellPos=NumberOfPositions(Symbol(), OP_SELL, Magic);
//+----------------------------------------------------------------------------+
 
alsu:
pasha5282:
Can you tell me how to choose the smallest lot among all open trades?

int i, ot = OrdersTotal();
double min=0;
int min_ticket=0;
for(i=0;i<ot;i++)
{
if(!OrderSelect(i,SELECT_BY_POS)) continue;
if(OrderType()!=OP_BUY && OrderType()!=OP_SELL) continue;
if(i==0||min>OrderLots()) {min = OrderLots(); min_ticket=OrderTicket();}
}


I've been browsing the thread. Your logic is strange. I am interested in the last line of code:

if(i==0||min>OrderLots()) {min = OrderLots(); min_ticket=OrderTicket();
The i variable has a value of 0 only at the beginning of the loop. Further it will add 1 value with each iteration.

Then there is a condition that will cause the loop to continue:

min>OrderLots()
But min is declared as zero and its value doesn't change anywhere else in the code. Which means that this value will never be true! Where is the logic?
 
hoz:


I've been browsing the thread. Your logic is strange. I am interested in the last line of code:

Variable i has a value of 0 only at the beginning of the loop. Further it will add value with each iteration by 1.

Then there is a condition for the cycle to continue:

But min is declared as zero, and its value never changes anywhere else in the code. Which means that this value will never be true! Where is the logic?

int i, ot = OrdersTotal();
double min=0.0;
int min_ticket=0;
for(i=0;i<ot;i++)
{
if(!OrderSelect(i,SELECT_BY_POS)) continue;
if(OrderType()!=OP_BUY && OrderType()!=OP_SELL) continue;
if(min==0.0||min>OrderLots()) {min = OrderLots(); min_ticket=OrderTicket();}
}
If we do it this way, all strange things should disappear.
 
hoz:


I looked through the branch. Your logic is strange. I am interested in the last code line:

Variable i has value 0 only at the beginning of the loop. Further it will add value with each iteration by 1.

Then there is a condition for the cycle to continue:

But min is declared as zero, and its value never changes anywhere else in the code. Which means that this value will never be true! Where is the logic?

Please note

if(i==0||min>OrderLots()) {min = OrderLots(); min_ticket=OrderTicket();

If min>OrderLots(), then min = OrderLots();

 
r772ra:

Please note

If min>OrderLots() then, min = OrderLots();

If min is zero, it can never become greater than OrderLots().