Array or not to array - page 2

 
FMIC:
True, but since the OP did not provide any skeleton code, I gave the advice that I believe will fulfill the requirements the best based on my own experience. Using, pure arrays can seem to be the quickest solution due to the use of the ArraySort, but he will soon reach a point where pure arrays will become rather messy and limiting. So, i suggested an array of structure because it will be the next best thing before having to suggest the use of Classes.

and very good advice it is.

I was just adding a bit of information about ArraySort - it was not meant to be critical in any way.

As yet, I have had no reason to sort a struct array, but I guess it wouldn't be too difficult.

Usually the only thing that I need to do is add or remove an element of a struct array when a new trade is opened, or one closed.

An example of my function to remove is

void RemoveElement(int x,int as,Trades &array[]) //x =element to be removed as=array size
  {
   if(x==as-1)
     {
      ArrayResize(array,as-1);
      return;
     }
   for(;x<as-1;x++)
     {
      array[x].ticket=array[x+1].ticket;
      array[x].breakeven=array[x+1].breakeven;
      array[x].entry=array[x+1].entry;
      array[x].startlotsize=array[x+1].startlotsize;
      array[x].signal_time=array[x+1].signal_time;
      array[x].stoploss=array[x+1].stoploss;
      array[x].takeprofit=array[x+1].takeprofit;
      array[x].trail=array[x+1].trail;
     }
   ArrayResize(array,as-1);
   return;
  }

but after reading a recent post by WHRoeder, it seems that this could be simplified to

void RemoveElement(int x,int as,Trades &array[]) //x =element(trade) to be removed, as=array size
  {
   if(x==as-1)
     {
      ArrayResize(array,as-1);
      return;
     }
   for(;x<as-1;x++)
     {
      array[x]=array[x+1];
     }
   ArrayResize(array,as-1);
   return;
  }

Does that seem right to you? as I haven't got around to testing it yet.

Any comments and possible improvements will be welcome.

Thanks

 
GumRai:

and very good advice it is.

I was just adding a bit of information about ArraySort - it was not meant to be critical in any way.

As yet, I have had no reason to sort a struct array, but I guess it wouldn't be too difficult.

Usually the only thing that I need to do is add or remove an element of a struct array when a new trade is opened, or one closed.

An example of my function to remove is

but after reading a recent post by WHRoeder, it seems that this could be simplified to

Does that seem right to you? as I haven't got around to testing it yet.

Any comments and possible improvements will be welcome.

Thanks

I don't usually do it that way because I find that Resizing and Reshuffling array elements is rather inefficient due too high RAM and CPU. Instead I try to use some kind of indexing depending on the requirement I may have. Sometimes I may just use a simple "Head" and "Tail" system, while other times I may use a full indexing system, where only the index array is managed and sorted. It really depends on the exact need.

 

I was going to ask about the ArraySort function.... because If I do set up an array as outlined... then I would have to sort it....

I'm beginning to think that static variables are the way to go.... but then again... FMIC... you are telling me that it becomes a processing issue.... My EA is processed to the max....

Fok...

Sorry.... other way around...

 
FMIC - What do you mean when you say a "Head" or "Tail" system....
 
Mike.T:
FMIC - What do you mean when you say a "Head" or "Tail" system....

Let's not complicate things just yet. Since you have no experience with "arrays", try to start simple and slowly move onto more complex things. Start with using just a simple array of ticket numbers for orders, and then slowly add more data to other arrays or if you get to understand structures, then use an array of the structure.

However, just as future reference; "head" and "tail" are just a means of keeping track of list of things and being able to add or remove items from either the beginning or the end of that list. In a simple array, you normally can only add or remove an item on one end only, without having to reshuffle all the items again.

 

Ok... let's not complicate things.... FMIC... I've sent you some mail.... Hopefully I won't have to get into this but once my EA is running smoothly then.... getting to know Arrays will be on top of my list.... purely from a programming point of view...

But then again.... do I waste my time learning about Arrays... or do I spend my time working out how to create a good website.... and how to manage it... how to market it...

It's all a flipp'n pain in the arse...

Anyways... 

 
Mike.T:

But then again.... do I waste my time learning about Arrays...


I very much doubt that it would be a waste of time.

Say you want to print some onformation on the chart with heading labels

By using an array

   string Headings[]={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o"};
   
   string name=WindowExpertName();
   int x=5,y=20;
   
   for(int z=0;z<15;z++)
      {
      ObjectCreate(0,name+Headings[z],OBJ_LABEL,0,0,0);
      ObjectSetInteger(0,name+Headings[z],OBJPROP_XDISTANCE,x);
      ObjectSetInteger(0,name+Headings[z],OBJPROP_YDISTANCE,y);
      ObjectSetInteger(0,name+Headings[z],OBJPROP_COLOR,ChartGetInteger(0,CHART_COLOR_FOREGROUND));
      ObjectSetString(0,name+Headings[z],OBJPROP_TEXT,Headings[z]);
      
      x+=15;
      }

That took me a few minutes to write the code to place 15 heading labels.

Now, of course, you can create the 15 objects individually by creating 1 and then copy and paste 14 times and modifying the names and text.

I can assure you that it will take a lot longer.

Hands up all those that have copied and pasted blocks of code and never missed modifying something that leads to errors!

You with your hand up, you're a liar!

 
Mike.T:

But then again.... do I waste my time learning about Arrays... or do I spend my time working out how to create a good website.... and how to manage it... how to market it...

Website? What are you talking about? Probably, you just joking around!

Anyway, Arrays is one of the most basic programing techniques in almost every language, so LEARN IT!!!!

 

Thanx guys...

I'm on it... 

 
  1. GumRai: but after reading a recent post by WHRoeder, it seems that this could be simplified to
    void RemoveElement(int x,int as,Trades &array[]) //x =element(trade) to be removed, as=array size
      {
       if(x==as-1)                
         {                        
          ArrayResize(array,as-1);
          return;                 
         }                        
       for(;x<as-1;x++)
         {
          array[x]=array[x+1];
         }
       ArrayResize(array,as-1);
       return;
      }
    A) You don't need the first if, the for loop would do nothing in that case. B) I wouldn't even pass as, the function can get it internally.
  2.       array[x].ticket=array[x+1].ticket;
          array[x].breakeven=array[x+1].breakeven;
          array[x].entry=array[x+1].entry;
          array[x].startlotsize=array[x+1].startlotsize;
          array[x].signal_time=array[x+1].signal_time;
          array[x].stoploss=array[x+1].stoploss;
          array[x].takeprofit=array[x+1].takeprofit;
          array[x].trail=array[x+1].trail;

    Array[x] = array[x+1] will work if the struct does not have any strings or dynamic arrays.

  3. If you do add a string, add a copy constructor and assignment operator to your structure. Make it responsible for itself, so array[x] = array[x+1] still works.

    struct Trades{
       void   Trades(void){} // Allow array of Trades
       void   Trades(const Trades& that) : // Copy
          ticket(that.ticket), breakeven(that.breakeven),
          :
          trail(that.trail){}
       void   operator=(const Trades& that){
          ticket = that.ticket; breakeven=that.breakeven;
          :
       }
       void  update_selected(void){
          ticket = OrderTicket();
          stoploss = OrderStopLoss();
          :
    };

  4. GumRai: With an array of a structure, it will be necessary to write your own function to sort the array. ArraySort will not operate on struct arrays.
    As yet, I have had no reason to sort a struct array, but I guess it wouldn't be too difficult.
    And once done, doesn't need to be done again. The attached file has not been compiled/tested
    struct Trades{
      :
      bool operator<(const Trades& that){ return this.ticket < that.ticket; } // Default ordering
    };
    
     insertion_sort(Arrays, 0, ArraySize(Arrays)); // Order by ticket.
    /////////
    class TradesByEntry{ public:                                              // Non-default ordering
      bool is_before(const Trades& lhs, const Trades& rhs){ return lhs.entry < rhs.entry; }
    };
    
     TradesByEntry OOP;
     insertion_sort(Arrays, 0 , ArraySize(Arrays), OOP); // sort by entry price.
    

Files:
Reason: