incomprehensible error

 

Hi all,

First Sorry for my bad english !

I would like to know if someone can explain that !

the simply code below is for changing takeprofit during night session !

it work fine but i have modify error 1 only when calling mode_Jour.

the problem seem to be this condition

oldtp = OrderTakeProfit();

newtp = OrderOpenPrice()+TPNuit*iPoint2Pip;

if(oldtp != newtp)

yet the two variables are equal ...

the strange thing is Mode_Jour is a paste copy of Mode_nuit only TPdefaut is changed.

I'm going to be mad, i dont understand what is wrong ......

Thanks

Here the code

//+------------------------------------------------------------------+
//|                                                    kittytest.mq4 |
//|                      Copyright © 2012, MetaQuotes Software Corp. |
//|                                        https://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012, MetaQuotes Software Corp."
#property link      "https://www.metaquotes.net"
extern int TPDefaut = 50;
extern string sepa4 ="----- Mode Nuit -----";
extern bool ModeTpNuit = False;
extern int Minute_trade_avant_mode_nuit = 120;
extern int TPNuit = 10;
extern int StartHour = 23;
extern int EndHour = 8;


//~~~~~~~~~~~~~~~MarketInfo Variables:
int     iSpreads;
double  iPoint2Pip;
double  iPip2Point;
double  iPip2Real;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
      Get_Market_Info();
      test_trade();
   int total = OrdersTotal();
   // Mode Nuit  / Jour
   string night_mode = "";
   if(ModeTpNuit == True)
   {
      if(Hour() <= StartHour && Hour() >= EndHour)
      {
         if(total > 0) Mode_Jour();
         night_mode =" Mode Jour En Cours";
      }else{
         if(total > 0) Mode_Nuit();
         night_mode =" Mode Nuit En Cours";
      }
   }else{
    night_mode =" Mode Nuit Desactive";     
   }
   
   Comment("Mode :"+night_mode);
//----
   return(0);
  }
//+------------------------------------------------------------------+

void test_trade()
{
   if(OrdersTotal() < 1)
   {
      OrderSend(Symbol(),OP_BUY,0.1,Ask,10,0,0,"",0,1);
   }
}

//+------------------------------------------------------------------+
//| Mode Nuit                                                        |
//+------------------------------------------------------------------+
void Mode_Nuit()
{
   RefreshRates();
   double oldtp;
   double newtp;
   for(int l = OrdersTotal()-1; l >= 0; l--) 
   {
      if(OrderSelect(l, SELECT_BY_POS, MODE_TRADES) && OrderSymbol()==Symbol() && OrderProfit() < 0 && 
         TimeCurrent() > OrderOpenTime()+Minute_trade_avant_mode_nuit*60 )
      {
         if(OrderType() == OP_BUY)
         {
            oldtp = OrderTakeProfit();
            newtp = OrderOpenPrice()+TPNuit*iPoint2Pip;
            //Print("Mode Nuit : "+oldtp+" - "+newtp);
            if(oldtp != newtp)
            {
               OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),newtp,0,White);
               //Print("ICI Nuit : orderopenprice = "+OrderOpenPrice()+" ordertakeprofit = "+oldtp+" newtp = "+newtp );
            }
         }  
         if(OrderType() == OP_SELL)
         {
            oldtp = OrderTakeProfit();
            newtp = OrderOpenPrice()-TPNuit*iPoint2Pip;
            if(oldtp != newtp)
            {
               OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),newtp,0,White);
            }
         }
      }     
   }
}

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

//+------------------------------------------------------------------+
//| Mode Jour                                                        |
//+------------------------------------------------------------------+
void Mode_Jour()
{
   RefreshRates();
   double oldtpj = 0;
   double newtpj = 0;
   
   for(int l = OrdersTotal()-1; l >= 0; l--) 
   {
      if(OrderSelect(l, SELECT_BY_POS, MODE_TRADES) && OrderSymbol()==Symbol() && OrderProfit() < 0 && 
         TimeCurrent() > OrderOpenTime()+Minute_trade_avant_mode_nuit*60 )
      {
         if(OrderType() == OP_BUY)
         {
            oldtpj = OrderTakeProfit();
            newtpj = OrderOpenPrice()+TPDefaut*iPoint2Pip;
            Print("Mode Jour : "+oldtpj+" - "+newtpj);
            if(oldtpj != newtpj)
            {
               OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),newtpj,0,White);
               Print("ICI Jour : orderopenprice = "+OrderOpenPrice()+" ordertakeprofit = "+oldtpj+" newtp = "+newtpj );
            }
         }  
         if(OrderType() == OP_SELL)
         {
            oldtpj = OrderTakeProfit();
            newtpj = OrderOpenPrice()-TPDefaut*iPoint2Pip;
            if(oldtpj != newtpj)
            {
               OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),newtpj,0,White);
            }
         }
      }     
   }
}

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


















//+------------------------------------------------------------------+
//| Market Info                                                      |
//+------------------------------------------------------------------+
void Get_Market_Info(){
    iSpreads=MarketInfo(Symbol(),MODE_SPREAD);
    //~~~~~~~~~~iPoint2Pip:
    iPoint2Pip=Point;
    if(Digits==3){iPoint2Pip=0.01;}//------------------ Japanese Yen Standard Pip
    if(Digits==5){iPoint2Pip=0.0001;}//---------------- Other Currency Standard Pip
    //~~~~~~~~~~iPip2Real:
    if(Digits==2 || Digits==3){iPip2Real=100;}//------- Japanese Yen Standard Pip in Integer
    if(Digits==4 || Digits==5){iPip2Real=10000;}//----- Other Currency Standard Pip in Integer
    //~~~~~~~~~~iPip2Point
    if(Digits==4){iPip2Point=1;}else{iPip2Point=10;}//- Turn Integer Standard Pip into Points
}
//+------------------------------------------------------------------+

 

Ah someone is using one of my old functions.... Cute :). Anyways if(oldtp != newtp) is not good enough. When it comes to error#1, its the (-) -difference- (subtraction) between oldtp vs newtp which matters. Also you have to realize that brokers would-not work with any Floating-Double-Value. Brokers work in Points (usually 0.00001). Traders work in Pips (usually 0.0001). Mql4 decimal/double values work in 0.000000001 (I cannot remember the max float for mql4 and don't feel like looking it up).

Given the above, asking if mql4-double is not equal to another mql4-double makes no sense to the broker. If 0.000000001 is not equal to 0.000000002 does not mean that its greater then Broker Point. Brokers usually do-not see pass the 5th decimal and only care about if the Old-Price does not look like the New-Price up-to the 5th decimal. If we toke the two above values up-to the 5th decimal point, it'll look like 0.00000 and that is the same as 0.00000.

What you should do is code if the Math-Absolute Value of (oldtp - newtp) > 0.00001. What I do is code if( MathAbs(oldtp - newtp) > iPoint2Pip){ OrderModify(...); }

Finally as always.... Please search before posting, thats how the rest of us get by. I/Myself/Personally have written the above explanation over a dozen times and that's just from my side, many other people here have done the same.

 
ubzen:

Ah someone is using one of my old functions.... Cute :). Anyways if(oldtp != newtp) is not good enough. When it comes to error#1, its the (-) -difference- (subtraction) between oldtp vs newtp which matters. Also you have to realize that brokers would-not work with any Floating-Double-Value. Brokers work in Points (usually 0.00001). Traders work in Pips (usually 0.0001). Mql4 decimal/double values work in 0.000000001 (I cannot remember the max float for mql4 and don't feel like looking it up).

Given the above, asking if mql4-double is not equal to another mql4-double makes no sense to the broker. If 0.000000001 is not equal to 0.000000002 does not mean that its greater then Broker Point. Brokers usually do-not see pass the 5th decimal and only care about if the Old-Price does not look like the New-Price up-to the 5th decimal. If we toke the two above values up-to the 5th decimal point, it'll look like 0.00000 and that is the same as 0.00000.

What you should do is code if the Math-Absolute Value of (oldtp - newtp) > 0.00001. What I do is code if( MathAbs(oldtp - newtp) > iPoint2Pip){ OrderModify(...); }

Finally as always.... Please search before posting, thats how the rest of us get by. I/Myself/Personally have written the above explanation over a dozen times and that's just from my side, many other people here have done the same.


Hooo thank you su much ubzen ! you save me lot of time .

I'v searched around the forum but i can't find my answer (my bad english not helping me)

anyway thanks for your help !!!

regards

Reason: