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);
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);
}
}