Hello, i want to build a simple indicator about my profit-loss analysis... as Select by pos is related to Ticket ID/Order Date... is it possible to create the same one but sort by Closing Date
- OrderSelect(1,SELECT_BY_POS,MODE_HISTORY) - returns order in the middle of the history, Build 482
- Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes
- How to store the last order type (Buy/Sell)?
veeco:
as Select by pos is related to Ticket ID/Order Date... is it possible to create the same one but sort by Closing Date
as Select by pos is related to Ticket ID/Order Date... is it possible to create the same one but sort by Closing Date
int GetHistoryOrderByCloseTime(int& tickets[], int dsc=1){ #define ASCENDING -1 /* https://www.mql5.com/en/forum/137936 zzuegg says history ordering "is not reliable * (as said in the doc)" [not in doc] dabbler says "the order of entries is * mysterious (by actual test)" */ int nOrders = 0; datetime OCTs[]; for(int iPos=OrdersHistoryTotal()-1; iPos >= 0; iPos--) if ( OrderSelect(iPos, SELECT_BY_POS, MODE_HISTORY) // Only orders w/ && OrderMagicNumber() == Magic.Number // my magic number && OrderSymbol() == chart.symbol // and my pair. && OrderType() <= OP_SELL//Avoid cr/bal https://www.mql5.com/en/forum/126192 ){ int nextTkt = OrderTicket(); datetime nextOCT = OrderCloseTime(); nOrders++; ArrayResize(tickets,nOrders); ArrayResize(OCTs,nOrders); for (int iOrders=nOrders - 1; iOrders > 0; iOrders--){ // Insertn sort. datetime prevOCT = OCTs[iOrders-1]; if ((prevOCT - nextOCT) * dsc >= 0) break; int prevTkt = tickets[iOrders-1]; tickets[iOrders] = prevTkt; OCTs[iOrders] = prevOCT; } tickets[iOrders] = nextTkt; OCTs[iOrders] = nextOCT; // Insert. } return(nOrders); } : int tickets[], nTickets = GetHistoryOrderByCloseTime(tickets); for(int iTicket = 0; iTicket < nTickets; iTicket++) if ( OrderSelect(tickets[iTicket], SELECT_BY_TICKET) ){ double profit = OrderProfit() + OrderSwap() + OrderCommission(), :
WHRoeder:
I factored the insertion sort from the code and generalized the order byint GetHistoryOrderByCloseTime(...)
//+------------------------------------------------------------------+ //| Get Ticket Numbers Sorted. | //+------------------------------------------------------------------+ #define ORDER_CLOSE_PRICE 0 #define ORDER_CLOSE_TIME 1 #define ORDER_LOTS 2 #define ORDER_MAGIC_NUMBER 3 #define ORDER_OPEN_PRICE 4 #define ORDER_OPEN_TIME 5 #define ORDER_NET_PROFIT 6 // Profit + Swap + Commission #define ORDER_STOP_LOSS 7 #define ORDER_TAKE_PROFIT 8 #define ORDER_TICKET 9 #define ORDER_TYPE 10 double OrderValue(int eOV){ switch(eOV){ case ORDER_CLOSE_PRICE: return( OrderClosePrice() ); case ORDER_CLOSE_TIME: return( OrderCloseTime() ); case ORDER_LOTS: return( OrderLots() ); case ORDER_MAGIC_NUMBER: return( OrderMagicNumber() ); case ORDER_OPEN_PRICE: return( OrderOpenPrice() ); case ORDER_OPEN_TIME: return( OrderOpenTime() ); case ORDER_NET_PROFIT: return( OrderProfit() + OrderSwap() + OrderCommission() ); case ORDER_STOP_LOSS: return( OrderStopLoss() ); case ORDER_TAKE_PROFIT: return( OrderTakeProfit() ); case ORDER_TICKET: return( OrderTicket() ); case ORDER_TYPE: return( OrderType() ); // case ORDER_COMMENT: // Not a number. Not reliable. } } int GetHistoryOrderByCloseTime(int& tickets[], int dsc=1){ #define ASCENDING -1 return( GetHistoryOrderBy(tickets, ORDER_CLOSE_TIME, dsc) ); } int GetHistoryOrderBy(int& tickets[], int eOB, int dsc){ #define DESCENDING 1 //{ https://forum.mql4.com/46182 zzuegg says history ordering "is not // reliable (as said in the doc)" [not in doc] dabbler says "the order of // entries is mysterious (by actual test)" forum.mql4.com/48153#633606 //} int nOrders = 0; for(int iPos=OrdersHistoryTotal()-1; iPos >= 0; iPos--) if ( MySelect(iPos, SELECT_BY_POS, MODE_HISTORY) ) InsertTicket(tickets, nOrders, eOB, dsc); return(nOrders); } void InsertTicket(int& tickets[], int& nTickets, int eOB, double dsc=1){ int ticketNext = OrderTicket(); double orderByNext = OrderValue(eOB); nTickets++; if (ArraySize(tickets)<nTickets) ArrayResize(tickets, nTickets); for(int iEmpty = nTickets - 1; iEmpty > 0; iEmpty--){ // Insertion sort. int ticketPrev = tickets[iEmpty-1]; OrderSelect(ticketPrev, SELECT_BY_TICKET); double orderByPrev = OrderValue(eOB); if ((orderByPrev - orderByNext) * dsc >= 0.) break; tickets[iEmpty] = ticketPrev; } tickets[iEmpty] = ticketNext; // Insert. } bool MySelect(int iWhat, int eSelect, int ePool=MODE_TRADES){ if (!OrderSelect(iWhat, eSelect, ePool) ) return (false); if (OrderMagicNumber() != magic.number ) return (false); if (OrderSymbol() != chart.symbol ) return (false); if (ePool != MODE_HISTORY ) return (true); return(OrderType() <= OP_SELL); // Avoid cr/bal forum.mql4.com/32363#325360 // https://forum.mql4.com/30708 // Never select canceled orders. }
For this to work, set Account History tab to All History (right click) or programmatically:
#include <WinUser32.mqh> #import "user32.dll" int GetAncestor(int, int); #import void EnableAllHistory(){ #define GA_ROOT 2 // https://forum.mql4.com/ru/14463/page5#401551 #define MT4_WMCMD_ALL_HISTORY 33058 int main = GetAncestor(WindowHandle(Symbol(), Period()), GA_ROOT); PostMessageA(main, WM_COMMAND, MT4_WMCMD_ALL_HISTORY, 0); }
Generalization, either pool, sorted:
#define NEWEST DESCENDING #define OLDEST ASCENDING int GetHistoryOrderByCloseTime(int& tickets[], int dsc=NEWEST){ return( GetHistoryOrderBy(tickets, ORDER_CLOSE_TIME, dsc) ); } int GetHistoryOrderBy(int& tickets[], int eOB, int dsc){ //{ https://www.mql5.com/en/forum/137936 zzuegg says history ordering "is not // reliable (as said in the doc)" [not in doc] dabbler says "the order of // entries is mysterious (by actual test)" https://www.mql5.com/en/forum/139483 //} return( GetSortedOrders(tickets, eOB, dsc, MODE_HISTORY) ); } int GetSortedOrders(int& tickets[], int eOB = ORDER_OPEN_TIME, int dsc = NEWEST, int eMODE = MODE_TRADES){ if(eMode == MODE_TRADES) int nPool = OrdersTotal(); else nPool = OrdersHistoryTotal(); int nOrders = 0; for(int iPos = nPool - 1; iPos >= 0; iPos--) if( MySelect(iPos, SELECT_BY_POS, eMODE) ) InsertTicket(tickets, nOrders, eOB, dsc); return(nOrders); }
Too much info....brain exploding...
How can this be used to retrieve the profit of the last X trades pleeeeease!!
palepalepale:
How can this be used to retrieve the profit of the last X trades pleeeeease!!
How can this be used to retrieve the profit of the last X trades pleeeeease!!
First, you copy all of whroeder1's functions.
Then you simply use the code as follows (of course somewhere under your OnTick() function):
//+------------------------------------------------------------------+ //| Setting the profit of the last X trades in sumProfit | //+------------------------------------------------------------------+ int X = 5; //example - last 5 trades int arrTickets[]; EnableAllHistory(); int numOfOrders=GetHistoryOrderByCloseTime(arrTickets); double sumProfit=0; for(int i=0; i<X; i++) if(MySelect(arrTickets[i],SELECT_BY_TICKET) sumProfit += OrderProfit() + OrderCommission() + OrderSwap(); Print("The profit sum is: "+DoubleToStr(sumProfit,2));

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