API example: Launching a custom window

Custom indicators, trading strategies, data export and recording and more...
Serg
Posts: 113
Joined: Mon Jun 11, 2018 12:40 pm
Has thanked: 15 times
Been thanked: 35 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 12572 times

Tags:

agan1337
Posts: 10
Joined: Tue Dec 10, 2019 9:31 pm
Been thanked: 1 time

Re: API example: Launching a custom window

Post by agan1337 » Wed Jun 03, 2020 5:07 pm

Hello Serg,

Works great !

Thank you very much !!!!

I debug and insert .java to simple-demo-master

No problem at all

Question:  Is it good idea to use it to see output ?
I try see output in LOG , but not successful

Thank you again !

AG

Svyatoslav
Site Admin
Posts: 278
Joined: Mon Jun 11, 2018 11:44 am
Has thanked: 2 times
Been thanked: 31 times

Re: API example: Launching a custom window

Post by Svyatoslav » Thu Jun 04, 2020 9:14 am

Hi,

> Is it good idea to use it to see output ?
Depending on the amount of output you might have to do some optimizations, but if there is not too much of it and it's not updated too often - sure.

> I try see output in LOG , but not successful
Which log level did you use? Out of the box everything below "info" is suppressed. You can either use info or above or you can change that, for which you need to edit bookmap config file (while bookmap is closed) and add
log_level:2,
or
log_level:1,
into "general" section. Just make sure to keep JSON structure valid. 2 is for "debug" and 1 is for "trace" log level. Trace will sometimes produce huge amount of log records, potentially degrading performance.
If done correctly log level will change on start. If JSON structure is corrupted file will be reset, so maybe make a copy.

Post Reply