JForexUtils - A collection of useful java classes for JForex - Part2

Welcome to the second part of the JForexUtils documentation.
In case you missed part 1 and want to start from the beginning -> Part 1

In this part we will see how to do pip and amount conversion calculations as well as fetching quotes for an instrument. Finally, we see how to easliy create order parameter instances which are later used for submitting orders to Dukascopy servers.
All following code snippets/pictures can be found as a Java file in ExamplePart2.
All links are listed at the end of this article.

Class CalculationUtil
This class has two aspects: the first are static methods for which you don't need an instance. The second covers calculations for amount conversions between currencies and instruments. These require an instance of CalculationUtil since it relies on live market data.

1)Static pip and amount calculations
[img class= src=]





Line 1: This method scales an amount to lots; one lot of the JForex platform is represented as 0.1. So the result in the example is 0.12345678.
Line 2: Here the signed pip difference of two prices of an instrument is calculated. The result of 1.12887-1.12743 is 14.4 pips. If you swap the prices the result is -14.4 pips.
Line 3: In this example you get the resulting price of 1.2991 when adding 10.4 pips to 1.12887 of EUR/USD. You can also provide negative pips to do a substraction.
Line 4: Here you can scale pips to the price format of an instrument. In this example representing 12.4 pips of EUR/USD price results in 0.00124(for JPY based instruments the result would be 0.0124).

2)Converting amounts; calculating pip values
Here we need an instance of CalculationUtil which you get from the JForexUtil class.This class has factory methods for almost all instances you want. See how you create an CalculationUtil instance and do some calculations with it:
[img class= src=]














Line 1: Creating an instance of CalculationUtil with the help of the factory JForexUtil.
Line 2: Imagine you want to convert 12540EUR to USD with the latest ASK quote on the market. First you provide the amount, second the source currency(which is EUR), third the target currency(which is USD) and fourth the offer side. Depending on the current market quote of EUR/USD you get the converted amount back. In the running example all these results are printed on the console when you run the strategy.
Line 3: Very similar to the second line, but here you convert between instruments. So 12540EUR are converted to GBP to represent the amount for GBP/AUD.
Line 4: Here you calculate one pip value for a given amount, instrument and offer side, converted to the currency you want.
Note: instances of CalculationUtil are thread safe; you can use them everywhere anywhere.

Class InstrumentUtil
With the help of this class you can easily access the latest quotes of ticks/bars for an instrument.
Instances of InstrumentUtil are created with a factory method of the JForexUtil class, see the following example:

1)Create an InstrumentUtil instance and get the latest quotes
[img class= src=]








Line 1: Creating an instance of InstrumentUtil with the help of the factory JForexUtil.
Line 2: Gets the latest tick
Line 3-4: Gets the latest bar for an offer side; you must specifiy for which Period you want the bar
Line 5-6: Gets the latest ask/bid prices
Line 7: Gets the latest spread of the instrument; very convenient method compared to what you'd have to do when working directly with the JForex API!
Note: you see how easy it is to retreive the latest quotes for an instrument. No ugly try-catch blocks and no use of IHistory are needed. There are more public methods which are self-explanatory and you can access them with your favorite IDE.

2)Static methods of InstrumentUtil
In case you don't need quotes there's no need to create an instance of InstrumentUtil. In the next snippet you see some of the static methods:
[img class= src=]





Line 1: Returns the number of digits for a given instrument; all non JPY instrument have 5 digits, all JPY instruments have 3 digits.
Line 2: Gets the instrument string without the slash, so EUR/USD would return "EURUSD". This might be interesting when dealing with log files etc.
Note: all static methods are also available as non-static versions for instances of InstrumentUtil.

Class OrderParams
Remember how hard it is to send an order with the Dukascopy API? You not only have to know which of the many overloads of IEngine.submitOrder you need to apply, but also know the order of all parameters of the method. Take for example this javadoc.
It would be nice if one could specify the parameters of an order in a declarative way and be able to use all of them for later use again.
Class OrderParams provides an easy solution for all these problems in a thread safe way; we jump right into the first snippet.

1)Creation of an OrderParams instance
[img class= src=]





Here we created an orderParams object for an EUR/USD buy market order. You can see the declarative coding nature and how easy it is to read! This is a minimal configuration example, as the instrument, the order command, the order amount and the order label are mandatory parameters.
To finalize the creation you have to call the build() method at the end of each instance creation. In order to help you not to forget about a mandatory parameter ,this build() command cannot be called before all are specified.

In the next example we build an orderParams instance with the same mandatory parameters but now with all possible optional parameters:
[img class= src=]











The main difference to the first example is that you can call build() at any time for optional parameters. For example, you could call build after you've set the stop loss price.
Note: I've just used default values for the parameters since their content does not matter here.

2)Cloning of oder parameters
The next use case is cloning of an orderParams instance. Suppose you want to open a new order which has exactly the same parameters as before but only differs for the amount:
[img class= src=]





As you see, we just take the instance we want to clone from, call clone() on it, set the amount and finalize the new instance again with build(). Of course you can change more or all parameters if you want.
Now that we have seen how easily we can create parameter objects, we'll use them in the next article.
---------------------------------------
This is it for part 2 of the article series. We covered various methods for pip calculations and amount conversions. We've seen how easy it is to fetch quotes for an instrument and create order parameter objects with a declarative style.
In the upcoming part 3 we'll discuss the very central OrderUtil class which handles all order submissions and changes in a very easy way, so stay tuned :-)


Project links:
Translate to English Show original