EA question - page 2

 
darchii:

Could you help me further because it is still opening position at the same price. My tick0 would like to be the price where not to open another position at. My buy condition will be something like:

I tried to search on forums but still with no results.

Thank you in advance,

Never compare doubles with == or != (exception is usually 0). If I really must compare prices with the == sign then I first convert it to integer. Every coder here have a different flavor of that function. Example of mines can be found here. I use a function called p2i (price to integer). You cannot see the floating point value in the 20th place for example.

1.12345000000000000001 vs  1.12345000000000000009. When you use Print commands, it'll say something like  1.123450000 and  1.123450000. But the above 2 values are not equal.

Additional reading.

 
RaptorUK:
It is possible that   Bid != tick0  even if Bid is equal to tick0 . . .  read this thread:  Can price != price ?


// Start function
int start()
        {
                // Moving averages
                double FastMA = iMA(NULL,0,FastMAPeriod,0,0,0,0);
                double SlowMA = iMA(NULL,0,SlowMAPeriod,0,0,0,0);
      
      int i;
                
                total = OrdersTotal();
      
      // Buy order 
       for (i = 0; i <= total; i++)
       {
**            if  (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))                   //since I write this line my EA stop open possition
            {    
               if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
               {
                  if (OrderType() == OP_BUY)
                 {
                                 if((FastMA > SlowMA)&& (CompareDoubles(Ask,OrderOpenPrice())))
                                      {
                                      
                                       double OpenPrice = Ask;
                                        // Calculate stop loss and take profit
                                        if(StopLoss > 0) double BuyStopLoss = OpenPrice - (StopLoss * UsePoint);
                                        if(TakeProfit > 0) double BuyTakeProfit = OpenPrice + (TakeProfit * UsePoint);
                                
                                        // Open buy order
                                        BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,OpenPrice,UseSlippage,BuyStopLoss,BuyTakeProfit,"Buy Order",MagicNumber,Green);
                                
                                           }
                 }
               }
             }
            }
return;
Could you help me with this one? Because I can not find my mistake, or if I did something wrong (since I write that line **). I read the topics that you suggested but I am stuck here.
 
darchii:

Could you help me with this one? Because I can not find my mistake, or if I did something wrong (since I write that line **). I read the topics that you suggested but I am stuck here.

Your loop is wrong,  why are you executing the loop if  total  equals 0 ?  i.e. if you have no open orders ?   if you fix your loop and you have no open orders then there is no way that your OrderSend() can ever be executed . . .

When you write code you need to read it and execute it in your mind,  line by line, to see if it makes sense and works.  So start at the beginning,  there are no open orders,  total  equals 0  what happens ? 

 

// Start function
int start()
   {
   // Moving averages
   double FastMA = iMA(NULL,0,FastMAPeriod,0,0,0,0);
   double SlowMA = iMA(NULL,0,SlowMAPeriod,0,0,0,0);
      
   int i;
                
   total = OrdersTotal();
      
   // Buy order 
   for (i = 0; i < total; i++)    // fixed    <  not  <=
      {
      if  (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))                   //since I write this line my EA stop open possition
         {    
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
            {
            if (OrderType() == OP_BUY)
               {
               if((FastMA > SlowMA)&& (CompareDoubles(Ask,OrderOpenPrice())))
                  {
                  double OpenPrice = Ask;
                  // Calculate stop loss and take profit
                  if(StopLoss > 0) double BuyStopLoss = OpenPrice - (StopLoss * UsePoint);
                  if(TakeProfit > 0) double BuyTakeProfit = OpenPrice + (TakeProfit * UsePoint);
                                
                  // Open buy order
                  BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,OpenPrice,UseSlippage,BuyStopLoss,BuyTakeProfit,"Buy Order",MagicNumber,Green);
                  }
               }
            }
         }
      }
   return(0);
   }

 

But you are not checking if a trade was opened at the current price,  isn't that what you wanted to do ?  you need to check for this inside the loop,  if there is a trade at current price then remember this fact by setting a bool variable to true,  then outside the loop check this variable and if false you can place your trade. 

 
RaptorUK:

Your loop is wrong,  why are you executing the loop if  total  equals 0 ?  i.e. if you have no open orders ?   if you fix your loop and you have no open orders then there is no way that your OrderSend() can ever be executed . . .

When you write code you need to read it and execute it in your mind,  line by line, to see if it makes sense and works.  So start at the beginning,  there are no open orders,  total  equals 0  what happens ? 

 

 

But you are not checking if a trade was opened at the current price,  isn't that what you wanted to do ?  you need to check for this inside the loop,  if there is a trade at current price then remember this fact by setting a bool variable to true,  then outside the loop check this variable and if false you can place your trade. 

 bool IsTrade=true;    
       for (int i = 0; i < total; i ++) 
       {
      if ( OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) 
      
      if(OrderType() <= OP_SELL &&  OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) 
      
      {
      
      if (CompareDoubles(OrderOpenPrice(), OpenPrice)) return(true);
       
       {   
                              
                                      
                                        // Calculate stop loss and take profit
                                        if(StopLoss > 0) double BuyStopLoss = OpenPrice - (StopLoss * UsePoint);
                                        if(TakeProfit > 0) double BuyTakeProfit = OpenPrice + (TakeProfit * UsePoint);
                                
                                        // Open buy order
                                        BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,OpenPrice,UseSlippage,BuyStopLoss,BuyTakeProfit,"Buy Order",MagicNumber,Green);
        }                       
        }
       else return(false);
               {
               Print ("Nu s-au deschis pozitii");
                }
        }
return(0);             
        
I tried this way but still open double position... I think my boll is wrong placed , and my check open position is that compare double ... is that one right?
       
 
darchii:
I tried this way but still open double position... I think my boll is wrong placed , and my check open position is that compare double ... is that one right?
       

What function is this code in ?

This suggests it returns a bool . . .

      if (CompareDoubles(OrderOpenPrice(), OpenPrice)) return(true);

 yet this suggests it returns an int ?

return(0);

 Why is your OrderSend() still within the loop ?  where does  OpenPrice  come from ?  

Reason: