Quote:
|
Originally Posted by yaniv_av
How can I code a time base exit command ?
I want to know the duration of an open position expresse by the number of bars that position already open.
Actually, I want to close a position automatically after 30 bars (in my expert-advisor)
How can I code that in mql4?
10X !
|
Hi yanuv_av,
I'm so sorry for the delay in replaying you; I've spent all the day fixing my damn car to reach my office and reply your questions

.
Now you have an EA and want to close the order after 30 bars (or whatever count you want), Right?
Well
Place this function on the top of
start() function:
PHP Code:
bool BarsCountDown(int count)
{
static bool first_call = true;
static int start_bar = 0;
if(first_call)
{
start_bar=Bars;
first_call=false;
}
if(Bars == (start_bar+count))
{
Print("(TRUE) Bars= " + Bars + " : start_bars = " + start_bar);
first_call=true;
return (true);
}
else
{
Print("(FALSE) Bars= " + Bars + " : start_bars = " + start_bar);
return (false);
}
}
How to use this function:
bool BarsCountDown(30);
The line above returns
false if the current bar hasn't exceeded the number 30 from the first call of the function (the 30 bars not yet counted)
And returns
true if the current bar has exceeded the 30 bars
So, when you get
true, close the position
In your
start() function you may use code like this:
PHP Code:
start()
{
....
if(BarsCountDwon(30))
OrderClose(OrderTicket(),OrderLots(),Bid,3,Red); // close position
.....
}
I hope you've got it.