How to open 1 trade per bar - page 4

 

Up buy down sell

Somebody tried to write an EA that buy on every new up bar and close it all wher a down bar appears, doing the same to the down side?

I think it's easy, let's try... Next, add some filters to improve stats... its a kind of trend follower perfect for tendencies (more positions as the trend gets stronger).

 

datetime timebar = 0;

//initialize variable

//before buying or selling

if(timebar == time[0]) return(0);

//your code here

//buy or sell

timebar = time[0];

 

if (barsPrev != Bars) {

// trade

barsPrev = Bars;

} else {

// no trade

}

 

http://rentasignal.com/signal/orders/31158#closed

Shinigami:
You will either need to post the EA's code and conditions you want implemented (but then the EA will be downloaded by everyone, shared, modified, sold ect) or you need to contact a programmer (most likely you'll need to pay for their time).

Or you can do what I did - learn MQL4 programming language

Think again, my friend! It works perfectly

Because you forgot timestamps are "number of seconds passed from 1st of january 1970" and Time contains opening of all bars, Time[0] is the opening time of the "now" bar of the current chart. Current time is TimeCurrent()

If the trade was opened in this specific bar, its open time is bigger (more seconds have passed) than Time[0]. So the code posted in the beginning by ralph.ronnquist will work properly. I'll quote it.

bool opened_on_current_bar = false;

for (int i = OrdersTotal() - 1; i >=0; i-- ) {

if ( ! OrderSelect( i, SELECT_BY_POS ) )

continue;

if ( OrderOpenTime() >= Time[ 0 ] ) {

opened_on_current_bar = true;

break;

}

}

// Here the variable opened_on_current_bar is set appropriately

It will only keep your EA from opening more than 1 trade per bar.

Hope this helps everyone who finds this thread

Doesnt work for me:

CODE:

bool opened_on_current_bar = false;

int start(){

for (int i = OrdersTotal() - 1; i >=0; i-- ) {

if ( ! OrderSelect( i, SELECT_BY_POS ) )

continue;

if ( OrderOpenTime() >= Time[ 0 ] ) {

opened_on_current_bar = true;

break;

}

}

OrderSend(Symbol(),OP_BUY,0.1,Ask,1000,0,0,"one per bar",13,0,CLR_NONE);}

return;

RESULTS:

1 2010.04.16 04:37 buy 1 0.10 125.659 0.000 0.000 0.00 1000000.00

2 2010.04.16 04:37 buy 2 0.10 125.657 0.000 0.000 0.00 1000000.00

3 2010.04.16 04:37 buy 3 0.10 125.661 0.000 0.000 0.00 1000000.00

4 2010.04.16 04:37 buy 4 0.10 125.652 0.000 0.000 0.00 1000000.00

5 2010.04.16 04:37 buy 5 0.10 125.654 0.000 0.000 0.00 1000000.00

6 2010.04.16 04:37 buy 6 0.10 125.646 0.000 0.000 0.00 1000000.00

7 2010.04.16 04:37 buy 7 0.10 125.648 0.000 0.000 0.00 1000000.00

8 2010.04.16 04:37 buy 8 0.10 125.637 0.000 0.000 0.00 1000000.00

9 2010.04.16 04:37 buy 9 0.10 125.641 0.000 0.000 0.00 1000000.00

10 2010.04.16 04:37 buy 10 0.10 125.639 0.000 0.000 0.00 1000000.00

11 2010.04.16 04:37 buy 11 0.10 125.643 0.000 0.000 0.00 1000000.00

12 2010.04.16 04:38 buy 12 0.10 125.640 0.000 0.000 0.00 1000000.00

13 2010.04.16 04:38 buy 13 0.10 125.642 0.000 0.000 0.00 1000000.00

14 2010.04.16 04:38 buy 14 0.10 125.639 0.000 0.000 0.00 1000000.00

15 2010.04.16 04:38 buy 15 0.10 125.643 0.000 0.000 0.00 1000000.00

16 2010.04.16 04:38 buy 16 0.10 125.646 0.000 0.000 0.00 1000000.00

17 2010.04.16 04:38 buy 17 0.10 125.644 0.000 0.000 0.00 1000000.00

18 2010.04.16 04:38 buy 18 0.10 125.650 0.000 0.000 0.00 1000000.00

19 2010.04.16 04:38 buy 19 0.10 125.649 0.000 0.000 0.00 1000000.00

20 2010.04.16 04:38 buy 20 0.10 125.652 0.000 0.000 0.00 1000000.00

21 2010.04.16 04:38 buy 21 0.10 125.654 0.000 0.000 0.00 1000000.00

22 2010.04.16 04:38 buy 22 0.10 125.651 0.000 0.000 0.00 1000000.00

23 2010.04.16 04:38 buy 23 0.10 125.653 0.000 0.000 0.00 1000000.00

24 2010.04.16 04:38 buy 24 0.10 125.652 0.000 0.000 0.00 1000000.00

25 2010.04.16 04:39 buy 25 0.10 125.646 0.000 0.000 0.00 1000000.00

26 2010.04.16 04:39 buy 26 0.10 125.639 0.000 0.000 0.00 1000000.00

27 2010.04.16 04:39 buy 27 0.10 125.632 0.000 0.000 0.00 1000000.00

28 2010.04.16 04:39 buy 28 0.10 125.633 0.000 0.000 0.00 1000000.00

29 2010.04.16 04:39 buy 29 0.10 125.634 0.000 0.000 0.00 1000000.00

30 2010.04.16 04:39 buy 30 0.10 125.635 0.000 0.000 0.00 1000000.00

31 2010.04.16 04:39 buy 31 0.10 125.628 0.000 0.000 0.00 1000000.00

32 2010.04.16 04:39 buy 32 0.10 125.621 0.000 0.000 0.00 1000000.00

33 2010.04.16 04:39 buy 33 0.10 125.622 0.000 0.000 0.00 1000000.00

34 2010.04.16 04:39 buy 34 0.10 125.624 0.000 0.000 0.00 1000000.00

35 2010.04.16 04:39 buy 35 0.10 125.625 0.000 0.000 0.00 1000000.00

36 2010.04.16 04:39 buy 36 0.10 125.616 0.000 0.000 0.00 1000000.00

37 2010.04.16 04:39 buy 37 0.10 125.607 0.000 0.000 0.00 1000000.00

38 2010.04.16 04:40 buy 38 0.10 125.611 0.000 0.000 0.00 1000000.00

39 2010.04.16 04:40 buy 39 0.10 125.610 0.000 0.000 0.00 1000000.00

40 2010.04.16 04:40 buy 40 0.10 125.609 0.000 0.000 0.00 1000000.00

41 2010.04.16 04:40 buy 41 0.10 125.608 0.000 0.000 0.00 1000000.00

42 2010.04.16 04:40 buy 42 0.10 125.609 0.000 0.000 0.00 1000000.00

43 2010.04.16 04:40 buy 43 0.10 125.610 0.000 0.000 0.00 1000000.00

44 2010.04.16 04:40 buy 44 0.10 125.611 0.000 0.000 0.00 1000000.00

45 2010.04.16 04:40 buy 45 0.10 125.610 0.000 0.000 0.00 1000000.00

46 2010.04.16 04:40 buy 46 0.10 125.609 0.000 0.000 0.00 1000000.00

47 2010.04.16 04:40 buy 47 0.10 125.608 0.000 0.000 0.00 1000000.00

Please tell me whats wrong

Thanks for your help guys!!....just beginning in mql4

Gotta advertise when I can so check out the link above on the title

 

this works pretty well and is simple. try it.

"Bars" contains the number of bars on the chart. Whenever a new bar occurs, the value changes. So you could simply copy the last seen value and confirm whether it has changed or not, instead of going through all of this.

if (barsPrev != Bars) {

// trade

barsPrev = Bars;

} else {

// no trade

}

 

how to limit the trade per bar?

hello everybody..

i have problem in making trade per bar..

i want every bar only can trade 1 times..

but can not find it..

this is my code

extern double TakeProfit = 50;

extern double StopLose = 40;

extern double Lots = 0.1;

datetime timebar = 0;

int start()

{

int total,ticket;

double HighEnvelope = iEnvelopes(NULL,0,4,MODE_LWMA,0,PRICE_CLOSE,0.30,MODE_LOWER,1);

if (Low[1]<HighEnvelope )

{

if(timebar == Time[0]) return(0);

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLose*Point,Ask+TakeProfit*Point,16384,0,Green);

timebar = Time[0];

}

return(0);

}

what is the wrong?

thx..

Reason: