i tried to build my own indicator. It's my first attempt and it failed.
I should be 2 Simple Moving Average with a standard period of 5. One Average with the last 5 highs, one with the last 5 lows.
I attached my failed code but i can't find the error.
The debugger result line 71: index out of range and 66: attempt to index upvalue 'avgHigh' (a nil value) but i don't know what it means.
Can anyone help me please?
(Sry english is not my first language)
Mirko
- Code: Select all
-- Indicator profile initialization routine
-- Defines indicator profile properties and indicator parameters
-- TODO: Add minimal and maximal value of numeric parameters and default color of the streams
function Init()
--indicator:name(resources:get("MVA High & Low"));
--indicator:description(resources:get("description"));
indicator:requiredSource(core.Bar);
indicator:type(core.Indicator);
indicator.parameters:addGroup("Style");
--10
indicator:setTag("group", "Moving Averages");
indicator.parameters:addGroup("Calculation");
indicator.parameters:addInteger("N", "Periods", "Number of Periods", 5,1,100);
indicator.parameters:addColor("clrLineH", "High_line_color_name","R_color_of_PARAM_description", core.rgb(255, 0, 0));
indicator.parameters:addColor("clrLineL", "Low_line_color_name","R_color_of_PARAM_description", core.rgb(0, 0, 255));
indicator.parameters:addInteger("widthDIP", "R_line_width_name","R_line_width_name", 1, 1, 5);
indicator.parameters:addInteger("styleDIP", "R_line_style_name","R_line_style_name", core.LINE_SOLID);
indicator.parameters:setFlag("styleDIP", core.FLAG_LEVEL_STYLE);
--20
-- indicator.parameters:addColor("clrLineH", resources:get("High_line_color_name"),
-- string.format(resources:get("R_color_of_PARAM_description"), resources:get("param_MVA_line_name")), core.rgb(255, 255, 0));
--indicator.parameters:addColor("clrLineL", resources:get("Low_line_color_name"),
-- string.format(resources:get("R_color_of_PARAM_description"), resources:get("param_MVA_line_name")), core.rgb(0, 255, 255));
--indicator.parameters:addInteger("widthDIP", resources:get("R_line_width_name"),
-- string.format(resources:get("R_width_of_PARAM_description"), resources:get("param_MVA_line_name")), 1, 1, 5);
-- indicator.parameters:addInteger("styleDIP", resources:get("R_line_style_name"),
-- string.format(resources:get("R_style_of_PARAM_description"), resources:get("param_MVA_line_name")), core.LINE_SOLID);
-- indicator.parameters:setFlag("styleDIP", core.FLAG_LEVEL_STYLE);
end --30
-- Indicator instance initialization routine
-- Processes indicator parameters and creates output streams
-- TODO: Refine the first period calculation for each of the output streams.
-- TODO: Calculate all constants, create instances all subsequent indicators and load all required libraries
-- Parameters block
local first;
local n;
--40
local source = nil;
local avgHigh;
local avgLow ;
-- initializes the instance of the indicator
function Prepare()
source = instance.source;
n = instance.parameters.N;
first = n + source:first() -1;
--50
local name = profile:id() .. "(" .. source:name() .. "," .. n .. ")";
instance:name(name);
AvgHigh = instance:addStream("MVAHigh", core.Line, name, "MVAHigh", instance.parameters.clrLineH, first)
AvgLow = instance:addStream("MVALow", core.Line, name, "MVALow", instance.parameters.clrLineL, first)
AvgHigh:setWidth(instance.parameters.widthDIP);
AvgHigh:setStyle(instance.parameters.styleDIP);
AvgLow:setWidth(instance.parameters.widthDIP);
AvgLow:setStyle(instance.parameters.styleDIP);
end
--60
-- calculate the value
function Update(period)
if period < first then
return ;
elseif period == first then
avgHigh[period] = instance.source[period];
avgLow[period] = instance.source[period];
else
--70
avgHigh[period] = mathex.avg(instance.source.high[period], period - n + 1, period);
avgLow[period] = mathex.avg(instance.source.low, period - n + 1, period);
end
end--74