Questions from Beginners MQL5 MT5 MetaTrader 5 - page 672
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
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);
}
}
}
}
}