Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Starting the application

Simon W edited this page Dec 22, 2017 · 5 revisions

(in progress...)
Take a look at the program structure for further informations.

  • This application uses JavaFX. That means one has to extend from the Application class and has to call the static Application.launch(AllpicationExtendingClass.class) function.
  • At this point the AbstractProgram class should facilitate the JavaFx handling. For example you could let your programm extend the AbstractProgram class and override the createIndicatorBox() function:
public class ApplicationExtendingClass extends AbstractProgram{

    @Override
    public ChartIndicatorBox createIndicatorBox() {
    // create ChartIndicatorBox, add indicators, etc...
    return MyChartIndicatorBox
    }
}

In the next step you would call the Application.launch(AllpicationExtendingClass.class) function to start the application. That can happen inside the class that extends AbstractProgramm or from another class like:

public class Example {

    /**
     * launch the Application that extends the AbstractProgram
     * @param args command line arguments
     */
    public static void main(String[] args){
        Application.launch(ApplicationExtendingClass.class); // starting the application
    }

Example implementation of createIndicatorBox(TimeSeries series) function:

public class ExampleProgramm extends AbstractProgramm {

    /**
     * This method can be overwritten to get custom {@link ChartIndicatorBox} with custom {@link Indicator indicators},
     * {@link Strategy strategies} and {@link TradingRecord}
     * @param series The corresponding {@link TimeSeries} for the chart
     * @return a {@link ChartIndicatorBox} for the Chart that is used in the {@link #start(Stage) start(Stage) function}
     * of this class
     */
    @Override
    public ChartIndicatorBox createIndicatorBox() {
        TimeSeries series = Loader.getMinuteTimeSeries(file, "fb");
        // define indicators
        ClosePriceIndicator cp = new ClosePriceIndicator(series);
        EMAIndicator ema20 = new EMAIndicator(cp,20);
        EMAIndicator ema60 = new EMAIndicator(cp, 60);

        // build a strategy
        Rule entry = new CrossedUpIndicatorRule(ema20,ema60);
        Rule exit = new CrossedDownIndicatorRule(ema20, ema60);
        BaseStrategy strategyLong = new BaseStrategy(entry,exit);
        BaseStrategy strategyShort = new BaseStrategy(exit, entry);

        // initialize and add your individual indicators to chart
        ChartIndicatorBox chartIndicatorBox = new ChartIndicatorBox(series);
        //chartIndicatorBox.initAllIndicators(); // add all ta4j indicators from properties file to the box/menu

        XYLineAndShapeRenderer emaShortRenderer = new XYLineAndShapeRenderer(); // specify how the lines should be rendered
        emaShortRenderer.setSeriesShape(0, TaShape.NONE.getShape());
        emaShortRenderer.setSeriesPaint(0,Color.RED);

        XYLineAndShapeRenderer emaLongRenderer = new XYLineAndShapeRenderer();
        emaLongRenderer.setSeriesShape(0,TaShape.NONE.getShape());
        emaLongRenderer.setSeriesPaint(0,Color.GREEN);

        chartIndicatorBox.addIndicator("ema1",ema20, "myEMA Short (20)",emaShortRenderer, false, TaCategory.CUSTOM);
        chartIndicatorBox.addIndicator("ema2",ema60, "myEMA Long (60)",emaLongRenderer, false, TaCategory.CUSTOM);

        // or add your whole strategy as
        List<Indicator> strategyIndicators = new ArrayList<>();
        List<String> names = new ArrayList<>();
        strategyIndicators.add(ema20);
        strategyIndicators.add(ema60);
        names.add("myEma (20)");
        names.add("myEma (60)");
        XYLineAndShapeRenderer strategieRenderer = new XYLineAndShapeRenderer();
        strategieRenderer.setSeriesShape(0,TaShape.NONE.getShape()); // specify how the both lines should be rendered
        strategieRenderer.setSeriesPaint(0,Color.RED);
        strategieRenderer.setSeriesShape(1,TaShape.NONE.getShape());
        strategieRenderer.setSeriesPaint(1,Color.GREEN);
        chartIndicatorBox.addIndicator("Strategy1",strategyIndicators, names,"my Strategy Ema Short/Long",
                strategieRenderer,false, TaCategory.STRATEGY);

        // run the strategies and add strategies
        TimeSeriesManager manager = new TimeSeriesManager(series);
        TradingRecord record = manager.run(strategyLong);
        TradingRecord record2 = manager.run(strategyShort, Order.OrderType.SELL);
        chartIndicatorBox.addTradingRecord("My Record Long", record);
        chartIndicatorBox.addTradingRecord("My Record Short", record2);

        return chartIndicatorBox;
    }
}
Clone this wiki locally