Page 1 of 4

Non-Lag Moving Average

PostPosted: Mon Sep 20, 2010 10:41 pm
by Alexander.Gettinger
viewtopic.php?f=27&t=2219

NonLagMA.png


NonLagMA.lua
(3.11 KiB) Downloaded 2572 times

SSNonLagMA.lua
Single Stream Version
(3.77 KiB) Downloaded 1718 times


NonLagMA Overlay.lua
(4.64 KiB) Downloaded 969 times


The indicator was revised and updated

Re: Non-Lag Moving Average

PostPosted: Mon Sep 20, 2010 11:26 pm
by colddog
Thanks so much for your help!

Re: Non-Lag Moving Average

PostPosted: Wed Sep 22, 2010 10:33 am
by colddog
Hi,

just wondering if its possible to include an option which changes the width of the line being drawn? so that i can make it thicker? :?: :D

Re: Non-Lag Moving Average

PostPosted: Wed Sep 22, 2010 11:55 am
by Apprentice
Added to development cue.

Re: Non-Lag Moving Average

PostPosted: Mon Oct 04, 2010 9:55 pm
by Alexander.Gettinger
Update Non-Lag Moving Average.
Added line styles.

Code: Select all
function Init()
    indicator:name("NonLagMA indicator");
    indicator:description("NonLagMA indicator");
    indicator:requiredSource(core.Tick);
    indicator:type(core.Indicator);
   
    indicator.parameters:addGroup("Calculation");
    indicator.parameters:addInteger("Length", "Length", "Length", 9);
    indicator.parameters:addInteger("Filter", "Filter", "Filter", 0);
    indicator.parameters:addInteger("ColorBarBack", "ColorBarBack", "ColorBarBack", 2);
    indicator.parameters:addDouble("Deviation", "Deviation", "Deviation", 0);

    indicator.parameters:addGroup("Style");
    indicator.parameters:addColor("clrUP", "UP color", "UP color", core.rgb(0, 0, 255));
    indicator.parameters:addColor("clrDN", "DN color", "DN color", core.rgb(255, 0, 0));
   
    indicator.parameters:addInteger("widthLinReg", "Line width", "Line width", 1, 1, 5);
    indicator.parameters:addInteger("styleLinReg", "Line style", "Line style", core.LINE_SOLID);
    indicator.parameters:setFlag("styleLinReg", core.FLAG_LINE_STYLE);
end

local first;
local source = nil;
local Length;
local Filter;
local ColorBarBack;
local Deviation;
local buffUP=nil;
local buffDN=nil;
local trend;
local buff;
local Coeff;
local Phase;
local Len;

function Prepare()
    source = instance.source;
    Length=instance.parameters.Length;
    Filter=instance.parameters.Filter;
    ColorBarBack=instance.parameters.ColorBarBack;
    Deviation=instance.parameters.Deviation;
    trend = instance:addInternalStream(0, 0);
    buff = instance:addInternalStream(0, 0);
    first = source:first()+2;
    local name = profile:id() .. "(" .. source:name() .. ", " .. instance.parameters.Length .. ", " .. instance.parameters.Filter .. ", " .. instance.parameters.ColorBarBack .. ", " .. instance.parameters.Deviation .. ")";
    instance:name(name);
    buffUp = instance:addStream("buffUp", core.Line, name .. ".Up", "Up", instance.parameters.clrUP, first);
    buffDn = instance:addStream("buffDn", core.Line, name .. ".Dn", "Dn", instance.parameters.clrDN, first);
    buffUp:setWidth(instance.parameters.widthLinReg);
    buffUp:setStyle(instance.parameters.styleLinReg);
    buffDn:setWidth(instance.parameters.widthLinReg);
    buffDn:setStyle(instance.parameters.styleLinReg);
    Coeff=3.*math.pi;
    Phase=Length-1;
    Len=Length*4.+Phase;
end

function Update(period, mode)
    if (period>first+Len+2) then
     local Weight=0;
     local Sum=0;
     local t=0;
     for i=0,Len-1,1 do
      local g=1./(Coeff*t+1.);
      if t<=0.5 then
       g=1.;
      end
      local beta=math.cos(math.pi*t);
      local alpha=g*beta;
      Sum=Sum+alpha*source[period-i];
      Weight=Weight+alpha;
      if t<1. then
       t=t+1./(Phase-1.);
      elseif t<Len-1. then
       t=t+7./(4.*Length-1.);
      end
     end
     if Weight>0. then
      buff[period]=(1.+Deviation/100.)*Sum/Weight;
     end
     if Filter>0. then
      if math.abs(buff[period]-buff[period-1])<Filter*source:pipSize() then
       buff[period]=buff[period-1];
      end
     end
     trend[period]=trend[period-1];
     if buff[period]-buff[period-1]>Filter*source:pipSize() then
      trend[period]=1;
     end
     if buff[period-1]-buff[period]>Filter*source:pipSize() then
      trend[period]=-1;
     end
     if trend[period]>0 then
      buffUp[period]=buff[period];
      if trend[period-ColorBarBack]<0 then
       buffUp[period-ColorBarBack]=buff[period-ColorBarBack];
      end
     end
     if trend[period]<0 then
      buffDn[period]=buff[period];
      if trend[period-ColorBarBack]>0 then
       buffDn[period-ColorBarBack]=buff[period-ColorBarBack];
      end
     end

    end
end

Re: Non-Lag Moving Average

PostPosted: Fri Oct 22, 2010 12:30 pm
by Apprentice
NonLagMA.lua
Version RC1
(4.34 KiB) Downloaded 1895 times

Single Line Option Added, Needed for compatibility with some indicators, signals.

Re: Non-Lag Moving Average

PostPosted: Fri Oct 29, 2010 3:28 pm
by Jigit Jigit
Can someone, please, test this indicator thoroughly?
It seems to struggle...

It is very slow to load, that is to say, it doesn't spread itself over the price action. One has to drag the chart back manually to actually make it draw itself (especially with a period greater than 100).

Also, is there a limit as to how many Non-lag MAs can be planted on one chart?
Once I load 10 of them they seem to kill Marketscope, it literally freezes.

I have already posted a request for a multiple non-lag indicator.
Do you think multiple non-lag MAs would work better if made into one indicator? Would it be easier for Marketscope to load?

Re: Non-Lag Moving Average

PostPosted: Mon Nov 01, 2010 3:02 am
by thejesters1
a signal for this would be great. thanks

Re: Non-Lag Moving Average

PostPosted: Mon Nov 01, 2010 4:00 am
by Apprentice
Added to developmental cue.

Re: Non-Lag Moving Average

PostPosted: Tue Nov 09, 2010 2:15 am
by Alexander.Gettinger
Indicator updated. Added non-color mode.

Code: Select all
function Init()
    indicator:name("NonLagMA indicator");
    indicator:description("NonLagMA indicator");
    indicator:requiredSource(core.Tick);
    indicator:type(core.Indicator);
   
    indicator.parameters:addGroup("Calculation");
    indicator.parameters:addInteger("Length", "Length", "Length", 9);
    indicator.parameters:addInteger("Filter", "Filter", "Filter", 0);
    indicator.parameters:addInteger("ColorBarBack", "ColorBarBack", "ColorBarBack", 2);
    indicator.parameters:addDouble("Deviation", "Deviation", "Deviation", 0);
    indicator.parameters:addBoolean("ColorMode", "ColorMode", "ColorMode", true);

    indicator.parameters:addGroup("Style");
    indicator.parameters:addColor("MainClr", "Main color", "Main color", core.rgb(0, 255, 0));
    indicator.parameters:addColor("clrUP", "UP color", "UP color", core.rgb(0, 0, 255));
    indicator.parameters:addColor("clrDN", "DN color", "DN color", core.rgb(255, 0, 0));
   
    indicator.parameters:addInteger("widthLinReg", "Line width", "Line width", 1, 1, 5);
    indicator.parameters:addInteger("styleLinReg", "Line style", "Line style", core.LINE_SOLID);
    indicator.parameters:setFlag("styleLinReg", core.FLAG_LINE_STYLE);
end

local first;
local source = nil;
local Length;
local Filter;
local ColorBarBack;
local Deviation;
local buffUP=nil;
local buffDN=nil;
local trend;
local buff;
local Coeff;
local Phase;
local Len;
local ColorMode;
local MainBuff=nil;

function Prepare()
    source = instance.source;
    Length=instance.parameters.Length;
    Filter=instance.parameters.Filter;
    ColorMode=instance.parameters.ColorMode;
    ColorBarBack=instance.parameters.ColorBarBack;
    Deviation=instance.parameters.Deviation;
    trend = instance:addInternalStream(0, 0);
    buff = instance:addInternalStream(0, 0);
    first = source:first()+2;
    local name = profile:id() .. "(" .. source:name() .. ", " .. instance.parameters.Length .. ", " .. instance.parameters.Filter .. ", " .. instance.parameters.ColorBarBack .. ", " .. instance.parameters.Deviation .. ")";
    instance:name(name);
    MainBuff = instance:addStream("MainBuff", core.Line, name .. ".MA", "MA", instance.parameters.MainClr, first);
    buffUp = instance:addStream("buffUp", core.Line, name .. ".Up", "Up", instance.parameters.clrUP, first);
    buffDn = instance:addStream("buffDn", core.Line, name .. ".Dn", "Dn", instance.parameters.clrDN, first);
    MainBuff:setWidth(instance.parameters.widthLinReg);
    MainBuff:setStyle(instance.parameters.styleLinReg);
    buffUp:setWidth(instance.parameters.widthLinReg);
    buffUp:setStyle(instance.parameters.styleLinReg);
    buffDn:setWidth(instance.parameters.widthLinReg);
    buffDn:setStyle(instance.parameters.styleLinReg);
    Coeff=3.*math.pi;
    Phase=Length-1;
    Len=Length*4.+Phase;
end

function Update(period, mode)
    if (period>first+Len+2) then
     local Weight=0;
     local Sum=0;
     local t=0;
     for i=0,Len-1,1 do
      local g=1./(Coeff*t+1.);
      if t<=0.5 then
       g=1.;
      end
      local beta=math.cos(math.pi*t);
      local alpha=g*beta;
      Sum=Sum+alpha*source[period-i];
      Weight=Weight+alpha;
      if t<1. then
       t=t+1./(Phase-1.);
      elseif t<Len-1. then
       t=t+7./(4.*Length-1.);
      end
     end
     if Weight>0. then
      buff[period]=(1.+Deviation/100.)*Sum/Weight;
     end
     if Filter>0. then
      if math.abs(buff[period]-buff[period-1])<Filter*source:pipSize() then
       buff[period]=buff[period-1];
      end
     end
     trend[period]=trend[period-1];
     if buff[period]-buff[period-1]>Filter*source:pipSize() then
      trend[period]=1;
     end
     if buff[period-1]-buff[period]>Filter*source:pipSize() then
      trend[period]=-1;
     end
     if ColorMode==true then
      if trend[period]>0 then
       buffUp[period]=buff[period];
       if trend[period-ColorBarBack]<0 then
        buffUp[period-ColorBarBack]=buff[period-ColorBarBack];
       end
      end
      if trend[period]<0 then
       buffDn[period]=buff[period];
       if trend[period-ColorBarBack]>0 then
        buffDn[period-ColorBarBack]=buff[period-ColorBarBack];
       end
      end
     else
      if trend[period]>0 then
       MainBuff[period]=buff[period];
       if trend[period-ColorBarBack]<0 then
        MainBuff[period-ColorBarBack]=buff[period-ColorBarBack];
       end
      end
      if trend[period]<0 then
       MainBuff[period]=buff[period];
       if trend[period-ColorBarBack]>0 then
        MainBuff[period-ColorBarBack]=buff[period-ColorBarBack];
       end
      end
     
     end

    end
end