buy/sell on new bar ?

 

hi

i want to send order buy or sell when new bar is appear. But i don't want to use function minute() to check.

Right know i check new bars with function Minute() == 0 then i send order, but last time i don't have any transaction in 0 minute and my order was not send.

Any idea ?

 
Use Time[1], when this changes you have a new bar . . .
 
RaptorUK:
Use Time[1], when this changes you have a new bar . . .
I think he needs more than that. He said minute() = 0 which is one hour TF. Use datetime or integer variable to check the change on iTime (Symbol(), Period_H1, 0).
 
onewithzachy:
I think he needs more than that.

OK

static datetime lastbarstart=0;

if (Time[1] != lastbaarstart)  // this is true for the first tick of a new bar
   {
   Print("We have a new bar . . .");
   lastbaarstart = Time[1];
   }
 

Or, because you don't know the tasks will finish, you can check even further:

   /* ... */
   if ( jobsDone == true )
   lastbaarstart = Time[1];
   else lastbaarstart = Time[1+1];
   /* ... */

it is useful if tasks do not finish even in the next-next new bar

 

Great solution, thx for help,

one more question, what if i want to buy/sell on close bar ? Is't any way to detect last ticks without Minute()==59? Maybe it's better to use minutes from windows clock not trending clock and when minute is 59 then buy/sell? What do you think ? Someone try ?

 
lukibest:

Great solution, thx for help,

one more question, what if i want to buy/sell on close bar ? Is't any way to detect last ticks without Minute()==59? Maybe it's better to use minutes from windows clock not trending clock and when minute is 59 then buy/sell? What do you think ? Someone try ?

You cannot detect the close of a bar . . . you detect the open of the next. Until you get a tick nothing happens, if there is no tick on H1 from 14:59:55 until 15:00:05 technically speaking the bar has closed but you don't know it has until a tick arrives.

You need to keep in mind that start() is tick driven . . .

 
lukibest:

Great solution, thx for help,

one more question, what if i want to buy/sell on close bar ? Is't any way to detect last ticks without Minute()==59? Maybe it's better to use minutes from windows clock not trending clock and when minute is 59 then buy/sell? What do you think ? Someone try ?

Do not use the Windows system clock for trading. Always use server time.

You can always trade on the next tick after any arbitrary time in hours, minutes and seconds.

 

I am a begginer and was also having problems with getting the EA to only open 1 trade per bar.

I found this soluting someonewhere on this site....can't remember where, but it uses a boolean
function:

int start

{

if (NewBar())
......
........
.........

return(0);
}

bool NewBar()

{


static datetime OldTime = 0;

if(OldTime < Time[0])
{
OldTime = Time[0];
return(true);
}
else
{
return(false);
}
}

Hope this helps.

 
JoBlo:

I am a begginer and was also having problems with getting the EA to only open 1 trade per bar.

I found this soluting someonewhere on this site....can't remember where, but it uses a boolean
function

Listen to the experts and use Time[1]. At the first tick after starting MT or network reconnecting the Time[0] changes immediately, but Time[1] and the rest of all price arrays remains the same, arrays are under refreshment, and should not be used. Using Time[1] filteres this.

I cant test now (because of weekend) but there is another issue around Time and price arrays refreshing, that is more tricky to overcome, read this:

http://mechanicalforex.com/2011/05/the-time-array-mismatch-problem-a-dark-mt4-secret-comes-to-light.html

Reason: