Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
I am writing an EA which works on a martingale basis.
How can I get it to close all the orders it has opened when the maximum permissible loss in pips on one currency pair is reached?
I am writing an EA which works on a martingale basis.
How can I get it to close all the orders it has opened when the maximum permissible loss in pips on one currency pair is reached?
double GetProfitinCurrency(string order_symbol="",int order_type=-1,int order_magic=-1)
{
double profit=0;
int i,k=OrdersTotal();
if(order_symbol=="0") order_symbol=Symbol();
for(i=0; i<k; i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if((OrderSymbol()==order_symbol || order_symbol=="") && (order_type<0 || OrderType()==order_type))
{
if(OrderType()==OP_BUY || OrderType()==OP_SELL)
{
if(order_magic<0 || OrderMagicNumber()==order_magic)
{
profit+=OrderProfit()+OrderCommission()+OrderSwap();
}
}
}
}
}
return(profit);
}
the function returns profits in open positions, can be filtered by symbol, type, magik
Then you close all positions
{
int i,k=OrdersTotal();
if(order_symbol=="0") order_symbol=Symbol();
for(i=k-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if((OrderSymbol()==order_symbol || order_symbol=="") && (order_type<0 || OrderType()==order_type))
{
if(OrderType()==OP_BUY || OrderType()==OP_SELL)
{
if(order_magic<0 || OrderMagicNumber()==order_magic) OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),1,CLR_GREEN);
}
}
}
}
}
...
function returns profits in october poses, can be filtered by symbol, type, magik
Then close all positions
I mean, I gave you the above,
So profit is a generic term.
-20 is -20
if(GetProfitinCurrency(Symbol(),-1,-1)<-20)CPD(Symbol(),-1,-1)
double GetProfitinCurrency(string order_symbol="",int order_type=-1,int order_magic=-1)
{
double profit=0;
int i,k=OrdersTotal();
if(order_symbol=="0") order_symbol=Symbol();
for(i=0; i<k; i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if((OrderSymbol()==order_symbol || order_symbol=="") && (order_type<0 || OrderType()==order_type))
{
if(OrderType()==OP_BUY || OrderType()==OP_SELL)
{
if(order_magic<0 || OrderMagicNumber()==order_magic)
{
profit+=OrderProfit()+OrderCommission()+OrderSwap();
}
}
}
}
}
return(profit);
}
Thanks now I understand.
What is still not clear in the code is:(order_type<0||OrderType()==order_type) THIS can be read as "and order to sell or buy"?
yesorder_type= OP_BUY OP_SELL -1
The compiler also complains aboutClosePosBySelect.
Does this function need to be written separately?
Vladislav Andruschenko
RichLux:
The compiler also complains aboutClosePosBySelect.
Does this function need to be written separately?
{
int i,k=OrdersTotal();
if(order_symbol=="0") order_symbol=Symbol();
for(i=k-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if((OrderSymbol()==order_symbol || order_symbol=="") && (order_type<0 || OrderType()==order_type))
{
if(OrderType()==OP_BUY || OrderType()==OP_SELL)
{
if(order_magic<0 || OrderMagicNumber()==order_magic) OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),1,CLR_GREEN);
}
}
}
}
}