FXCM Forex Trading

MACD With Histogram Coloring

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

MACD With Histogram Coloring

Postby TonyMod » Wed Jan 06, 2010 2:15 pm

Hello Everybody,

Here i'm posting a new indicator variation of MACD which has small change to it, histogram is colored now. It compares current histogram bar to previous one and colors current one into Green if this current bar is bigger than previous, or if bar is smaller than previous then current will be colored with Red.

This was developed on user's request, here is the original topic: http://www.fxcodebase.com/code/viewtopic.php?f=18&t=134

Description on how to use "Moving Average Convergence/Divergence (MACD)" can be found here: http://www.investopedia.com/terms/m/macd.asp

Screenshot:
MACD1.gif
Screenshot of MACD in MarketScope.


Download:
MACD - With Histogram Coloring.lua
DOWNLOAD: MACD with Histogram Coloring .Lua
(5.16 KiB) Downloaded 4824 times


Source:
Code: Select all
-- MACD
-- Moving Average Convergence/Divergence
-- MACD uses moving averages, which are lagging indicators, to include
-- some trend-following characteristics. These lagging indicators are
-- turned into a momentum oscillator by subtracting the longer moving
-- average from the shorter moving average. The resulting plot forms a
-- line that oscillates above and below zero, without any upper or lower
-- limits.
-- The MACD produces three lines: MACD, SIGNAL and HISTOGRAM.
-- The classic formulae is:
-- MACD = EMA(price; 12) - EMA(price; 26)
-- SIGNAL = EMA(MACD; 9)
-- HISTOGRAM = MACD - SIGNAL

-- NOTE: **************************************************************************************************
-- NOTE: THIS VERSION OF THE INDICATOR HAS ADDITIONAL COLORING TO UNDERLINE HISTOGRAM UP AND DOWN MOVEMENTS
-- NOTE: **************************************************************************************************

-- The indicator corresponds to the MACD indicator in MetaTrader.
-- The formula is described in the Kaufman "Trading Systems and Methods" chapter 6 "Momentum and Oscillators" (page 128-130)

-- Indicator profile initialization routine
-- Defines indicator profile properties and indicator parameters

function Init()
    indicator:name("MACD1");
    indicator:description("Same as MACD indicator only with histogram movement coloring.");
    indicator:requiredSource(core.Tick);
    indicator:type(core.Oscillator);

    indicator.parameters:addInteger("SN", "Short EMA", "(SN)No Description", 12, 2, 1000);
    indicator.parameters:addInteger("LN", "Long EMA", "(LN)No Description", 26, 2, 1000);
    indicator.parameters:addInteger("IN", "Signal Line", "(IN)No Description", 9, 2, 1000);
    indicator.parameters:addColor("MACD_color", "MACD color", "(MACD Color)Red", core.rgb(255, 0, 0));
    indicator.parameters:addColor("SIGNAL_color", "Signal color", "(Signal Color) Blue", core.rgb(0, 0, 255));
    indicator.parameters:addColor("HISTOGRAM_color", "Histogram GREEN", "Histogram GREEN", core.rgb(0, 255, 0));
    indicator.parameters:addColor("HISTOGRAM2_color", "Histogram RED", "Histogram RED", core.rgb(255, 0, 0));
end

-- Indicator instance initialization routine
-- Processes indicator parameters and creates output streams
-- Parameters block
local SN;
local LN;
local IN;

local firstPeriodMACD;
local firstPeriodSIGNAL;
local source = nil;

local EMAS = nil;
local EMAL = nil;
local MVAI = nil;

local INHIST = nil;

-- Streams block
local MACD = nil;
local SIGNAL = nil;
local HISTOGRAM = nil;
local HISTOGRAM2 = nil;

-- Routine
function Prepare()
    SN = instance.parameters.SN;
    LN = instance.parameters.LN;
    IN = instance.parameters.IN;
    source = instance.source;

    -- Check parameters
    if (LN <= SN) then
       error("The short EMA period must be smaller than long EMA period");
    end

    -- Create short and long EMAs for the source
    EMAS = core.indicators:create("EMA", source, SN);
    EMAL = core.indicators:create("EMA", source, LN);

    -- Base name of the indicator.
    local name = profile:id() .. "(" .. source:name() .. ", " .. SN .. ", " .. LN .. ", " .. IN .. ")";
    instance:name(name);

    -- Create the output stream for the MACD. The first period is equal to the
    -- biggest first period of source EMA streams
    firstPeriodMACD = EMAL.DATA:first();
    MACD = instance:addStream("MACD", core.Line, name .. ".MACD", "MACD", instance.parameters.MACD_color, firstPeriodMACD);

    -- Create MVA for the MACD output stream.
    MVAI = core.indicators:create("MVA", MACD, IN);
   
    -- Create output for the signal and histogram
    firstPeriodSIGNAL = MVAI.DATA:first();
    SIGNAL = instance:addStream("SIGNAL", core.Line, name .. ".SIGNAL", "SIGNAL", instance.parameters.SIGNAL_color, firstPeriodSIGNAL);
    HISTOGRAM = instance:addStream("HISTOGRAMUP", core.Bar, name .. ".HISTOGRAMUP", "HISTOGRAMUP", instance.parameters.HISTOGRAM_color, firstPeriodSIGNAL);
    HISTOGRAM2 = instance:addStream("HISTOGRAMDOWN", core.Bar, name .. ".HISTOGRAMDOWN", "HISTOGRAMDOWN", instance.parameters.HISTOGRAM2_color, firstPeriodSIGNAL);
    INHIST = instance:addInternalStream(firstPeriodSIGNAL);
end

-- Indicator calculation routine
function Update(period, mode)
    -- and update short and long EMAs for the source.
    EMAS:update(mode);
    EMAL:update(mode);

    if (period >= firstPeriodMACD) then
        -- calculate MACD output
         MACD[period] = EMAS.DATA[period] - EMAL.DATA[period];
    end

    -- update MVA on the MACD
    MVAI:update(mode);
   
    if (period >= firstPeriodSIGNAL) then
        SIGNAL[period] = MVAI.DATA[period];
        -- calculate histogram as a difference between MACD and signal
        local diff = MACD[period] - SIGNAL[period];
        INHIST[period] = MACD[period] - SIGNAL[period];
       
        if(period >= firstPeriodSIGNAL + 2) then
            if( INHIST[period] > INHIST[period - 1]) then
                HISTOGRAM[period] = MACD[period] - SIGNAL[period];
            else
                HISTOGRAM2[period] = MACD[period] - SIGNAL[period];
            end
        end       
    end   
end


Any questions please respond to this topic.
Best Regards,

"TonyMod"
(FXCodeBase.com Forum Moderator)
TonyMod
FXCodeBase: Site Admin
 
Posts: 70
Joined: Wed Oct 21, 2009 1:57 pm
Location: New Jersey

Re: MACD With Histogram Coloring

Postby Panther » Mon Nov 21, 2011 7:43 am

Great Indicator. Could you program "width" and "style" options like those available for MVA's, etc ?
Panther
 
Posts: 71
Joined: Mon Nov 21, 2011 7:31 am

Re: MACD With Histogram Coloring

Postby Apprentice » Tue Nov 22, 2011 4:36 am

Style Option Added.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 17153
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: MACD With Histogram Coloring

Postby Laurus12 » Tue Nov 29, 2011 8:21 pm

Is it possible to have the histogram paint in the background? I have been looking through the code, but are not able to understand which code lines are defining the plotting order.

Thanks.
Laurus
Laurus12
FXCodeBase: Confirmed User
 
Posts: 45
Joined: Tue Jan 26, 2010 6:49 pm
Location: Norway

Re: MACD With Histogram Coloring

Postby Apprentice » Thu Dec 01, 2011 9:33 am

Your request is added to the developmental cue.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 17153
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: MACD With Histogram Coloring

Postby Trader1 » Mon Dec 12, 2011 9:29 am

Thanks for this indicator,
could you please change it so the MACD line is not obscured by the histogram?
the MACD line and signal line should be on top of the histogram for better view,
as it is now it is difficult to se the lines,
many thanks
Trader1
 
Posts: 17
Joined: Fri Jul 22, 2011 6:15 am

Re: MACD With Histogram Coloring

Postby Apprentice » Wed Dec 14, 2011 3:04 am

Output Stream sequence changed.
MACD - With Histogram Coloring v2.lua
(5.84 KiB) Downloaded 1356 times
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 17153
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: MACD With Histogram Coloring

Postby Laurus12 » Wed Dec 14, 2011 6:50 am

Thanks a lot Apprentice. Really appreciate it :)
Laurus12
FXCodeBase: Confirmed User
 
Posts: 45
Joined: Tue Jan 26, 2010 6:49 pm
Location: Norway

Re: MACD With Histogram Coloring

Postby Panther » Tue Feb 07, 2012 7:21 pm

Could you add a "Histo Multiplier" that would adjust the height of the histogram?
Thanks.
Panther
 
Posts: 71
Joined: Mon Nov 21, 2011 7:31 am

Re: MACD With Histogram Coloring

Postby Apprentice » Wed Feb 08, 2012 6:02 am

Histo Multiplier Added.
MACD - With Histogram Coloring.lua
(6.07 KiB) Downloaded 1282 times
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 17153
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Next

Return to Custom Indicators

Who is online

Users browsing this forum: Bing [Bot], Yahoo [Bot] and 12 guests