keesje1 Posted February 6, 2010 Report Share Posted February 6, 2010 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 ? Quote Link to comment Share on other sites More sharing options...
finimej Posted February 6, 2010 Report Share Posted February 6, 2010 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.... } } Quote Link to comment Share on other sites More sharing options...
keesje1 Posted February 7, 2010 Author Report Share Posted February 7, 2010 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 Quote Link to comment Share on other sites More sharing options...
finimej Posted February 8, 2010 Report Share Posted February 8, 2010 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.