Well, this will fix your new bar problem (got it from an article out at mql4.com):
Code:
bool funcIsNewBar()
{
bool res=false;
// the array contains open time of the current (zero) bar
// for 7 (seven) timeframes
static datetime _sTime[7];
int i=6;
int timeFrame = Period();
switch (timeFrame)
{
case 1 : i=0; break;
case 5 : i=2; break;
case 15 : i=3; break;
case 30 : i=4; break;
case 60 : i=5; break;
case 240: i=6; break;
case 1440:break;
default: timeFrame = 1440;
}
//----
if (_sTime[i]==0 || _sTime[i]!=iTime(Symbol(),timeFrame,0))
{
_sTime[i] = iTime(Symbol(),timeFrame,0);
res=true;
}
//----
return(res);
}
Call this function like this
Code:
int start()
{
if (funcIsNewBar)
{
//run some code
}
return (0);
}
That'll get code to run ONLY when there is a new bar.
What you need to do is find out in the data window of MT4 what the values are when there is NO arrow being put on the chart by your indicator. For example, the indicator may may have 0's or may be blank.
So all you do then is call the value of the indicator at each open
Code:
varMyIndieValue=iCustom(<blah blah>);
if (varMyIndieValue>0) //there's an arrow
{
//run some code
}
Quote:
Originally Posted by bdht
Hi, Everyone.
Recently I was trying to make a simple EA that would work on an arrow-based indicator. I am trying to make the EA to maintain one order at any given time. If the arrow points down, the previous buy order is closed and sell order is opened. If the arrow points up, the previous sell order is closed and buy order is opened. I am using the tester (visualization mode) to verify my code. It seems that no matter how I try, the EA does not close and open the positions when arrow indicator points up or down. The back test confirms that the EA is not working properly. Instead of opening and closing the orders at the arrow points shown by the indicator, the EA closes/opens order at some different time. I cannot understand why my code doesn't work.
In the beginning of start statement, I have the following code:
if (Time[0] == savedTime) {
return (0);
} else {
savedTime = Time [0];
}
This (I hope) will ensure that the code in the start statement is executed only when new bar has formed. Later in the body of the start subroutine, I query the indicator with iCustom function. The request looks as below:
iCustom (... 1)
The last argument of one specifies the previous formed bar, which is why it is not 0. Yet later I close the opened order with OrderClose and open new one with OrderSend. I suppose that both functions must be able to execute instantaneously.
The bottom line is: I am trying to create an EA based on arrow indicator. The indicator points either up or down. The way I see it (and I am probably incorrect), the only thing that I need to do is to close previous order and open new one when the next bar has formed. I would greatly appreciate any input into this problem.
Thanks to all.
|