Mean

If you need an Indicator or Signal developed or translated from other language, please post all Indicator development REQUESTS to this section here.

Moderator: admin

Mean

Postby posurf » Fri Oct 08, 2010 3:04 pm

"moving average" since beginning of the session

Time session to be defined as a variable.

"moving average"= sum of the n bar close ('or else' ) since the beginning of the session devided by n

example
third bar: 1st bar plus second bar plus third bar divided by 3.

Interesting to keep as a line the close of last session.
Image
posurf
 
Posts: 5
Joined: Wed Sep 22, 2010 6:56 am


Re: Mean

Postby Alexander.Gettinger » Tue Oct 12, 2010 1:33 am

You may find this indicator here: viewtopic.php?f=17&t=2378
Alexander.Gettinger
FXCodeBase: Confirmed User
 
Posts: 3785
Joined: Wed Mar 31, 2010 9:40 pm
Location: Russia, Omsk

Re: Mean

Postby posurf » Wed Oct 13, 2010 1:15 am

Thanks

but is it possible to change the beginning of the time session, especially for the forex.
posurf
 
Posts: 5
Joined: Wed Sep 22, 2010 6:56 am


Re: Mean

Postby Alexander.Gettinger » Mon Oct 18, 2010 3:34 am

Added other periods of the time sessions.

Code: Select all
function Init()
    indicator:name("Mean indicator");
    indicator:description("Mean indicator");
    indicator:requiredSource(core.Tick);
    indicator:type(core.Indicator);

    indicator.parameters:addGroup("Calculation");
    indicator.parameters:addString("Mode", "Mode", "", "Day");
    indicator.parameters:addStringAlternative("Mode", "M1", "", "M1");
    indicator.parameters:addStringAlternative("Mode", "M5", "", "M5");
    indicator.parameters:addStringAlternative("Mode", "M15", "", "M15");
    indicator.parameters:addStringAlternative("Mode", "H1", "", "H1");
    indicator.parameters:addStringAlternative("Mode", "H2", "", "H2");
    indicator.parameters:addStringAlternative("Mode", "H3", "", "H3");
    indicator.parameters:addStringAlternative("Mode", "H4", "", "H4");
    indicator.parameters:addStringAlternative("Mode", "H6", "", "H6");
    indicator.parameters:addStringAlternative("Mode", "H8", "", "H8");
    indicator.parameters:addStringAlternative("Mode", "Day", "", "Day");
    indicator.parameters:addStringAlternative("Mode", "Month", "", "Month");

    indicator.parameters:addGroup("Style");
    indicator.parameters:addColor("clrUP", "UP Color", "UP Color", core.rgb(0, 255, 0));
    indicator.parameters:addColor("clrDN", "DN Color", "DN Color", core.rgb(255, 0, 0));
    indicator.parameters:addColor("clrPrev", "Prev Color", "Prev Color", core.rgb(0, 0, 255));
    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 MeanUP=nil;
local MeanDN=nil;
local Buff=nil;
local PrevBuff=nil;
local Mode;

function Prepare()
    source = instance.source;
    Mode=instance.parameters.Mode;
    first = source:first()+2;
    Buff = instance:addInternalStream(first, 0);
    local name = profile:id() .. "(" .. source:name() .. ", " .. instance.parameters.Mode .. ")";
    instance:name(name);
    MeanUP = instance:addStream("MeanUP", core.Line, name .. ".UP", "UP", instance.parameters.clrUP, first);
    MeanDN = instance:addStream("MeanDN", core.Line, name .. ".DN", "DN", instance.parameters.clrDN, first);
    PrevBuff = instance:createTextOutput ("Prev", "Prev", "Wingdings", 10, core.H_Center, core.V_Center, instance.parameters.clrPrev, first);
    MeanUP:setWidth(instance.parameters.widthLinReg);
    MeanUP:setStyle(instance.parameters.styleLinReg);
    MeanDN:setWidth(instance.parameters.widthLinReg);
    MeanDN:setStyle(instance.parameters.styleLinReg);
end

function Update(period, mode)
   if (period>first) then
      local d1,t1;
      d1,t1=source:date(period);
      local table1=core.dateToTable(d1);
      local CurD;
      if Mode=="M1" then
       CurD=table1.min;
      elseif Mode=="M5" then
       CurD=math.floor((table1.min)/5);
      elseif Mode=="M15" then
       CurD=math.floor((table1.min)/15);
      elseif Mode=="H1" then
       CurD=table1.hour;
      elseif Mode=="H2" then
       CurD=math.floor((table1.hour)/2);
      elseif Mode=="H3" then
       CurD=math.floor((table1.hour)/3);
      elseif Mode=="H4" then
       CurD=math.floor((table1.hour)/4);
      elseif Mode=="H6" then
       CurD=math.floor((table1.hour)/6);
      elseif Mode=="H8" then
       CurD=math.floor((table1.hour)/8);
      elseif Mode=="Day" then
       CurD=table1.day;
      elseif Mode=="Month" then
       CurD=table1.month;
      end
      local shift=period;
      local PrevD=CurD;
      while PrevD==CurD and shift>first do
       local d2,t2;
       d2,t2=source:date(shift);
       local table2=core.dateToTable(d2);
       if Mode=="M1" then
        PrevD=table2.min;
       elseif Mode=="M5" then
        PrevD=math.floor((table2.min)/5);
       elseif Mode=="M15" then
        PrevD=math.floor((table2.min)/15);
       elseif Mode=="H1" then
        PrevD=table2.hour;
       elseif Mode=="H2" then
        PrevD=math.floor((table2.hour)/2);
       elseif Mode=="H3" then
        PrevD=math.floor((table2.hour)/3);
       elseif Mode=="H4" then
        PrevD=math.floor((table2.hour)/4);
       elseif Mode=="H6" then
        PrevD=math.floor((table2.hour)/6);
       elseif Mode=="H8" then
        PrevD=math.floor((table2.hour)/8);
       elseif Mode=="Day" then
        PrevD=table2.day;
       elseif Mode=="Month" then
        PrevD=table2.month;
       end
       shift=shift-1;
      end
      shift=shift+1;
     
      if period>=shift+1 then
       Buff[period]=core.avg(source,core.range(shift+1,period));
       if Buff[period]>Buff[period-1] then
        MeanUP[period]=Buff[period];
        if Buff[period-1]<Buff[period-2] then
         MeanUP[period-1]=Buff[period-1];
        end
       else
        MeanDN[period]=Buff[period];
        if Buff[period-1]>Buff[period-2] then
         MeanDN[period-1]=Buff[period-1];
        end
       end
       if period>shift then
        PrevBuff:set(period, Buff[shift], "\159", "");
       end
      end
   
   end
end
Attachments
Mean.lua
(5.1 KiB) Downloaded 612 times
Alexander.Gettinger
FXCodeBase: Confirmed User
 
Posts: 3785
Joined: Wed Mar 31, 2010 9:40 pm
Location: Russia, Omsk


Return to Indicator and Signal Requests

Who is online

Users browsing this forum: Bing [Bot] and 20 guests