Multiple strategies within a strategy

Call of a strategy within another strategy is not supported directly, howerver, one can instantiate an IStrategy object within his strategy and on call of any of its methods, "forward" the method's parameters to the correspoding method of the object.

Call inner strategy

Consider adding another strategy class InnerStrategy in the same java file and creating 2 strategy instances in ParallelInnerStrategyCalls. Then at each call of any of ParallelInnerStrategyCalls methods, their parameters get forwarded to InnerStrategy objects.

ParallelInnerStrategyCalls.java

Call strategy from a jar file

In the given example StrategySimpleParallel class holds two instances of SimpleTpSlStrategy as fields. At each call of any of StrategySimpleParallel methods, their parameters get forwarded to SimpleTpSlStrategy objects.

StrategySimpleParallel.java

myLib.jar

Managing multiple strategy instances from a tab/window

Consider a strategy which allows the user to conveniently run and manage multiple strategies from a platform tab. The advantage of this approach is that one can see all parameters of all launched strategy instances and manage the collection of launched strategies - save strategy parameter configurations, add or remove strategies from the list, start all strategies, and change parameters while the concrete strategy is in a running state. If the users changes a parameter in the strategy table, the changes immediately affects the running strategy - the strategy from then on works with the new parameter value.

Running the sample strategy

Consider the following steps:

  • Download MultistrategiesTest.java.

  • Open the strategy in jForex platform, locate the InnerStrategy class in the MultistrategiesTest.java and replace its contents with the contents of your strategy class contents (don't change the InnerStrategy strategy name).

  • Compile and run the strategy.

  • Define Parameters dialog gets opened. By default the Strategy Configuration is opened in a new tab of the jForex platform. If one deselects Show "Strategy Properties" as tab, then the table of strategies gets opened in new window. Start immediately option works only if one indicates Choose property xml file. If selected, then all strategies are started immediately. Choose Run.

    001_MultistrategiesTest_001

  • A new tab (Strategy Configuration) gets opened in the jForex platform

    001_MultistrategiesTest_002

  • Add some additional strategies to the table by pressing Add Strategy button.

  • If one wants to remove some strategies from the list, then he needs to select them by holding the Ctrl button and press Remove Selected button.

    001_MultistrategiesTest_004

  • Modify the settings of the strategies.

    001_MultistrategiesTest_005

  • Press Save button to save the configuration of the strategies as an xml file for later use. Name the file and save it on the local file system.

  • Press Start button to start all strategies.

  • Consider, that the logging information is printed only for these strategies which has a Logg 10Second Bar Values* of true.

  • Change some parameters - e.g. deselect the Logg 10 Second Bar Values check-box. Now the log is not printed. The changes takes place immediately as the table cells are modified.

  • Now close the strategy by right-clicking the strategy's node in jForex and choosing Stop. The Strategies Configuration tab/window is closed automatically.

  • Run the strategy again. At this time, indicate for the saved file in Choose property xml file property and deselect the Show "Strategy Properties" as tab.

    001_MultistrategiesTest_007

  • Press Run. Consider that the previously saved strategies are opened with the saved parameters in a new window. Press Start button to start the strategies.

The source code

Some notes about this example:

  • The code is written to work with a client strategy which uses parameters of the following types of objects and java primitives: Instrument, Enum (e.g. OfferSide), Period, double, boolean, int, String.
  • The code adds instance variables of the client strategy to the table only if the variable is identified with the @Configurable annotation.
  • The table of strategies adds drop-down lists only for Enum and Period property data types.
  • Code does not validate changed data in the table of strategies (e.g. one can easy set Stop-Loss value for -100000).
  • If the client's strategy works with specific instruments, then don't forget to subscribe them before launching the strategy. If one changes the instrument type in the table of strategies, then he needs to check whether he has subscribed for this type of the instrument.
  • Defined instance parameters (specified with @Configurable annotation) of client strategy must be initialized with default values at the declaration time.

Here is the code of the strategy including the client's sample strategy (InnerStrategy):

MultistrategiesTest.java

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