Download MetaTrader 5

What's wrong with this code!

To add comments, please log in or register
Mohammad Aghaali
1204
Mohammad Aghaali  
Hello
This code should give price at 12:20 and add it to x

Then check every thick , and when Ask price is equal to x-300*Point then set order while order not equal to 0 (this is for low that code maximum order once a day)

But not work!!

Please help me

Thanks
#define  DAY 86400 // day is 86400 seconds
#define  HOUR 3600 // hour is 3600 seconds
#define  MINUTE 60 // minute is 60 seconds

input double Lots = 0.1;
input int  TP = 200;
input int  SL = 200;
input int slippage = 3;
input string comment = "MyOrder";
input int magic = 1234;

input int StartHour   = 12;
input int StartMinute = 20;
datetime  NextTrade   = StartHour*HOUR + StartMinute*MINUTE;
double x=0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
  
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+

void OnTick()
{

  datetime now = TimeCurrent();
   datetime midnight = now - (now % DAY);
   NextTrade = midnight + DAY + StartHour*HOUR + StartMinute*MINUTE;
   if(now >= NextTrade){
         x=Ask;
        }
        
   if (Ask==(x-(300*Point)))
          {
          do {
          int ticket =OrderSend(Symbol(),OP_BUY,Lots,Ask,1,0,Ask+(TP*Point),comment,magic,0,0);
         }
          while (OrderSend(Symbol(),OP_BUY,Lots,Ask,1,0,Ask+(TP*Point),comment,magic,0,0)==0 );
            
          }
    
}
     //---
Mohamad Zulhairi Baba
8409
Mohamad Zulhairi Baba  

i believe this wont happen?

if(now >= NextTrade){
         x=Ask;
        }
        
   if (Ask==(x-(300*Point)))



 

Mohammad Aghaali
1204
Mohammad Aghaali  
Hairi Baba:

i believe this wont happen?

if(now >= NextTrade){
         x=Ask;
        }
        
   if (Ask==(x-(300*Point)))



 

I don't know , i wrote that for get price at 12:20 and next check every thick , when thick == (x-(300*Point)) then set order with order function

Do you have any idea for this?

Thanks so much for your answer.
Carl Schreiber
7679
Carl Schreiber  
I can be as 300 is an integer Point is converted to an integer and becomes zero!

Show you calculation on the chart using Comment and you'll most probably see where you code does follow your intention..
whroeder1
16575
whroeder1  
Carl Schreiber: I can be as 300 is an integer Point is converted to an integer and becomes zero!
Wrong, the int becomes a double. Typecasting - Data Types - Language Basics - MQL4 Reference



trader92 , and when Ask price is equal to x-300*Point

Doubles rarely compare equal.

Understand the links in The == operand. - MQL4 forum Ask >= OOP+n could be true because of round off and Ask >= OOP+n - 0.000005 and could be false at Ask >= OOP+n + 0.000005.

If the equality is important use (must be true at Ask == OOP+n) Use "definitely >=": Ask - (OOP+n) > -_Point/2. // Note the minus

If the equality is important use (must be false at Ask == OOP+n) Use "definitely >" Ask - (OOP+n) > _Point/2.

If the equality is not important (false when equal or true when not) just use Ask > OOP+n

Keith Watford
Moderator
10541
Keith Watford  

   NextTrade=midnight+DAY+StartHour*HOUR+StartMinute*MINUTE;
   if(now>=NextTrade)
     {
      x=Ask;
     }
On a Monday, NextTrade will be 12:20 on Tuesday.

When it passes midnight on Monday, NextTrade will be 12:20 on Wednesday.

So the if condition will never be true.

Even if it could be true x would be constantly updated so

   if(Ask==(x-(300*Point)))

cannot be true.

      do
        {
         int ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,0,Ask+(TP*Point),comment,magic,0,0);
        }
      while(OrderSend(Symbol(),OP_BUY,Lots,Ask,1,0,Ask+(TP*Point),comment,magic,0,0)==0);

I think that this will just continuously open orders. Did you mean

   int ticket=-1;
   if(Ask==(x-(300*Point)))
     {
      do
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,0,Ask+(TP*Point),comment,magic,0,0);
        }
      while(ticket==-1);

     }
?
Keith Watford
Moderator
10541
Keith Watford  
I think that you need to do something like
void OnTick()
  {
   static bool get_price=true;
   static datetime current_midnight=0;
  
   datetime now=TimeCurrent();
   datetime midnight=now -(now%DAY);
  
   if(midnight>current_midnight)
      {
      current_midnight=midnight;
      NextTrade=midnight+StartHour*HOUR+StartMinute*MINUTE;
      get_price=true;
      }
  
   if(get_price && now>=NextTrade)
     {
      x=Ask-(300*Point);
      get_price=false;
     }

   int ticket=-1;
   if(Ask<=x)
     {
      do
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,0,Ask+(TP*Point),comment,magic,0,0);
        }
      while(ticket==-1);
     }
  }


Not tested.
Mohammad Aghaali
1204
Mohammad Aghaali  
Keith Watford:
I think that you need to do something like
void OnTick()
  {
   static bool get_price=true;
   static datetime current_midnight=0;
  
   datetime now=TimeCurrent();
   datetime midnight=now -(now%DAY);
  
   if(midnight>current_midnight)
      {
      current_midnight=midnight;
      NextTrade=midnight+StartHour*HOUR+StartMinute*MINUTE;
      get_price=true;
      }
  
   if(get_price && now>=NextTrade)
     {
      x=Ask-(300*Point);
      get_price=false;
     }

   int ticket=-1;
   if(Ask<=x)
     {
      do
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,0,Ask+(TP*Point),comment,magic,0,0);
        }
      while(ticket==-1);
     }
  }


Not tested.
Thanks for your answer

result of this code is:

1       2017.01.04 13:47        buy     1       0.10    85.376  0.000   85.377  0.00    300.00
2       2017.01.04 13:47        buy     2       0.10    85.376  0.000   85.377  0.00    300.00
3       2017.01.04 13:47        buy     3       0.10    85.375  0.000   85.376  0.00    300.00
4       2017.01.04 13:47        buy     4       0.10    85.374  0.000   85.375  0.00    300.00
5       2017.01.04 13:47        buy     5       0.10    85.373  0.000   85.374  0.00    300.00
6       2017.01.04 13:47        buy     6       0.10    85.372  0.000   85.373  0.00    300.00
7       2017.01.04 13:47        buy     7       0.10    85.373  0.000   85.374  0.00    300.00
8       2017.01.04 13:47        buy     8       0.10    85.372  0.000   85.373  0.00    300.00
9       2017.01.04 13:47        buy     9       0.10    85.373  0.000   85.374  0.00    300.00
10      2017.01.04 13:47        buy     10      0.10    85.372  0.000   85.373  0.00    300.00
11      2017.01.04 13:47        buy     11      0.10    85.373  0.000   85.374  0.00    300.00
12      2017.01.04 13:47        buy     12      0.10    85.372  0.000   85.373  0.00    300.00
13      2017.01.04 13:47        buy     13      0.10    85.373  0.000   85.374  0.00    300.00
Why while not work ? it should stop after once order !!
Mohammad Aghaali
1204
Mohammad Aghaali  
whroeder1:
Wrong, the int becomes a double. Typecasting - Data Types - Language Basics - MQL4 Reference



Doubles rarely compare equal.

Understand the links in The == operand. - MQL4 forum Ask >= OOP+n could be true because of round off and Ask >= OOP+n - 0.000005 and could be false at Ask >= OOP+n + 0.000005.

If the equality is important use (must be true at Ask == OOP+n) Use "definitely >=": Ask - (OOP+n) > -_Point/2. // Note the minus

If the equality is important use (must be false at Ask == OOP+n) Use "definitely >" Ask - (OOP+n) > _Point/2.

If the equality is not important (false when equal or true when not) just use Ask > OOP+n

Did you mean this :

void OnTick()
{

  datetime now = TimeCurrent();
   datetime midnight = now - (now % DAY);
   NextTrade = midnight + DAY + StartHour*HOUR + StartMinute*MINUTE;
   if(now >= NextTrade){
         double x=Ask;
        
        
   if ((Ask-x)>=-_Point*300)
          {
          do {
          int ticket =OrderSend(Symbol(),OP_BUY,Lots,Ask,1,0,Ask+(TP*Point),comment,magic,0,0);
         }
          while (OrderSend(Symbol(),OP_BUY,Lots,Ask,1,0,Ask+(TP*Point),comment,magic,0,0)==0 );
            
          }
    }
}
This is not work!!

And i tried to understand information of links but because of my poor language i can't understand !



And i tried this to but not work again !!

void OnTick()
  {
   datetime now = TimeCurrent();


  double x=Ask;
  double z=(x/Point);
    double  y=(z-10);     //with this 3 steps , first
Fixed Ask is now an integer number but when i used it in AUDJPY it set Point=0.00001 why ? it should set Point=0.001 !


   if(now >= NextTrade)


     { double s=Ask;
     double g=(s/Point);    //And with this 2 steps ,
Variable Asks is now an integer
     if (y<=g)
     {
     do {
     ticket =OrderSend(Symbol(),OP_BUY,Lots,Ask,1,0,Ask+(TP*Point),comment,magic,0,0);
     }
     while (ticket==0);
     }
     }
      if(ticket < 0)
        {
         printf("OrderSend() failed. Error code: %i", GetLastError());
        }
      else
        {
         datetime midnight = now - (now % DAY);
         NextTrade = midnight + DAY + StartHour*HOUR + StartMinute*MINUTE;
         printf("Next Trade scheduled for %s", TimeToString(NextTrade));
        }
        
    
  }


Thanks so much for your answer .
Keith Watford
Moderator
10541
Keith Watford  
trader92:
Thanks for your answer

result of this code is:

1       2017.01.04 13:47        buy     1       0.10    85.376  0.000   85.377  0.00    300.00
2       2017.01.04 13:47        buy     2       0.10    85.376  0.000   85.377  0.00    300.00
3       2017.01.04 13:47        buy     3       0.10    85.375  0.000   85.376  0.00    300.00
4       2017.01.04 13:47        buy     4       0.10    85.374  0.000   85.375  0.00    300.00
5       2017.01.04 13:47        buy     5       0.10    85.373  0.000   85.374  0.00    300.00
6       2017.01.04 13:47        buy     6       0.10    85.372  0.000   85.373  0.00    300.00
7       2017.01.04 13:47        buy     7       0.10    85.373  0.000   85.374  0.00    300.00
8       2017.01.04 13:47        buy     8       0.10    85.372  0.000   85.373  0.00    300.00
9       2017.01.04 13:47        buy     9       0.10    85.373  0.000   85.374  0.00    300.00
10      2017.01.04 13:47        buy     10      0.10    85.372  0.000   85.373  0.00    300.00
11      2017.01.04 13:47        buy     11      0.10    85.373  0.000   85.374  0.00    300.00
12      2017.01.04 13:47        buy     12      0.10    85.372  0.000   85.373  0.00    300.00
13      2017.01.04 13:47        buy     13      0.10    85.373  0.000   85.374  0.00    300.00
Why while not work ? it should stop after once order !!

   int ticket=-1;
   if(Ask<=x)
     {
      do
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,0,Ask+(TP*Point),comment,magic,0,0);
         if(ticket>0)
            x=0;

        }
      while(ticket==-1);
     }
You need to include code to stop it repeating every tick. As Ask cannot be <0, the above will work for this case.
Mohammad Aghaali
1204
Mohammad Aghaali  
Keith Watford:

   int ticket=-1;
   if(Ask<=x)
     {
      do
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,0,Ask+(TP*Point),comment,magic,0,0);
         if(ticket>0)
            x=0;

        }
      while(ticket==-1);
     }
You need to include code to stop it repeating every tick. As Ask cannot be <0, the above will work for this case.


Thanks so much for your answer
12
To add comments, please log in or register