Colorful MACD (aka Golden MT4's MACD)

Here you can post and download custom indicators. PLEASE: Do not start topics unless you are posting your own indicator, they will be moved to appropriate section even if you do.

Moderator: admin

Colorful MACD (aka Golden MT4's MACD)

Postby Nikolay.Gekht » Tue Feb 16, 2010 6:16 pm

This is the usual MACD indicator with the following features:
  • Unlike the standard TS implementation this indicator shows the MACD as a histogram and SIGNAL as a line (the standard indicator shows MACD and SIGNAL as lines and the difference between MACD and SIGNAL as a histogram)
  • The histogram bars are colored in case the bar is above (below) the signal line. The same coloring is used in MT4 Golden MACD indicator.

Formula:
MACD = EMA(price, short) - EMA(price, long)
SIGNAL = MVA(MACD, signal)

Preview:
gmacd.png


Download:
GMACD.lua
(3.87 KiB) Downloaded 3220 times


GMACD with Alert.lua
(16.01 KiB) Downloaded 1075 times


EURUSD m1 (07-20-2016 1247).png

Above 0 & above signal = Blue.
Above 0 & below signal = Light Blue.
Below 0 & below signal = Red.
Below 0 & above signal = Light Red.
4 color GMACD histogram with Alerts.lua
(18.17 KiB) Downloaded 1051 times


The indicator was revised and updated
Nikolay.Gekht
FXCodeBase: Site Admin
 
Posts: 1235
Joined: Wed Dec 16, 2009 6:39 pm
Location: Cary, NC

Re: Colorful MACD (aka Golden MT4's MACD)

Postby Hybrid » Sun Aug 08, 2010 6:49 am

This is a very useful indicator.
Can a bigger time frame version be created?
Hybrid
 
Posts: 42
Joined: Fri Apr 02, 2010 7:52 am

Re: Colorful MACD (aka Golden MT4's MACD)

Postby Apprentice » Sun Aug 08, 2010 1:17 pm

Added to the Development cue.
I have to check but I think someone has already written what one.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 27196
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: Colorful MACD (aka Golden MT4's MACD)

Postby Alexander.Gettinger » Mon Aug 09, 2010 9:50 pm

Bigger time frame indicator.

BF_GMACD.png


Code: Select all
function Init()
    indicator:name("Bigger timeframe GMACD");
    indicator:description("");
    indicator:requiredSource(core.Bar);
    indicator:type(core.Oscillator);

    indicator.parameters:addGroup("Calculation");
    indicator.parameters:addString("BS", "Time frame to calculate GMACD", "", "D1");
    indicator.parameters:setFlag("BS", core.FLAG_PERIODS);
    indicator.parameters:addInteger("SN", "Short EMA", "", 12, 2, 1000);
    indicator.parameters:addInteger("LN", "Long EMA", "", 26, 2, 1000);
    indicator.parameters:addInteger("IN", "Signal Line", "", 9, 2, 1000);
    indicator.parameters:addGroup("Display");
    indicator.parameters:addColor("SIGNAL_color", "The signal line color", "", core.rgb(127, 255, 0));
    indicator.parameters:addColor("HISTOGRAM_color", "The histogramm color", "", core.rgb(127, 127, 127));
    indicator.parameters:addColor("HISTOGRAM1_color", "The histogramm color 1", "", core.rgb(0, 0, 255));
    indicator.parameters:addColor("HISTOGRAM2_color", "The histogramm color 2", "", core.rgb(255, 0, 0));
end

local source;                   -- the source
local bf_data = nil;          -- the high/low data
local SN;
local LN;
local IN;
local BS;
local bf_length;                 -- length of the bigger frame in seconds
local dates;                    -- candle dates
local host;
local HISTOGRAM;
local HISTOGRAM1;
local HISTOGRAM2;
local SIGNAL;
local GMACD;
local day_offset;
local week_offset;
local extent;

function Prepare()
    source = instance.source;
    host = core.host;

    day_offset = host:execute("getTradingDayOffset");
    week_offset = host:execute("getTradingWeekOffset");

    BS = instance.parameters.BS;
    SN = instance.parameters.SN;
    LN = instance.parameters.LN;
    IN = instance.parameters.IN;
    extent = LN*2;

    local s, e, s1, e1;

    s, e = core.getcandle(source:barSize(), core.now(), 0, 0);
    s1, e1 = core.getcandle(BS, core.now(), 0, 0);
    assert ((e - s) <= (e1 - s1), "The chosen time frame must be bigger than the chart time frame!");
    bf_length = math.floor((e1 - s1) * 86400 + 0.5);

    local name = profile:id() .. "(" .. source:name() .. "," .. BS .. "," .. SN .. "," .. LN .. "," .. IN .. ")";
    instance:name(name);
    HISTOGRAM = instance:addStream("H", core.Bar, name .. ".H", "H", instance.parameters.HISTOGRAM_color, 0);
    HISTOGRAM1 = instance:addStream("H1", core.Bar, name .. ".H1", "H1", instance.parameters.HISTOGRAM1_color, 0);
    HISTOGRAM2 = instance:addStream("H2", core.Bar, name .. ".H2", "H2", instance.parameters.HISTOGRAM2_color, 0);
    SIGNAL = instance:addStream("SIGNAL", core.Line, name .. ".SIGNAL", "SIGNAL", instance.parameters.SIGNAL_color, 0);
end


local loading = false;
local loadingFrom, loadingTo;
local pday = nil;

-- the function which is called to calculate the period
function Update(period, mode)
    -- get date and time of the hi/lo candle in the reference data
    local bf_candle;
    bf_candle = core.getcandle(BS, source:date(period), day_offset, week_offset);

    -- if data for the specific candle are still loading
    -- then do nothing
    if loading and bf_candle >= loadingFrom and (loadingTo == 0 or bf_candle <= loadingTo) then
        return ;
    end

    -- if the period is before the source start
    -- the do nothing
    if period < source:first() then
        return ;
    end

    -- if data is not loaded yet at all
    -- load the data
    if bf_data == nil then
        -- there is no data at all, load initial data
        local to, t;
        local from;

        if (source:isAlive()) then
            -- if the source is subscribed for updates
            -- then subscribe the current collection as well
            to = 0;
        else
            -- else load up to the last currently available date
            t, to = core.getcandle(BS, source:date(period), day_offset, week_offset);
        end

        from = core.getcandle(BS, source:date(source:first()), day_offset, week_offset);
        HISTOGRAM:setBookmark(1, period);
        -- shift so the bigger frame data is able to provide us with the stoch data at the first period
        from = math.floor(from * 86400 - (bf_length * extent) + 0.5) / 86400;
        local nontrading, nontradingend;
        nontrading, nontradingend = core.isnontrading(from, day_offset);
        if nontrading then
            -- if it is non-trading, shift for two days to skip the non-trading periods
            from = math.floor((from - 2) * 86400 - (bf_length * extent) + 0.5) / 86400;
        end
        loading = true;
        loadingFrom = from;
        loadingTo = to;
        bf_data = host:execute("getHistory", 1, source:instrument(), BS, loadingFrom, to, source:isBid());
        GMACD = core.indicators:create("GMACD", bf_data.close, SN, LN, IN);
        return ;
    end

    -- check whether the requested candle is before
    -- the reference collection start
    if (bf_candle < bf_data:date(0)) then
        HISTOGRAM:setBookmark(1, period);
        if loading then
            return ;
        end
        -- shift so the bigger frame data is able to provide us with the stoch data at the first period
        from = math.floor(bf_candle * 86400 - (bf_length * extent) + 0.5) / 86400;
        local nontrading, nontradingend;
        nontrading, nontradingend = core.isnontrading(from, day_offset);
        if nontrading then
            -- if it is non-trading, shift for two days to skip the non-trading periods
            from = math.floor((from - 2) * 86400 - (bf_length * extent) + 0.5) / 86400;
        end
        loading = true;
        loadingFrom = from;
        loadingTo = bf_data:date(0);
        host:execute("extendHistory", 1, bf_data, loadingFrom, loadingTo);
        return ;
    end

    -- check whether the requested candle is after
    -- the reference collection end
    if (not(source:isAlive()) and bf_candle > bf_data:date(bf_data:size() - 1)) then
        HISTOGRAM:setBookmark(1, period);
        if loading then
            return ;
        end
        loading = true;
        loadingFrom = bf_data:date(bf_data:size() - 1);
        loadingTo = bf_candle;
        host:execute("extendHistory", 1, bf_data, loadingFrom, loadingTo);
        return ;
    end

    GMACD:update(mode);
    local p;
    p = findDateFast(bf_data, bf_candle, true);
    if p == -1 then
        return ;
    end
    if GMACD:getStream(0):hasData(p) then
        HISTOGRAM[period] = GMACD:getStream(0)[p];
    end
    if GMACD:getStream(1):hasData(p) then
        HISTOGRAM1[period] = GMACD:getStream(1)[p];
    end
    if GMACD:getStream(2):hasData(p) then
        HISTOGRAM2[period] = GMACD:getStream(2)[p];
    end
    if GMACD:getStream(3):hasData(p) then
        SIGNAL[period] = GMACD:getStream(3)[p];
    end
end

-- the function is called when the async operation is finished
function AsyncOperationFinished(cookie)
    local period;

    pday = nil;
    period = HISTOGRAM:getBookmark(1);

    if (period < 0) then
        period = 0;
    end
    loading = false;
    instance:updateFrom(period);
end

function findDateFast(stream, date, precise)
    local datesec = nil;
    local periodsec = nil;
    local min, max, mid;

    datesec = math.floor(date * 86400 + 0.5)

    min = 0;
    max = stream:size() - 1;

    while true do
        mid = math.floor((min + max) / 2);
        periodsec = math.floor(stream:date(mid) * 86400 + 0.5);
        if datesec == periodsec then
            return mid;
        elseif datesec > periodsec then
            min = mid + 1;
        else
            max = mid - 1;
        end
        if min > max then
            if precise then
                return -1;
            else
                return min - 1;
            end
        end
    end
end

BF_GMACD.lua
(8.09 KiB) Downloaded 1611 times
Alexander.Gettinger
FXCodeBase: Confirmed User
 
Posts: 3785
Joined: Wed Mar 31, 2010 9:40 pm
Location: Russia, Omsk

Re: Colorful MACD (aka Golden MT4's MACD)

Postby Hybrid » Tue Aug 10, 2010 9:54 am

Thank you, Alexander.
You guys rock!
Hybrid
 
Posts: 42
Joined: Fri Apr 02, 2010 7:52 am

Re: Colorful MACD (aka Golden MT4's MACD)

Postby Belvatrader » Mon Oct 25, 2010 5:16 pm

This is one of the best indicators for trading.
Can someone add the ability to change the type of MA?
Simple, exponential weighted, etc..

It would be very interesting!
Thanks in advance
Belvatrader
 
Posts: 4
Joined: Mon Oct 25, 2010 5:04 pm

Re: Colorful MACD (aka Golden MT4's MACD)

Postby Apprentice » Tue Oct 26, 2010 4:34 am

GMACD.lua
(7.11 KiB) Downloaded 1944 times

BF_GMACD.lua
(10.85 KiB) Downloaded 1710 times


This version allows you requested.

Price Type, Selection, Added.
Added MA Type Selection.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 27196
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: Colorful MACD (aka Golden MT4's MACD)

Postby Belvatrader » Tue Oct 26, 2010 9:37 am

Hello,
thank you very much for speed.
Let me ask you another thing.
Currently, the color of the bars is determined on the basis of the signal line.
You can set a variable so that the bar color is determined by the difference in length with the previous bar?
Thanks!
Belvatrader
 
Posts: 4
Joined: Mon Oct 25, 2010 5:04 pm


Re: Colorful MACD (aka Golden MT4's MACD)

Postby upsunx » Fri Dec 10, 2010 7:22 am

hi, Nice work.
Can you give a option to add a Simple Moving agave line to the bars? That will make easier to see the single across.
Please see the picture about my MACD in MT4.

Thanks again.
Attachments
MACD.JPG
MACD with MA(1)
upsunx
 
Posts: 1
Joined: Fri Dec 10, 2010 7:10 am

Next

Return to Custom Indicators

Who is online

Users browsing this forum: Google [Bot] and 7 guests