API example: Launching a custom window

Custom indicators, trading strategies, data export and recording and more...
Serg
Posts: 106
Joined: Mon Jun 11, 2018 12:40 pm
Has thanked: 17 times
Been thanked: 28 times

API example: Launching a custom window

Post by Serg » Mon Dec 17, 2018 5:15 pm

This is a demonstration of an ability to create entirely custom window (not only the settings panel). A use case for this could be, for instance, creating a footprint chart and rendering / updating it from a custom API module (the addon).


In this example our custom window displays only the last traded price as following:

Code: Select all

        labelSide.setText(isBuy ? "BUY " : "SELL");
        labelSize.setText("" + size);
        labelPrice.setText("" + price);
This and any more advance rendering on the custom window requires standard Java knowledge, unrelated to Bookmap API. The addon also creates a custom settings panel, which in this simple case contains a button that launches the custom window. Also, in this case the addon needs to subscribe only on trades data. Market depth and other available functionality can be found in API documentation.

Here is the entire code of the addon:

Code: Select all

@Layer1SimpleAttachable
@Layer1StrategyName("Example Custom Window")
@Layer1ApiVersion(Layer1ApiVersionValue.VERSION1)
public class ExampleCustomWindow implements CustomModule, TradeDataListener, CustomSettingsPanelProvider {

    private JFrame frame;
    private JLabel labelSide;
    private JLabel labelSize;
    private JLabel labelPrice;

    private double pips;

    @Override
    public void initialize(String alias, InstrumentInfo info, Api api, InitialState initialState) {
        pips = info.pips;

        frame = new JFrame("Custom API window for " + alias);
        frame.setMinimumSize(new Dimension(400, 200));
        frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

        labelSide = new JLabel("");
        labelPrice = new JLabel("");
        labelSize = new JLabel("");
        updateLabels(true, 0, Double.NaN);

        JPanel panel = new JPanel();
        panel.add(new JLabel("Last trade: "));
        panel.add(labelSide);
        panel.add(new JLabel("  "));
        panel.add(labelSize);
        panel.add(new JLabel(" at "));
        panel.add(labelPrice);

        frame.add(panel);
        frame.pack();
    }

    @Override
    public void stop() {
        frame.dispose();
    }

    @Override
    public void onTrade(double price, int size, TradeInfo tradeInfo) {
        if (size != 0) {
            if (frame.isVisible()) {
                updateLabels(tradeInfo.isBidAggressor, size, pips * price);
            }
        }
    }

    @Override
    public StrategyPanel[] getCustomSettingsPanels() {
        StrategyPanel panel = new StrategyPanel("Custom setttings panel");
        JButton button = new JButton("Launch custom window");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                frame.setVisible(true);
            }
        });
        panel.add(button);
        return new StrategyPanel[] { panel };
    }

    private void updateLabels(boolean isBuy, int size, double price) {
        labelSide.setText(isBuy ? "BUY " : "SELL");
        labelSize.setText("" + size);
        labelPrice.setText("" + price);
    }
}
And here is the resulting view:

custom-window.png
custom-window.png (152.87 KiB) Viewed 887 times

Tags: