Need Help- Expert Advisor- Closing Specific position (based on Specific Conditions

umairsheikh1111  

Dear ALL,

              I am developing an EA. I am still in learning stage. Now as mentioned in the code below, i am only opening one position at a specific time, if any of the condition is true. But for each specific position, i have different specific condition to close. Now i am not able to correlate this. Please help. I just want to find out which condition executed and how to put its id to close function to close it. 

 if((cond1 && cond2 etc)     
    {
     if(m_position.Select(m_symbol.Name()) && m_position.Magic() == m_magic)
          {
     if(m_position.PositionType() == POSITION_TYPE_BUY)
     return;

             }
              else 
              {
             mybuyOrder();
         }       
    
       } 
 
       else
       
        if((cond3 && cond4 etc)     
    {
          if(m_position.Select(m_symbol.Name()) && m_position.Magic() == m_magic)
          {
           if(m_position.PositionType() == POSITION_TYPE_BUY)
            return;

             }
              else 
              {
             mybuyOrder();
         }       
    
       }     


//////////


void mybuyOrder()
    {

     double ask = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits);
     double buy_sl = ask - ExtStopLoss;  
     bool  ticket=OrderSelect(ticket_no); 


     string comment = StringFormat("Buy %s %G lots at %s, SL=%s TP=%s",
                                   _Symbol, Lots,
                                   DoubleToString(ask),
                                   DoubleToString(buy_sl));
                                   
        

     if(!m_trade.Buy(Lots, m_symbol.Name(), ask, buy_sl, 0.0, comment))
         {
          
          Print("Buy() method failed. Return code=", m_trade.ResultRetcode(),
                ". Code description: ", m_trade.ResultRetcodeDescription());

         }
     else
         {
          Print("Buy() method executed successfully. Return code=", m_trade.ResultRetcode(),
                " (", m_trade.ResultRetcodeDescription(), ")");
         }
    }    
Vladimir Karputov  

"Combed" your code:

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(cond1 && cond2 etc)
     {
      if(m_position.Select(m_symbol.Name()) && m_position.Magic() == m_magic)
        {
         if(m_position.PositionType() == POSITION_TYPE_BUY)
            return;
        }
      else
        {
         mybuyOrder();
        }
     }
   else
     {
      if(cond3 && cond4 etc)
        {
         if(m_position.Select(m_symbol.Name()) && m_position.Magic() == m_magic)
           {
            if(m_position.PositionType() == POSITION_TYPE_BUY)
               return;
           }
         else
           {
            mybuyOrder();
           }
        }
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void mybuyOrder()
  {
   double ask = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits);
   double buy_sl = ask - ExtStopLoss;
   bool  ticket=OrderSelect(ticket_no);
   string comment = StringFormat("Buy %s %G lots at %s, SL=%s TP=%s",
                                 _Symbol, Lots,
                                 DoubleToString(ask),
                                 DoubleToString(buy_sl));
   if(!m_trade.Buy(Lots, m_symbol.Name(), ask, buy_sl, 0.0, comment))
     {
      Print("Buy() method failed. Return code=", m_trade.ResultRetcode(),
            ". Code description: ", m_trade.ResultRetcodeDescription());
     }
   else
     {
      Print("Buy() method executed successfully. Return code=", m_trade.ResultRetcode(),
            " (", m_trade.ResultRetcodeDescription(), ")");
     }
  }
umairsheikh1111  
Vladimir Karputov:

"Combed" your code:

Buyorder function is outside. Just to show i pasted like this.

Vladimir Karputov  
umairsheikh1111 :

Buyorder function is outside. Just to show i pasted like this.

Everything you want to show should compile. If the example doesn't compile, nobody will help.

umairsheikh1111  
Apology for this. It was my first time i posted here in this forum. I just need a hint or if you can explain through small code, it will be highly appreciated. Thankyou
Reason: