Current Open Order Not Selected

 
Dear Programmers,

Please I am still learning to code EA with MQL5 language however I wrote a code that involved selection of current order. There is no error when I run it but the implementation is different from my intention. My observation was that this code always referenced the first opened order instead of the currently opened one. 

The code is pasted below, kindly assist and thanks in advance for your assistance:     

#include <Trade\PositionInfo.mqh>

#include <Trade\Trade.mqh>



CPositionInfo  m_position;       // trade position object

CTrade trade;



//--- input parameters



input double   Lot     = 0.01;  // Lot size

input int      TP      = 100;  // TakeProfit





//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+



void OnTick()

 {

  

  // Get the Ask and Bid price

double Ask=NormalizeDouble (SymbolInfoDouble (_Symbol,SYMBOL_ASK),_Digits);

double Bid=NormalizeDouble (SymbolInfoDouble (_Symbol,SYMBOL_BID),_Digits);



// Get the Balance

double Balance=AccountInfoDouble (ACCOUNT_BALANCE);

// Get the Equity

double Equity=AccountInfoDouble (ACCOUNT_EQUITY);



// Place a Sell when there is no Open Order

 if(Equity==Balance)

   trade.Sell (Lot,NULL,Bid,0, (Bid-TP * _Point),NULL); 



             if (PositionsTotal()==1)

               for(int i=PositionsTotal()-1; i>=0; i--)               

                 if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties                  

                   if((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)                  

                       {                  

                       if (m_position.PriceOpen() < m_position.PriceCurrent() - (TP * _Point))       

                         trade.Buy (Lot,NULL,Ask,0,  (Ask+TP * _Point),NULL) ;

                        }

                       else

                      {                        

                     if (m_position.PriceOpen() > m_position.PriceCurrent() + (TP * _Point))       

                    trade.Sell (Lot,NULL,Bid,0, (Bid-TP * _Point),NULL);                         

                   }

 }
 
CodeFx:
Dear Programmers,

Please I am still learning to code EA with MQL5 language however I wrote a code that involved selection of current order. There is no error when I run it but the implementation is different from my intention. My observation was that this code always referenced the first opened order instead of the currently opened one. 

The code is pasted below, kindly assist and thanks in advance for your assistance:     

What I noticed is a line having "Balance == Equity". Be careful in doing this. Both Balance and Equity are of type double. These may have a rounding error. Due to this it could be for example be that Balance = 100.23 and Equity is 100.2300001. In that case are they not equal and your code will not act on that.

If you want to compare two parameters of type double is it better to define a certain limit of how much they are allowed to be different. For example: if(MathAbs(Balance-Equity) < 0.01*Balance). This allows a difference of up to 1% of the Balance value.
 
WindmillMQL:

What I noticed is a line having "Balance == Equity". 

I can't believe people actually do this. Whenever your open position is break-even your Balance == Equity. Never-ever-ever-ever use this code, OP!

 
Doubles are rarely equal. Understand the links in:
          The == operand. - MQL4 programming forum
 
WindmillMQL:

What I noticed is a line having "Balance == Equity". Be careful in doing this. Both Balance and Equity are of type double. These may have a rounding error. Due to this it could be for example be that Balance = 100.23 and Equity is 100.2300001. In that case are they not equal and your code will not act on that.

If you want to compare two parameters of type double is it better to define a certain limit of how much they are allowed to be different. For example: if(MathAbs(Balance-Equity) < 0.01*Balance). This allows a difference of up to 1% of the Balance value.

I really appreciate your time and effort at giving more insight and enlightenment on spotted error in my code, thanks so much

 
William Roeder:
Doubles are rarely equal. Understand the links in:
          The == operand. - MQL4 programming forum

Thanks for educating me.