Need help with EA conditions please

 

Martingle Strategy.

EA places a buy Order, then when price falls below the spicified amount of pips below first order it places another buy order.

I'm having problems printing the second test line, which is where the second buy order would execute from.

e.g. Print(" ----- SECOND TEST LINE ----- ");

I can print the first test line, so I'm guessing it is the condition before the second test line.

Pulling my hair out … any help appriciated .

extern int    Magic                    = 772177;
   extern double TakeProfit               = 50;
   extern double LotSize                  = 0.1;
   extern double StopLoss                 = 50;           
   extern int    MaxMartingleTrades       = 3;
   extern int    firstMartinglePosition   = 25;
   extern int    slippage                 = 5;

   double UsePoint;
   int UseSlippage;
   int myOrderType;
   int cnt;


int init()
{
      {
         UsePoint = PipPoint(Symbol());
         UseSlippage = GetSlippage(Symbol(),slippage);

      }
      
   return(0);
}

int deinit()
  {
   return(0);
  }

int start()
  {
//----
          myOrderType = ConditionsMet();
 
         //+------------------------------------------------------------------+
         //| First Buy Order                                                  |
         //+------------------------------------------------------------------+
    
         if(OrdersTotal() < 1 && myOrderType==1) 
            {
               double OpenPrice = Ask;
                  if(StopLoss > 0) 
                     {
                        double BuyStopLoss = OpenPrice - (StopLoss * UsePoint);
                     }
                  if(TakeProfit > 0) 
                     {
                        double BuyTakeProfit = OpenPrice + (TakeProfit * UsePoint);
                     }
                     
               OrderSend(Symbol(),OP_BUY,LotSize,OpenPrice,UseSlippage,
                  BuyStopLoss,BuyTakeProfit,"Buy Order",Magic,0,Lime);
               
               bool MartingleOne = True;
            }
            
         //+------------------------------------------------------------------+
         //| First Buy Martingle Position                                     |
         //+------------------------------------------------------------------+
         
         while(MartingleOne)
            {   
               for(cnt=OrdersTotal();cnt>=0;cnt--)
                  {
                     OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
                          if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) 
                            {   
                                    if (OrderType()==OP_BUY) 
                                       {
                                          Print(" ---- FIRST TEST LINE ----- ");
                                                        
                                                       if (OrderOpenPrice()-Ask >= (firstMartinglePosition * UsePoint))  // NEED HELP Here!
                                                          {                                                    
                                          Print(" ----- SECOND TEST LINE ----- ");   
                                       
                 
                                       MartingleOne = false;    
                                    }
                  
                              }
                           }
                        }
                    }    
  
//----
   return(0);
  }
//+------------------------------------------------------------------+

//----------- Pip Calculation
   
   //----------- Pip Point Function
   
   double PipPoint(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);
      }
      
   //---------- Get Slippage Function
   
   int GetSlippage(string Currency, int SlippagePips)
      {
         int CalcDigits = MarketInfo(Currency,MODE_DIGITS);
            if(CalcDigits == 2 || CalcDigits == 4) 
               {
                  double CalcSlippage = SlippagePips;
               }    
            else if(CalcDigits == 3 || CalcDigits == 5) 
               {
                  CalcSlippage = SlippagePips * 10;
               } 
            return(CalcSlippage);
      }
      
      
    int ConditionsMet()
      {
         int NoTrade=0,ConditionBuy_1=0,ConditionBuy_2=0,ConditionSell_1=0,ConditionSell_2=0;
         
         // -----------------Custom Indicators here -----------
         
         // Buy Order
         if(((ConditionBuy_1 + ConditionBuy_2) >= 6)) 
         {
         myOrderType=2;    
         }  
      
      // Sell Order
      if(((ConditionSell_1 + ConditionSell_2) <= -6))  
         {
         myOrderType=1;    
         } 

      return(myOrderType);
      
      }
 
         if(OrdersTotal() < 1 && myOrderType==1) 
            {         
               bool MartingleOne = True;
            }

Cutting out the other code, you can see that MartingleOne, being a local variable can only be true when the if condition is met.

ie when there are no open orders

The following code will only be executed during the same tick as the order send, so the conditions for the 2nd print are not possible.

         
         while(MartingleOne)
            {   
               for(cnt=OrdersTotal();cnt>=0;cnt--)
                  {
                     OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
                          if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) 
                            {   
                                    if (OrderType()==OP_BUY) 
                                       {
                                          Print(" ---- FIRST TEST LINE ----- ");
                                                        
                                                       if (OrderOpenPrice()-Ask >= (firstMartinglePosition * UsePoint))  // NEED HELP Here!
                                                          {                                                    
                                          Print(" ----- SECOND TEST LINE ----- ");   
                                       
                 
                                       MartingleOne = false;    
                                    }
                  
                              }
                           }
                        }
 
krystalskull: Martingle Strategy.

  1. Guaranteed to wipe your account. Just send the money to me. I'll be happier, you'll still be broke.
  2.                for(cnt=OrdersTotal();cnt>=0;cnt--)
                      {
                         OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
    
    There are OrdersTotal() open orders [0 .. OrdersTotal()-1] so the first OrderSelect(OrdersTotal()) will ALWAYS fail, but you continue on as if you do have a selected order.
  3. Check What are Function return values ? How do I use them ? - MQL4 forum
 
GumRai:

Cutting out the other code, you can see that MartingleOne, being a local variable can only be true when the if condition is met.

ie when there are no open orders

The following code will only be executed during the same tick as the order send, so the conditions for the 2nd print are not possible.

OK thx GumRai ... I can see now, thanks alot for that!
 
WHRoeder:
  1. Guaranteed to wipe your account. Just send the money to me. I'll be happier, you'll still be broke.
  2. There are OrdersTotal() open orders [0 .. OrdersTotal()-1] so the first OrderSelect(OrdersTotal()) will ALWAYS fail, but you continue on as if you do have a selected order.
  3. Check What are Function return values ? How do I use them ? - MQL4 forum

1. ok thanks for the warning !!!
2. ok I will add -1
3. thanks for the link, yes was very helpful.
4. Great forum by the way!
 

Thanks for previous help.

I removed the bool condition and while loop, and added -1 to the orderselect() for loop.

I am still not able to print the second Test line.

I am able to store the OrderOpenPrice() from the original buy position into the BuyOpenPrice varible ( see first Test Line below).

2014.02.07 13:06:01 2013.12.30 16:00 MBFX EA v1.02 EURUSD,H4: ---- FIRST TEST LINE ----- Buy Open Price = 1.3788
2014.02.07 13:06:01 2013.12.30 16:00 MBFX EA v1.02 EURUSD,H4: open #7 buy 0.10 EURUSD at 1.37882 sl: 1.37382 tp: 1.38382 ok

However, I still can't get the line before the Second Test Line to execute!

int BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,OpenPrice,UseSlippage,
                  BuyStopLoss,BuyTakeProfit,"Buy Order",Magic,0,Lime);
               
              
            }

            
         //+------------------------------------------------------------------+
         //| First Buy Martingle                                              |
         //+------------------------------------------------------------------+
         
         if(BuyTicket > 0  && MartingleOneTicket == 0)
            {   
                for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
                  {
                     OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
                          if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) 
                            {
                            double BuyOpenPrice = OrderOpenPrice();
                
                             if (OrderType()==OP_BUY)   
                                {
                                   Print(" ---- FIRST TEST LINE ----- Buy Open Price = ", BuyOpenPrice); 
                                        
                                           if (BuyOpenPrice -Ask >= (firstMartinglePosition * UsePoint))  // Issue Here!
                                              {                                                
                                             Print(" ----- SECOND TEST LINE ----- ");
                              
                           
                           
                                       double FirstBuyMartingleOpen = Ask;

                                          if(StopLoss > 0) 
                                             {
                                                BuyStopLoss = FirstBuyMartingleOpen - (StopLoss * UsePoint);  // Calculate stop loss 
                                             }

                                          if(TakeProfit > 0) 
                                             {
                                                BuyTakeProfit = FirstBuyMartingleOpen + (TakeProfit * UsePoint); // Calculate  take profit
                                             } 

                                          MartingleOneTicket = OrderSend(Symbol(),OP_BUY,LotSize*FirstMultiplier,FirstBuyMartingleOpen,UseSlippage,
                                                BuyStopLoss,BuyTakeProfit,"Buy Order",Magic,0,Blue); 
                           
        
                                   
                                          }
         
                                 }
                         }
                     }
                }
Reason: