if i have many open orders, how to get the last order opened open price?

AhmedMorra
61

Hello fellow traders,

I am developing an ea that opens a lot of orders but only open buy order if their is a buy order opened already

what i want is the next order to open it has to be  at price above the last opened price if that makes sense.

and vice versa for sell


that's a code i tried but it just doesn't work

int LastOpenPrice(){
    datetime lastTime  = 0;
    int      lastPrice = -1; 
    for(int pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
        OrderSelect(pos, SELECT_BY_POS)                 
    &&  OrderSymbol()       == Symbol()                 
    &&  OrderOpenTime()     >  lastTime
    ){
      lastTime   = OrderOpenTime();
      lastPrice = OrderOpenPrice();
    }
    return(lastPrice);
}

thanks traders

William Roeder
27830
William Roeder  
AhmedMorra:

I am developing an ea that opens a lot of orders but only open buy order if their is a buy order opened already

what i want is the next order to open it has to be  at price above the last opened price if that makes sense.

that's a code i tried but it just doesn't work

  1. You don't have enough free margin to "opens a lot of orders." Even at min lot a $1K account can only open less than 10 total (every broker I've used.) Once they start going against you they will margin call you very quickly.
    • You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
    • Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the SPREAD, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
    • Do NOT use TickValue by itself - DeltaPerLot
    • You must normalize lots properly and check against min and max.
    • You must also check FreeMargin to avoid stop out

  2. That makes sense
  3. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here.
  4. Perhaps you should return a double (a price) not an int. You would know that had you used strict.
Sudha365
29
Sudha365  
AhmedMorra:

Hello fellow traders,

I am developing an ea that opens a lot of orders but only open buy order if their is a buy order opened already

what i want is the next order to open it has to be  at price above the last opened price if that makes sense.

and vice versa for sell


that's a code i tried but it just doesn't work

thanks traders

HI

I am looking for the same solution, do you have this code or any other code working ?

Sudha365
29
Sudha365  

HI

I am posting it here after a several failed attempts to GET THE LAST OPEN TRADE OF BUY POSTIONS OUT OF MANY AND MIX OF BUY AND SELL POSITIONS. Below code is working , but it getting all the buy open buy position but I need "ONLY the LAST" buy postion.  Really appriciate any help


int CountBuyPositions()
   {

    int cnt=0;
    for(int i=OrdersTotal()-1; i >= 0; i--)

   {
    OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
    if(OrderMagicNumber()!=Magic&&OrderSymbol()==Symbol())

   {

    if(OrderType()==OP_BUY)

   {

    double ordopeprice = NormalizeDouble((OrderOpenPrice()-Bid),digits);  //(Dist*point )-
    Print("-----------------------Send before buy Order " +ordopeprice);
   }
  }

 }
return (0);
}
William Roeder
27830
William Roeder  
Sudha365 #: n but I need "ONLY the LAST" buy postion. 

So when it finds one, it has found the last. Stop the loop.

Sudha365
29
Sudha365  
William Roeder #:

So when it finds one, it has found the last. Stop the loop.

Hi Thank you.

I stoped the loop and tried the below code, it works only if the last opened order is "BUY", if last order is "SELL" then its not picking up the last "BUY" position.

Buy----

Buy---

Sell---

Buy----

It works for the above scenario


Buy---

Buy---

Buy---

Selll---

Its NOT working for this above scenario.

The changes I have made is below


//for(int i=OrdersTotal()-1; i >= 0; i--)

int i=(OrdersTotal()-1);

{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
Vladislav Boyko
2259

Primitive example

#property strict
#property script_show_inputs

input int inpMagic = 1; // Magic

void OnStart()
  {
   double upperPriceBuy,
          lowerPriceSell;
   int    ordersNumberBuy,  // if ordersNumberBuy < 1, the upperPriceBuy will contain -1
          ordersNumberSell; // if ordersNumberSell < 1, the lowerPriceSell will contain INT_MAX
   getMarket(upperPriceBuy, lowerPriceSell, ordersNumberBuy, ordersNumberSell);
   // Alert(StringFormat("upperPriceBuy %s, upperPriceBuy %s", DoubleToString(upperPriceBuy, _Digits), DoubleToString(lowerPriceSell, _Digits)));
  }

void getMarket(double &upperPriceBuy, double &lowerPriceSell, int &ordersNumberBuy, int &ordersNumberSell)
  {
   ordersNumberBuy = 0;
   ordersNumberSell = 0;
   upperPriceBuy = -1;
   lowerPriceSell = INT_MAX;
   for(int i = OrdersTotal() - 1; i >= 0; i--)
     {
      if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
         continue; // I use return here and do not trade on this tick
      if(OrderMagicNumber() != inpMagic || OrderSymbol() != _Symbol)
         continue;
      double price = NormalizeDouble(OrderOpenPrice(), _Digits);
      if(OrderType() == 0)
        {
         ordersNumberBuy++;
         if(price > upperPriceBuy)
            upperPriceBuy = price;
        }
      else if(OrderType() == 1)
        {
         ordersNumberSell++;
         if(price < lowerPriceSell)
            lowerPriceSell = price;
        }
     }
  }
Sudha365
29
Sudha365  

Thanks a Lot for the lead and tips.

I have the below code working for my need but I am sure someone can do the same function using fewer line rather than many lines/functions. However return(ordopeprice) "NOT RETURNING" float/double/decimal values, Please let me know if anyone know how to do it if that option exist.

//---------------------------------------------------------------------------------------
//Getting ticket number for last BUY Postion regardless of any number of sell positions//  
//---------------------------------------------------------------------------------------
 int LastBuyOrdTkt()
   {  
    datetime LastBuyTime  = 0;
    int  LastBuyTkt = 0.0; //dummy number
     for(int i = 0 ; i<OrdersTotal() ; i++) 
         {
            if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==True)
                  {
                     if(OrderType()==0)
                     
                        {
                             if(OrderOpenTime()> LastBuyTime)
                                  {
                                       LastBuyTkt = OrderTicket(); 
                                       
                                  }
                              else     continue;
                         }
                     else   continue;
                  }
             else      continue;
          }
    return(LastBuyTkt);
    }
    
    
//---------------------------------------------------------------------------------------
//Getting the OpenPrice of last BUY Postion using the "LastBuyOrdTkt" function          //  
//---------------------------------------------------------------------------------------
    
int LastBuyOrder()

{
    double ordopeprice = 0;
    int lastBuyticket = LastBuyOrdTkt();
   // for(int i = lastBuyticket ; i<OrdersTotal(); i++)
         {
           OrderSelect(lastBuyticket,SELECT_BY_TICKET,MODE_TRADES);
              if (lastBuyticket > 0)
              {
                     
                     {      
                       ordopeprice = NormalizeDouble(OrderOpenPrice(),digits); 
                       // do the calsulation here 
                       // Ordersend
                       
                     }     
                     
              }
  
         }
          
      return(ordopeprice);
  
  }