Open new trade only when no trades are on

 

Hi all

Ok I need to put some code in an EA which will ensure that no trades are ever taken (long or short) if any trade is currently on (long or short). Eg: If I am long, then no long or short trades will be entered until this trade is over and no trades are on (same if I am short).

 

Obviously this needs to be linked to a magic number and current symbol.

 

I found a similar question here: https://www.mql5.com/en/forum/137374

 

WHRoeder replied with some code which I have slightly adjusted to my needs:

int count=0;
for(int iPos = OrdersTotal()-1; iPos >= 0; iPos--) if( 
        OrderSelect(iPos, SELECT_BY_POS)                // Only my orders w/
    &&  OrderMagicNumber()  == MagicNumber              // my magic number
    &&  OrderSymbol()       == Symbol()             // and my pair.
    ){ count++; }
if (count == 0)

 I have put the above code in my EA immediately below where it says:

int start()
{

My thinking was that having this code at the very beginning of the EA (just after the "int start" bit) would mean that the EA would check how many orders are open on this symbol and magic number and then never open a new trade unless the order count was 0. But it doesn't work. I obviously haven't put it into my EA correctly. :(

 

I'm guessing that straight after the "if (count == 0)" there needs to be something else there before the rest of my code starts? 

But it does compile without errors though!!

 

Can anybody please help me and point me in the right direction?

 

Many thanks

 

Pinky 

 
pinky:

Hi all

Ok I need to put some code in an EA which will ensure that no trades are ever taken (long or short) if any trade is currently on (long or short). Eg: If I am long, then no long or short trades will be entered until this trade is over and no trades are on (same if I am short).

You could use this . . .

if (count != 0) return(0);
 
RaptorUK:

You could use this . . .

 


Hey thanks for answer.

 

Where would I put this? you mean delete the previous code I put in and just put your one line after "int start"?

 

Thanks

 

Pinky 

 
RaptorUK:

You could use this . . .

 


Ok scrap my last comment: i put your line in instead of the last line of the code so it reads:

int count=0;
for(int iPos = OrdersTotal()-1; iPos >= 0; iPos--) if( 
        OrderSelect(iPos, SELECT_BY_POS)                // Only my orders w/
    &&  OrderMagicNumber()  == MagicNumber              // my magic number
    &&  OrderSymbol()       == Symbol()             // and my pair.
    ){ count++; }


if (count != 0) return(0);

but even though this works, it now stops my exit criteria exiting any trades.

 

Any thoughts?

 

Pinky 

 

Locate the OrderSend() function. Put the command before it. Like so.

if(count == 0) OrderSend(...........................);
 
ubzen:

Locate the OrderSend() function. Put the command before it. Like so.

 


I don't get it.

 

Is there not a simple instruction that can be put at the start of the EA that just says "if active trades >0 then do nothing"? 

 

Pinky 

 

EDIT: ok I put it in before the OrderSend() like you said, but now it's telling me "count" variable is not defined. But I thought is was defined from the code I originally put in at the start of this thread? 

 
pinky:


I don't get it. Is there not a simple instruction that can be put at the start of the EA that just says "if active trades >0 then do nothing"?  Pinky 

Yeah, RaptorUK give it to you.

if (count != 0) return(0);
//and
if (count  > 0) return(0);

Will basically Return the program out of the start().

But then you want your Exit_Criteria to work as well. [Can't have your cake and eat it]

If you tell the program to exit the start at the beginning when total order count is >0 then it'll never get to your exit criteria. There are a couple of solutions and I provided one of them. Just tell it to only place orders when there are no orders. 

 

For your level, think of programs as reading from left-to-right and from top-to-bottom [just like an English Book]. Speaking of Books, [IMHO] you're going about this the hard/slow way. If you have no intro to programming knowledge, then you'll need to know what:

if: means

(): means

!=: means

return: means

function: means

etc

No-one here have that amount to time to explain all of these to another person. However the Book does, you can go at your own time and pace and learn the basics. That way if someone on the forum makes a reference, you can make a connection.

Honestly, most of the active helpers here have almost read all the pages of the Book, Article and Documents.

Writing a program is about learning to Think like a programmer. Read the book, don't skip chapters, do the simple home-work on each chapters. Really it's just like school all over again :). You can try to short-cut but it'll only cost you more time in the long run.

 
ubzen:

Yeah, RaptorUK give it to you.

Will basically Return the program out of the start().

But then you want your Exit_Criteria to work as well. [Can't have your cake and eat it]

If you tell the program to exit the start at the beginning when total order count is >0 then it'll never get to your exit criteria. There are a couple of solutions and I provided one of them. Just tell it to only place orders when there are no orders. 

 


Hey thanks for your help

 

Yeah I found the place to add your line of code, but it just tells me count is not defined.

 

I do understand that RaptorUK's code was a complete kill switch which then affects my exits. So I tried it your way, I just don't see why it's now telling me that count is not defined when surely it is defined in the original block of code at the start of this thread? 

 

count needs to be with-in the same function-block ie.... the start function and also it need to come before ordersend. example.

int count;

............

............

ordersend()

Your ordersend() function is perhaps within another function or ordersend() comes before the count_variable is declared.

 
ubzen:

count needs to be with-in the same function-block ie.... the start function and also it need to come before ordersend. example.

int count;

............

............

ordersend()

Your ordersend() function is perhaps within another function or ordersend() comes before the count_variable is declared.


ok, so this is what I have got (but still wont work although it compiles correctly now):

 

At the start of the EA:

int start()
{

int count=0;

 

Then in the block where the ordersend() is:

 int count=0;
   
   for(int iPos = OrdersTotal()-1; iPos >= 0; iPos--) if( 
        OrderSelect(iPos, SELECT_BY_POS)                // Only my orders w/
    &&  OrderMagicNumber()  == MagicNumber              // my magic number
    &&  OrderSymbol()       == Symbol()             // and my pair.
    ){ count++; }

 

Then the actual ordersend looks like this:

if(count == 0) {
         ticket_bn=OrderSend(symbol_bn,ordertype_bn,lotsize_bn,price_bn,Slippage,stoploss_bn,takeprofit_bn,comment_bn,magic_bn,0,ordercolor_bn);
 }

 

 

It compiles fine, but still it doesn't do the job of only opening orders if no other order is active.

 

Any ideas?

 

Thanks

 

Pinky 

Reason: