Need help with arrays

 

Hi,

I am working on a basket trading EA. So far I have written most of the code and I came to the basket BUY and SELL Buttons function.

What I have so far is several arrays to build the buttons and feed the several formulas with the needed values. I now have the following:

string sym[]={"EURUSD", "GBPUSD", "USDJPY", "USDCHF", "NZDUSD","AUDUSD","USDCAD"}; // already existing

string EURUSDb[]={"EURUSD", "EURGBP", "EURJPY", "EURCHF", "EURNZD", "EURAUD", "EURCAD"}; //already existing

....and more arrays with different symbols to create the correct basket for the remaining pairs.

In the OnChartEvent section, I want the following to be possible:

for(i=0; i<=6; i++)

{

obj=sym+DoubleToString((i+4),0); //to match the correct denomination of the button - not of much importance in this case.

if(sparam)=obj //the button has been pressed

{

for(r=0; r<=6; r++)

Ordersend(EURUSDb[r], .......); // this is where I have the problem! At the first run of the loop when i=0, sym[0]=EURUSD - I can construct the array name to be called in the next function as such: sym[0]+"b". How to use the (sym[0]+"b") result to call the correct array, in this case: EURUSDb[r]. I tried (sym+"b")[r] to get the equivalent of EURUSDb[r] but did not work.

}

ObjectSetString(...OBJPROP_STATE, false);

}

I know it can be done with the if loop to assign manually the array to be used in the ordersend function and/or add a 2 dimentional array to the rest of the array list. I dont want to create additional lines or any additional arrays - the rest of the code is already built around the current arrays and would really not want to rewrite the whole code before it is confirmed there's no other way to achieve what I am after.

If any function exist to do what I have explained above (hoping that I have explained it well enough), please show me the way or any other simple/easy workaround to this case.

I really appreciate any help.

-pete

 
pete_36:
Hi,

I am working on a basket trading EA. So far I have written most of the code and I came to the basket BUY and SELL Buttons function.

What I have so far is several arrays to build the buttons and feed the several formulas with the needed values. I now have the following:

string sym[]={"EURUSD", "GBPUSD", "USDJPY", "USDCHF", "NZDUSD","AUDUSD","USDCAD"}; // already existing

string EURUSDb[]={"EURUSD", "EURGBP", "EURJPY", "EURCHF", "EURNZD", "EURAUD", "EURCAD"}; //already existing

....and more arrays with different symbols to create the correct basket for the remaining pairs.

In the OnChartEvent section, I want the following to be possible:

for(i=0; i<=6; i++)

{

obj=sym+DoubleToString((i+4),0); //to match the correct denomination of the button - not of much importance in this case.

if(sparam)=obj //the button has been pressed

{

for(r=0; r<=6; r++)

Ordersend(EURUSDb[r], .......); // this is where I have the problem! At the first run of the loop when i=0, sym[0]=EURUSD - I can construct the array name to be called in the next function as such: sym[0]+"b". How to use the (sym[0]+"b") result to call the correct array, in this case: EURUSDb[r]. I tried (sym+"b")[r] to get the equivalent of EURUSDb[r] but did not work.

}

ObjectSetString(...OBJPROP_STATE, false);

}

I know it can be done with the if loop to assign manually the array to be used in the ordersend function and/or add a 2 dimentional array to the rest of the array list. I dont want to create additional lines or any additional arrays - the rest of the code is already built around the current arrays and would really not want to rewrite the whole code before it is confirmed there's no other way to achieve what I am after.

If any function exist to do what I have explained above (hoping that I have explained it well enough), please show me the way or any other simple/easy workaround to this case.

I really appreciate any help.

-pete

pete

It seems to be OK (your code)

I have tested something like this, and all works OK :

#property indicator_chart_window

string EURUSDb[]={"EURUSD", "EURGBP", "EURJPY", "EURCHF", "EURNZD", "EURAUD", "EURCAD"};

int init() { return(0); }

int start()

{

string result ="";

for(int r=0; r<=6; r++)

result = result + EURUSDb[r]+"b\n";

Comment(result);

return(0);

}
 

Hi mladen,

Maybe I did not explain myself correctly. This part of the ea is working fine as you mentioned.

Attached is how the screen looks. Lets take EURUSD section for example. I called all the buttons EURUSD in this section and gave them a sequence EURUSD1, EURUSD2... so on. The button with the text BUY BASKET is EURUSD6.

In the section on chart event, I am able to make reference to this button whenever it is clicked. The problem I am facing is the execution of the OrderSend function. I should have it in this form OrderSend(EURUSDb,...); to execute orders for all the pairs within this basket that are present within the EURUSDb array while keeping one loop for this operation.

for(i=0; i<=6; i++)

{

obj=sym+DoubleToString((i+4),0);

if(sparam)=obj

In this part of the code, I am able to successfully find the pair to be traded when the corresponding button is pressed

{

for(r=0; r<=6; r++)

Ordersend(EURUSDb[r], .......);

}

In this part I will execute the BUY or SELL based on the specific currency symbol array that has all the cross pairs already stored in it. A way to do this is to have 7 different loops each for a specific pair and call explicitly the array in question i.e: OrderSend(EURUSDb[r]...) and so on for each pair. This will result in 7 loops for the BUY BASKET and another 7 loops for the SELL BASKET.

I am trying to substitute the array in the OrderSend function with a dynamic variable to call the respective array of the corresponding pair as needed. So there will be two nested "for" functions in this operation. I am able to deduce the EURUSDb but to make the OrderSend function process it as an array with the pairs stored in it.

As I was researching the internet, I found a way of having a 2 dimensional array. In a graphical representation of the array (http://book.mql4.com/variables/arrays), the first line will have the currency pair and each column will have its cross pairs, this way I would be able to call it as EURUSD[1][r] - but again I will have to learn who to build a 2 dimensional array of string (I am not a coder, not even close!) and re-write part of the code.

I hope to have explained it well enough this time - thank you for commenting. If you think you have a way to achieve this, please share. I am also open to any other way that can make this operation simplified without having to code 7 different loops for each operation.

-pete

Files:
 

Hi mladen,

Maybe I did not explain myself correctly. This part of the ea is working fine as you mentioned.

Here is how the screen looks like. Lets take EURUSD section for example. I called all the buttons EURUSD in this section and gave them a sequence EURUSD1, EURUSD2... so on. The button with the text BUY BASKET is EURUSD6.

In the section on chart event, I am able to make reference to this button whenever it is clicked. The problem I am facing is the execution of the OrderSend function. I should have it in this form OrderSend(EURUSDb,...); to execute orders for all the pairs within this basket that are present within the EURUSDb array while keeping one loop for this operation.

for(i=0; i<=6; i++)

{

obj=sym+DoubleToString((i+4),0);

if(sparam)=obj

In this part of the code, I am able to successfully find the pair to be traded when the corresponding button is pressed

{

for(r=0; r<=6; r++)

Ordersend(EURUSDb[r], .......);

}

In this part I will execute the BUY or SELL based on the specific currency symbol array that has all the cross pairs already stored in it. A way to do this is to have 7 different loops each for a specific pair and call explicitly the array in question i.e: OrderSend(EURUSDb[r]...) and so on for each pair. This will result in 7 loops for the BUY BASKET and another 7 loops for the SELL BASKET.

I am trying to substitute the array in the OrderSend function with a dynamic variable to call the respective array of the corresponding pair as needed. So there will be two nested "for" functions in this operation. I am able to deduce the EURUSDb but to make the OrderSend function process it as an array with the pairs stored in it.

As I was researching the internet, I found a way of having a 2 dimensional array. In a graphical representation of the array (http://book.mql4.com/variables/arrays), the first line will have the currency pair and each column will have its cross pairs, this way I would be able to call it as EURUSD[1][r] - but again I will have to learn who to build a 2 dimensional array of string (I am not a coder, not even close!) and re-write part of the code.

I hope to have explained it well enough this time - thank you for commenting. If you think you have a way to achieve this, please share. I am also open to any other way that can make this operation simplified without having to code 7 different loops for each operation.

-pete

Files:
 

Hi mladen,

Maybe I did not explain myself correctly. This part of the ea is working fine as you mentioned.

Attached is how the screen looks. Lets take EURUSD section for example. I called all the buttons EURUSD in this section and gave them a sequence EURUSD1, EURUSD2... so on. The button with the text BUY BASKET is EURUSD6.

In the section on chart event, I am able to make reference to this button whenever it is clicked. The problem I am facing is the execution of the OrderSend function. I should have it in this form OrderSend(EURUSDb,...); to execute orders for all the pairs within this basket that are present within the EURUSDb array while keeping one loop for this operation.

for(i=0; i<=6; i++)

{

obj=sym+DoubleToString((i+4),0);

if(sparam)=obj

In this part of the code, I am able to successfully find the pair to be traded when the corresponding button is pressed

{

for(r=0; r<=6; r++)

Ordersend(EURUSDb[r], .......);

}

In this part I will execute the BUY or SELL based on the specific currency symbol array that has all the cross pairs already stored in it. A way to do this is to have 7 different loops each for a specific pair and call explicitly the array in question i.e: OrderSend(EURUSDb[r]...) and so on for each pair. This will result in 7 loops for the BUY BASKET and another 7 loops for the SELL BASKET.

I am trying to substitute the array in the OrderSend function with a dynamic variable to call the respective array of the corresponding pair as needed. So there will be two nested "for" functions in this operation. I am able to deduce the EURUSDb but to make the OrderSend function process it as an array with the pairs stored in it.

As I was researching the internet, I found a way of having a 2 dimensional array. In a graphical representation of the array (http://book.mql4.com/variables/arrays), the first line will have the currency pair and each column will have its cross pairs, this way I would be able to call it as EURUSD[1][r] - but again I will have to learn who to build a 2 dimensional array of string (I am not a coder, not even close!) and re-write part of the code.

I hope to have explained it well enough this time - thank you for commenting. If you think you have a way to achieve this, please share. I am also open to any other way that can make this operation simplified without having to code 7 different loops for each operation.

-pete

 

Hi mladen,

Maybe I did not explain myself correctly. This part of the ea is working fine as you mentioned.

Attached is how the screen looks. Lets take EURUSD section for example. I called all the buttons EURUSD in this section and gave them a sequence EURUSD1, EURUSD2... so on. The button with the text BUY BASKET is EURUSD6.

In the section on chart event, I am able to make reference to this button whenever it is clicked. The problem I am facing is the execution of the OrderSend function. I should have it in this form OrderSend(EURUSDb,...); to execute orders for all the pairs within this basket that are present within the EURUSDb array while keeping one loop for this operation.

for(i=0; i<=6; i++)

{

obj=sym+DoubleToString((i+4),0);

if(sparam)=obj

In this part of the code, I am able to successfully find the pair to be traded when the corresponding button is pressed

{

for(r=0; r<=6; r++)

Ordersend(EURUSDb[r], .......);

}

In this part I will execute the BUY or SELL based on the specific currency symbol array that has all the cross pairs already stored in it. A way to do this is to have 7 different loops each for a specific pair and call explicitly the array in question i.e: OrderSend(EURUSDb[r]...) and so on for each pair. This will result in 7 loops for the BUY BASKET and another 7 loops for the SELL BASKET.

I am trying to substitute the array in the OrderSend function with a dynamic variable to call the respective array of the corresponding pair as needed. So there will be two nested "for" functions in this operation. I am able to deduce the EURUSDb but to make the OrderSend function process it as an array with the pairs stored in it.

As I was researching the internet, I found a way of having a 2 dimensional array. In a graphical representation of the array (mql4 on arrays), the first line will have the currency pair and each column will have its cross pairs, this way I would be able to call it as EURUSD[1][r] - but again I will have to learn who to build a 2 dimensional array of string (I am not a coder, not even close!) and re-write part of the code.

I hope to have explained it well enough this time - thank you for commenting. If you think you have a way to achieve this, please share. I am also open to any other way that can make this operation simplified without having to code 7 different loops for each operation.

-pete

 
Files:
Reason: