Fan of MA
Posted: Wed May 19, 2010 12:27 am
Fan of 7 MA.
- Code: Select all
function Init()
indicator:name("FAN");
indicator:description("");
indicator:requiredSource(core.Tick);
indicator:type(core.Oscillator);
indicator.parameters:addInteger("Period_1", "Period 1", "No description", 1);
indicator.parameters:addInteger("Period_2", "Period 2", "No description", 25);
indicator.parameters:addInteger("Period_3", "Period 3", "No description", 50);
indicator.parameters:addInteger("Period_4", "Period 4", "No description", 75);
indicator.parameters:addInteger("Period_5", "Period 5", "No description", 100);
indicator.parameters:addInteger("Period_6", "Period 6", "No description", 150);
indicator.parameters:addInteger("Period_7", "Period 7", "No description", 200);
indicator.parameters:addColor("color_1", "Color 1", "Color 1", core.rgb(0, 255, 0));
indicator.parameters:addColor("color_2", "Color 2", "Color 2", core.rgb(0, 128, 0));
indicator.parameters:addColor("color_3", "Color 3", "Color 3", core.rgb(255, 255, 0));
indicator.parameters:addColor("color_4", "Color 4", "Color 4", core.rgb(255, 128, 0));
indicator.parameters:addColor("color_5", "Color 5", "Color 5", core.rgb(255, 0, 0));
end
local Period1;
local Period2;
local Period3;
local Period4;
local Period5;
local Period6;
local Period7;
local first;
local source = nil;
local buff1=nil;
local buff2=nil;
local buff3=nil;
local buff4=nil;
local buff5=nil;
local MA1;
local MA2;
local MA3;
local MA4;
local MA5;
local MA6;
local MA7;
function Prepare()
Period1 = instance.parameters.Period_1;
Period2 = instance.parameters.Period_2;
Period3 = instance.parameters.Period_3;
Period4 = instance.parameters.Period_4;
Period5 = instance.parameters.Period_5;
Period6 = instance.parameters.Period_6;
Period7 = instance.parameters.Period_7;
source = instance.source;
MA1 = core.indicators:create("EMA", source, Period1);
MA2 = core.indicators:create("EMA", source, Period2);
MA3 = core.indicators:create("EMA", source, Period3);
MA4 = core.indicators:create("EMA", source, Period4);
MA5 = core.indicators:create("EMA", source, Period5);
MA6 = core.indicators:create("EMA", source, Period6);
MA7 = core.indicators:create("EMA", source, Period7);
first = math.max(MA1.DATA:first(),MA2.DATA:first(),MA3.DATA:first(),MA4.DATA:first(),MA5.DATA:first(),MA6.DATA:first(),MA7.DATA:first());
local name = profile:id() .. "(" .. source:name() .. ", " .. Period1 .. ", " .. Period2 .. ", " .. Period3 .. ", " .. Period4 .. ", " .. Period5 .. ", " .. Period6 .. ", " .. Period7 .. ")";
instance:name(name);
buff1 = instance:addStream("buff1", core.Bar, name .. ".buff1", "buff1", instance.parameters.color_1, first);
buff2 = instance:addStream("buff2", core.Bar, name .. ".buff2", "buff2", instance.parameters.color_2, first);
buff3 = instance:addStream("buff3", core.Bar, name .. ".buff3", "buff3", instance.parameters.color_3, first);
buff4 = instance:addStream("buff4", core.Bar, name .. ".buff4", "buff4", instance.parameters.color_4, first);
buff5 = instance:addStream("buff5", core.Line, name .. ".buff5", "buff5", instance.parameters.color_5, first);
end
function Update(period, mode)
if (period>first) then
MA1:update(mode);
MA2:update(mode);
MA3:update(mode);
MA4:update(mode);
MA5:update(mode);
MA6:update(mode);
MA7:update(mode);
local DeltaMA1=MA1.DATA[period]-MA2.DATA[period];
local DeltaMA2=MA1.DATA[period]-MA3.DATA[period];
local DeltaMA3=MA1.DATA[period]-MA4.DATA[period];
local DeltaMA4=MA1.DATA[period]-MA5.DATA[period];
local DeltaMA5=MA1.DATA[period]-MA6.DATA[period];
local DeltaMA6=MA1.DATA[period]-MA7.DATA[period];
local DL=buff1[period-1]+buff2[period-1]+buff3[period-1]+buff4[period-1];
local DeltaFan=DeltaMA6-DeltaMA1;
buff1[period]=0;
buff2[period]=0;
buff3[period]=0;
buff4[period]=0;
if DeltaFan==DL then
buff1[period]=buff1[period-1];
buff2[period]=buff2[period-1];
buff3[period]=buff3[period-1];
buff4[period]=buff4[period-1];
else
if ((MA2.DATA[period]>MA3.DATA[period] and MA3.DATA[period]>MA4.DATA[period] and MA4.DATA[period]>MA5.DATA[period] and MA5.DATA[period]>MA6.DATA[period] and MA6.DATA[period]>MA7.DATA[period]) or (MA2.DATA[period]<MA3.DATA[period] and MA3.DATA[period]<MA4.DATA[period] and MA4.DATA[period]<MA5.DATA[period] and MA5.DATA[period]<MA6.DATA[period] and MA6.DATA[period]<MA7.DATA[period])) then
if (DeltaFan>DL and DeltaFan>0) or (DeltaFan<DL and DeltaFan<0) then
buff1[period]=DeltaFan;
else
buff2[period]=DeltaFan;
end
else
if (DeltaFan>DL and DeltaFan>0) or (DeltaFan<DL and DeltaFan<0) then
buff3[period]=DeltaFan;
else
buff4[period]=DeltaFan;
end
end
end
buff5[period]=(DeltaMA1+DeltaMA2+DeltaMA3+DeltaMA4+DeltaMA5+DeltaMA6)/6.;
end
end