Questions from Beginners MQL5 MT5 MetaTrader 5 - page 672 1...665666667668669670671672673674675676677678679...1605 New comment Alexandr Vetrov 2016.11.15 12:38 #6711 fxsaber: Thank you!!! RichLux 2016.11.15 18:25 #6712 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 need help in Thoughts on some of [ARCHIVE!] Any rookie question, Vladislav Andruschenko 2016.11.15 18:34 #6713 RichLux: 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, magikThen you close all positionsvoid CPD(string order_symbol="",int order_type=-1,int order_magic=-1) { 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); } } } } } Any questions from newcomers Coding help Any EA's that OPEN/CLOSE RichLux 2016.11.15 20:18 #6714 Vladislav Andruschenko: ...function returns profits in october poses, can be filtered by symbol, type, magikThen close all positions Thanks, but I am interested in closing on loss. How can this be implemented? Vladislav Andruschenko 2016.11.15 20:31 #6715 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) RichLux 2016.11.15 20:59 #6716 Vladislav Andruschenko: 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"? Vladislav Andruschenko 2016.11.15 21:01 #6717 RichLux: yesorder_type= OP_BUY OP_SELL -1 RichLux 2016.11.15 21:53 #6718 The compiler also complains aboutClosePosBySelect.Does this function need to be written separately? Vladimir Karputov 2016.11.16 13:16 #6719 Comments not relevant to this topic have been moved to "Putting the current time on the graph". Vladimir Karputov 2016.11.16 13:20 #6720 Vladislav AndruschenkoRichLux:The compiler also complains aboutClosePosBySelect.Does this function need to be written separately?void CPD(string order_symbol="",int order_type=-1,int order_magic=-1) { 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); } } } } } 1...665666667668669670671672673674675676677678679...1605 New comment
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);
}
}
}
}
}