[Archive!] Any rookie question, so as not to clutter up the forum. Professionals, don't pass it by. Couldn't go anywhere without you - 2. - page 288

 

Good afternoon who can help solve a problem, maybe I'm doing something wrong,

I have a Klima block that returns the closing flag of the last position at takei. and the same for the stop,

//+----------------------------------------------------------------------------+
//|  Версия   : 19.05.2008                                                     |
//|  Описание : Возвращает флаг закрытия последней позиции по тейку.           |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
bool isCloseLastPosByTake(string sy="", int op=-1, int mn=-1) {
  datetime t;
  double   ocp, otp;
  int      dg, i, j=-1, k=OrdersHistoryTotal();

  if (sy=="0") sy=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, 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 (t<OrderCloseTime()) {
                t=OrderCloseTime();
                j=i;
              }
            }
          }
        }
      }
    }
  }
  if (OrderSelect(j, SELECT_BY_POS, MODE_HISTORY)) {
    dg=MarketInfo(sy, MODE_DIGITS);
    if (dg==0) if (StringFind(OrderSymbol(), "JPY")<0) dg=4; else dg=2;
    ocp=NormalizeDouble(OrderClosePrice(), dg);
    otp=NormalizeDouble(OrderTakeProfit(), dg);
    if (ocp==otp) return(True);
  }
  return(False);
}

but here's how I use it.

bool Buystop=isCloseLastPosByStop(NULL,OP_BUY,MagBuy);
bool BuyTake=isCloseLastPosByTake(NULL,OP_BUY,MagBuy);
bool Sellstop=isCloseLastPosByStop(NULL,OP_SELL,MagBuy);
bool SellTake=isCloseLastPosByTake(NULL,OP_SELL,MagBuy);

//--------------------------------------------------------------------------------+
if(total==1) 
  {
   if(Buystop==True)
   {   OpenPosition(NULL, OP_SELL, Lot,Bid+Sl3*Point, Bid-Tp3*Point,MagBuy);
   }  
   if(BuyTake==True)  
    { OpenPosition(NULL, OP_BUY,  Lot, Ask-Sl*Point, Ask+Tp*Point,MagBuy); 
    }     
    
   if(Sellstop==True)
   {   OpenPosition(NULL, OP_BUY,  Lot, 0, Ask+Tp*Point,MagBuy);
   }  
   if(SellTake==True)  
    { OpenPosition(NULL, OP_BUY,  Lot, Ask-Sl*Point, Ask+Tp*Point,MagBuy); 
    } }

and at first everything goes as planned and at lot 4 opens the one I need and this one

if(Buystop==True)
   {   OpenPosition(NULL, OP_SELL, Lot,Bid+Sl3*Point, Bid-Tp3*Point,MagBuy);
   } 

I can't figure out what it's for.

for clarity

Time Type Order Volume Price S / L T / P Profit Balance
1 2011.01.03 00:00 buy 1 1.00 1.3346 1.3146 1.3446
2 2011.01.03 00:00 sell 2 1.00 1.3344 0.0000 0.0000
3 2011.01.05 15:18 s/l 1 1.00 1.3146 1.3146 1.3446 -2001.70 47998.30
4 2011.01.05 15:18 sell 3 1.00 1.3146 1.3546 1.2946
5 2011.01.07 14:38 t/p 3 1.00 1.2946 1.3546 1.2946 1983.88 49982.18
6 2011.01.07 14:38 sell 4 1.00 1.2944 1.3344 1.2744
7 2011.01.07 14:38 buy 5 1.00 1.2946 1.2746 1.3046

[Deleted]  
FoxUA:

I can't figure out what it's for


That's right,

The first check (total==1) takes place when the order is still 1. Inside the block, you find the last closed Sell -> open, the last closed Buy -> open again. That makes 3 orders.

 
Figar0:


Yeah, that's right,

The first check (total==1) takes place when the order is still 1. Inside the block you find the last closed sell -> open, the last closed buy -> open again. This already results in 3 orders.


Yes, but how to make it was only 2 orders, so it does not open an order under number 6,

Time Type Order Volume Price S / L T / P Profit Balance
1 2011.01.03 00:00 buy 1 1.00 1.3346 1.3146 1.3446
2 2011.01.03 00:00 sell 2 1.00 1.3344 0.0000 0.0000
3 2011.01.05 15:18 s/l 1 1.00 1.3146 1.3146 1.3446 -2001.70 47998.30
4 2011.01.05 15:18 sell 3 1.00 1.3146 1.3546 1.2946
5 2011.01.07 14:38 t/p 3 1.00 1.2946 1.3546 1.2946 1983.88 49982.18
6 2011.01.07 14:38 sell 4 1.00 1.2944 1.3344 1.2744
7 2011.01.07 14:38 buy 5 1.00 1.2946 1.2746 1.3046

[Deleted]  

The easiest way is to check for the presence of an open order of Buy or Sell type. Take this (variables BuyOrders, SellOrders, TotalOrders are global variables in the program; they must be declared in the start() function as int BuyOrders, SellOrders, TotalOrders;) :

void OrdersRecount()
{
  BuyOrders=0; SellOrders=0; TotalOrders=0;
  if (OrdersTotal()>0)
  {
    for (int j = 0; j < OrdersTotal(); j++) 
    {
      if (OrderSelect(j, SELECT_BY_POS)) 
      {
        if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagBuy)
        {
          if (OrderType() == OP_BUY) BuyOrders++;
          if (OrderType() == OP_SELL) SellOrders++;        
          TotalOrders++;
        }
      }
    } 
  }
}
 
Figar0:

There are plenty of options, the easiest is to check for an open Buy or Sell order. Take this one:


I don't understand how to use it please help me.

[Deleted]  
FoxUA:


I don't understand how to use it, so help me if you can.


I will, but I still don't understand the logic of your code:

   if(Buystop==True)
   {   OpenPosition(NULL, OP_SELL, Lot,Bid+Sl3*Point, Bid-Tp3*Point,MagBuy);
   }  
   if(BuyTake==True)  
    { OpenPosition(NULL, OP_BUY,  Lot, Ask-Sl*Point, Ask+Tp*Point,MagBuy); 
    }     
    
   if(Sellstop==True)
   {   OpenPosition(NULL, OP_BUY,  Lot, 0, Ask+Tp*Point,MagBuy);
   }  
   if(SellTake==True)  
    { OpenPosition(NULL, OP_BUY,  Lot, Ask-Sl*Point, Ask+Tp*Point,MagBuy); 
    } }
Are you sure it opens buy in three cases and sell in one? It's not symmetrical...
 
Figar0:


I'll help you, but I don't understand the logic of your code yet:

Are you sure that in three cases you open a Sell position and in one a Buy position? It's not symmetrical...


I want it to open when there is no position in the market and if one of the values is correct then open 1 order and wait for the position to close then i tried to do it that way but nothing works

bool totalBuy=ExistPositions(NULL,OP_BUY,MagBuy) ;
bool totalSell=ExistPositions(NULL,OP_SELL,MagBuy) ;


if(totalBuy==False||totalSell==False) 
[Deleted]  
FoxUA:

I want it to open only if there is no position in the market and if one of the chosen values is correct then open 1 order and wait for the position to close.

So it turns out you may have 2 buy orders and no sell?
 

Figar0:

Т.е. получается что возможна ситуация когда у вас будет 2 ордера на покупку и не одного на продаж

No on the contrary, there should be only 2 positions, including sell without stops and tracking, the statement shows how it happens. everything is correct, but an extra position under number 6 opens
[Deleted]  
FoxUA:
No vice versa, the positions should be just 2 including sell without stops and keepers, the statement shows how it works. right, but an extra position opens under number 6


Well, then everything will be much more complicated. Describe in words how the algorithm should work and I will try to help you while I'm at it)

- 2 positions are possible at the same time;

- first open 2 positions: buy and sell;

- if the long position is closed by a stop loss -> open a short position with tripled SL and TP, we get two sell positions, one with a regular SL and TP, and one with tripled TP;

Next?