Jump to content

Newbee question: how to restrict in number of open positions


keesje1

Recommended Posts

good time,

 

would like to know how to restrict an EA in opening positions in one directions.

 

for example, maximum 10 positions more long than short

 

i can imagine something like a number with memory

 

NumberX

 

buy=NumberX + 1

Sell = NumberX -1

 

and than only let it buy if the number is <=10, sell if the number is <=-10

 

but how to code this ?

Link to comment
Share on other sites

Re: Newbee question: how to restrict in number of open positions

 

first in the variable, you define

extern int maxbuy=10;

extern int Magic=12345; //magic number of your orders

 

then you check position and count the number of long and number of short

 

int GetOpenLongpositions()

{

int buyorders=0;

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

if (OrderSelect(j, SELECT_BY_POS,MODE_TRADES)) {

if (OrderSymbol() == Symbol()&&OrderMagicNumber() == Magic) {

if (OrderType()==OP_BUY) buyorders++;

}

}

} //end for

return(buyorders);

}

 

int GetOpenShortpositions()

{

int sellorders=0;

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

if (OrderSelect(j, SELECT_BY_POS,MODE_TRADES)) {

if (OrderSymbol() == Symbol()&&OrderMagicNumber() == Magic) {

if (OrderType()==OP_SELL) sellorders++;

}

}

} //end for

return(sellorders);

}

 

then in your main function start()

 

{

if ( GetOpenLongpositions()<=10)

{

open more trades....

}

}

Link to comment
Share on other sites

Re: Newbee question: how to restrict in number of open positions

 

Thanks, i've put it into my code but am not sure if it's all correct now.

 

      if ((diClose0<diMA5)&&GetOpenLongpositions()<=10){
        OpenBuy();
        return(0);
     }

     if ((diClose2>diMA4)&&GetOpenShortpositions()<=10){
        OpenSell();
        return(0);

 

The idea was that i can have only for example 60 long positions if 50 short are open, or 61 long if 51 short are open. Or 60 short positions if 50 long are open, or 61 short if 51 long are open.

 

Never more than 10more long than short, never more than 10 more short than long. Is this what is in the code now?

 

Thanks.

 

-- 07 Feb 2010, 16:39 --

 

   if (!ExistPositions()){

     if ((diClose0<diMA5)&&(GetOpenLongpositions()-GetOpenShortpositions())<=10){
        OpenBuy();
        return(0);
     }

     if ((diClose2>diMA4)&&(GetOpenShortpositions()-GetOpenLongpositions())<=10){
        OpenSell();
        return(0);
     }
  }

 

Maybe this is it? :-)

 

Thanks

Link to comment
Share on other sites

Re: Newbee question: how to restrict in number of open positions

 

please solve the problem by yourself. I have already give your the code above.

 

debug in MT4 is doing by print or comments out the number of operations, then you will find out if your code is right or not. learn to code, you shall also learn to debug.

 

I also very often write my parameters into log file and to check if the code is doing right.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...