Download MetaTrader 5

using a name in a string to get the value of a variable of same name

To add comments, please log in or register
Log in to MQL5.community using OpenID. It is convenient!
Andy Nicholson
157
Andy Nicholson 2013.11.08 22:22 

If i have a variable "double x=1.1;" and a string "string variables[] = {"a","b","x","y"};"

I can obvioulsy get my x string by saying "variables[3]" but how do i get this back into my variable value of 1.1 if i only know that variables[3] contains the name of the variable that holds my value of 1.1 but i cant just type x without bringing it from the string I will attach some code that might help explain better what i need to do if you look at the line near the end with the print function it has // explaining my problem

Ian Venner
2584
Ian Venner 2013.11.09 05:03  

Your string array is filled:

string pplevels[] = {"Dwp","Dws1","Dws2","Ds1","Ds2"}; 

after sorting, dSortThis[3][1] == 4

So, pplevels[dSortThis[3][1]]) is calling pplevels[4];

so when you do

Alert ( " the 4th value sorted is ", pplevels[dSortThis[3][1]]);

You are printing pplevels[4] which holds the string, "Ds2". If you printed dSortThis[3][0]; it might be what you wanted

Andy Nicholson
157
Andy Nicholson 2013.11.09 11:53  
Thanks for looking SDC the value in [3][0] is what i want but the the whole purpose of the 2 dim array is to sort the variable names in order of which hold the highest to lowest value so that i can say if price is lower than Ds2 then i ask if price is lower than the variable name in dSortThis[4][1] etc that code was just code that MaxDoom from forex factory wrote to test the theory of if it arragenged both dimensions or just the first and it does what i thought i needed by arranging the variable names according to the value they hold via the numbered strings but I want to be able to use the variable name or those strings that corrosponds to the next highest or lowest piece of data . I am making some code that will eventually give me lots of data regarding where the price goes in relation to all pivot points this bit is purely so that i can use the correct variable relating what to variables the price is in between then when it crosses i can use the next varible in the array to ask if the price reached it. if that makes sense. but am thinking as long as i can track the name of the value i am using i can just use the value like you said and when it comes to putting data into another array i can use the name there but just use the value in all calculations ya you made me think more about it thanks. For full thread on forex factory with all code and story see http://www.forexfactory.com/showthread.php?p=7078894#post7078894
Ubzen
5394
Ubzen 2013.11.09 18:18  
4xAndy: ... the value in [3][0] is what i want but the the whole purpose of the 2 dim array is to sort the variable names in order of which hold the highest to lowest value so that i can say if price is lower than Ds2 then i ask if price is lower than the variable name in dSortThis[4][1] ...

You're trying to ask questions like highest and lowest but you want to work with strings. That is going to cause you headaches. Within all the examples you've provided, strings are being associated with Array_Indexes, i.e 0-1-2-3-4. <--- If you break this association, you'll have to result to static methods of tracing the strings which throws all advantages of using an array out the window.

If price is lower than variable (string) name makes no sense. What you're looking for is should price be lower than the value of dSortThis[4][0].

  • Example of what you're looking for is,
  • get the Volatility of 5 different symbols within an array.
  • sort the double_array from lowest to highest as example.
  • create another loop which search for eurusd_volatility and returns the index
  • then put that string_symbol_name i.e "eurusd" in a string_array @ the found index location.
  • you end up with string array which looks like sym_sort_volatility_arr[] = {"eurchf","eurusd","eurjpy" etc};

Now if you want to know if eurusd is lower than eurjpy, you'll have to create another function which returns the index of string arrays.

So yea, don't work with strings.

added* just as a side note, I probably shouldn't say don't work with strings because that's how I've handles most of my multi_currency logics up_to_this_point. I used the strings because it's easier to remember that i want the value of "eurjpy" instead of trying to remember which index it's associated with. In your case, you're already using the 2d_array, in my case, I was trying to avoid them.

Ian Venner
2584
Ian Venner 2013.11.09 18:24  

4xAndy you already have the names and values linked you're just not seeing it. The link between the values in SortThis[] and the names in pplevels[] is the second dimension of each value index in SortThis[] that link is good even after sorting the array

Andy Nicholson
157
Andy Nicholson 2013.11.09 20:36  
SDC:

4xAndy you already have the names and values linked you're just not seeing it. The link between the values in SortThis[] and the names in pplevels[] is the second dimension of each value index in SortThis[] that link is good even after sorting the array


Ya I was the one who linked them, but with the hope i could use the linked strings to call the value, but like I say I can work purely from the data in the first dimension and insert the variable name as reference when I am outputting my data to spreadsheet. If you look at the other thread in forex factory you will hopefully see how this all came together. Max doom created the code to demonstrate whether my theory would work and then i added the pplevels string array to show him roughly where i was going with it. But I really did want to carry on with the actual variable names in the correct order and call the value from the correctly placed variable name. All the same thanks to you for making me realize that i can work round it by using the values from the first dimension and reinserting the strings that correspond only when I need to output the data for analysis.


Thanks Andy

Andy Nicholson
157
Andy Nicholson 2013.11.10 12:06  
ubzen:

You're trying to ask questions like highest and lowest but you want to work with strings. That is going to cause you headaches. Within all the examples you've provided, strings are being associated with Array_Indexes, i.e 0-1-2-3-4. <--- If you break this association, you'll have to result to static methods of tracing the strings which throws all advantages of using an array out the window.

If price is lower than variable (string) name makes no sense. What you're looking for is should price be lower than the value of dSortThis[4][0].

  • Example of what you're looking for is,
  • get the Volatility of 5 different symbols within an array.
  • sort the double_array from lowest to highest as example.
  • create another loop which search for eurusd_volatility and returns the index
  • then put that string_symbol_name i.e "eurusd" in a string_array @ the found index location.
  • you end up with string array which looks like sym_sort_volatility_arr[] = {"eurchf","eurusd","eurjpy" etc};

Now if you want to know if eurusd is lower than eurjpy, you'll have to create another function which returns the index of string arrays.

So yea, don't work with strings.

added* just as a side note, I probably shouldn't say don't work with strings because that's how I've handles most of my multi_currency logics up_to_this_point. I used the strings because it's easier to remember that i want the value of "eurjpy" instead of trying to remember which index it's associated with. In your case, you're already using the 2d_array, in my case, I was trying to avoid them.

I did get the idea from someone who was working on currency strength but i only wanted a simple way of sorting the values of various pivot points variables. I am writing code that will go once through history of say 5 years noting how close to pivot point the market went if it broke and how far it broke basically as much info as possible on every move in relation to the many pivot points then write it all to a spread sheet for analysis then use the results in an ea i am developing that is already doing quite well. So basically i need my code to see where in relation to 66 different pivot point levels the price is and what are the next levels up and what are next lower. The way I am implementing the code now is working but if there is a better way that does not involve more cpu usage or extensive code i would use it. But like i say i can arrange the values for access then when outputting the data use the value from second dimension to tell the spreadsheet what levels the data relates to. If I could do this without strings, I would love to know how. if the price was above the daily pivot point I can't just tell my code to look for it to touch or break r1 because the weekly pivot point could be the next level of resistance how can i make my code ask if the weekly pivot point is next when i have 66 levels to check? Its hard to explain the full picture. Even if it is simple!
Andy Nicholson
157
Andy Nicholson 2013.11.10 13:03  
I started by writting code that gave me my 66 pivot lines, then I wanted to get them in order of value and found that putting them in a one dim array was easiest but then how would I know what level was what, I then found a post that uses currencies with the second dimension being numbered to the currency strings and thought this was my only option and have not found anything better as yet. When I get my results I can do away with the pivots that have the least affect but till then I do not want to throw away a pivot that may affect a big bounce or cross and I need my code to know where the next closest pivot is at all times not just its value but its name. If I could have code that shifted the variables around in the code according to there value I would not even have to dream. I normally figure out my solutions myself, but after finally getting my ea where I wanted it myself, then finding more problems just trying to collect data I thought of seeking help but because of misunderstandings it has become equally as difficult i think. I guess when you are communicating a fairly technical subject with people you have never communicated with or met, it is to be expected that wires will get crossed. However most comments are from those who want to help so thanks to all!
Andy Nicholson
157
Andy Nicholson 2013.11.14 22:43  
This is the pivot info for each day backtested layed out in csv its not the final data that i will be gathering just a filewrite spreadsheet of the 66 pivots in order with their values for each day to show more clearly where i am going with this am still working on code to analyse what the price did in relation to this data. Thanks again to all who helped and tried to help.
Andy Nicholson
157
Andy Nicholson 2013.11.14 22:45  
The file did not show in above post will try again or is there a problem with attaching csv files?
Andy Nicholson
157
Andy Nicholson 2013.11.14 22:46  
ok here it is renamed to txt it should be renamed back to csv to open in excel as spreadsheet
Files:
To add comments, please log in or register