# What's wrong with this code!

1204

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!!

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;
}

{
do {
}

}

}
//---
8409

i believe this wont happen?

}

1204

Hairi Baba:

i believe this wont happen?

}

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?

7679

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..
16575

Moderator
10541

{
}
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

cannot be true.

do
{
}

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

int ticket=-1;
{
do
{
}
while(ticket==-1);

}
?
Moderator
10541

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;
get_price=true;
}

{
get_price=false;
}

int ticket=-1;
{
do
{
}
while(ticket==-1);
}
}

Not tested.
1204

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;
get_price=true;
}

{
get_price=false;
}

int ticket=-1;
{
do
{
}
while(ticket==-1);
}
}

Not tested.

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 !!
1204

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 minusIf 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;

{
do {
}

}
}
}
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 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 !

double g=(s/Point);    //And with this 2 steps ,
Variable Asks is now an integer
if (y<=g)
{
do {
}
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;
}

}

Moderator
10541

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;
{
do
{
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.
1204

Keith Watford:

int ticket=-1;
{
do
{