Subtracting Averages

Section for discussions related to indicators, use of indicators, and building of trading stategies using indicators.

Moderator: admin

Subtracting Averages

Postby tdesiato » Mon Jun 10, 2013 1:51 am

I'm trying to figure out how to find the difference between two moving averages, at different time scales. I'm just playing with a simple MVA indicator. I have this code, but if I try to create out3, it won't run.

-- calculate the value
function Update(period)
if (period >= first) then
out[period] = mathex.avg(instance.source, period - n + 1, period);
out2[period] = mathex.avg(instance.source, period - n2 + 1, period);
-- out3[period] =mathex.avg(instance.source, period - n + 1, period) - mathex.avg(instance.source, period - n2 + 1, period);
end
end

I don't understand why it can plot two nice lines on the screen, but I can't apply a simply subtraction to get the difference between them.

Any help is appreciated. Thanks!
tdesiato
 
Posts: 13
Joined: Sun Jun 02, 2013 11:47 pm

Re: Subtracting Averages

Postby Apprentice » Mon Jun 10, 2013 3:09 am

There are several possible reasons.
Can you post your indicator complete code.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36476
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: Subtracting Averages

Postby tdesiato » Mon Jun 10, 2013 8:20 am

Here's the whole thing. Thanks!

Code: Select all
-- initializes the indicator
function Init()
    indicator:name(resources:get("name"));
    indicator:description(resources:get("description"));
    indicator:requiredSource(core.Tick);
    indicator:type(core.Indicator);
    indicator:setTag("group", "Moving Average 1");
    indicator.parameters:addGroup("Calculation");
    indicator.parameters:addInteger("N", resources:get("R_number_of_periods_name"), resources:get("R_number_of_periods_desciption"), 7, 1, 10000);
    indicator.parameters:addGroup("Style");
    indicator.parameters:addColor("clrDIP", resources:get("R_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: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);
   indicator:setTag("group2", "Moving Average 2");
    indicator.parameters:addGroup("Calculation2");
    indicator.parameters:addInteger("N2", resources:get("R_number_of_periods_name"), resources:get("R_number_of_periods_desciption"), 7, 1, 10000);
    indicator.parameters:addGroup("Style2");
    indicator.parameters:addColor("clrDIP2", resources:get("R_line_color_name"),
        string.format(resources:get("R_color_of_PARAM_description"), resources:get("param_MVA2_line_name")), core.rgb(255, 255, 0));
    indicator.parameters:addInteger("widthDIP2", resources:get("R_line_width_name"),
        string.format(resources:get("R_width_of_PARAM_description"), resources:get("param_MVA2_line_name")), 1, 1, 5);
    indicator.parameters:addInteger("styleDIP2", resources:get("R_line_style_name"),
        string.format(resources:get("R_style_of_PARAM_description"), resources:get("param_MVA2_line_name")), core.LINE_SOLID);
    indicator.parameters:setFlag("styleDIP2", core.FLAG_LEVEL_STYLE);
end

local first = 0;
local first2 = 0;
local n = 0;
local n2 = 0;
local source = nil;
local source2 = nil;
local out = nil;
local out2 = nil;
local out3 = nil;

-- initializes the instance of the indicator
function Prepare()
    source = instance.source;
   
    n = instance.parameters.N;
   n2 = instance.parameters.N2;
   first = n + source:first() - 1;
   
    local name = profile:id() .. "(" .. source:name() .. "," .. n .. "," .. n2 .. ")";
    instance:name(name);
   out = instance:addStream("MVA", core.Line, name, "MVA", instance.parameters.clrDIP, first);
   out:setWidth(instance.parameters.widthDIP);
   out:setStyle(instance.parameters.styleDIP);
   
   first = n2 + source:first() - 1;
   out2 = instance:addStream("MVA2", core.Line, name, "MVA2", instance.parameters.clrDIP2, first);
   out2:setWidth(instance.parameters.widthDIP2);
   out2:setStyle(instance.parameters.styleDIP2);
end

-- calculate the value
function Update(period)
  if (period >= first) then
    out[period] = mathex.avg(instance.source, period - n + 1, period);
   out2[period] = mathex.avg(instance.source, period - n2 + 1, period);
   -- out3[period] =mathex.avg(instance.source, period - n + 1, period) - mathex.avg(instance.source, period - n2 + 1, period);
  end
end
tdesiato
 
Posts: 13
Joined: Sun Jun 02, 2013 11:47 pm

Re: Subtracting Averages

Postby tdesiato » Tue Jun 11, 2013 12:28 am

I'm getting closer, but still not working.

Code: Select all
-- initializes the indicator
function Init()
    indicator:name(resources:get("name"));
    indicator:description(resources:get("description"));
    indicator:requiredSource(core.Tick);
    indicator:type(core.Indicator);
    indicator:setTag("group", "Moving Average 1");
    indicator.parameters:addGroup("Calculation");
    indicator.parameters:addInteger("N", resources:get("R_number_of_periods_name"), resources:get("R_number_of_periods_desciption"), 7, 1, 10000);
    indicator.parameters:addGroup("Style");
    indicator.parameters:addColor("clrDIP", resources:get("R_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: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);
   indicator:setTag("group2", "Moving Average 2");
    indicator.parameters:addGroup("Calculation2");
    indicator.parameters:addInteger("N2", resources:get("R_number_of_periods_name"), resources:get("R_number_of_periods_desciption"), 7, 1, 10000);
    indicator.parameters:addGroup("Style2");
    indicator.parameters:addColor("clrDIP2", resources:get("R_line_color_name"),
        string.format(resources:get("R_color_of_PARAM_description"), resources:get("param_MVA2_line_name")), core.rgb(255, 255, 0));
    indicator.parameters:addInteger("widthDIP2", resources:get("R_line_width_name"),
        string.format(resources:get("R_width_of_PARAM_description"), resources:get("param_MVA2_line_name")), 1, 1, 5);
    indicator.parameters:addInteger("styleDIP2", resources:get("R_line_style_name"),
        string.format(resources:get("R_style_of_PARAM_description"), resources:get("param_MVA2_line_name")), core.LINE_SOLID);
    indicator.parameters:setFlag("styleDIP2", core.FLAG_LEVEL_STYLE);
   indicator.parameters:addGroup("Style3");
    indicator.parameters:addColor("clrDIP3", resources:get("R_line_color_name"),
        string.format(resources:get("R_color_of_PARAM_description"), resources:get("param_MVA2_line_name")), core.rgb(255, 255, 0));
    indicator.parameters:addInteger("widthDIP3", resources:get("R_line_width_name"),
        string.format(resources:get("R_width_of_PARAM_description"), resources:get("param_MVA2_line_name")), 1, 1, 5);
    indicator.parameters:addInteger("styleDIP3", resources:get("R_line_style_name"),
        string.format(resources:get("R_style_of_PARAM_description"), resources:get("param_MVA2_line_name")), core.LINE_SOLID);
    indicator.parameters:setFlag("styleDIP3", core.FLAG_LEVEL_STYLE);
end

local first = 0;
local first2 = 0;
local n = 0;
local n2 = 0;
local source = nil;
local source2 = nil;
local out = nil;
local out2 = nil;
local out3 = nil;

-- initializes the instance of the indicator
function Prepare()
    source = instance.source;
   
    n = instance.parameters.N;
   n2 = instance.parameters.N2;
   first = source:first() + n - 1;
   
    local name = profile:id() .. "(" .. source:name() .. "," .. n .. "," .. n2 .. ")";
    instance:name(name);
   out = instance:addStream("MVA", core.Line, name, "MVA", instance.parameters.clrDIP, first);
   out:setWidth(instance.parameters.widthDIP);
   out:setStyle(instance.parameters.styleDIP);
   
   first2 = source:first() + n2 - 1;
   out2 = instance:addStream("MVA2", core.Line, name, "MVA2", instance.parameters.clrDIP2, first2);
   out2:setWidth(instance.parameters.widthDIP2);
   out2:setStyle(instance.parameters.styleDIP2);
   
   if MVA >= MVA2 then
    instance.parameters.clrDIP2 = instance.parameters.clrDIP3;
   else
    instance.parameters.clrDIP2 = instance.parameters.clrDIP3;
   end
   
   --out3 = instance:addStream("MVA3", core.Bar, name, "MVA3", instance.parameters.clrDIP3, first);
   --out3:setWidth(instance.parameters.widthDIP3);
   --out3:setStyle(instance.parameters.styleDIP3);
end

-- calculate the value
function Update(period)
  if (period >= first) then
    out[period] = mathex.avg(instance.source, period - n + 1, period);
  end
  if (period >= first2) then
   out2[period] = mathex.avg(instance.source, period - n2 + 1, period);
  end

end
tdesiato
 
Posts: 13
Joined: Sun Jun 02, 2013 11:47 pm



Return to Discussions

Who is online

Users browsing this forum: No registered users and 10 guests