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

 
vik-777:

Help me solve a problem

I sample all the closed items

for (int i=0; i<OrdersHistoryTotal(); i++)// For all orders
if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)==true)

then filter by magic number

if (OrderMagicNumber()==12)

filter satisfies 3 positions, but I need only the last closed one

can't figure out how to leave only the last one?

Thanks

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

  if (sy=="" || sy=="0") sy=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) {
      if (OrderSymbol()==sy) {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (op<0 || OrderType()==op) {
            if (mn<0 || OrderMagicNumber()==mn) {
              if (t<OrderCloseTime()) t=OrderCloseTime();
            }
          }
        }
      }
    }
  }
  return(iBarShift(sy, tf, t, True));
}
What is relevant to your question is underlined.
 

Again, very much needed.

Help me solve the problem.

I am selecting by all closed positions

for (int i=0; i<OrdersHistoryTotal(); i++)// on all orders of the terminal
if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)==true)

afterwards filter by magic number

if (OrderMagicNumber()==12)

the filter matches 3 positions but I need only the last closed one

Can't figure out how to leave only the last one?

Thanks

 
- It's good to be dumb ! Tumtz-tumtz (lyrics from a song)
 

Please help! I need it to open order immediately after closing the old one, at Tp or SL price.

#property copyright "Copyright © 2010, MetaQuotes Software Corp.
#property link "http://www.metaquotes.net"
#property show_confirm
extern int MaxRisk=3;
extern bool Buy=false; //for opening a Buy order
extern bool Sell=true; //to open a sell order.
extern inttern MagicNumber=7749;
extern TP=210;
extern inttern SL=420;

{
double point=MarketInfo(Symbol(),MODE_POINT);//request Point
double Free=AccountFreeMargin();
double One_Lot =MarketInfo(Symbol(),MODE_MARGINREQUIRED);
double Step =MarketInfo(Symbol(),MODE_LOTSTEP);
double Lot =MathFloor(Free*MaxRisk/100/One_Lot/Step)*Step;
int pos,total=OrdersTotal();
//----

if(Buy==true && OrdersTotal()<=1)
{
OrderSend(Symbol(),OP_BUY,Lot,Ask,3,Ask-SL*Point,Ask+TP*Point, "777"+ Symbol(), MagicNumber, 0, DarkGreen);

Buy=false;
}
if(Sell==true && OrdersTotal()<=1)
{
OrderSend(Symbol(),OP_SELL,Lot,Bid,3,Bid+SL*Point,Bid-TP*Point, "4949"+ Symbol(), MagicNumber, 0, DarkGreen);

Sell=false;
}

for (pos=0; pos<total; pos++)
if(OrderSelect(pos,SELECT_BY_POS )==true)

if(OrdersTotal()==OP_BUY)
if(PRICE_CLOSE==OrderTakeProfit()) Buy=true;
if(PRICE_CLOSE==OrderStopLoss()) Sell=true;

}
}

if(OrderSelect(pos,SELECT_BY_POS )==true)
if (OrdersTotal()==OP_SELL)
{
if(PRICE_CLOSE==OrderStopLoss()) Buy=true;
if(PRICE_CLOSE==OrderTakeProfit()) Sell=true;

}
} }

//----
return(0);
}
//+------------------------------------------------------------------+




 

Guys, here's a question, an EA tries to open an order, but the price changes and displays an error wrong price, how can I bypass it, so it tries to buy until it buys?

void CLOSEORDER(string ord)
{
   for (int i=0; i<OrdersTotal(); i++)
   {                                               
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
      {
         if (OrderSymbol()!=Symbol()) continue;
         if (OrderType()==OP_BUY && ord=="Buy")
            OrderClose(OrderTicket(),OrderLots(),Bid,30,CLR_NONE);// Закрываем Buy
         if (OrderType()==OP_SELL && ord=="Sell")
            OrderClose(OrderTicket(),OrderLots(),Ask,30,CLR_NONE);// Закрываем Sell
      }   
   }
}
//--------------------------------------------------------------------
void OPENORDER(string ord)
{
   int error;
   if (ord=="Buy" ) error=OrderSend(Symbol(),OP_BUY, LOT,Ask,20,SL,TP,"", 1,3);
   if (ord=="Sell") error=OrderSend(Symbol(),OP_SELL,LOT,Bid,20,SL,TP,"",-1,3);
   if (error==-1) //неудачная покупка OK
   {  
      ShowERROR(error,0,0);
   }

It used to be.

Bid,3,CLR_NONE

I added 30 because I have a 5 sign but it's useless.

 
vik-777:

Again, very much needed.

Help me solve the problem.

I'm sampling all the closed items.



Start at the end, then the first matching one is the one.

for (int i=OrdersHistoryTotal()-1;i>=0; i--)//
if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)==true)


 
Craft:

There is still one more annoyance, there are sections where there is only a closing of a position and not a reversal, although the conditions for a stop and opening a new position are the same.


These situations can occur because the buy and sell conditions in the Expert Advisor are not mutually exclusive. Try to trace the values of the required variables in the tester according to

if(Time[0]=='время_нужного_бара') //чтоб не засорять логи берем на конкретном баре - посмотрите на картинке, где именно ошибка
{
   Print("переменная=", значение);
}
 
Pyxlik2009:

Guys, here's a question, an EA tries to open an order, but the price changes and displays an error wrong price, how can I bypass it, so it tries to buy until it buys?

It used to be.

I added 30 because I have a 5 sign but it's useless.

Do an order while(true), check for 135 errors and then RefreshRates(). Look, there are scripts with examples in the package with the terminal, everything is explained there.
 
alsu:

this needs to be investigated in detail, such situations can arise because the buy and sell opening conditions in the EA are not mutually exclusive. Try to trace the values of the required variables in the tester along the lines of

OK, in which part of the code should this be inserted, how practically applied?
 
SergNF:

What is relevant to your question is underlined.
so i select the last closed order and i need the last closed with the magician, for example it is lines 3,6 and 9 therefore i need line 3 as it is the last closed with the magician
Reason: