//+------------------------------------------------------------------+ //| mTrackPositions.mq4 | //| Copyright © 2009, Mark Carver | //| | //| Indicator to track position profit in .csv file for later | //| analysis on the life-cycle of a position. | //| | //| Array size max is 32, max # of files in MT4 that can be open | //| | //| Keep files open or eventually MT4 will choke from buffer overflow| //| | //+------------------------------------------------------------------+ #property copyright "Copyright © 2009, Mark Carver" #property link "" #property indicator_chart_window extern bool EveryTick=false; //---If false, takes profit snapshot every minute extern bool ShowComment=true; int myPositions[32,2]; int numPositions=0; int ThisMinute; bool Debug=false; //+------------------------------------------------------------------+ void init() {ThisMinute=Minute()+1;return;} //+------------------------------------------------------------------+ void deinit() {if(ShowComment) Comment("");return;} //+------------------------------------------------------------------+ void start() { string myTime=TimeToStr(TimeCurrent(),TIME_DATE|TIME_MINUTES), Comment.Line; bool Found; int myProfit, myTicket, myRow, myHandle, myOrders, i, j; int myTempPositions[32,2]; int myTempNumPositions=0; Comment.Line=StringConcatenate("\nCurrent Broker Time: ",myTime); if(!EveryTick&&ThisMinute==Minute()) return; ThisMinute=Minute(); myOrders=OrdersTotal(); int myAllTickets[32]; Comment.Line=StringConcatenate(Comment.Line,"\nNumber Orders = ",myOrders); for(i=0;i<myOrders;i++) { Found=false; myRow=0; OrderSelect(i,SELECT_BY_POS,MODE_TRADES); myTicket=OrderTicket(); myAllTickets[i+1]=myTicket; RefreshRates(); myProfit=OrderProfit()/OrderLots()/MarketInfo(OrderSymbol(),MODE_TICKVALUE); for(j=1;j<=numPositions;j++) { if(myPositions[j,0]==myTicket) { Found=true; myRow=j; break; } } if(!Found) { numPositions+=1; myPositions[numPositions,0]=myTicket; myHandle=FileOpen(GetFileName(myTicket),FILE_WRITE|FILE_CSV,","); FileWrite(myHandle,myTime,myProfit); myPositions[numPositions,1]=myHandle; Comment.Line=StringConcatenate(Comment.Line,"\nAdding position: ",myPositions[numPositions,0]); } else { FileWrite(myPositions[myRow,1],myTime,myProfit); Comment.Line=StringConcatenate(Comment.Line,"\nUpdating position: ",myPositions[myRow,0]," myProfit= ",myProfit); } if(Debug) Comment.Line=StringConcatenate(Comment.Line,"\nmyTicket = ",myTicket," myProfit = ",myProfit); } if(Debug) Comment.Line=StringConcatenate(Comment.Line,"\nnumPositions=",numPositions); if(Debug) { Comment.Line=StringConcatenate(Comment.Line,"\nDebug.myPositions. "); for(i=1;i<=numPositions;i++){Comment.Line=StringConcatenate(Comment.Line," i=",i," 0=",myPositions[i,0]," 1=",myPositions[i,1]," ... ");} Comment.Line=StringConcatenate(Comment.Line,"\nDebug.myAllTickets. "); for(i=1;i<=myOrders;i++){Comment.Line=StringConcatenate(Comment.Line," i=",i," myAllTickets[i]=",myAllTickets[i]);} } if(numPositions>0) //---------Move to temp array cleaned with new or dropped positions and rebuild primary array { for(i=1;i<=numPositions;i++) { Found=false; if(Debug) Comment.Line=StringConcatenate(Comment.Line,"\nEntering debug. numPositions=",numPositions," i=",i," myPositions[i,0]=",myPositions[i,0]," myPositions[i,1]=",myPositions[i,1]); for(j=1;j<=myOrders;j++) { if(myPositions[i,0]==myAllTickets[j]) //-----Is position still open? If so, keep { Found=true; break; } } if(!Found) //-----Position closed this run. Close file and don't reload in array { FileClose(myPositions[i,1]); if(Debug) Comment.Line=StringConcatenate(Comment.Line,"\nClosing position ",myPositions[i,0]); } else { myTempNumPositions+=1; myTempPositions[myTempNumPositions,0]=myPositions[i,0]; myTempPositions[myTempNumPositions,1]=myPositions[i,1]; if(Debug) Comment.Line=StringConcatenate(Comment.Line,"\nAdding to array position ",myTempPositions[myTempNumPositions,0]," myPositions[i,0] ",myPositions[i,0]); } } if(myTempNumPositions>0) //-----Move back from temp array to primary array { for(i=1;i<=myTempNumPositions;i++) { myPositions[i,0]=myTempPositions[i,0]; myPositions[i,1]=myTempPositions[i,1]; } } numPositions=myTempNumPositions; } if(Debug) { Comment.Line=StringConcatenate(Comment.Line,"\nAfter.Debug.myPositions. numPositions=",numPositions," "); for(i=1;i<=numPositions;i++) { Comment.Line=StringConcatenate(Comment.Line," i=",i," 0=",myPositions[i,0]," 1=",myPositions[i,1]," ... "); } } if(ShowComment) Comment(Comment.Line); return; } //+------------------------------------------------------------------+ string GetFileName(int myTicket) {return(StringConcatenate("/PositionData/p",myTicket,".csv"));} //+------------------------------------------------------------------+
Sample
Analysis
Market Information Used:
Indicator Curves created:
Indicators Used:
Custom Indicators Used:
Order Management characteristics:
Checks for the total of open orders
Other Features:
Uses files from the file system
It writes information to file