PercentSliderDescriptor - How to retrieve value?

sunnywalia

Member
Joined
Dec 6, 2021
Posts
8
Likes
2
Hi all. New to Java here and really struggling with the PercentSliderDescriptor class. I've created a PercentSliderDescriptor object and shows up fine in the settings dialogue but I can't figure out how to get the value held by it. For now I'm using the debug line below, but I've tried several different things and haven't had any luck. What am I doing wrong?

Creation of object:
inputs.addRow(new PercentSliderDescriptor(Inputs.INPUT, "Percentage",.10,.10));

Attempt to call value:
debug("Percentage Value " + getSettings().getDouble(Inputs.INPUT));
 

TradersCustom

Member
Joined
Sep 8, 2022
Posts
12
Likes
6
That's the correct way to do it, you shouldn't be having any problems (provided it's compiling).

What do you mean by 'haven't had any luck'? Is there an exception, or is the value just appearing as null/NaN in yourStudy Log? Do you see no message at all? Is your Study Log filtering out 'debug statements' by accident (Log Type dropdown)?

Are you using 'Inputs.INPUT' anywhere else in your code by accident (ie. using it as the string reference for another setting descriptor - this would be my first guess)? If you can post the entirety of your 'initialize' function here I can help you solve it.
 

sunnywalia

Member
Joined
Dec 6, 2021
Posts
8
Likes
2
Wow, thanks for the quick response! I'm just getting "Percentage Value null" in the Study log. It's set to 'All' on the Study Log filter. I added a RuntimeDescriptor Label and can see the value populating on the study label. Could just be something weird with the debug/Study Log output. Was trying to make sure I was calling this properly so when I perform calculations it's correct. Here is my initialize function:

public void initialize(Defaults defaults)
{
SettingsDescriptor sd = new SettingsDescriptor();
setSettingsDescriptor(sd);
SettingTab tab = new SettingTab("General");
sd.addTab(tab);

SettingGroup inputs = new SettingGroup("Inputs");
inputs.addRow(new PercentSliderDescriptor(Inputs.INPUT, "Percentage",.10,.10));
tab.addGroup(inputs);

SettingGroup colors = new SettingGroup("Colors");
colors.addRow(new ColorDescriptor(Inputs.UP_COLOR, "Up Color", Color.blue));
colors.addRow(new ColorDescriptor(Inputs.DOWN_COLOR, "Down Color", Color.magenta));
tab.addGroup(colors);

RuntimeDescriptor rd = new RuntimeDescriptor();
setRuntimeDescriptor(rd);
rd.setLabelSettings(Inputs.INPUT);
rd.exportValue(new ValueDescriptor(test, "TPercentage", new String[] {Inputs.INPUT}));


debug("Percentage Value " + getSettings().getDouble(Inputs.INPUT));

}
 

TradersCustom

Member
Joined
Sep 8, 2022
Posts
12
Likes
6
Ok great this clears it up. The reason for your problem is that a MW study does not 'know' what the value of a setting is until after the initialize() function has completed. During the initialize function, we are simply telling MW what the initial values are. MW does not 'set them' until the function has finished being called. This is why, if you place your debug statement in the initialize function, it will show the value of every setting to be 'null'.

If you put a debug or info statement within the initialize function that tries to output a setting value, it won't work. The study needs that function to complete/return before it will actually 'set' the settings to their initial values. If you move your debug statement to calculateValues() or calculate(), for example, you won't have this problem anymore.

You can't read setting values from within the initialize() function. That's the issue. In general, there's no reason for you to read them within the initialize function in the first place.

The initialize() function only gets called once during the lifetime of a study: when it first gets clicked, and the options menu opens up for the first time, before it is actually applied to the chart.


Try this and you'll see that the values now show up properly:

public void initialize(Defaults defaults)
{
SettingsDescriptor sd = new SettingsDescriptor();
setSettingsDescriptor(sd);
SettingTab tab = new SettingTab("General");
sd.addTab(tab);

SettingGroup inputs = new SettingGroup("Inputs");
inputs.addRow(new PercentSliderDescriptor(Inputs.INPUT, "Percentage",.10,.10));
tab.addGroup(inputs);

SettingGroup colors = new SettingGroup("Colors");
colors.addRow(new ColorDescriptor(Inputs.UP_COLOR, "Up Color", Color.blue));
colors.addRow(new ColorDescriptor(Inputs.DOWN_COLOR, "Down Color", Color.magenta));
tab.addGroup(colors);

RuntimeDescriptor rd = new RuntimeDescriptor();
setRuntimeDescriptor(rd);
rd.setLabelSettings(Inputs.INPUT);
rd.exportValue(new ValueDescriptor(test, "TPercentage", new String[] {Inputs.INPUT}));
}

@Override
protected void calculateValues(DataContext ctx) {
debug("Percentage Value " + getSettings().getDouble(Inputs.INPUT));
}
 
Last edited:
Top