David:
// calclate risk value %
double Equity = AccountEquity();
double Balance = AccountBalance();
double risk = MathFloor (Equity/Balance)*100;
}
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
if(OrderType()==OP_BUY) // long position is opened
{
// should it be closed?
if (risk>50)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
return(0); // exit
}
else // go to short position
{
if(OrderType()==OP_SELL) // short position is opened
{
// should it be closed?
if (risk>50)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
return(0); // exit
}
}
}
}
}
}
return(0);
}
}
}
}
// the end.
Hi,
Any help appreciated. Im trying to make a code to close an open order at a percentage of my account equity / account balance ie. 50%.Ive tried this below. It compiles but doesnt work on test. any ideas?
Cheers
// Exit market
// calclate risk value %
double Equity = AccountEquity();
double Balance = AccountBalance();
double risk = MathFloor (Equity/Balance)*100;
}
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
if(OrderType()==OP_BUY) // long position is opened
{
// should it be closed?
if (risk>50)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
return(0); // exit
}
else // go to short position
{
if(OrderType()==OP_SELL) // short position is opened
{
// should it be closed?
if (risk>50)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
return(0); // exit
}
}
}
}
}
}
return(0);
}
}
}
}
// the end.
double RiskLimit=0.5 // (...) if(AccountFreeMargin()<AccountEquity()*RiskLimit) //---> OrderClose
Perhaps you're trying to do this.
-
When you post code please use the CODE button (Alt-S)!
(For large amounts of code,
attach it.)
Please edit
your post.
General rules and best pratices of the Forum. - General - MQL5 programming forum
Messages Editor -
In the presence of multiple orders (one EA
multiple charts, multiple EAs, manual
trading,) while you are waiting for the current operation (closing, deleting,
modifying) to complete, any number of other operations on other orders could
have concurrently happened and changed the position indexing:
-
For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you
can simply count down in a position loop, and you won't miss
orders. Get in the habit of always counting down.
Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum
For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.) -
and check OrderSelect in case earlier positions were deleted.
What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles - and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead, on the next order/server call.
-
For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you
can simply count down in a position loop, and you won't miss
orders. Get in the habit of always counting down.
- Check your return codes for errors, report them
and you would know why.
Don't just silence the compiler, it is trying to help you.
What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.
Sorry, I mean something like this. Regards.
double RiskLimit=0.5 // (...) if(AccountEquity()<AccountBalance()*RiskLimit) //---> ticket=OrderClose...

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hi,
Any help appreciated. Im trying to make a code to close an open order at a percentage of my account equity / account balance ie. 50%.Ive tried this below. It compiles but doesnt work on test. any ideas?
Cheers
// Exit market
// calclate risk value %
double Equity = AccountEquity();
double Balance = AccountBalance();
double risk = MathFloor (Equity/Balance)*100;
}
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
if(OrderType()==OP_BUY) // long position is opened
{
// should it be closed?
if (risk>50)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
return(0); // exit
}
else // go to short position
{
if(OrderType()==OP_SELL) // short position is opened
{
// should it be closed?
if (risk>50)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
return(0); // exit
}
}
}
}
}
}
return(0);
}
}
}
}
// the end.