Working with arrays, how to delete information

 

Hi Folks, 

So I'm setting arrays, in particular I want to remember which pairs and profit for that pair per individual trade, which appear to be a problem already. 

I can't seem to have a String and Double Array, so Pairs[string 1][ double 1][double 1]; or Pairs[1][1][1] appears to be not possible? 

I can get around that by running separate Arrays, but a big problem appears to be deleting information.

So I do it like this, my Pairs array is Pairs[eurusd] and then do an array Profit[1][1][1], so now I know if I look at position 1 on Pairs and I look at position 1 on Profit I am calling the profit for eurusd. So lets say I add a second pair, I can resize Pairs to Pairs[string 2] and Profit[2][1][1] and do the same thing. 

Now at some point I'm going to close out those eurusd trades in line 1 and want to delete the line on both arrays. But can you simply resize? If I resize Pairs[2] to Pairs[1] which bit of information will be deleted? The 1st line or the second ?

In short how do I edit my array and keep it up to date and current? No problem writing to lines, but when actions happen based on the arrays and that information is no longer usefull I need to clean up the array, iow's there 's no more eurusd trades and as such no profit. So the line is obsolete in both arrays.  I just can't see anyway of doing it. In particular my string array if I set line 1 to 0 will no longer return a string and the program will simply stop working?

 
PumPuiMonkey: I want to remember

No you don't. The first power glitch/BSOD/accidental close and you've lossed your data. Just recreate it when you need it. Could EA Really Live By Order_History Alone? - MQL4 forum

First You need to define your data, you have pairs, tickets, and profit.

tickets and profit, for example go together. They are NOT separate dimensions.

Pairs and tickets could be separate dimensions, except you can't mix data types, and Mq4 doesn't allow variable dimension sizes except for the first.

string pairs[]; int nPairs=0;
#define DATA_PAIR 0
#define DATA_TICKET 1
#define DATA_PROFIT 2
   #define DATA_COUNT 3
double data[DATA_COUNT];  int nData=0;

for(){
   string sym = ..
   int iPair = StringFindArray(pairs, sym, nPairs);
   if(iPair == EMPTY){ resizeArray(pairs, nPairs+1); pairs[nPairs]=sym; nPairs++;}
   resizeArray(data, nData+1);
   data[DATA_Pair] = iPair; data[DATA_TICKET] = ..
   nData++;
}
:
int      StringFindArray(string ar[], string w, int n=WHOLE_ARRAY, int iBeg=0){
   if(n == WHOLE_ARRAY) n = ArraySize(ar);
   for(; iBeg < n; iBeg++) if(ar[iBeg] == w) return(iBeg);
   return(EMPTY);                                                             }

Now you can process all pairs and process all tickets

for(iPair=0; iPair < nPairs; iPair++){
  double Eprofit=0.
  for(iData=0; iData < nData; iData++){ if (Data[iData][DATA_PAIR]!=iPair) continue;
      Print("Sym="+Pair[iPair]+" ticket="+Data[iData][DATA_TICKET]+" profit=$"+Data[iData][DATA_PROFIT]);
      Eprofit += Data[iData][DATA_PROFIT];
  }
  Print("Sym="+Pair[iPair]+" total profit=$"+Eprofit);
}
 
PumPuiMonkey:

Hi Folks, 

In short how do I edit my array and keep it up to date and current? No problem writing to lines, but when actions happen based on the arrays and that information is no longer usefull I need to clean up the array, iow's there 's no more eurusd trades and as such no profit. So the line is obsolete in both arrays.  I just can't see anyway of doing it. In particular my string array if I set line 1 to 0 will no longer return a string and the program will simply stop working?

You arrange the cell that you want to remove to be the last cell in the array and then you ArrayResize(array, ArraySize(array) - 1)  or ou read the array into a temporary array and when you get to the cell you want to delete you skip it and don't copy it,  then resize the original array and read back from the temporary array.
 

Thanks guys, 

 I'll read that tomorrow slowly, but clearly a better option would be to write this all to file, but same story, I can't see anything that will allow me to delete just portions of data? So lets say I have trade x and Y and closed them I would then obviously want to remove those numbers from the file?

 
RaptorUK:
You arrange the cell that you want to remove to be the last cell in the array and then you ArrayResize(array, ArraySize(array) - 1)  or ou read the array into a temporary array and when you get to the cell you want to delete you skip it and don't copy it,  then resize the original array and read back from the temporary array.
Don't need a temporary array. Just overwrite the deleted cell by either moving all higher elements down (if you need everything in order) or just copy the highest over deleted. And then Resize.
 
Nice thread. The solution is not clear though. Can someone please elaborate for folks like me who might stumble in here in the future?
 
Nelson Wanyama:
Nice thread. The solution is not clear though. Can someone please elaborate for folks like me who might stumble in here in the future?
William Roeder: moving all higher elements down (if you need everything in order) or just copy the highest over deleted. And then Resize.
/** Removes the element at the given _index_ by overwriting it with the last
 * element and remove that via resize_vector().
 * @returns False if the _vector_ couldn't be resized.                        */
template <typename Datatype>
static bool    erase_at(Datatype&   vector[],         ///<[in,out] The vector.
                        INDEX       index,            ///<[in]Location.
                         string     msg="")           /**<[in]Diagnostic. */{
   if(msg == "")                    msg = __FUNCTION__;
   INDEX    iLast = get_size(vector) - 1;
   vector[index] = vector[iLast];
   return resize_vector(vector, iLast, msg);
}
/** Removes the element at the given _index_ by by copying all higher elements
 * down one position and removes the last element via resize_vector().
 * @returns False if the _vector_ couldn't be resized.                        */
template <typename Datatype>
static bool    erase_ordered(Datatype& vector[],      ///<[in,out] The vector.
                             INDEX     index,         ///<[in]Location.
                             string    msg="")        /**<[in]Diagnostic. */{
   if(msg == "")                       msg = __FUNCTION__;
   for(INDEX iLast = get_size(vector) - 1; index < iLast; ++index)
      vector[index] = vector[index + 1];
   return resize_vector(vector, iLast, msg);
}
Files:
Arrays.mqh  28 kb
Reason: