Dynamic Array

 

Hi all ....

I'm absolutely hopeless when it comes to arrays, sort of a brain freeze, so I hope to get some help here.

To really simplify, I am trying to add an orders ticket number to an array whenever a new order is opened. For example, the first orders ticket number will be in the first cell of the array, and then, when another order is opened, the array will grow dynamically and the new order's ticket number will be in the second cell and so on. The array needs to be dynamic because I have no idea what the final size will be. This is a little test routine that I made up .... can someone help me with the missing code?


Thanks in advance ...


int myarray[0];      // Is this how you declare a dynamic array?
int g_count;
int g_ticket;
double g_units = 1;

int start()
   {  // Open 3 buy contracts
      for (g_count = 2; g_count >=0; g_count--) 
      {
   
      g_ticket = OrderSend(Symbol(),OP_BUY,g_units,Ask,3,0,0,"",0,0);          // if successful = ticket number, else a negative number
  
         if(g_ticket<0)  // failed
         {
            Alert("OrderSend failed with error #",GetLastError());
            return(0);
         }
   
  // Now add the ticket number to the array ????
  
  /////
  /////
  /////
  
      }
  
   return(0);
   }
 
G-fer:

Hi all ....

I'm absolutely hopeless when it comes to arrays, sort of a brain freeze, so I hope to get some help here.

To really simplify, I am trying to add an orders ticket number to an array whenever a new order is opened. For example, the first orders ticket number will be in the first cell of the array, and then, when another order is opened, the array will grow dynamically and the new order's ticket number will be in the second cell and so on. The array needs to be dynamic because I have no idea what the final size will be. This is a little test routine that I made up .... can someone help me with the missing code?


Thanks in advance ...



Hi,

I dont think the language supports dynamic arrays, you have to declare the size of the array when declaring the array and cant see a way of changing that size dynamically.(Could be wrong here). Why don't you just make your array sufficiently large(say 20?) to hold a number of records that you estimate it will not exceed and have a counter to keep track of the number of records in the array.

ie:

int array[20];

int index = 0;

void addTicket(int ticket)

{

array[index++] = ticket;

}

 

Try this . . .

G-fer:

Hi all ....

I'm absolutely hopeless when it comes to arrays, sort of a brain freeze, so I hope to get some help here.

To really simplify, I am trying to add an orders ticket number to an array whenever a new order is opened. For example, the first orders ticket number will be in the first cell of the array, and then, when another order is opened, the array will grow dynamically and the new order's ticket number will be in the second cell and so on. The array needs to be dynamic because I have no idea what the final size will be. This is a little test routine that I made up .... can someone help me with the missing code?

Try this . . .

int TicketNumbers[];


// Now add the ticket number to the array ????

ArrayResize(TicketNumbers, ArraySize(TicketNumbers) + 1);     //  grow the array size by 1 cell
TicketNumbers[ArraySize(TicketNumbers) - 1] = g_ticket;       //  save the ticket number into the last cell of the array

Instead of, or as well as, your Alert use Print(), Alert() doesn't work in the Strategy Tester.

 
RaptorUK:

Try this . . .

Try this . . .

Instead of, or as well as, your Alert use Print(), Alert() doesn't work in the Strategy Tester.


Thanks for the instant answer folks, it's helped a lot. If it's not too much trouble, could you explain how I would include both the ticket number and the orderopenprice() in the array.

Also, I'll remember your tip about the alert function.


Thanks a million ...

 
G-fer:

Thanks for the instant answer folks, it's helped a lot. If it's not too much trouble, could you explain how I would include both the ticket number and the orderopenprice() in the array.

I can, but before I do . . . why are you doing this ? if your EA stops and restarts for any reason all this information will be lost unless you save it to file and retrieve it when you restart.
 
RaptorUK:
I can, but before I do . . . why are you doing this ? if your EA stops and restarts for any reason all this information will be lost unless you save it to file and retrieve it when you restart.


Yep ... I thought of that ... I was assuming that i could declare it as a global array so that it doesn't disappear in the event of a crash. As far as why ... I'm going to use it in a proportional stop loss routine that will use the array to determine the last stop loss level of each order, and then adjust it as required.

G-fer

 
G-fer:

Yep ... I thought of that ... I was assuming that i could declare it as a global array so that it doesn't disappear in the event of a crash. As far as why ... I'm going to use it in a proportional stop loss routine that will use the array to determine the last stop loss level of each order, and then adjust it as required.

G-fer

Even a globally declared array is wiped when the EA stops, using Global Variables isn't an option either. Why don't you just read the SL from the order History using OrderSelect() and then OrderStopLoss() ?

Ticket Number & OOP

int OrderInfo[];   // array holds ticket number in the even cells and OrderOpenPrice in the odd cells



OrderSelect(g_ticket, SELECT_BY_TICKET);

// Now add the ticket number to the array ????

ArrayResize(OrderInfo, ArraySize(OrderInfo) + 2);              //  grow the array size by 2 cells fro each Order
OrderInfo[ArraySize( ( OrderInfo) / 2 ) - 1] = g_ticket;       //  save the ticket number into the last even numbered cell of the array
OrderInfo[ArraySize( ( OrderInfo) / 2 ) ] = OrderOpenPrice();  //  save the OrderOpenPrice in the array last odd numbered cell
 
RaptorUK:

Even a globally declared array is wiped when the EA stops, using Global Variables isn't an option either. Why don't you just read the SL from the order History using OrderSelect() and then OrderStopLoss() ?

Ticket Number & OOP


Heh heh .... hidden stop ... call me neurotic :)

Umm ... I thought Global Variables remained on the client terminal for 4 weeks ... does this exclude arrays?

G-fer

 
G-fer:

Heh heh .... hidden stop ... call me neurotic :)
No, I'll call you paranoid and a fool instead. No offence intended . . . but I guess you are trading such large position sizes that your Broker is going to adjust price to all it's customers just so it can take your SL . . .
 
RaptorUK:
No, I'll call you paranoid and a fool instead. No offence intended . . . but I guess you are trading such large position sizes that your Broker is going to adjust price to all it's customers just so it can take your SL . . .


Mmmm .... you trade your way, I'll trade my way. I'm asking for assistance, not for sarcastic criticism ...

It may be that dozens of stop loss changes may need to be made in a short period of time ... much easier to make them and track them locally than to continually complicate matters by choking bandwidth and continually presenting order modifications. I manually, not automatically, close my trades, and I manually set a fixed stop loss after opening a position.

Now ... about the Global Variables thing ??

 
G-fer:

Now ... about the Global Variables thing ??

A Global Variable != globally declared variable

Global Variables are stored in files and are doubles, they can't hold arrays . . .

You are going to need to store your info in a file . . .

Reason: