Use In NetBeans

To start working with JForex SDK in Netbeans do the following:

  1. Download NetBeans IDE with Java SE from netbeans.org

  2. Download the maven project and unpack the zip file.

  3. Open the project:

  4. Locate the Main.java, change in the source code userName and password to the ones of your account and select Run File:

Strategy development in NetBeans

It is much more easier to develop a strategy in some kind of Integrated Development Environment (IDE) than to write the code directly in the code editor of the jForex platform. In this section we will enlist a few advantages of IDE usage. In this sample we are using an open-source IDE platform called NetBeans (version 7.2), but there are many other open-source IDE platforms for Java™. See NetBeans for Java SE to learn more about NB or click here to learn how to install NB. We will show how to create strategies with NB and how to run them in the NB and jForex platform.

Advantages of the NetBeans

There are many advantages for using IDE over the jForex editor. We will enlist the most significant ones:

  • Live Parsing and Hints - the editor marks all errors immediately as one writes the code. So it is very easy to track compile-time errors. Every time an error occurs, NB shows an error icon at the line number where the error happens. If one clicks on the icon, NB provides solutions to correct the error. For example, if one declares that a class implements some interface but does not override methods of that interface, then the error icon is shown near the line number. By clicking on the icon, one can choose how and if to correct the error :

    If one chooses to Implement all abstract methods then NB automatically implements all of the required methods of the interface.

  • Refactoring - the IDE automatically renames, moves, copies, deletes (etc. functions) preferred variables, classes, files, etc. Fore example, if one wants to rename a method in a class, then he needs manually search for all usages of this method in all classes. This can be a very error prone solution even in small projects. NB provides a tool called Refactoring. By renaming (or doing many other operations) a method through refactoring, NB automatically renames the method in all places where it is used.

  • Smart Code Completion - a very useful feature of NB is the automatic code completion. One needs to press only few keys and NB will provide a list of code completion. For example, one can write some reference variable, press a dot and NB will provide a list of all methods this variable can invoke. When browsing the list, the NB shows javadoc (method/class/interface description) for a chosen method.

  • Code Templates - one can choose to use NB's Code Templates, too. This also provides automatic code completion. For example, one writes 'sout' and presses a Tab key. The 'sout' is transformed to 'System.out.println()' code. One can define his own code blocks and shortcuts, too. This feature really fastens a development of code.

  • Go to File/Type/Symbol - one can easy go to a declaration of instance variable/class/symbol. Simply hold Ctrl button a click on the variable. NB will open the java file in which the variable is declared and set the cursor. As a result one can very fast and easy find all of the information needed about the used variable. For example, we are holding Ctrl key and clicking one the IContext reference type. NB opens the file and puts the cursof in a place where the interface is declared.

  • Insert Code - one can easy insert code (e.g. setters and getters for predefined instance variables, constructors, etc.). Simply press Ctrl+Alt+Insert and a list of options appears from which one can choose what a kind of code to insert.

  • Navigation - NB provides a good overlook of projects, packages, files, variables, etc.. So one can easily navigate through a whole tree of elements and invoke many actions for a particular element directly from the tree.

    adv

  • Fully Configurable User Interface - It is possible to adjust NB to very specific needs of an individual. One can adjust and allocate all windows, create specific shortcuts, change look and feel, etc.

  • File History and Compare - NB provides a historical track of all changes made for a document. If one later on needs to recover from an older version of a file or just wants to look at the earlier stage of a code, then he can use a History feature of NB. This shows a graphical/textual comparison of the current and previously saved file. One can compare two different files, too.

  • Wizards - one can use wizards for adding new projects, files, packages, etc. By stepping through a few steps, one can generate a lot of basic code. Otherwise this code should be written manually. For example, press File-New File. A New File wizard opens. Here one can choose the file type and step though the following steps. As a result a new file is generated, the package statement is added (if file is created in a package) and other optional code is added, too.

Create a Strategy in NetBeans

One can create a strategy with NB and run it in jForex platform. Here are described the necessary steps:

  1. Open JForex-SDK project in NB. Look here to see how to do this. We can expand the new project by clicking on the plus sign near the project.

  2. Create a new package. Right-click on the Source Packages (src) node and select New-Java Package.

    net1

    Indicate the name of the new package and press Finish.

    net2

    The new package is added and displayed in the Projects window.

    net3

  3. Create a new java file. Now we're going to write the code for our strategy. At first, we create a new file by right-clicking the newly created package node and choosing New-Java Class. A New Java File wizard is opened. Give a class name and click Finish.

    net4

    The new java file automatically gets opened in the editor.

  4. Write the code for a strategy. Now, when the strategy's java file is opened in the editor, we can write our code. At first, we indicate that our created class will implement the IStrategy interface. When we write that the class implements IStrategy interface, the NB tries to guess what we will write and lists all classes/interfaces related to the written symbols (if it does not, then check the parameters in Tools-Options-Editor-Code Completion) plus it shows the documentation for a chosen class/interface/method:

    net5

    If one chooses the class/interface from drop-down list of code-completion popup window, then the import statement of class/interface is also added. Otherwise one can easy add all required imports or remove unused ones by pressing Ctrl+Shift+I. After declaring the implements, a new error icon appears near the line number

    net6

    This happens because the NB checks that we haven't yet implemented IStrategy's methods. Click on the error icon and choose Implement all abstract methods. NB will implement all required methods. By default, NB provides a method body for all methods which throws an exception - this is done for one who writes the code not to forget to write a real method body. Declare instance variables and instantiate them in the onStart method as shown next:

    net7

    Press Ctrl+Shift+I to add imports. Add a primitive statement that will print a hello message on strategy's startup:

    net8

    Delete the method bodies (throws clauses) for all methods except the onStart method. Now our strategy is completed:

    net9

    Here is a complete java file - MyFirstNBStrategy

  5. Running the strategy in jForex. It is easy to add a new strategy created in NB - simply right-click the Strategies node in jForex platform and choose Open Strategy. All NB java files are stored in the project's directory under the src\package_name\file_name.java path. In this case - ..\MyStrategies\src\com\myCompany\myStrategies\MyFirstNBStrategy.java. After importing the strategy, the code gets opened in jForex editor. Remember an important note - if you edit the code in NB and save the file, the code is altered in jForex editor as well. This works in both directions! So one doesn't need to import the strategy every time he edits the code in NB. Just save the file in NB and the changes will affect jForex, too. For example, if we open the strategy in jForex and later edit the code in NB (and save!) and then again return to jForex editor, the following notification appears:

    net10

    Choose Yes to update the code in jForex editor. Now compile and run the strategy in jForex platform. One can see the output - the message is printed:

    net11

Use External Library

We will create a new project in NB and use this new project as an external library. We will modify our previously created strategy (click here to see how to create strategies in NB) in two different ways. We will add the created project as a dependency:

  1. for JForex-SDK project and run the strategy in NB,
  2. for a strategy so that one will be able to run it in JForex platform.

Create an External Library

We will create an external library as a project and use it in strategy's code later on.

  • Create a new project. Press Ctrl+Shift+N (or choose File-New Project) and choose Maven-Java Application project type. Choose Next and indicate the title of the new project - MyExternalLibrary.

    lib1

  • Right-click the Source Packages node of the new project and choose New-Java Package. We call the new package com.xCompany.strategyGUI.

  • Create a new file in com.xCompany.strategyGUI package by right-clicking the package's node and choose New-Java Class. Name the new class file ShowGUI. The result should look like the following one:

    lib2

  • Write the following code in the ShowGUI class:

package com.xCompany.strategyGUI;

import javax.swing.JDialog;
import javax.swing.JOptionPane;

public class ShowGUI {
    public void showMe(){
        JOptionPane pane = new JOptionPane("Executing onStart method", JOptionPane.INFORMATION_MESSAGE);
        JDialog dialog = pane.createDialog(null, "OnStart Message");
        dialog.setVisible(true);
    }
}
  • Press Ctrl+Shift+I to fix imports.
  • Next, build the project by selecting it in the Projects window and choose Run-Build Project (Project_Name).

The complete code - ShowGUI.java.

Add the Library as a Dependency to JForex-SDK

  • Now we can add our project as a dependance to other projects. From projects panel do a right click on Dependencies - Add depedency - Open projects and select MyExternalLibrary.

    lib3

    lib4

  • The project is now added to the list of libraries. This means, that we can use MyExternalLibrary project's classes in JForex-SDK project.

Use the Library in JForex-SDK

We can run our strategies right in NB. This is possible through JForex-SDK project. Check the following steps:

  • Use the newly created class in JForex-SDK project's MyFirstNBStrategy class. Open the class and add an import of the new class:
import com.xCompany.strategyGUI.ShowGUI;
  • Next, call a showMe method from onStart method. Here is the complete onStart method:
public void onStart(IContext context) throws JFException {
    this.engine = context.getEngine();
    this.console = context.getConsole();
    this.history = context.getHistory();
    this.context = context;        
    console.getOut().println("Greetings from NetBeans!");
    //call the external library's method:
    new ShowGUI().showMe();     
}
  • Find the following lines of code in the Main.java class (JForex-SDK project) and replace the MY_USERNAME and MY_PASSWORD with real ones:
//user name
private static String userName = "MY_USERNAME";
//password
private static String password = "MY_PASSWORD";
  • To run strategies in NB, open the Main.java class file of the JForex-SDK project and create an import of the strategy's class as shown next:
import com.myCompany.myStrategies.MyFirstNBStrategy;
  • Now one can change the call for a strategy in the main method of Main class. Find the following lines (approx. at the very end of main method) of code and alter them so that they look as the following ones:
//start the strategy
LOGGER.info("Starting strategy");
client.startStrategy(new MyFirstNBStrategy()); //replace the MA_Play() with MyFirstNBStrategy()
//now it's running
  • Right-click the Main.java file in Projects window and choose Run File option.

lib5

  • One should see the following popup window indicating that the strategy's onStart method is invoked.

lib6

  • Because we haven't wrote a logic to stop the strategy, we need to stop it manually. One can stop it by clicking the exit icon in the lower right corner of NB window:

lib9

Add the Library as a Dependency for JForex platform

To add a library to a strategy so that it can be used when running the strategy in JForex platform, one must use a @Library annotation and give the full path to library's jar file as an argument for the @Library annotation. We previously created a project and added it as a dependency to JForex-SDK project. This time we will add a concrete jar file as a library to our strategy. After the project is built, NB creates a folder called dist in the project's directory tree. Here are located jar files of the project. We need to specify the full path to this jar file as an argument for @Library annotation. @RequiresFullAccess annotation is also required if one needs to import jar files from location different from a default workspace. We modify the previously created strategy MyFirstNBStrategy so, that it uses the @Library and @RequiresFullAccess annotation:

//..
import com.dukascopy.api.RequiresFullAccess;
import com.xCompany.strategyGUI.ShowGUI;

@RequiresFullAccess
@Library("C:/ExternalLibraries/MyExternalLibrary.jar")
public class MyFirstNBStrategy implements IStrategy{
    private IEngine engine;
    private IConsole console;
//..

Use the Library in JForex Platform

Now we can use the strategy in JForex platform, too. Open the strategy's java file in JForex platform and run the strategy. The result is the same as running it in the JForex-SDK project:

lib10 lib11

The information on this web site is provided only as general information, which may be incomplete or outdated. Click here for full disclaimer.