Problem on equation as additional condition

 

Hi all,

I'm having problem on the equation which will be one of the condition to my code. As you can see on my code below. I was trying to used counter "n" as a condition to my code. "n" can be n=n+3, n=n+2 and n=n-1 depend on the condition as below. But this equation seem not work at all. Please help me to solve this problem. Thanks. 

int CheckForTrade()
{  
//------------------ Check OrderType in History
   for(int i=0; i<OrdersHistoryTotal();i++)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) break;
      if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
      OProfit = OrderProfit();
      OType   = OrderType();
      OLot    = OrderLots();      
   }      
//------------------ Hedge Lot Calculation
   Hedge_Lot = NormalizeDouble((OLot*3),2);  
//------------------ Order is closed
   if(OrdersTotal()==0)
   {
//------------------ Initial trades
      if(OProfit==0)
      {
         n=0;
         Lot = Lot_Size;
         Selling();        
         return(n);
      }
//------------------ Hedge condition    
      if(OType==OP_SELL && OProfit>0)    
      {
         if(n==0)
         {            
            Lot=Lot_Size;
            Buying();            
         }
         else
         {
            n=n-1; //does not work
            Lot = Lot_Size;
            Buying();                      
         }
         return(n);
      }
      if(OType==OP_BUY && OProfit>0)
      {
         if(n==0)
         {
            Lot=Lot_Size;
            Selling();            
         }
         else
         {
            n=n-1;  //does not work
            Lot = Lot_Size;
            Selling();                      
         }
         return(n);
      }
      if(OType==OP_SELL && OProfit<0)
      {
         if (OLot>1.3)
         {
            if(n==0)
            {
               n=n+3; //does not work
            }
            else
            {
               n=n+2; //does not work
            }
            Lot=OLot;
            Buying();            
         }
         else
         {
            Lot = Hedge_Lot;
            Buying();
         }
         return(n);
      }
      if(OType==OP_BUY && OProfit<0)
      {
         if (OLot>1.3)
         {
            if(n==0)
            {
               n=n+3; //does not work
            }
            else
            {
               n=n+2; //does not work
            }
            Lot=OLot;
            Buying();            
         }
         else
         {
            Lot = Hedge_Lot;
            Selling();        
         }
         return(n);
      }
    }
   return(n);    
}
 
1) Where is n declared? What is the initial value of n - have you checked it with the debugger?

2) Why e.g. n=n+2; doe not work? Have you checked it with the debugger - I bet you if n == 5 after that n==7!

3) Instead of n=n+1 you can use either n++; or  n+=1;
 
  1. Why did you post here and not in the MQ4 section?
  2. for(int i=0; i<OrdersHistoryTotal();i++){
       if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) break;
       if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
       OProfit = OrderProfit();
       OType   = OrderType();
       OLot    = OrderLots();
    }
    What happens when OrderSelect loop doesn't find any order?
  3. What happens when OrderSelect loop finds a deleted order?
  4. You assume history is ordered by date, it's not. Could EA Really Live By Order_History Alone? (ubzen) - MQL4 forum
  5. Profit is profit + commission + swap
  6. if(OrdersTotal()==0)
    Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.) Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 forum
  7. "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.
  8. Print out your variables, and find out why.
 
zaharulrizal:

Hi all,

I'm having problem on the equation which will be one of the condition to my code. As you can see on my code below. I was trying to used counter "n" as a condition to my code. "n" can be n=n+3, n=n+2 and n=n-1 depend on the condition as below. But this equation seem not work at all. Please help me to solve this problem. Thanks. 

int CheckForTrade()
{  
//------------------ Check OrderType in History
   for(int i=0; i<OrdersHistoryTotal();i++)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) break;
      if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;

Why do you use break instead of continue in OrderSelect statement?