it will return the ProfitLoss of only one deal of the selected magic number, but it will not add up the ProfitLoss of all ANY magic number or ANY direction :
Why does it need to check out deals which have the opposite direction ?
double sqGetClosedPLInMoney(string symbol, int magicNo, int direction, string comment, int shift) { int index = 0; HistorySelect(startTime, TimeCurrent()); for(int i=HistoryDealsTotal()-1; i>=0; i--) { ulong ticket = HistoryDealGetTicket(i); if(HistoryDealGetInteger(ticket, DEAL_ENTRY) != DEAL_ENTRY_OUT) continue; if(dealFits(ticket, symbol, magicNo, direction * -1, comment)) { //we need to check out deals which have the opposite direction if(index == shift) { return(HistoryDealGetDouble(ticket, DEAL_PROFIT)); } index++; } } return(0); } double sqGetClosedPLInPips(string symbol, int magicNo, int direction, string comment, int shift) { int index = 0; HistorySelect(startTime, TimeCurrent()); for(int i=HistoryDealsTotal()-1; i>=0; i--) { ulong ticket = HistoryDealGetTicket(i); if(HistoryDealGetInteger(ticket, DEAL_ENTRY) != DEAL_ENTRY_OUT) continue; if(dealFits(ticket, symbol, magicNo, direction * -1, comment)) { //we need to check out deals which have the opposite direction if(index == shift) { double profit = HistoryDealGetDouble(ticket, DEAL_PROFIT); double volume = HistoryDealGetDouble(ticket, DEAL_VOLUME); return profit/(SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_VALUE) * volume)/10; } index++; } } return(0); }
Description changed:
it will return the ProfitLoss of only one deal of the selected magic number, but it will not add up the ProfitLoss of all ANY magic number or ANY direction :
Why does it need to check out deals which have the opposite direction ?
double sqGetClosedPLInMoney(string symbol, int magicNo, int direction, string comment, int shift) { int index = 0; HistorySelect(startTime, TimeCurrent()); for(int i=HistoryDealsTotal()-1; i>=0; i--) { ulong ticket = HistoryDealGetTicket(i); if(HistoryDealGetInteger(ticket, DEAL_ENTRY) != DEAL_ENTRY_OUT) continue; if(dealFits(ticket, symbol, magicNo, direction * -1, comment)) { //we need to check out deals which have the opposite direction if(index == shift) { return(HistoryDealGetDouble(ticket, DEAL_PROFIT)); } index++; } } return(0); } double sqGetClosedPLInPips(string symbol, int magicNo, int direction, string comment, int shift) { int index = 0; HistorySelect(startTime, TimeCurrent()); for(int i=HistoryDealsTotal()-1; i>=0; i--) { ulong ticket = HistoryDealGetTicket(i); if(HistoryDealGetInteger(ticket, DEAL_ENTRY) != DEAL_ENTRY_OUT) continue; if(dealFits(ticket, symbol, magicNo, direction * -1, comment)) { //we need to check out deals which have the opposite direction if(index == shift) { double profit = HistoryDealGetDouble(ticket, DEAL_PROFIT); double volume = HistoryDealGetDouble(ticket, DEAL_VOLUME); return profit/(SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_VALUE) * volume)/10; } index++; } } return(0); }
Subject changed from Functions : sqGetClosedPLInMoney and sqGetClosedPLInPips to Functions : sqGetClosedPLInMoney and sqGetClosedPLInPips returning PL of one order only