Strategy Parameters
@Configurable annotation can be used in a strategy for it to use user defined parameters. For a strategy field to appear among the strategy's parameters, it has to be public and has to be preceded by a @Configurable annotation. The parameters are used both working with live data and back-testing with Historical Tester. They are shown in a strategy parameters dialog before the strategy start. Additionally, the parameters are used in Optimization.
Parameter properties
The user not only can assign a parameter name that appears besides its value in GUI, but also set read-only condition with readOnly attribute, set increment size with stepSize attribute, set tooltip text with description attribute and others. Consider the following example which shows the features of the @Configurable annotation.
@Configurable("Instrument 1")
public Instrument instrument = Instrument.EURUSD;
@Configurable(value ="Instrument 2", description = "an obligatory instrument", obligatory = true)
public Instrument instrument2;
@Configurable(value = "Period", readOnly = true, description = "This period can not be edited")
public Period selectedPeriod = Period.ONE_MIN;
@Configurable(value = "Amount" , stepSize = 0.9, description = "Amount with a custom step size")
public double amount = 0.02;
Supported parameter types
Depending on the parameter type it gets represented differently in the parameters dialog. The following parameter types are supported:
Parameter type | Applicable Java types | Representation | Remote mode differences |
---|---|---|---|
Number | int, double, short, long, Integer, Double, Short, Long | Number field with a modifiable step size | |
Boolean | boolean, Boolean | Checkbox | |
String | String | Text field | |
File | java.util.File | Text field with a path and a file chooser | |
Date | java.util.Calendar, java.util.Date, long and Long with Configurable.datetimeAsLong = true | Date picker | |
Color | java.util.Color | Color picker | |
Constants | any enum or Enum or a class containing self-typed public static final fields | Single-selection combobox | User-defined constants via Configurable.options |
Collection of constants | java.util.Collection of any enum or Enum or a class containing self-typed public static final fields | Multi-selection dialog | User-defined constants are not allowed |
IFeedDescriptor | IFeedDescriptor implementations from com.dukascopy.api.feed.util | Data feed chooser dialog |
Usage of all types
Consider an example strategy which demonstrates the usage of each of the parameter types:
@Configurable(value = "int param", stepSize = 3)
public int intParam = 1;
@Configurable(value = "double param", stepSize = 0.5)
public double doubleParam = 0.5;
@Configurable("bool param")
public boolean boolParam = true;
@Configurable("text param")
public String textParam = "some text";
@Configurable("")
public File file = new File(".");
@Configurable(value="current time", description="default is current time")
public Calendar currentTime = Calendar.getInstance();
@Configurable("")
public Color color = new Color(100, 100, 100);
@Configurable("instrument (enum)")
public Instrument instrument = Instrument.EURUSD;
@Configurable("")
public Set<Instrument> instruments = new HashSet<Instrument>(
Arrays.asList(new Instrument[] {Instrument.EURUSD, Instrument.AUDCAD})
);
@Configurable("")
public IFeedDescriptor renkoFeedDescriptor = new RenkoFeedDescriptor(Instrument.EURUSD, PriceRange.TWO_PIPS, OfferSide.ASK);
Usage of date and time
In order to set a particular date, one needs to do it within a static block, since there are no Calendar constructors for this and when using SimpleDateFormat.parse, the ParseException needs to get handled.
private static Calendar myCalendar;
static {
myCalendar = Calendar.getInstance();
myCalendar.set(2012, Calendar.JULY, 17, 14, 30, 00);
}
@Configurable(value="particular time", description="17th july 14:30")
public Calendar particularTime = myCalendar;
private static Calendar calTodayAt5am;
static {
calTodayAt5am = Calendar.getInstance();
calTodayAt5am.set(Calendar.HOUR_OF_DAY, 5);
calTodayAt5am.set(Calendar.MINUTE, 0);
calTodayAt5am.set(Calendar.SECOND, 0);
}
@Configurable(value="time in millis", description="default is today at 5am", datetimeAsLong=true)
public long timeInMillis = calTodayAt5am.getTimeInMillis();
Usage of custom enumeration
Besides using JForex-API enums like Instrument, OfferSide and Filter, one can also define in strategy an enum of his own, for instance:
enum Mode {
BUY,
SELL,
NONE
}
@Configurable("mode (enum param)")
public Mode mode = Mode.BUY;
Alternatively one can use a class with self-typed constants (available with JForex-API 2.8.1)
static class Person {
public static final Person FOO = new Person("foo");
public static final Person BAR = new Person("bar");
public final String name;
public Person(String name){
this.name = name;
}
@Override
public String toString(){
return name;
}
}
@Configurable("")
public Person person;
@Configurable("")
public List<Person> persons;
Constants for Remote mode
By using the Configurable.options field one can define string constant alternatives from which thereafter he can load an enum value. Consider a strategy which does that:
private static final String BULLISH = "BULLISH";
private static final String BEARISH = "BEARISH";
enum Mode{
BULLISH(ConfigOptionsEnum.BULLISH),
BEARISH(ConfigOptionsEnum.BEARISH);
private final String name;
private Mode(String name){
this.name = name;
}
private static Mode fromString(String name){
for(Mode mode : Mode.values()){
if(mode.name.equals(name)){
return mode;
}
}
return null;
}
}
@Configurable(value = "mode1", options = { BULLISH, BEARISH})
public String mode1 = BULLISH;
@Override
public void onStart(IContext context) throws JFException {
Mode mode = Mode.fromString(mode1);
context.getConsole().getOut().println("chosen mode: " + mode);
}