settings applied to all instruments

Custom indicators, trading strategies, data export and recording and more...
mdtrader
Posts: 39
Joined: Sat Oct 27, 2018 4:05 pm
Location: germany
Has thanked: 12 times
Been thanked: 41 times

settings applied to all instruments

Post by mdtrader » Mon Apr 08, 2019 8:47 am

Hi,
I tried to implement some setting parameters and it worked but then I realized that this settings apply to all instruments and not specific to each instrument only, what is wrong in my code?

Code: Select all

    Color lineColor = Color.GREEN;
...
	@Override
	public StrategyPanel[] getCustomSettingsPanels() {
		BookmapSettingsPanel settings = new BookmapSettingsPanel("settings");
		addColorsSettings(settings);
        return new StrategyPanel[] { settings };
	}

	// ------- Start: SweepColor Setting -------------------
    private void addColorsSettings(final BookmapSettingsPanel panel) {
        panel.addSettingsItem("Indicator color:", createColorsConfigItem());
    }

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


Andry API support
Posts: 548
Joined: Mon Jul 09, 2018 11:18 am
Has thanked: 25 times
Been thanked: 85 times

Re: settings applied to all instruments

Post by Andry API support » Mon Apr 08, 2019 2:17 pm

Hi mdtrader,
Do I get you right, you have a bunch of indicators within your module and when you change the color of any, the new color is applied to all of them?
If I don't, please provide more detailed problem description.
I also need to see the full package code (or the full class code if it is contained within a class) to find out what's wrong.

mdtrader
Posts: 39
Joined: Sat Oct 27, 2018 4:05 pm
Location: germany
Has thanked: 12 times
Been thanked: 41 times

Re: settings applied to all instruments

Post by mdtrader » Tue Apr 09, 2019 7:04 am

I've one indicator applied on 2 instruments, say NQ and CL, if I change the color setting in the NQ and switch to the CL, than I'll have the same color here, so the question is, whats the right way to work with the settings panel so each settings are unique for each instrument?

Andry API support
Posts: 548
Joined: Mon Jul 09, 2018 11:18 am
Has thanked: 25 times
Been thanked: 85 times

Re: settings applied to all instruments

Post by Andry API support » Tue Apr 09, 2019 12:32 pm

Hi mdtrader,
Please take a look at the code attached. This is a simplified lastTrade indicator which uses your code for setting Color.
I have tested it on different instruments simultaneously and they never share the same color. They always have their individual color.
I have no clue what might be wrong with your code because I haven't seen it (I've seen only a part). So what I suggest is to let us take a look at your code.
If you have any privacy concern (e.g. you don't want anybody to see it) you can simplify your code so you can show it but the undesirable behavior is preserved.

Code: Select all

package com.bookmap.api.simple.simplified;

import java.awt.Color;
import java.util.function.Consumer;

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.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 Colors: live")
@Layer1ApiVersion(Layer1ApiVersionValue.VERSION2)
public class LastTradeDemoColor implements CustomModule, TradeDataListener, CustomSettingsPanelProvider {
    private Indicator lastTradeIndicator;

    @Override
    public void initialize(String alias, InstrumentInfo info, Api api, InitialState initialState) {
        lastTradeIndicator = api.registerIndicator("Last trade, no history", GraphType.PRIMARY);
        lastTradeIndicator.setColor(lineColor);
    }

    @Override
    public void stop() {
    }

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

    // mdtrader's code here
    Color lineColor = Color.GREEN;

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

    private void addColorsSettings(final BookmapSettingsPanel panel) {
        panel.addSettingsItem("Indicator color:", createColorsConfigItem());
    }

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


mdtrader
Posts: 39
Joined: Sat Oct 27, 2018 4:05 pm
Location: germany
Has thanked: 12 times
Been thanked: 41 times

Re: settings applied to all instruments

Post by mdtrader » Thu Apr 11, 2019 6:27 am

Hi Andrey,
I copied the code into my project and it works, that's very strange because it was the same as before...
But one problem still exists, if I restart Bookmap, than all the settings are back to the default ones and I need to re-set all parameters for each instrument, is there still something I've to do like loading something during the initalisation?

mdtrader
Posts: 39
Joined: Sat Oct 27, 2018 4:05 pm
Location: germany
Has thanked: 12 times
Been thanked: 41 times

Re: settings applied to all instruments

Post by mdtrader » Thu Apr 11, 2019 8:34 am

Hi Andrey,
unfortunately I need to come back with the same problem, I wrote it worked, but this was only yesterday after inkluding the new jar file, today after starting bookmab I see the same problem, I change the color in one instrument and it applied also to the second instrument (the problem with not saving after bookmap closed still exist). If I chanfe it in the second instrument then it applies to the first one, it's like the setting is "general".
I've now removed the indicator and added it new (without closing bookmap) and vola, the settings are saved different for each instrument, but I think if I close Bookmap (can't do it right now) that I'll end up with the same problem again

Andry API support
Posts: 548
Joined: Mon Jul 09, 2018 11:18 am
Has thanked: 25 times
Been thanked: 85 times

Re: settings applied to all instruments

Post by Andry API support » Thu Apr 11, 2019 9:54 am

Hi mdtrader,
what Bookmap build are you running? (Help -> About -> Version 7.0.0 build ???)
I'll give some ideas about saving settings later roday.

Andry API support
Posts: 548
Joined: Mon Jul 09, 2018 11:18 am
Has thanked: 25 times
Been thanked: 85 times

Re: settings applied to all instruments

Post by Andry API support » Thu Apr 11, 2019 1:30 pm

The easiest way to save your settings is to use the @Parameter annotation. Take a look at the code below.

Code: Select all

package com.bookmap.api.simple.simplified;

import java.awt.Color;
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.simplified.Api;
import velox.api.layer1.simplified.CustomModule;
import velox.api.layer1.simplified.Indicator;
import velox.api.layer1.simplified.InitialState;
import velox.api.layer1.simplified.Parameter;
import velox.api.layer1.simplified.TradeDataListener;

@Layer1SimpleAttachable
@Layer1StrategyName("Last trade Colors @Parameter: live")
@Layer1ApiVersion(Layer1ApiVersionValue.VERSION2)
public class LastTradeDemoColorParameter implements CustomModule, TradeDataListener {
    private Indicator lastTradeIndicator;
    
    @Parameter(name = "lineColor")
    Color lineColor = Color.GREEN;

    @Override
    public void initialize(String alias, InstrumentInfo info, Api api, InitialState initialState) {
        lastTradeIndicator = api.registerIndicator("Last trade, no history", GraphType.PRIMARY);
        lastTradeIndicator.setColor(lineColor);
    }

    @Override
    public void stop() {
    }

    @Override
    public void onTrade(double price, int size, TradeInfo tradeInfo) {
        lastTradeIndicator.addPoint(price);
    }
}
It is as functional as the one I gave you yesterday, but it is shorter. In a few words: annotated field gets shown in the GUI and its value gets saved. Attention: only Numeric Objects (e.g. Integer, Double...) or String or Boolean or Color are accepted. You can also choose whether to reload your indicator after resetting it's value from the GUI. Use reloadOnChange annotation parameter for that.
Warning: new Color gets applied only after the instrument is reloaded. If you write code like this

Code: Select all

@Parameter(name = "lineColor", reloadOnChange = false)
Color lineColor = Color.GREEN;
you assign a new Color value, but you'll see it at the screen only after the indicator gets reloaded (like you reload it manually or change another annotated field value where 'reloadOnChange = true')

Andry API support
Posts: 548
Joined: Mon Jul 09, 2018 11:18 am
Has thanked: 25 times
Been thanked: 85 times

Re: settings applied to all instruments

Post by Andry API support » Thu Apr 11, 2019 2:40 pm

Another way to make it is to save your settings using your custom settings class (any class containing settings fields annotated with @StrategySettingsVersion). You get the saved settings from Api object. What you also need is to store the Api object and call its setSettings(obj) method to save the settings when unloading the module.

Code: Select all

package com.bookmap.api.simple.simplified;

import java.awt.Color;
import java.util.function.Consumer;

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;
    }
    
    private Settings settings;
    private Indicator lastTradeIndicator;
    private Api api;

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

    // mdtrader's code here
    Color lineColor = Color.GREEN;

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

    private void addColorsSettings(final BookmapSettingsPanel panel) {
        panel.addSettingsItem("Indicator color:", createColorsConfigItem());
    }

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

mdtrader
Posts: 39
Joined: Sat Oct 27, 2018 4:05 pm
Location: germany
Has thanked: 12 times
Been thanked: 41 times

Re: settings applied to all instruments

Post by mdtrader » Sat Apr 13, 2019 1:00 pm

The second example works fine but the first example with the @Parameter but end up with this error:

java.lang.NullPointerException
at velox.api.layer1.simplified.InstanceUtils.getColorPanel(InstanceUtils.java:182)
at velox.api.layer1.simplified.SimplifiedL1ApiLoader.getCustomGuiFor(SimplifiedL1ApiLoader.java:1768)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at velox.bookmap.dd.invoke(SourceFile:60)
at com.sun.proxy.$Proxy40.getCustomGuiFor(Unknown Source)
at velox.bookmap.Dy.a(SourceFile:343)
at velox.bookmap.Lk.a(SourceFile:40)
at velox.bookmap.Dy.a(SourceFile:344)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.JToggleButton$ToggleButtonModel.setPressed(JToggleButton.java:308)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:290)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at velox.bookmap.NC.dispatchEvent(SourceFile:608)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Post Reply