In first place i want to apologize the gap in time for my articles, but some personal problems prevented me from to have the time, the required concentration and patience to write them. In this article i try to talk about exporting JForex values we want to files on a textual manner for human readability or to CSV (comma separated values) to import them on other programs, for example Excel, Mathlab or any compatible program. Also you can import csv files directly on database. The ideia to this article came from my necessity to export some values for later analisys and use with other objectives without the need to be connected or doing tedious copy and paste of wanted values. In this article i will describe and exemplify all the process and obstacles faced during my learning process to achieve this. With this article you will be capable of extending it for other ideias you might have.

The ideia...

I need to loop by all available instruments to get some values from them. If we are doing this manually, that will be a tedious and time consuming task. For this article we assume we need the high, open, close, low, range and ATR values from monthly, weekly and daily periods of all available instruments. For this we need to find all available instruments and get each one intrument, get the desired values from it and save them on a file with the format we desire. To achieve this it's not hard... We can get all available pairs in the "Instrument" object enumeration. For this we can use something like this: for each instrument on the "Instrument" object enumeration, get one instrument, process the values of it , after skip to the next one till we don't have any instrument left. The initial possible snipet of code for this is:
Image 2: Possible snipet of code on how to get all available instruments in JForex.

The first obstacle...

Trying to do this literaly as the snipet code above with the needed implementation i find some troubles. I only get the values of the pairs i have open (subscribed) on JForex client, all others, i can't get anything at all. By "open" i don't mean with the chart open, but with the instrument or pair present on the "Instruments" table on the left of JForex client. To pass this problem, we need to subscribe any instrument we want to process. To do this we can subscribe all instruments, all at one time or each one individualy as we need them. The second aproach seems to me to not overload too much as the first one and because of that i choose it. We can do it with the code snipet below.
Image 3: Code snipet with instrument subscription.

Second obstacle...

After trying with this code snipet, other surprise is waiting... The subscription of the instrument may take some time ! And as result, i get errors trying to process the values i want of the instrument because at that time the instrument was not yet correctly subscribed. Ok, we need to give some time to the subscription and process the instrument later when it's correctly subscribed. We can use a predefined time to pause the strategy to give time to the subscription process but the required time for subscription for each pair can vary. Ones can take more time than others to get totally subscribed. We need to find a optimum way to spend the less time possible in pausing the strategy. One possible way is the one on the code snipet below where we verify if the pair is not subscribed yet, then wait a second, give a alert on message tab, try again till the maximum of 11 seconds. If not subscribed after this time, send the output of the error to the message tab.
Image 4: Code snipet giving time to the subscription of the instrument.

Third obstacle...

Oh no! Other surprise... Well, developing is plenty of this i think. The third problem i faced is because on the "Instrument" object enumeration we have some instruments wich are not in use by the Dukascopy servers/data feed and can't be subscribed. Unfortunatly appears that the API don't have any way to get only the "good" instruments (at least i can't find anything related on API javadoc and on the wiki)... But not is this problem that stops me... I find a way to get over this problem, maybe not the most elegant way as i have to determine all "bad" instruments before by try/error and if the available instruments change in future, we need to change the code to reflect this change. I fill a string variable with all the names of "bad instruments" and on each interaction of the loop we first verify if the current instrument is on the "bad list". If yes, we skip it, if not we process it. Code snipet below.
Image 5: Code snipet modified to not allow the subscription of "bad" instruments. Before we use the instrument, we see if it is present on the " bad list". If so, we skip to the next one.

Note: Dukascopy development team will implement a way to get available instruments to trade with IClient.getAvailableInstruments in version 2.7.9 of JForex API.

Getting ready to process the instrument values

As what we pretend is to write lines of text (independently of text or csv format) we only need to use a string to store the text and/or values to write them later to a file. Don't be worried with the lenght or the limit capacity of the "String" object... it can store almost 2 GB of data! Also on the "String" object we can place formating characters as new line for example with "\n" sequence as you can see in one of the code snipets below. In the next snipet of code we have some examples of extracting data from the selected instrument and place it on the string variable using the CSV format we use later to write on the file. For the CSV format we only need to place between each value a "comma". Each line represents a record and we need to finalize the line with the new line formating character to create a new line for next record. On the code snipet after the one we talk before, we see a example with that fills the string with human readable text.
Image 6: This code snipet don't is complete, is only a brief of what you can do. We see the use of "IHistory" bar values, but you can use any available value in JForex, indicator values included. Example for csv format.
Image 7: Same as above, but for human readable text.

Writing to a file

With the string variable where we place the text or instrument values we want to store on a file, we need to write this variable content to a file. In this case, i created a method to do this and call it at the end of each pair processed. The file is created (if not exist) or opened in "append mode" to add the information after each pair processed. This means that the contend of the string is added at the end of the file after the last one inserted. You place a call to this method with the string variable at the end of the "for" loop: "writeToFile(myText);". Code of the method below.
Image 8: Method used to write the content of the string with the content desired to a file.

All the "core" needed for this strategy to run is presented on the code snipets above. Them lacks some code needed on the strategy, and because of that i provide the complete strategy with choice for text and csv format on the forum thread. You can download it here.

The results of the strategy

Below i present you a image with the content of the resulting files that are possible to create using this strategy. When you run the strategy, at the beggining you have the choice for text (default) or csv format.
Image 9: In this image you can see the results of this strategy for both formats possible. The first part as human readable text present in one file generated in one strategy run and below as csv format on other file on other strategy run with different parameters, both generated by same strategy and same content values but different format.

Final words

With this article we learn how to write JForex data we may want to a file in text or in CSV format. In continuation of this ideia, maybe in my next article we will learn how to export the values of JForex we want to a database for later use. We will not use any intermediary file, just place the values directly on a database for later use.

I hope you like this article.

Trade well


Translate to English Show original