BE Stop doesn't work [Please Help]

 

Hi guys,

My BE Stop doesn't work, trying to find ... :( Please help

Best regards

BE Stop doesn't work

#include <Trade\trade.mqh>
CTrade trade;



void OnTick()
   {

      double Balance=AccountInfoDouble(ACCOUNT_BALANCE);

      double Equity=AccountInfoDouble(ACCOUNT_EQUITY);

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

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

   if(PositionsTotal()==0 && OrdersTotal()==0)
     {

         trade.BuyStop(1,Ask+1*_Point,_Symbol,Ask-9*_Point,Ask+21*_Point, ORDER_TIME_GTC,0,0);

         trade.SellStop(1,Bid-1*_Point,_Symbol,Bid+9*_Point,Bid-21*_Point, ORDER_TIME_GTC,0,0);

     }

   if(Balance!=Equity)
         
         CancelOrder();
            
         BE_Ask(Ask);
         BE_Bid(Bid);

   }



void CancelOrder()
  {
   for(int i=OrdersTotal()-1; i>=0; i--)
     {

      ulong OrderTicket = OrderGetTicket(i);

      trade.OrderDelete(OrderTicket);

     }

  }
  
void BE_Ask(double Ask)
   {
   
      for (int i=PositionsTotal()-1; i>=0; i--)
   {
   
      string symbol=PositionGetSymbol(i);
      
      if (_Symbol==symbol)
      
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
         {
         
               ulong PositionTicket = PositionGetInteger(POSITION_TICKET);
         
               double PositionOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
               
               double SL = PositionGetDouble(POSITION_SL);
            
               if (Ask==(PositionOpenPrice+6*_Point))
                  {
               
                     trade.PositionModify(PositionTicket,SL+14*_Point,0);
               
                  }
                  
   
               if (Ask==(PositionOpenPrice-2*_Point))
                  {
               
                     trade.PositionModify(PositionTicket,SL+6*_Point,0);
               
                  }
         
            }
            
            
         }
   
     }
     
     
void BE_Bid(double Bid)
   {
   
      for (int i=PositionsTotal()-1; i>=0; i--)
   {
   
      string symbol=PositionGetSymbol(i);
      
      if (_Symbol==symbol)
      
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
         {
         
               ulong PositionTicket = PositionGetInteger(POSITION_TICKET);
         
               double PositionOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
               
               double SL = PositionGetDouble(POSITION_SL);  
     
     
     if (Bid==(PositionOpenPrice+6*_Point))
                  {
               
                     trade.PositionModify(PositionTicket,SL+14*_Point,0);
               
                  }
               
               if (Bid==(PositionOpenPrice-2*_Point))
                  {
               
                     trade.PositionModify(PositionTicket,SL+6*_Point,0);
               
                  }
                  
         }
                  
    }
                 
   }
 
hackintosh:

Hi guys,

My BE Stop doesn't work, trying to find ... :( Please help

Best regards

I don't see any "BE Stop" in your code, so I don't know what you refer to.


What I did notice 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 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:

I don't see any "BE Stop" in your code, so I don't know what you refer to.


What I did notice 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 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.

Thank you for your answer,

My BE Stop is defined in the BE_Ask(); and BE_Bid(); functions, but it doesn't work and I'm trying to understand why ...

About Balance et Equities, I'm not trading forex but only Futures Dow Jones (MYM)

Thank you 

 
hackintosh:

Thank you for your answer,

My BE Stop is defined in the BE_Ask(); and BE_Bid(); functions, but it doesn't work and I'm trying to understand why ...

About Balance et Equities, I'm not trading forex but only Futures Dow Jones (MYM)

Thank you 

first you need to do different logic for buys and sells

and you don't do that

why two different funcitons for Ask and Bids ? do it but for buys and sells

also:

if (Ask==(PositionOpenPrice+6*_Point))
                  {
               
                     trade.PositionModify(PositionTicket,SL+14*_Point,0);


i would rather program somthing more like that (for a buy):

if (Ask>(PositionOpenPrice+6*_Point))
                  {
               
                     trade.PositionModify(PositionTicket,PositionStopLoss+14*_Point,0);


because as it has been said, the price will rarely be exactly the value you compare it too

after, there are many ways to program a breakeven, you need to explain it to us.
 
Jean Francois Le Bas:

first you need to do different logic for buys and sells

and you don't do that

why two different funcitons for Ask and Bids ? do it but for buys and sells

also:


i would rather program somthing more like that (for a buy):


because as it has been said, the price will rarely be exactly the value you compare it too

after, there are many ways to program a breakeven, you need to explain it to us.

Bonjour Jean François,

Thank you for your answer, I'm a beginner and I'm trying to make a small EA with OCO Orders + BreakEvenStop

OCO Orders works well, the only thing is breakeven stop doesn't work ...

I tried to put both breakevenstop in 1 funtction but even this doesn't work, if you can give me an example or explain me please where is wrong ?

Best regards 

 
hackintosh:

Thank you for your answer,

My BE Stop is defined in the BE_Ask(); and BE_Bid(); functions, but it doesn't work and I'm trying to understand why ...

About Balance et Equities, I'm not trading forex but only Futures Dow Jones (MYM)

Thank you 

The instrument you trade is irrelevant. You simply should not compare two parameters of type double with the == or != operators.

 
WindmillMQL:

The instrument you trade is irrelevant. You simply should not compare two parameters of type double with the == or != operators.

Thank you for your answer Windmill,

I want to trade only futures, especially Micro E Mini Dow Jomes (MYM), so when you say "The instrument you trade is irrelevant" what does it mean exactly ?

"You simply should not compare two parameters of type double with the == or != operators" - which condition I can put on that case if I can't use if(Balance!=Equity)


Best regards

if(Balance!=Equity) // In this condition I put the function CancelOrder which allows to cancel one of the pending order from OCO Orders 
         
         CancelOrder(); // This function works well on this condition if(Balance!=Equity)
            
         BE_Stop(Ask, Bid) // I'm trying to put the both condition in one fuction (This function doesn't work)

   }



void CancelOrder()
  {
   for(int i=OrdersTotal()-1; i>=0; i--)
     {

      ulong OrderTicket = OrderGetTicket(i);

      trade.OrderDelete(OrderTicket);

     }

  }

void BE_Stop(double Ask, double Bid)
   {
   
      ulong PositionTicket = PositionGetInteger(POSITION_TICKET);
         
      double PositionOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
               
      double SL = PositionGetDouble(POSITION_SL);
      
      for (int i=PositionsTotal()-1; i>=0; i--)
   {
   
      string symbol=PositionGetSymbol(i);
      
      if (_Symbol==symbol)
      
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
         {
            
               if (Ask==(PositionOpenPrice+6*_Point))
                  {
               
                     trade.PositionModify(PositionTicket,SL+14*_Point,0);
               
                  }
               
                  
       if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
          {
               
               if (Bid==(PositionOpenPrice+6*_Point))
                  {
               
                     trade.PositionModify(PositionTicket,SL+14*_Point,0);
               
                  }
         
            }
            
            
         }
   
     }


     
 
hackintosh:

Thank you for your answer Windmill,

I want to trade only futures, especially Micro E Mini Dow Jomes (MYM), so when you say "The instrument you trade is irrelevant" what does it mean exactly ?

"You simply should not compare two parameters of type double with the == or != operators" - which condition I can put on that case if I can't use if(Balance!=Equity)


Best regards


here is some pseudo-code for you


if trade == BUY

        if (Bid - OpenPrice > BESize * Point)

                OrderModifySL(OrderOpenprice)



if trade == SELL

        if (OpenPrice - Ask > BESize * Point)

                OrderModifySL(OrderOpenprice)

good enough ?


but I think you want a trailing stop rather than a breakeven ? the code is textbook breakeven

EDIT : alos you need to check values before modifying the SL (check if SL value isn't already the open price, etc) but do it with > and < not ==
 
hackintosh:

Thank you for your answer Windmill,

I want to trade only futures, especially Micro E Mini Dow Jomes (MYM), so when you say "The instrument you trade is irrelevant" what does it mean exactly ?

"You simply should not compare two parameters of type double with the == or != operators" - which condition I can put on that case if I can't use if(Balance!=Equity)


Best regards


I had already answered your question about what condition to apply in my first message to you. Please read it again.

What I mean by "the instrument you trade is irrelevant" means that it does not matter whether you trade EURUSD, MYM, any kind of stock or something else.

Reason: