BookmapSettingsPanel - String parameters

Custom indicators, trading strategies, data export and recording and more...
mcpandre
Posts: 3
Joined: Tue May 12, 2020 5:28 am
Has thanked: 1 time

BookmapSettingsPanel - String parameters

Post by mcpandre » Fri May 07, 2021 6:29 am

Hi guys, sorry for the silly question, but I am getting crazy with this...I understood how to manage different parameters for my indicators but when it comes to String I cannot make it work. If the parameter is a long, int, double, everything works, if it is a String it does not.

I mean, I can catch the event but if I choose the option "TEXT" when I open again the module the Combobox still shows "STRING". Can anyone please help me?

Thanks!

Andrea

Code: Select all

package com.bookmap.api.mcpandre.indicators;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.function.Consumer;

import javax.swing.JComboBox;

import com.bookmap.api.simple.demo.utils.gui.BookmapSettingsPanel;

import velox.api.layer1.annotations.Layer1ApiVersion;
import velox.api.layer1.annotations.Layer1ApiVersionValue;
import velox.api.layer1.annotations.Layer1SimpleAttachable;
import velox.api.layer1.annotations.Layer1StrategyName;
import velox.api.layer1.data.InstrumentInfo;
import velox.api.layer1.data.TradeInfo;
import velox.api.layer1.messages.indicators.Layer1ApiUserMessageModifyIndicator.GraphType;
import velox.api.layer1.settings.StrategySettingsVersion;
import velox.api.layer1.simplified.Api;
import velox.api.layer1.simplified.CustomModule;
import velox.api.layer1.simplified.CustomSettingsPanelProvider;
import velox.api.layer1.simplified.Indicator;
import velox.api.layer1.simplified.InitialState;
import velox.api.layer1.simplified.TradeDataListener;
import velox.gui.StrategyPanel;
import velox.gui.colors.ColorsConfigItem;

@Layer1SimpleAttachable
@Layer1StrategyName("Last trade Color Settings: live")
@Layer1ApiVersion(Layer1ApiVersionValue.VERSION2)
public class LastTradeDemoColorSettings implements CustomModule, TradeDataListener, CustomSettingsPanelProvider {

    @StrategySettingsVersion(currentVersion = 1, compatibleVersions = {})
    public static class Settings {
        public Color lineColor = Color.GREEN;
        public String des = "WORD";
        public long di = 1;
    }
    
    private Settings settings;
    private Indicator lastTradeIndicator;
    private Api api;
    protected long interval;
    Color lineColor = Color.GREEN;

    @Override
    public void initialize(String alias, InstrumentInfo info, Api api, InitialState initialState) {
        this.api = api;
        settings = api.getSettings(Settings.class);
        lineColor = settings.lineColor;

        lastTradeIndicator = api.registerIndicator("Last trade, no history", GraphType.PRIMARY);
        lastTradeIndicator.setColor(lineColor);
    }

    @Override
    public void stop() {
        settings.lineColor = lineColor;
        api.setSettings(settings);
    }

    @Override
    public void onTrade(double price, int size, TradeInfo tradeInfo) {
        lastTradeIndicator.addPoint(price);
    }
   

    @Override
    public StrategyPanel[] getCustomSettingsPanels() {
        BookmapSettingsPanel settings = new BookmapSettingsPanel("settings");
        addColorsSettings(settings);
        addVolumeTypeSettings(settings);
        addIntervalSettings(settings);
        return new StrategyPanel[] { settings };
    }

    private void addColorsSettings(final BookmapSettingsPanel panel) {
        panel.addSettingsItem("Indicator color:", createColorsConfigItem());
    }
    
    private void addVolumeTypeSettings(final BookmapSettingsPanel panel) {
        JComboBox<String> c = new JComboBox<>(new String[] {"STRING","TEXT","WORD"});
        c.setEditable(false);
        c.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
            	String s = (String)c.getSelectedItem();
            	c.setSelectedItem(s);
            }
        });
        panel.addSettingsItem("String Type:", c);
    }
    
    private void addIntervalSettings(final BookmapSettingsPanel panel) {
        JComboBox<Integer> c = new JComboBox<>(
                new Integer[] { 1, 2, 3, 5, 10, 15, 30, 60, 120, 180, 300, 600, 900, 1800, 3600 });
        int selected = (int) (interval / 1_000_000_000L);
        c.setSelectedItem(selected);
        c.setEditable(false);
        c.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                long newInterval = 1_000_000_000L * (int) c.getSelectedItem();
                if (newInterval != interval) {
                    interval = newInterval;
                    c.setSelectedItem(newInterval);
                }
            }
        });
        panel.addSettingsItem("Time interval [seconds]:", c);
    }

    private ColorsConfigItem createColorsConfigItem() {
        Consumer<Color> c = new Consumer<Color>() {
            @Override
            public void accept(Color color) {
                lineColor = color;
                lastTradeIndicator.setColor(color);
            }
        };
        return new ColorsConfigItem(lineColor, lineColor, c);
    }
}




 

AndreyR
Posts: 409
Joined: Mon Jul 09, 2018 11:18 am
Has thanked: 17 times
Been thanked: 70 times

Re: BookmapSettingsPanel - String parameters

Post by AndreyR » Fri May 07, 2021 11:36 am

Hi! Before we correct you code let us first recall the stages of using module settings (see my comments after //):
1) getting saved settings (you have it)

Code: Select all

    @Override
    public void initialize(String alias, InstrumentInfo info, Api api, InitialState initialState) {
        this.api = api;
        settings = api.getSettings(Settings.class);// <- this one
2) using settings throughout your code (to be discussed)
3) saving settings (you also have it)

Code: Select all

    @Override
    public void stop() {
        settings.lineColor = lineColor;
        api.setSettings(settings);// <- this one

For stage 2, you may store new selected values in the settings object OR (your case) you may use temporary variables

Code: Select all

    protected long interval;
    Color lineColor = Color.GREEN;
which later are saved to settings when stopping the module.

Code: Select all

    @Override
    public void stop() {
        settings.lineColor = lineColor;// <- line color value written into settings
        api.setSettings(settings);
In your case, you just missed a temporary variable for the String value and (I am not sure if it is done on purpose) only color is saved to settings when stopping the module. I would recommend store actual values in the settings object. The example for String value:

Code: Select all

    private void addVolumeTypeSettings(final BookmapSettingsPanel panel) {
        JComboBox<String> c = new JComboBox<>(new String[] {"STRING","TEXT","WORD"});
        c.setEditable(false);
        c.setSelectedItem(settings.des);// <- setting selected value from your settings
        c.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
            	settings.des = (String)c.getSelectedItem();// <- writing selected value to your settings
            	c.setSelectedItem(settings.des);
            }

Post Reply