Thanks for your reply. Where exactly should I paste this code to give it a try? I am not a coder.pascalx » Fri Apr 08, 2016 11:14 am wrote:Something like this might do it. It might be missing spread, but you can easily correct the math if it is wrong. And you may need to work with MagicNumber to safely select the order.Code: Select all
#define INVALID_TICKET_ID -1 bool GetRealOrderProfit(double& profit, int ticketId) { if (ticketId != INVALID_TICKET_ID) { if (OrderSelect(ticketId, SELECT_BY_TICKET)) { profit = OrderProfit() + OrderSwap() + OrderCommission(); return true; } } return false; } void MyFunction() { int myTicketId; // ... double myProfit; if (GetRealOrderProfit(myProfit, myTicketId) && myProfit > 0.0) { // my order is profitable, yay !! } }
MQL Empty4 CODE FOR PROFIT CHECK BEFORE CLOSE
- keydcuk
- Trader
- Posts: 104
- Joined: Tue Dec 04, 2012 8:01 pm
MQL Empty4 CODE FOR PROFIT CHECK BEFORE CLOSE
-
jlcgarcia
MQL Empty4 CODE FOR PROFIT CHECK BEFORE CLOSE
Hi,
I am using a very similar subroutine for one of my multipair EAs and here there is the code:
I am sure that this code can be simplified, but it works for me.
Regards
José Luis
I am using a very similar subroutine for one of my multipair EAs and here there is the code:
Code: Select all
//declarations
extern bool ProfitAsPercentageOfBalance = true;
extern double PercentageProfit = 1;
//Start()
if(ProfitAsPercentageOfBalance)
{
ProfitPercentage=AccountBalance() *(PercentageProfit/100);
if( OpenProfitPartial()>ProfitPercentage) {Print(OpenProfitPartial());closeOrdersF(OP_SELL);closeOrdersF(OP_BUY);Sleep(60000);}
}
double OpenProfitPartial()
{
double Profit=0.0;
for(int i=0; i<OrdersTotal(); i++)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
if(/*OrderSymbol() != Symbol() ||*/ OrderMagicNumber()!=magic) continue;
// if(OrderType() != OrdType) continue;
if((OrderProfit()+OrderSwap()+OrderCommission())<0)continue;
Profit+=(OrderProfit()+OrderSwap()+OrderCommission());
}
return(Profit);
}
int closeOrdersF(int type)
{
int ticket,ordertype,total1=OrdersTotal();
double priceClose;
bool oc,od;
if(total1==0) {Print("Nothing to close"); return(0);}
total1--;
for(int x=total1; x>=0; x--)
{
if(OrderSelect(x,SELECT_BY_POS))
{
ordertype= OrderType();
if(type != ordertype /*|| OrderSymbol() != Symbol()*/||OrderMagicNumber()!=magic) continue;
ticket=OrderTicket();
if ((OrderProfit()+OrderCommission()+OrderCommission())<0) continue;
switch(ordertype)
{
case 0:
priceClose=NormalizeDouble(MarketInfo(OrderSymbol(),MODE_BID),Digits);
Print("Closing order ¹ ",x," ",ticket);
oc=OrderClose(ticket,OrderLots(),priceClose,3,Yellow);
break;
case 1:
priceClose=NormalizeDouble(MarketInfo(OrderSymbol(),MODE_ASK),Digits);
Print("Closing order ¹ ",x," ",ticket);
oc=OrderClose(ticket,OrderLots(),priceClose,3,Yellow);
break;
default:
Print("Delete order ¹ ",x," ",ticket);
od=OrderDelete(ticket);
break;
}
}
}
return(0);
}
Regards
José Luis