# How to trade according 3 last bar?

hello every body…
i want to make ea based of 3 last bar…
example if i choose 1 hour timeframe…
and now 12.40,so the trade will open on 13.00. he will check bar time 10, 11 and 12…
the if want open trade on time 14.00, he will check bar time 11,12,and 13…
and etc…

if i am choose time frame 30, and want open position on 12.00, so he will check bar time 10.30,11.00,11.30.

the 1 last bar will check based of ema, last bar 2 check based of stoch, last bar 3 check based of macd or other indicator…
of more simple like:
last bar 1: check open[1]>high[1]
last bar 2: check open[1]=high[1]
last bar 3:check close[1]>low[1]

but the important i want to know how to make can check 3 last bar…
is it can?
thx…

I have no idea how to build an ea… but i am just curious why dont you trade your method manually?

because using ea will not late to open all signal from indicator…
play mannual can win if always check the signal every hour or minutes… very tired… thx…

I guess I do not understand completely>? Sorry, but, from what I understand,
you can use code like this open[1] OR open[2] OR open[3] this will give you the open of each of the previous 3 bars.

is this what your looking for?

so the [1] is mean last 1 bar ya?
[2] is last 2 bar from now bar?
ok, i try ya… thx…

``````
if ((Open[1]>High[1]) && (Open[2]=High[2]) && (Close[3]>Low[3]))
{
//Do something, such as:

}

``````

I can translate english to code pretty good, but I don’t understand what you want to do in the first place.

If the open of the last closed bar is greater then the high, and the open of the bar before that is equal to its high, and the bar before that closed above its low then buy at the current price 1 lot with a maximum 2 pip slippage and a 50pip SL, and TP. Notate this on the chart with a green arrow and call it “My EA”. Put in a Magic number of “12345” so that I can sort through the orders later and see what this EA did (or this part of this EA) if I decide to go through the orders some where else in the code.

What it does not do is check for errors If your order doesn’t go through, it doesn’t care.

You may want to check out MQL4 Documentation or have somebody else code it for you if you code worse then I do

ok thx simy…
but after i doing like that, and test in backtest, not all bar that have suit based of that condition, for open psoition… only some…

my code like this:
if(Open[2]>Close[2] && Open[1]>Close[1]){
}

the result should be : open position based of every two last bar down…
but i see after 2 bar down, next bar not open position, and until 4 bar to open position… sometime until 3 last bar down, next bar open position…

sometimes not open position even have show 3 last bar down…

what is the problem?
thx…

Just so I’m clear please write what the program should do in a format similar to my “translation” if my translation is not the way you want it.

``````if(Open[2]>Close[2] && Open[1]>Close[1]){
}

``````

Translation:
If the last 2 bars (excluding the one that has yet to close) are both bear candles, then buy.

However to be clear I would rewrite it like the following:

``````
if ( (Open[2]>Close[2]) && (Open[1]>Close[1]) )
{
}

``````

I would like to add that you should have it do this only at the start of a new bar. The start() function is ran at every tick. to get it to run at every bar use something like this:

``````
int barcount = Bars();

void CheckforLong()
{
if ( (Open[2]>Close[2]) && (Open[1]>Close[1]) )
{
}
}

int start()
{
if ( barcount < Bars() )
{
CheckforLong();
//Anything else that should run once per candle
barcount = Bars();
}

//Anything here that should be run per tick, eg trailing stops, etc.
}

``````

Hope this helps…

sory, your code is difficult… i don’t understand… why the ordersend put before int start() function?

my code like this:
extern double TakeProfit = 50;
extern double StopLose = 40;
extern double Lots = 0.1;

double
Pip;
//±-----------------------------------------------------------------+
int init()
{
Pip = Point;
if(Digits==3||Digits==5)Pip*=10;
TakeProfit *= Pip;
StopLose *= Pip;
}

//±-----------------------------------------------------------------+
int start()
{
int total, ticket;
if(New_Bar()){
if((Open[2]>Close[2]) && (Open[1]>Close[1])){
}
}
return(0);
}

//±-----------------------------------------------------------------+
bool New_Bar()
{
static datetime New_Time=0;
if(New_Time!=Time[0]){
New_Time=Time[0];
return(true);
}
return(false);
}

so what should i change or add something?
thx…

Your code is a pain to read to me, but other then that all seems fine. I use a different method to see if were in a new bar…

I’ve never seen "static " declared before, don’t feel like looking it up, I’m assuming it keeps its value even if the function is no longer active judging by its usage.

I suggest you add some debugging to your code. I make this:

and add the following after it:
if (ticket<0) Print ("Error sending order: ", GetLastError()));

Then check the logs and see what they say.

oh… i get error log like this:

2010.09.15 00:46:37 TestGenerator: unmatched data error (high value 1.2700 at 2010.09.09 23:30 is not reached from the least timeframe, high price 1.2699 mismatches)
2010.09.15 00:46:37 TestGenerator: unmatched data error (low value 1.2699 at 2010.09.09 23:15 is not reached from the least timeframe, low price 1.2699 mismatches)
2010.09.15 00:46:37 TestGenerator: unmatched data error (high value 1.2706 at 2010.09.09 23:15 is not reached from the least timeframe, high price 1.2706 mismatches)
2010.09.15 00:46:37 TestGenerator: unmatched data error (low value 1.2701 at 2010.09.09 22:45 is not reached from the least timeframe, low price 1.2701 mismatches)

2010.09.15 00:46:37 2010.01.04 17:00 ultimate EURUSD,M15: OrderSend error 4107
2010.09.15 00:46:37 2010.01.04 17:00 ultimate EURUSD,M15: invalid price 1.44253000 for OrderSend function
2010.09.15 00:46:37 2010.01.04 15:00 ultimate EURUSD,M15: invalid price 1.43981000 for OrderSend function
2010.09.15 00:46:37 2010.01.04 14:00 ultimate EURUSD,M15: OrderSend error 4107
2010.09.15 00:46:37 2010.01.04 14:00 ultimate EURUSD,M15: invalid price 1.43908000 for OrderSend function
2010.09.15 00:46:37 2010.01.04 13:22 Tester: take profit #1 at 1.4404 (1.4404 / 1.4406)

what is the wrong?
is my broker fxopen problem?
thx…

thx very much simy… it can work…
but the log still error in :

2010.09.15 11:35:28 TestGenerator: unmatched data error (high value 1.2710 at 2010.09.10 23:45 is not reached from the least timeframe, high price 1.2685 mismatches)
2010.09.15 11:35:28 TestGenerator: unmatched data error (low value 1.2690 at 2010.09.10 16:33 and price 1.2689 mismatched)
2010.09.15 11:35:28 TestGenerator: unmatched data error (high value 1.2706 at 2010.09.10 15:45 is not reached from the least timeframe, high price 1.2705 mismatches)
2010.09.15 11:35:28 TestGenerator: unmatched data error (low value 1.2686 at 2010.09.10 15:58 and price 1.2685 mismatched)
2010.09.15 11:35:28 TestGenerator: unmatched data error (low value 1.2686 at 2010.09.10 15:55 and price 1.2685 mismatched)
2010.09.15 11:35:28 TestGenerator: unmatched data error (high value 1.2724 at 2010.09.10 15:30 is not reached from the least timeframe, high price 1.2723 mismatches)
2010.09.15 11:35:28 TestGenerator: unmatched data error (low value 1.2701 at 2010.09.10 15:43 and price 1.2700 mismatched)
2010.09.15 11:35:28 TestGenerator: unmatched data error (high value 1.2727 at 2010.09.10 15:15 is not reached from the least timeframe, high price 1.2727 mismatches)

your code have solved the invalid price, but still have 1 problem like that…
is it have a effect or not?
thx…

hello simy…
what about code in euro jpy, because after i change NormalizeDouble(Ask,4) to
NormalizeDouble(Ask,2), it can display buy euro jpy, but stop lose and tp are wrong…
my tp and sl only 12, but the tp and sl jump arround more 100, i don’t know which code must change… maybe in this:

int init()
{
Pip = Point;
[B]if(Digits==2||Digits==5)Pip*=[B]10[/B];
TakeProfit *= Pip;
StopLose *= Pip;[/B]
}

}

if i change the 10 to 100 or 1 or other, it’s not open position…

so what should i do?
the problem of euro usd have finished, but now at eur jpy problem in sl and tp…

I believe these unmatched data errors are errors within the strategy testers chart history data, not within the EA code.
They occur when the chart timeframe you are using for the test has prices which do not match those of the lowest timeframe chart.
For instance if you are testing on the 15 minute chart and the 15 minute chart has a high of 1.2727 but the history data on your 1 minute chart at the same time does not reach the same high this causes the unmatched data error.
I believe this can occur when your chart history contains a mixture of data from different brokers, or if you have downloaded some history data from metaquotes while other data was recieved from your broker and the two sources contain different price quotes.

This is a common problem with mt4 strategy tester.

so is it have problem in how many position that have opened or not?
example: the 15 timeframe from jan-february should have total open based of condition is 50 position, because this error " unmatched data errors…"
so the total being 30…
is it like that or not?
thx…

No I think it is a problem with your historical chart data.
It looks like it means some of the historical bars in the chart being used for the test have highs which are not replicated in the data for the 1 minute chart.

For instance this error:

2010.09.15 11:35:28 TestGenerator: unmatched data error (high value 1.2724 at 2010.09.10 15:30 is not reached from the least timeframe, high price 1.2723 mismatches)

I believe this error means the historical chart being used for the test had a bar at 15:30 on 2010/09/10 which reached or went above 1.2724 but the 1 minute bars that covered the same date and time as that bar never reached 1.2724.
You could check this by looking in your mt4 history center and comparing the bar at 15:30 of the timeframe you tested on, with the 1 minute bars that cover the same period.

so it’s not effect for open position…
ok…
do u know how to change my code so can trade in euro jpy?
because my code only can trade 4 digit like euro usd, gu…

i want it can work on 2 digit like ej, so what the code?
thx…

I created this article awhile ago about getting accurate backtests from MT4. This is exactly what you’re looking for.

Setting up Metatrader for better backtesting quality | Jeremy Whittaker