Jump to content

⤴️-Paid Ad- Check advertising disclaimer here. Add your banner here.🔥

Newbee question: how to restrict in number of open positions


Recommended Posts

Posted

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 ?

Posted

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....

}

}

Posted

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

Posted

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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


⤴️-Paid Ad- Check advertising disclaimer here. Add your banner here.🔥

×
×
  • Create New...