Zero Lag 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

Zero Lag MACD

Postby Nikolay.Gekht » Fri Jul 30, 2010 2:17 pm

Zero Lag MACD is initially introduced in the Stock & Commodities in 2000.

This version of MACD has much smaller delay in comparison with classic MACD.

Formula:
MACD = (2 * EMA(price, FAST) - EMA(EMA(price, FAST), FAST)) - (2 * EMA(price, SLOW) - EMA(EMA(price, SLOW), SLOW))
SIGNAL = 2 * EMA(MACD, SIG) - EMA(EMA(MACD, SIG), SIG))
HISTOGRAM = MACD - SIGNAL


zerolagmacd.png


Download:
ZeroLagMACD.lua
(2.88 KiB) Downloaded 4612 times

ZeroLagMACD With Alert.lua
(15.84 KiB) Downloaded 1875 times


EURUSD m1 (11-28-2016 1156).png

On Screen ZeroLagMACD.lua
(4.48 KiB) Downloaded 1470 times


Indicator-based strategy.
viewtopic.php?f=31&t=66908

Reverse Engineered ZeroLagMACD
https://fxcodebase.com/code/viewtopic.php?f=17&t=71262
Nikolay.Gekht
FXCodeBase: Site Admin
 
Posts: 1235
Joined: Wed Dec 16, 2009 6:39 pm
Location: Cary, NC

Re: Zero Lag MACD

Postby abcdefg » Thu Aug 05, 2010 11:32 am

Thank you Nikolay for this indicator :D . A higher time frame of this indicator would be highly appreciated (to place on my tick chart).
abcdefg
 
Posts: 4
Joined: Thu Aug 05, 2010 11:16 am

Re: Zero Lag MACD

Postby Alexander.Gettinger » Mon Aug 09, 2010 2:17 pm

This indicator for higher timeframe.

BF_ZeroLagMACD.png


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

    indicator.parameters:addGroup("Calculation");
    indicator.parameters:addString("BS", "Time frame to calculate indicator", "", "D1");
    indicator.parameters:setFlag("BS", core.FLAG_PERIODS);
    indicator.parameters:addInteger("FMA", "Fast EMA periods", "", 12, 1, 1000);
    indicator.parameters:addInteger("SMA", "Slow EMA Periods", "", 24, 1, 1000);
    indicator.parameters:addInteger("SigMA", "Signal EMA periods", "", 9, 1, 1000);
    indicator.parameters:addGroup("Display");
    indicator.parameters:addColor("MACD_color", "Color of MACD", "", core.rgb(255, 0, 0));
    indicator.parameters:addColor("SIG_color", "Color of Signal", "", core.rgb(0, 0, 255));
    indicator.parameters:addColor("HIS_color", "Color of Historgram", "", core.rgb(0, 255, 0));
end

local source;                   -- the source
local bf_data = nil;          -- the high/low data
local FMA;
local SMA;
local SigMA;
local BS;
local bf_length;                 -- length of the bigger frame in seconds
local dates;                    -- candle dates
local host;
local MACD;
local SIG;
local HIS;
local ZLMACD;
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;
    FMA = instance.parameters.FMA;
    SMA = instance.parameters.SMA;
    SigMA = instance.parameters.SigMA;
    extent = SMA*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 .. "," .. FMA .. "," .. SMA .. "," .. SigMA .. ")";
    instance:name(name);
    MACD = instance:addStream("MACD", core.Line, name .. ".MACD", "MACD", instance.parameters.MACD_color, 0);
    SIG = instance:addStream("SIG", core.Line, name .. ".SIG", "SIG", instance.parameters.SIG_color, 0);
    HIS = instance:addStream("HISTOGRAM", core.Bar, name .. ".HIS", "HIS", instance.parameters.HIS_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);
        MACD: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());
        ZLMACD = core.indicators:create("ZEROLAGMACD", bf_data.close, FMA, SMA, SigMA);
        return ;
    end

    -- check whether the requested candle is before
    -- the reference collection start
    if (bf_candle < bf_data:date(0)) then
        MACD: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
        MACD: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

    ZLMACD:update(mode);
    local p;
    p = findDateFast(bf_data, bf_candle, true);
    if p == -1 then
        return ;
    end
    if ZLMACD:getStream(0):hasData(p) then
        MACD[period] = ZLMACD:getStream(0)[p];
    end
    if ZLMACD:getStream(1):hasData(p) then
        SIG[period] = ZLMACD:getStream(1)[p];
    end
    if ZLMACD:getStream(2):hasData(p) then
        HIS[period] = ZLMACD:getStream(2)[p];
    end
end

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

    pday = nil;
    period = MACD: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
Attachments
BF_ZeroLagMACD.lua
(7.42 KiB) Downloaded 2235 times
Alexander.Gettinger
FXCodeBase: Confirmed User
 
Posts: 3785
Joined: Wed Mar 31, 2010 9:40 pm
Location: Russia, Omsk

Re: Zero Lag MACD

Postby abcdefg » Mon Aug 09, 2010 10:37 pm

Thank you kindly. You guys are super :D
Time to take over the world :lol:
abcdefg
 
Posts: 4
Joined: Thu Aug 05, 2010 11:16 am

Re: Zero Lag MACD

Postby Alexander.Gettinger » Thu Jul 05, 2012 4:43 pm

MQL4 version of Zero Lag MACD: viewtopic.php?f=38&t=20872
Alexander.Gettinger
FXCodeBase: Confirmed User
 
Posts: 3785
Joined: Wed Mar 31, 2010 9:40 pm
Location: Russia, Omsk

Re: Zero Lag MACD

Postby juju1024 » Thu Jan 03, 2013 5:43 pm

Hi,

Please, Can you create simple strategy for zerolagmacd, with alert when signal cross macd ?

thanks
juju1024
 
Posts: 48
Joined: Wed Sep 26, 2012 5:05 pm

Re: Zero Lag MACD

Postby Apprentice » Sat Jan 05, 2013 7:08 am

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


Re: Zero Lag MACD

Postby juju1024 » Mon Jan 14, 2013 10:40 pm

hi team,

Can you create a MTF zerolagmacd from this posted code for Mt4 platform

I would be most grateful to you for this

source code :
Code: Select all
//+------------------------------------------------------------------+
//|                                                 ZeroLag MACD.mq4 |
//|                                                               RD |
//|                                                 marynarz15@wp.pl |
//+------------------------------------------------------------------+
#property copyright "RD"
#property link      "marynarz15@wp.pl"
//----
#property indicator_separate_window
#property  indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red
//---- input parameters
extern int FastEMA = 12;
extern int SlowEMA = 24;
extern int SignalEMA = 9;
//---- buffers
double MACDBuffer[];
double SignalBuffer[];
double FastEMABuffer[];
double SlowEMABuffer[];
double SignalEMABuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(5);
   SetIndexBuffer(0, MACDBuffer);
   SetIndexBuffer(1, SignalBuffer);
   SetIndexBuffer(2, FastEMABuffer);
   SetIndexBuffer(3, SlowEMABuffer);
   SetIndexBuffer(4, SignalEMABuffer);
   SetIndexStyle(0, DRAW_HISTOGRAM);
   SetIndexStyle(1, DRAW_LINE,EMPTY);
   SetIndexDrawBegin(0, SlowEMA);
   SetIndexDrawBegin(1, SlowEMA);
   IndicatorShortName("ZeroLag MACD(" + FastEMA + "," + SlowEMA + "," + SignalEMA + ")");
   SetIndexLabel(0, "MACD");
   SetIndexLabel(1, "Signal");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int limit;
   int counted_bars = IndicatorCounted();
   if(counted_bars < 0)
       return(-1);
   if(counted_bars > 0)
       counted_bars--;
   limit = Bars - counted_bars;
   double EMA, ZeroLagEMAp, ZeroLagEMAq;
   for(int i = 0; i < limit; i++)
     {
       FastEMABuffer[i] = iMA(NULL, 0, FastEMA, 0, MODE_EMA, PRICE_CLOSE, i);
       SlowEMABuffer[i] = iMA(NULL, 0, SlowEMA, 0, MODE_EMA, PRICE_CLOSE, i);
     }
   for(i = 0; i < limit; i++)
     {
        EMA = iMAOnArray(FastEMABuffer, Bars, FastEMA, 0, MODE_EMA, i);
        ZeroLagEMAp = FastEMABuffer[i] + FastEMABuffer[i] - EMA;
        EMA = iMAOnArray(SlowEMABuffer, Bars, SlowEMA, 0, MODE_EMA, i);
        ZeroLagEMAq = SlowEMABuffer[i] + SlowEMABuffer[i] - EMA;
        MACDBuffer[i] = ZeroLagEMAp - ZeroLagEMAq;
     }
   for(i = 0; i < limit; i++)
       SignalEMABuffer[i] = iMAOnArray(MACDBuffer, Bars, SignalEMA, 0, MODE_EMA, i);
   for(i = 0; i < limit; i++)
     {
       EMA = iMAOnArray(SignalEMABuffer, Bars, SignalEMA, 0, MODE_EMA, i);
       SignalBuffer[i] = SignalEMABuffer[i] + SignalEMABuffer[i] - EMA;
     }
   return(0);
  }
//+------------------------------------------------------------------+
juju1024
 
Posts: 48
Joined: Wed Sep 26, 2012 5:05 pm

Re: Zero Lag MACD

Postby Apprentice » Tue Jan 15, 2013 5:31 am

I'm not sure what is your intention.
Something like this MTF HA.
viewtopic.php?f=17&t=3934&p=9727&hilit=MTF#p9727
Written for MT4.

And The basis for the calculation is attached MT4 code.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Next

Return to Custom Indicators

Who is online

Users browsing this forum: Majestic-12 [Bot] and 41 guests