designer01
Member
- Joined
- Aug 19, 2019
- Posts
- 12
- Likes
- 6
I have a script of an indicator that the Methods EMA etc. are not plotting on the chart panel. It can only plot the SMA but not the others. See code below. I must be missing something. Please advise. Thanks in advance.
Code:
enum Values { ATR, ATRAvg }
@Override
public void initialize(Defaults defaults)
{
var sd = createSD();
var tab = sd.addTab(get("GENERAL"));
var inputs = tab.addGroup(get("INPUTS"));
inputs.addRow(new IntegerDescriptor(Inputs.PERIOD, get("LBL_PERIOD"), 14, 1, 9999, 1));
inputs.addRow(new IntegerDescriptor(Inputs.SIGNAL_PERIOD, get("Period Avg"), 14, 1, 9999, 1));
inputs.addRow(new MAMethodDescriptor(Inputs.METHOD, get("Avg Method"), Enums.MAMethod.SMA));
var settings = tab.addGroup(get("COLORS"));
settings.addRow(new PathDescriptor(Inputs.PATH, get("LBL_LINE"), defaults.getLineColor(), 1.0f, null, true, false, false));
settings.addRow(new PathDescriptor(Inputs.PATH2, get("LBL_LINEAVG"), defaults.getLineColor(), 1.0f, null, true, false, false));
settings.addRow(new IndicatorDescriptor(Inputs.IND, get("LBL_INDICATOR"), null, null, false, true, true));
settings.addRow(new IndicatorDescriptor(Inputs.IND2, get("LBL_INDICATOR2"), null, null, false, true, true));
// Quick Settings (Tool Bar and Popup Editor)
//sd.addQuickSettings(new SliderDescriptor(Inputs.PERIOD, get("LBL_PERIOD"), 14, 1, 9999, true, () -> Enums.Icon.SINE_WAVE.get()));
//sd.addQuickSettings(Inputs.PATH, Inputs.IND);
//var desc = createRD();
RuntimeDescriptor desc = new RuntimeDescriptor();
setRuntimeDescriptor(desc);
//-------------------DECLARE AND EXPORT VALUES (Initialized under Enums Above------------------------------\\
//-------------------Values and Signals
desc.setLabelPrefix(get("ATR w/ MAvg"));//Ind Name declaration to be shown on upper left corner of panel
desc.setLabelSettings(Inputs.INPUT, Inputs.PERIOD,Inputs.METHOD,Inputs.SIGNAL_PERIOD);//Inputs declaration to be shown on upper left corner of panel
//Export values to be detected by strategy and Strategy Builder
desc.exportValue(new ValueDescriptor(Values.ATR, get("TAB_ATR"), new String[] {Inputs.PERIOD}));
desc.exportValue(new ValueDescriptor(Values.ATR, get("TAB_ATR"), new String[] {Inputs.SIGNAL_PERIOD}));
desc.exportValue(new ValueDescriptor(Values.ATR, get("TAB_ATR"), new String[] {Inputs.METHOD}));
//Declare Paths to be detected by line configuration on chart
desc.declarePath(Values.ATR, Inputs.PATH);
desc.declarePath(Values.ATRAvg, Inputs.PATH2);
//Declare indicators
desc.declareIndicator(Values.ATR, Inputs.IND);
//desc.declareIndicator(Values.ATRAvg, Inputs.IND2);
//Set Ranges
desc.setRangeKeys(Values.ATR, Values.ATRAvg);
}
@Override
protected void calculate(int index, DataContext ctx)
{
Enums.MAMethod method=getSettings().getMAMethod(Inputs.METHOD);
var series = ctx.getDataSeries();//DEFAULT STATEMENT DO not change or remove
//-------------------------VARIABLES--------------------------------------------------------------------------------------------------------------\\
int period = getSettings().getInteger(Inputs.PERIOD);//Gets the Period value from inputs
int period2 = getSettings().getInteger(Inputs.SIGNAL_PERIOD);//Gets the Period value from inputs
//Makes sure there is enough Bar History in Chart before calculating. This calculates the max of the two
if (index < Math.max(period, period2)) return; // not enough data loaded. Should never happen, since we load it in 'onLoad'
//Variable used in ATR Avg formula
//Double signal = series.ma(getSettings().getMAMethod(Inputs.METHOD, Enums.MAMethod.SMA), index, period2, Values.ATR);
Double signal = series.ma(method, index, period2, Values.ATR);//ma(maMethod, maPeriod, of ATR)
//-------------------------PLOTS----------------------------------------------------------------------------------------------------------------------\\
series.setDouble(index, Values.ATR, series.atr(index, period));//PLOTS ATR
series.setDouble(index, Values.ATRAvg, signal);//PLOTS ATRAvg
series.setComplete(index);
}
}