Code Editor
This manual explains how to create, open, and edit the source code of a strategy, indicator, or plugin using JForex4's built-in Code Editor, and how to compile and save your changes.
Renamed: This tool was previously called the "Strategy Editor." It is now called the Code Editor, since it is used to edit not only Strategies, but also Indicators and Plugins.
1. Open the Editor
You can reach the Code Editor in two ways:
- Create new code: In the Navigator panel (bottom-left), right-click the relevant node — Strategies, Indicators, Plugins (or their respective Custom sub-nodes) — and select New Strategy, New Indicator, or New Plugin accordingly. The platform generates a new
.javafile with the appropriate skeleton and opens it directly in the editor. - Edit existing code: Right-click a strategy, indicator, or plugin in the Navigator or in its corresponding panel and select Edit. This opens its source (
.java) file in the editor. If only a compiled.jfxfile is present (no source), editing isn't possible — you'd need the original.javafile.
Note: New files are saved by default to JCloud. If you save the file under a custom name, remember to also rename the Java class name inside the file to match — the class name and file name must agree for compilation to succeed.
2. Understand the Code Skeleton
The skeleton generated depends on what you chose to create:
- New Strategy generates a class implementing
IStrategy. - New Indicator generates a class implementing
IIndicator. - New Plugin generates a class implementing
IPlugin.
The rest of this manual focuses on strategy editing as the primary example, but the same editor workflow (open, edit, compile, save) applies to Indicators and Plugins — only the way each is launched afterward differs (see Section 6).
Every strategy is a Java class implementing IStrategy, with a minimum set of methods required to compile:
package jforex;
import com.dukascopy.api.*;
public class Strategy1 implements IStrategy {
public void onStart(IContext context) throws JFException {}
public void onAccount(IAccount account) throws JFException {}
public void onMessage(IMessage message) throws JFException {}
public void onStop() throws JFException {}
public void onTick(Instrument instr, ITick tick) throws JFException {}
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {}
}
| Method | Purpose |
|---|---|
onStart |
Runs once when the strategy launches — initialization, subscribing to instruments, storing the IContext/IConsole. |
onTick |
Runs on every incoming tick for subscribed instruments. |
onBar |
Runs whenever a new bar closes for subscribed instruments/periods. |
onAccount |
Runs on account state changes (balance, equity, margin). |
onMessage |
Runs on order/position lifecycle events (fills, rejections, etc.). |
onStop |
Runs once when the strategy is stopped — cleanup logic. |
Indicators and Plugins have their own required interface methods (e.g.
calculate()forIIndicator). The Code Editor generates the correct method stubs automatically based on which "New ..." option you selected in step 1.
3. Edit the Code
Work directly in the editor pane, adding your logic inside the relevant lifecycle methods. For example, to print incoming ticks to the log in a strategy:
package jforex;
import com.dukascopy.api.*;
public class Strategy1 implements IStrategy {
private IConsole con = null;
public void onStart(IContext context) throws JFException {
// store the console for printing to the Messages tab
this.con = context.getConsole();
}
public void onAccount(IAccount account) throws JFException {}
public void onMessage(IMessage message) throws JFException {}
public void onStop() throws JFException {}
public void onTick(Instrument instr, ITick tick) throws JFException {
con.getOut().println(instr + " " + tick.getAsk() + "/" + tick.getBid());
}
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {}
}
Common edits at this stage include:
- Adding
@Configurablepublic fields so parameters are exposed in the Define Parameters dialog when running the strategy, indicator, or plugin. - Adding order logic (market/conditional orders, stop loss/take profit) inside
onTick,onBar, oronStart(strategies). - Adding custom indicator calculations or references to built-in indicators.
Tip: Custom indicator classes used by a strategy must be defined within the same file if you intend to run the strategy remotely — the remote environment cannot load separate local class files. This also applies to helper classes used by Indicators or Plugins.
4. Compile Your Changes
After editing, compile the file using either:
- The Compile button in the Code Editor toolbar, or
- The keyboard shortcut F5, or
- Right-clicking the strategy, indicator, or plugin in the corresponding Navigator list and selecting Compile.
Compilation output appears in the Messages tab:
Compiling... OK— the code compiled successfully and a matching.jfxfile is saved alongside the.javasource.- Error details — if compilation fails, the Messages tab lists the specific errors (line numbers, syntax issues, etc.) so you can locate and fix them in the editor.
5. Save Your Work
- Standard save (Ctrl+S or the Save toolbar button) writes your changes to the
.javasource file. - If you Save As under a new file name, open the file afterward and rename the
public classdeclaration to match the new file name — Java requires the public class name and file name to be identical.
6. Run the Edited Code
How you launch your edited code once it's compiled depends on what type of artifact you edited — Strategy, Indicator, or Plugin each has a different launch mechanism in JForex4:
-
Strategies are launched via Local run or Remote run:
- Left-click the strategy in the Navigator, or select it in the Strategies panel.
- Choose Local run or Remote run (see mode differences below).
- Configure parameters in the Define Parameters dialog if prompted, then click Run.
Mode Notes for edited code Local run Full access to local files, other Java classes, and charts — convenient while actively debugging. Remote run Runs on the Dukascopy server. No file system, no chart access ( IContext.getChart()returnsnull); all instruments must be subscribed explicitly inonStart, and custom indicators must be self-contained in the strategy file.Only Strategies support Remote run on the Dukascopy server. Indicators and Plugins do not have a remote execution mode.
-
Indicators are not "run" the way strategies are — instead, they are added to a chart. Select the compiled indicator in the Navigator or Indicators panel and add it to an open chart (e.g., via drag-and-drop, double-click, or the chart's indicator menu) to see it calculate and plot against live or historical price data. Configure its
@Configurableparameters in the indicator's settings dialog on the chart. -
Plugins are launched inside the platform itself, rather than attached to a chart or run remotely. Select the compiled plugin in the Navigator or Plugins panel and launch it directly; it runs within the JForex4 application, integrating with the platform UI or workspace as designed.
Best Practices for Editing
- Use an external IDE for serious development. JForex4's built-in Code Editor is convenient for quick tweaks, but an IDE (Eclipse, IntelliJ IDEA, NetBeans) offers live error-checking, auto-completion, and refactoring tools that catch mistakes before you even try to compile.
- Recompile after every meaningful change and check the Messages tab — don't assume a save succeeded without a compile pass.
- Test in a demo account first. After editing strategy logic that affects order placement or risk parameters, always back-test (via the Historical Tester) and demo-test before running live.
- Keep a backup of working versions before major edits, since the editor overwrites the
.javafile directly on save. - Match file name and class name any time you rename or duplicate a strategy, indicator, or plugin file, to avoid compilation errors.