Simple code for re-open orders

Back to topics list To post a new topic, please log in or register
avatar
40
EgyAlgoTrader 2015.06.21 17:05
 

Hi, 

I just start learning mql4 . I am here trying to code a simple EA that start with:

1- Check if no orders opened, i open 3 orders ( 2 buys and one sell). Each order has a specific magic number so i could select them easily.

2- Check if opened orders < 3 orders then i check which ones are closed. What i need here is to check if bMagicf AND sMagicf are closed, then i reopen them. It has to be both closed to reopen them together.

 

It seems i am doing something wrong. I know my coding still very primitive but your help will be much appreciated to get it better. Thanks in advance.

//+------------------------------------------------------------------+
//|                                          CheckSelectOpenCode.mq4 |
//|                                                        MQL4stuff | 
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "MQL4stuff"
#property link      ""
#property version   "1.00"
#property strict

extern string TradingConditions              = " ---  Trading Conditions --- ";
extern double target_profitBF1 = 15.0;    //  TP 
extern double stopLossBF1 = 15.0 ;// Stop loss

extern double target_profitSF2 = 15.0;    //  TP 
extern double stopLossSF2 = 15.0 ;// Stop loss

extern double target_profitB3 = 25.0;    //  TP 
extern double stopLossB3 = 25.0 ;// Stop loss


extern double slippage = 5.0; // Slippage
extern double starting_lot_size = 0.01;    //  Start Lot Size

//+------------------------------------------------------------------+
//| Global Variables                                                 |
//+------------------------------------------------------------------+


double pt;
double stoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL); // minimal pips for stop
                                                      //stoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL); 
                                                      // get broker's stoplevel

extern int bMagicf = 1000; // Martingale Buy magic # for subsequent orders
extern int sMagicf = 2000; // Martingale Sell magic # for subsequent orders
extern int bMagic = 3000; // Martingale Buy magic # for subsequent orders



 

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
 
  {
    if(Digits==3 || Digits==5) pt=10*Point;  // when use at broker with 5 digits 
    else                       pt=Point;     // pricing, this function is useful
                                       
    return(0);
  }
 
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  
  
  
    double stoploss1 = Ask - (stopLossBF1 * pt);
    double tp1       = Ask + (target_profitBF1 * pt);
    
    double stoploss2 = Bid + (stopLossSF2 * pt);
    double tp2       = Bid - (target_profitSF2 * pt);
    
    double stoploss3 = Ask - (stopLossB3 * pt);
    double tp3       = Ask + (stopLossB3 * pt);
    
    int ordersCount = OrdersTotal();
 
 if (ordersCount == 0)// No orders are there so let us open the three orders
        {
 
           OrderSend(Symbol(),OP_BUY,starting_lot_size,Ask,slippage,stoploss1,tp1,"",bMagicf,0,clrLawnGreen);
 
           OrderSend(Symbol(),OP_SELL,starting_lot_size,Bid,slippage,stoploss2,tp2,"",sMagicf,0,clrOrangeRed);
 
            OrderSend(Symbol(),OP_BUY,starting_lot_size,Ask,slippage,stoploss3,tp3,"",bMagic,0,clrLawnGreen);

         return;
        }
 
 else if (ordersCount >= 1 && ordersCount < 3)// so there is some orders opened
       {

      for (  int i = OrdersTotal (); i >= 0; i--)// looping among orders
         {    
         if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
           if ( !OrderMagicNumber () == bMagicf) // if bMagicf not there, i loop again to check if there is also no sMagicf
           {
           for (  int i = OrdersTotal (); i >= 0; i--)
             {
             if (!OrderMagicNumber () == sMagicf) // so no both , sMagicf and bMagicf
                { 
                  Comment ( " No both bMagicf and sMagicf orders");
                  OrderSend(Symbol(),OP_BUY,starting_lot_size,Ask,slippage,stoploss1,tp1,"BOback",bMagicf,0,clrLawnGreen);//reopen
                  OrderSend(Symbol(),OP_SELL,starting_lot_size,Bid,slippage,stoploss2,tp2,"SOback",sMagicf,0,clrOrangeRed);//reopen
                }
             }
           }
  
          }
        }
  }
               
           
     
 

            

 

avatar
13006
WHRoeder 2015.06.21 19:36 #
 
  1.  if (ordersCount == 0)// No orders are there so let us open the three orders
    Fails in the presence of other orders on other charts, or EA's or manual trading https://forum.mql4.com/54001#758222
  2.            OrderSend(Symbol(),OP_BUY,starting_lot_size,Ask,slippage,stoploss1,tp1,"",bMagicf,0,clrLawnGreen);
    
    What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  3.            if ( !OrderMagicNumber () == bMagicf) // if bMagicf not there, i loop again to check if there is also no sMagicf
    
    Why are you converting the selected order's magic number (123) to a bool (true) inverting it by "!" (0), converting it to an int (0) and comparing it to you bMagicf? When will that ever be true?
  4.          for (  int i = OrdersTotal (); i >= 0; i--)
                 {
                 if (!OrderMagicNumber () == sMagicf) // so no both , sMagicf and bMagicf
    Why do you have a second loop where the only thing changing is i. OrderMagicNumber is still the order selected in #3
  5. One loop, count if bMagicf or sMagicf is there. Then decide what to do.

avatar
40
EgyAlgoTrader 2015.06.23 05:50 #
 
WHRoeder:
  1. Fails in the presence of other orders on other charts, or EA's or manual trading https://forum.mql4.com/54001#758222
  2. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  3. Why are you converting the selected order's magic number (123) to a bool (true) inverting it by "!" (0), converting it to an int (0) and comparing it to you bMagicf? When will that ever be true?
  4. Why do you have a second loop where the only thing changing is i. OrderMagicNumber is still the order selected in #3
  5. One loop, count if bMagicf or sMagicf is there. Then decide what to do.
    //+------------------------------------------------------------------+
    //|                                          CheckSelectOpenCode.mq4 |
    //|                                                        MQL4stuff | 
    //|                                                                  |
    //+------------------------------------------------------------------+
    #property copyright "MQL4stuff"
    #property link      ""
    #property version   "1.00"
    #property strict
    
    extern string TradingConditions              = " ---  Trading Conditions --- ";
    extern double target_profitBF1 = 15.0;    //  TP 
    extern double stopLossBF1 = 15.0 ;// Stop loss
    
    extern double target_profitSF2 = 15.0;    //  TP 
    extern double stopLossSF2 = 15.0 ;// Stop loss
    
    extern double target_profitB3 = 25.0;    //  TP 
    extern double stopLossB3 = 25.0 ;// Stop loss
    
    
    extern double slippage = 5.0; // Slippage
    extern double starting_lot_size = 0.01;    //  Start Lot Size
    
    //+------------------------------------------------------------------+
    //| Global Variables                                                 |
    //+------------------------------------------------------------------+
    
    
    double pt;
    double stoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL); // minimal pips for stop
                                                          //stoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL); 
                                                          // get broker's stoplevel
    
    extern int bMagicf = 1000; // Martingale Buy magic # for subsequent orders
    extern int sMagicf = 2000; // Martingale Sell magic # for subsequent orders
    extern int bMagic = 3000; // Martingale Buy magic # for subsequent orders
    
    
    
     
    
    //+------------------------------------------------------------------+
    //| Expert initialization function                                   |
    //+------------------------------------------------------------------+
    int OnInit()
     
      {
        if(Digits==3 || Digits==5) pt=10*Point;  // when use at broker with 5 digits 
        else                       pt=Point;     // pricing, this function is useful
                                           
        return(0);
      }
     
    //+------------------------------------------------------------------+
    //| Expert deinitialization function                                 |
    //+------------------------------------------------------------------+
    void OnDeinit(const int reason)
      {
    //---
       
      }
    //+------------------------------------------------------------------+
    //| Expert tick function                                             |
    //+------------------------------------------------------------------+
    void OnTick()
      {
      
      
      
        double stoploss1 = Ask - (stopLossBF1 * pt);
        double tp1       = Ask + (target_profitBF1 * pt);
        
        double stoploss2 = Bid + (stopLossSF2 * pt);
        double tp2       = Bid - (target_profitSF2 * pt);
        
        double stoploss3 = Ask - (stopLossB3 * pt);
        double tp3       = Ask + (stopLossB3 * pt);
        
        int ordersCount = OrdersTotal();
     
     if (ordersCount == 0)// No orders are there so let us open the three orders
            {
     
               
               int  ticketNumber1 = OrderSend(Symbol(),OP_BUY,starting_lot_size,Ask,slippage,stoploss1,tp1,"",bMagicf,0,clrLawnGreen);
               if( ticketNumber1 > 0 )
                    {
                      Print("Order bMagicf placed # ", ticketNumber1);
                    }
               else
                    {
                      Print("Order bMagicf Send failed, error # ", GetLastError() );
                    }
               int ticketNumber2 = OrderSend(Symbol(),OP_SELL,starting_lot_size,Bid,slippage,stoploss2,tp2,"",sMagicf,0,clrOrangeRed);
               if( ticketNumber2 > 0 )
                    {
                      Print("Order sMagicf placed # ", ticketNumber2);
                    }
               else
                    {
                      Print("Order sMagicf Send failed, error # ", GetLastError() );
                    }
               
     
               int ticketNumber3 = OrderSend(Symbol(),OP_BUY,starting_lot_size,Ask,slippage,stoploss3,tp3,"",bMagic,0,clrLawnGreen);
                
               if( ticketNumber3 > 0 )
                    {
                      Print("Order bMagic placed # ", ticketNumber3);
                    }
              else
                    {
                      Print("Order bMagic Send failed, error # ", GetLastError() );
                    }
             return;
            }
     
     else if (ordersCount >= 1 && ordersCount < 3)// so there is some orders opened
           {
    
          for (  int i = OrdersTotal (); i >= 0; i--)// looping among orders
             {    
             if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; 
               if ( OrderMagicNumber () != bMagicf || OrderMagicNumber () != sMagicf) // if bMagicf or sMagic? i want if both are not there
                    {
                      Comment ( "  both bMagicf and sMagicf to be reopened");
                      int ticketNumber4 = OrderSend(Symbol(),OP_BUY,starting_lot_size,Ask,slippage,stoploss1,tp1,"BOback",bMagicf,0,clrLawnGreen);//reopen
                      if (ticketNumber4 >0) Print ("bMagicf reopened");
                      else Print (" bMagicf reopened failed ", GetLastError ());
    
                      int ticketNumber5 = OrderSend(Symbol(),OP_SELL,starting_lot_size,Bid,slippage,stoploss2,tp2,"SOback",sMagicf,0,clrOrangeRed);//reopen
                      if (ticketNumber5 > 0) Print (" sMagicf reopened");
                      else Print (" bMagicf reopened failed ", GetLastError ());
                     }
              }
           }
      
        }
           

I have tried to modify my code based on your kind remarks.

1- I got your point but i will postpone to add this snippet for later because i am focusing now to make my code do what i want for this specific function. It is not for trading now but educational

2- I added the check status for OrderSend

3-This part, i did not understand completely. I omit the second loop but the code is not doing what i want still. I want to check if those 2 specific orders " BOTH" are not there, so i reopen them. If one of them is still there, i can not reopen.

4- I used  if (OrderMagicNumber () != sMagicf || OrderMagicNumber () != bMagicf). However, i thought i need to loop first to check if i do not have one and then within i loop again to see if there is also no the other. 

 Thank you so much for your kind help


  

avatar
13006
WHRoeder 2015.06.23 16:37 #
 
  1. if ( OrderMagicNumber () != bMagicf || OrderMagicNumber () != sMagicf) // if bMagicf or sMagic? i want if both are not there
    If the order is bMagicf then this if is true. If the order is sMagicf then this if is true. If the order is anything else, this if is true.
  2. What part of counting how many of each you have and deciding what to do after the loop, was unclear?
avatar
40
EgyAlgoTrader 2015.06.24 05:04 #
 

 

//+------------------------------------------------------------------+
//|                                          CheckSelectOpenCode.mq4 |
//|                                                        MQL4stuff | 
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "MQL4stuff"
#property link      ""
#property version   "1.00"
#property strict

extern string TradingConditions              = " ---  Trading Conditions --- ";
extern double target_profitBF1 = 15.0;    //  TP buyfirst 
extern double stopLossBF1 = 15.0 ;// Stop loss

extern double target_profitSF2 = 30.0;    //  TP sellfirst
extern double stopLossSF2 = 30.0 ;// Stop loss

extern double target_profitB3 = 45.0;    //  TP buysecond
extern double stopLossB3 = 45.0 ;// Stop loss


extern double slippage = 5.0; // Slippage
extern double starting_lot_size = 0.01;    //  Start Lot Size

//+------------------------------------------------------------------+
//| Global Variables                                                 |
//+------------------------------------------------------------------+
double pt;
double stoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL); // minimal pips for stop
                                                      //stoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL); 
                                                      // get broker's stoplevel

extern int magic = 5555; // Martingale Buy magic # for subsequent orders

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
    if(Digits==3 || Digits==5) pt=10*Point;  // when use at broker with 5 digits 
    else                       pt=Point;     // pricing, this function is useful
                                       
    return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
    double stoploss1 = Ask - (stopLossBF1 * pt);
    double tp1       = Ask + (target_profitBF1 * pt);
    
    double stoploss2 = Bid + (stopLossSF2 * pt);
    double tp2       = Bid - (target_profitSF2 * pt);
    
    double stoploss3 = Ask - (stopLossB3 * pt);
    double tp3       = Ask + (stopLossB3 * pt);
    
    int ordersCount = OrdersTotal();
 
 if (ordersCount == 0)// No orders are there so let us open the three orders
        {
           int  ticketNumber1 = OrderSend(Symbol(),OP_BUY,starting_lot_size,Ask,slippage,stoploss1,tp1,"",((magic*10)+1),0,clrLawnGreen);
           if( ticketNumber1 > 0 )Print("Order buyfirst placed # ", ticketNumber1);   
           else                   Print("Order buyfirst Send failed, error # ", GetLastError() );
   
           int ticketNumber2 = OrderSend(Symbol(),OP_SELL,starting_lot_size,Bid,slippage,stoploss2,tp2,"",((magic*10)+2),0,clrOrangeRed);
           if( ticketNumber2 > 0 )Print("Order sellfirst placed # ", ticketNumber2);  
           else                   Print("Order sellfirst Send failed, error # ", GetLastError() );

           int ticketNumber3 = OrderSend(Symbol(),OP_BUY,starting_lot_size,Ask,slippage,stoploss3,tp3,"",((magic*10)+3),0,clrLawnGreen);
            
           if( ticketNumber3 > 0 ) Print("Order buysecond placed # ", ticketNumber3); 
           else                    Print("Order buysecond Send failed, error # ", GetLastError() );
           
           Print ( "Orders Total now is " , OrdersTotal ());
           
         return;
        }
 
 else if (ordersCount >= 1 && ordersCount < 3)// some orders are closed
       {
       Print (" Orders now are less than 3");

      for (  int i = OrdersTotal (); i >= 0; i--)// looping among orders
         {    
         if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; 
           if ( OrderMagicNumber () == (magic*10)+1 || OrderMagicNumber() == (magic*10)+2) break;// if selected order is buyfirst or sellfirst, do nothing and break from looping
           else // otherwise, reopen both. The problem if the selected order is the buysecond :( but still there is one of the other orders
                {
                  Comment ( "  both buyfirst and sellfirst are not there");
                  int ticketNumber4 = OrderSend(Symbol(),OP_BUY,starting_lot_size,Ask,slippage,stoploss1,tp1,"BOback",((magic*10)+1),0,clrLawnGreen);//reopen
                  if (ticketNumber4 >0) Print ("buyfirst reopened");
                  else Print (" buyfirst reopened failed ", GetLastError ());

                  int ticketNumber5 = OrderSend(Symbol(),OP_SELL,starting_lot_size,Bid,slippage,stoploss2,tp2,"SOback",((magic*10)+2),0,clrOrangeRed);//reopen
                  if (ticketNumber5 > 0) Print (" sellfirst reopened");
                  else Print (" sellfirst reopened failed ", GetLastError ());
                 }
          }
          
       }
     return;
    }

 

I did some changes but still not working as i want :(

 

avatar
40
EgyAlgoTrader 2015.06.24 12:36 #
 

Now, i think it is working as i want. I like programming by doing and trying. Thanks WHRoeder for your kind help

//+------------------------------------------------------------------+
//|                                          CheckSelectOpenCode.mq4 |
//|                                                        MQL4stuff | 
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "MQL4stuff"
#property link      ""
#property version   "1.00"
#property strict

extern string TradingConditions              = " ---  Trading Conditions --- ";
extern double target_profitBF1 = 15.0;    //  TP buyfirst 
extern double stopLossBF1 = 15.0 ;// Stop loss

extern double target_profitSF2 = 30.0;    //  TP sellfirst
extern double stopLossSF2 = 30.0 ;// Stop loss

extern double target_profitB3 = 45.0;    //  TP buysecond
extern double stopLossB3 = 45.0 ;// Stop loss


extern double slippage = 5.0; // Slippage
extern double starting_lot_size = 0.01;    //  Start Lot Size

//+------------------------------------------------------------------+
//| Global Variables                                                 |
//+------------------------------------------------------------------+
double pt;
double stoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL); // minimal pips for stop
                                                      //stoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL); 
                                                      // get broker's stoplevel

extern int magic = 5555; // magic # for first bunch of orders

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
    if(Digits==3 || Digits==5) pt=10*Point;  // when use at broker with 5 digits 
    else                       pt=Point;     // pricing, this function is useful
                                       
    return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
    double stoploss1 = Ask - (stopLossBF1 * pt);
    double tp1       = Ask + (target_profitBF1 * pt);
    
    double stoploss2 = Bid + (stopLossSF2 * pt);
    double tp2       = Bid - (target_profitSF2 * pt);
    
    double stoploss3 = Ask - (stopLossB3 * pt);
    double tp3       = Ask + (stopLossB3 * pt);
    
    int ordersCount = OrdersTotal();
 
 if (ordersCount == 0)// No orders are there so let us open the three orders
        {
           int  ticketNumber1 = OrderSend(Symbol(),OP_BUY,starting_lot_size,Ask,slippage,stoploss1,tp1,"",((magic*10)+1),0,clrLawnGreen);
           if( ticketNumber1 > 0 )Print("Order buyfirst placed # ", ticketNumber1);   
           else                   Print("Order buyfirst Send failed, error # ", GetLastError() );
   
           int ticketNumber2 = OrderSend(Symbol(),OP_SELL,starting_lot_size,Bid,slippage,stoploss2,tp2,"",((magic*10)+2),0,clrOrangeRed);
           if( ticketNumber2 > 0 )Print("Order sellfirst placed # ", ticketNumber2);  
           else                   Print("Order sellfirst Send failed, error # ", GetLastError() );

           int ticketNumber3 = OrderSend(Symbol(),OP_BUY,starting_lot_size,Ask,slippage,stoploss3,tp3,"",((magic*10)+3),0,clrLawnGreen);
            
           if( ticketNumber3 > 0 ) Print("Order buysecond placed # ", ticketNumber3); 
           else                    Print("Order buysecond Send failed, error # ", GetLastError() );
           
           Print ( "Orders Total now is " , OrdersTotal ());
        }
 
 else if (ordersCount >= 1 && ordersCount < 3)// some orders are closed
       {
       Print (" Orders now are less than 3");

     if ( ! IsBuyfirstOpen () && ! IsSellfirstOpen () ) 
      
                {
                  Print ( "  both buyfirst and sellfirst are not there");
                  int ticketNumber4 = OrderSend(Symbol(),OP_BUY,starting_lot_size,Ask,slippage,stoploss1,tp1,"BOback",((magic*10)+1),0,clrLawnGreen);//reopen
                  if (ticketNumber4 >0) Print ("buyfirst reopened");
                  else Print (" buyfirst reopened failed ", GetLastError ());

                  int ticketNumber5 = OrderSend(Symbol(),OP_SELL,starting_lot_size,Bid,slippage,stoploss2,tp2,"SOback",((magic*10)+2),0,clrOrangeRed);//reopen
                  if (ticketNumber5 > 0) Print (" sellfirst reopened");
                  else Print (" sellfirst reopened failed ", GetLastError ());
                 }
        }
       return;
  }

//+------------------------------------------------------------------+
//| Check if still there is buyfirst                                 |
//+------------------------------------------------------------------+

 bool IsBuyfirstOpen ()
     {         
      for (  int i = OrdersTotal (); i >= 0; i--)// looping among orders
         {    
         if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; 
           if ( OrderMagicNumber () == (magic*10)+1 ) 
             return (true);// if selected order is buyfirst, return true
             
          }
         return (false); 
      }  
      
//+------------------------------------------------------------------+
//| Check if still there is sellfirst                                |
//+------------------------------------------------------------------+

  bool IsSellfirstOpen ()
     {         
      for (  int i = OrdersTotal (); i >= 0; i--)// looping among orders
         {    
         if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; 
           if ( OrderMagicNumber () == (magic*10)+2 ) 
             return (true);// if selected order is sellfirst, return true
              
          }
        return (false);  
      }
      
//+------------------------------------------------------------------+
//| Check if still there is buysecond                                |
//+------------------------------------------------------------------+

  bool IsBuysecondOpen ()
     {         
      for (  int i = OrdersTotal (); i >= 0; i--)// looping among orders
         {    
         if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; 
           if ( OrderMagicNumber () == (magic*10)+3 ) 
             return (true);// if selected order is buysecond, return true
             
          }
        return (false); 
      }  

 

 

 

Back to topics list  

To add comments, please log in or register