Help to Implement Moving Average Mean Deviation Channel

Moderator: admin

Help to Implement Moving Average Mean Deviation Channel

Postby FrostyTrader » Wed May 24, 2017 12:50 am

Gday folks, i've been hammering away at a few different indicators and strategies for a while now, if anything to just really orientate myself with all things lua and indicore. I've hit a solid problem i can't quite solve. Essentially, all this indicator does is take a moving average, moving average source (open, close, etc) and moving average period (all user defined), and output to the screen the standard moving average, with 1 standard deviation channel above and below the moving average - however this standard deviation can also be user defined to calculate and display a greater deviation. Below is my attempt at demonstrating this however my understanding 'source' and 'period' and how they work together within the Update function is still in its infancy. Thanks again for any help

Code: Select all
function Init()
    indicator:name("Moving Average Channel Indicator");
    indicator:description("Moving averages with standard deviation channels");
    indicator:requiredSource(core.Bar);
    indicator:type(core.Indicator);
    SetParameters();
end

function SetParameters()
    indicator.parameters:addGroup("Moving Average Parameters");
   indicator.parameters:addString("MA_METHOD", "MA Method", "", "EMA");
   indicator.parameters:addStringAlternative("MA_METHOD", "MVA", "", "MVA");
    indicator.parameters:addStringAlternative("MA_METHOD", "EMA", "", "EMA");
    indicator.parameters:addStringAlternative("MA_METHOD", "LWMA", "", "LWMA");
    indicator.parameters:addStringAlternative("MA_METHOD", "TMA", "", "TMA");
    indicator.parameters:addStringAlternative("MA_METHOD", "SMMA*", "", "SMMA");
    indicator.parameters:addStringAlternative("MA_METHOD", "VIDYA", "", "VIDYA");
    indicator.parameters:addStringAlternative("MA_METHOD", "Wilders*", "", "WMA");
   indicator.parameters:addStringAlternative("MA_METHOD" , "FRAMA", "", "FRAMA");
   indicator.parameters:addString("MA_SOURCE", "MA Source Price", "", "close");
   indicator.parameters:addStringAlternative("MA_SOURCE", "Close", "", "close");
   indicator.parameters:addStringAlternative("MA_SOURCE", "Open", "", "open");
   indicator.parameters:addStringAlternative("MA_SOURCE", "High", "", "high");
   indicator.parameters:addStringAlternative("MA_SOURCE", "Low", "", "low");
   indicator.parameters:addStringAlternative("MA_SOURCE", "Median", "", "median");
   indicator.parameters:addStringAlternative("MA_SOURCE", "Typical", "", "typical");
   indicator.parameters:addStringAlternative("MA_SOURCE", "Weighted", "", "weighted");
   indicator.parameters:addInteger("MA_PERIOD","Fast MA Period", "", 20,1,1000);
   indicator.parameters:addColor("MA_COLOR", "MA Color", "", core.COLOR_LABEL);
   
   indicator.parameters:addGroup("Deviation Parameters");
   indicator.parameters:addDouble("MA_DEV", "MA Deviation", "", 1, 1, 10);
   indicator.parameters:addColor("MA_DEV_COLOR", "Top Deviation Line Color", "", core.COLOR_LABEL);
end

local MA;
local MA_METHOD;
local MA_SOURCE;
local MA_PERIOD;
local MA_COLOR;
local MA_STREAM;
local MA_DEV;
local MA_DEV_STREAM;
local MA_DEV_COLOR;

local FIRST;
local SOURCE = nil;

function Prepare()
   SOURCE = instance.source;
    MA_METHOD = instance.parameters.MA_METHOD;
    MA_SOURCE = instance.parameters.MA_SOURCE;
    MA_PERIOD = instance.parameters.MA_PERIOD;
    MA_COLOR = instance.parameters.MA_COLOR;
   MA_DEV = instance.parameters.MA_DEV;
   MA_DEV_COLOR = instance.parameters.MA_DEV_COLOR;
   
   local name = profile:id() .. "(" .. SOURCE:name() .. "," .. MA_PERIOD .. " Dev: " .. MA_DEV .. ")";
    instance:name(name);
   
    MA = core.indicators:create(MA_METHOD, SOURCE[MA_SOURCE], MA_PERIOD);
   MA_DEV = core.indicators:create(MA_METHOD, SOURCE[MA_SOURCE], MA_PERIOD);
   
    FIRST = SOURCE:first() + MA_PERIOD - 1;
   
    MA_STREAM = instance:addStream(MA_METHOD, core.Line, name, MA_METHOD, MA_COLOR, FIRST);
   MA_DEV_STREAM = instance:addStream("MA_DEV", core.Line, name, "MA_DEV", MA_DEV_COLOR, FIRST);
end

function Update(period, mode)
    if(period >= FIRST and SOURCE:hasData(period)) then
        MA:update(mode);
      MA_DEV:update(mode);
      
      local range = core.rangeTo(period, MA_PERIOD);
      
      MA_DEV_STREAM[period] = MA_DEV[mathex.meandev(SOURCE[MA_SOURCE], range)];
      
        MA_STREAM[period] = MA.DATA[period];
    end
end


As the moving average will be user defined, i figured i could take the pre-existing stream from the original moving average and plot a deviation based on that data....
FrostyTrader
 
Posts: 7
Joined: Sat Jul 25, 2015 9:54 pm

Return to Indicator Development

Who is online

Users browsing this forum: No registered users and 14 guests

cron