Download MetaTrader 5

Open another Order

To add comments, please log in or register
ayman.maarouf
8
ayman.maarouf  

Can somebody help me with this code please

How to get last open order sell or buy then open a new order at same entry conditions and to close at opposite



// External variables
extern double LotSize = 1;
extern double StopLoss = 0;
extern double TakeProfit = 20;
extern double TrailingStopLimit = 0;
extern double TrailingStopStop = 0;
extern int MagicNumber = 1111111111111;


// Global variables
int LongTicket;
int ShortTicket;
double RealPoint;


// Init function
int init()
 {
      RealPoint = RealPipPoint(Symbol());
     return(0);
 }


// Start function
int start()
 {
        //Variables
       
  
  // Long
  
  OrderSelect(LongTicket,SELECT_BY_TICKET);
  if(OrderCloseTime() != 0 || LongTicket == 0)
  {
               
                   
bool buy_condition_1 = Open[]  > Close[]   ;
bool buy_condition_2 = Open[]  < Close[]   ;
bool buy_condition_3 = Open[]  < iCustom() ;


                                
  if( buy_condition_1  &&  buy_condition_2  &&  buy_condition_3   )
   {
    
    OrderSelect(ShortTicket,SELECT_BY_TICKET);
    
    if(OrderCloseTime() == 0 && ShortTicket > 0)
     {
           
      bool Closed = OrderClose(ShortTicket,OrderLots(),Ask,0,Red);
     }    

    
 
    
    LongTicket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,0,0,0,"Buy Order",MagicNumber,0,Blue);
    
    
    OrderSelect(LongTicket,SELECT_BY_TICKET);
    double OpenPrice = OrderOpenPrice();
    
           
            
            if(StopLoss > 0) double LongStopLoss = OpenPrice - (StopLoss * RealPoint);
            if(TakeProfit > 0) double LongTakeProfit = OpenPrice + (TakeProfit * RealPoint);
           
           
    if(LongStopLoss > 0 || LongTakeProfit > 0)
    {
               bool LongMod = OrderModify(LongTicket,OpenPrice,LongStopLoss, LongTakeProfit,0);
    }
    
       
    ShortTicket = 0;
   }
     double oldorderopenprice;
   int oldticketnumber;
   double unused = 0;
   int ticketnumber = 0;
   int cnt=0;
   for (  cnt = OrdersTotal() - 1; cnt >= 0; cnt--) {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
      if( OrderType() == OP_BUY) {
         oldticketnumber = OrderTicket();
         if (oldticketnumber > ticketnumber) {
            oldorderopenprice = OrderOpenPrice();
            unused = oldorderopenprice;
            ticketnumber = oldticketnumber;
         }
      }}
  
   return (oldorderopenprice);
}
 
                              
  
  // Short
  
  OrderSelect(ShortTicket,SELECT_BY_TICKET);
  if(OrderCloseTime() != 0 || ShortTicket == 0)
  {
  
               
                                   
bool sell_condition_1 = Open[]  < Close[]   ;
bool sell_condition_2 = Open[]  > Close[]   ;
bool sell_condition_3 = Open[]  > iCustom() ;
 
                                   
  if( sell_condition_1  &&  sell_condition_2  &&  sell_condition_3   )
   {
     
    OrderSelect(LongTicket,SELECT_BY_TICKET);
  
    if(OrderCloseTime() == 0 && LongTicket > 0)
     {

      Closed = OrderClose(LongTicket,OrderLots(),Bid,0,Red);
     }  
    
    
    ShortTicket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,0,0,0,"Sell Order",MagicNumber,0,Red);
    
    OrderSelect(ShortTicket,SELECT_BY_TICKET);
    OpenPrice = OrderOpenPrice();
    
           
           
            if(StopLoss > 0) double ShortStopLoss = OpenPrice + (StopLoss * RealPoint);
            if(TakeProfit > 0) double ShortTakeProfit = OpenPrice - (TakeProfit * RealPoint);
           
    if(ShortStopLoss > 0 || ShortTakeProfit > 0)
    {
               bool ShortMod = OrderModify(ShortTicket,OpenPrice,ShortStopLoss, ShortTakeProfit,0);
    }
    
        
    LongTicket = 0;
   }
    for (  cnt = OrdersTotal() - 1; cnt >= 0; cnt--) {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
      if( OrderType() == OP_SELL) {
         oldticketnumber = OrderTicket();
         if (oldticketnumber > ticketnumber) {
            oldorderopenprice = OrderOpenPrice();
            unused = oldorderopenprice;
            ticketnumber = oldticketnumber;
         }
      }
  
   return (oldorderopenprice);
}
 
   }

  
   return (0);
}


// Pip Point Function
double RealPipPoint(string Currency)
 {
  int CalcDigits = MarketInfo(Currency,MODE_DIGITS);
  if(CalcDigits == 2 || CalcDigits == 3) double CalcPoint = 0.01;
  else if(CalcDigits == 4 || CalcDigits == 5) CalcPoint = 0.0001;
  return(CalcPoint);
 }
    

whroeder1
16764
whroeder1  
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it

  2. Don't double post

  3. Why did you post your MT4 code in the Root / MT5 EA section instead of the MQL4 section?
  4. bool sell_condition_1 = Open[]  < Close[]   ;
    Your posted code is gibberish.
  5. ayman.maarouf: How to get last open order sell or buy then open a new order at same entry conditions and to close at opposite
       int cnt=0;
       for (  cnt = OrdersTotal() - 1; cnt >= 0; cnt--) {
          OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
          if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
          if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
          if( OrderType() == OP_BUY) {
             oldticketnumber = OrderTicket();
             if (oldticketnumber > ticketnumber) {
                oldorderopenprice = OrderOpenPrice();
                unused = oldorderopenprice;
                ticketnumber = oldticketnumber;
             }
          }}
    Isn't what that code is trying to do? What do you do with the result? Nothing!
  6. learn to code it, or pay (Freelance) someone to code it. We're not going to code it for you. We are willing to help you when you post your attempt (using SRC) and the nature of your problem.
To add comments, please log in or register