cancel
Showing results for 
Search instead for 
Did you mean: 

The MACD calculator

hzadonis
New Contributor
Hi, Dear all:
  I'm trying to draw the MACD curve based on Close Price. You know, MACD are based on the EMA short(12 periods) and EMA long(26 periods). So, I write a function to do the calculation.
  '''
    / calculate expma, cp: Close Price; n: n_period
    calexpma:{[cp;n]
      emaend: enlist 0f;    / declare a list to contain the ema value of every day based on Close Price
      expmaend: enlist 0f; / declare a list to contain the expma value of every day based on Close Price and previous ema value
      cnt_i: 0;
      while[cnt_i < n;
        emaend:emaend, (((2*cp[cnt_i])+(last emaend)*cnt_i)%cnt_i+2);    / calculate ema and append it into emaend list
  expmaend:expmaend, ((2*cp[cnt_i]%n+1) + (n-1)*(first -2#emaend)%n+1);    / calculate expma and append it into emaend list
        /break;
  cnt_i+:1;
       ];
      last expmaend / return the last value of expmaend
  }
  '''

  I did test with a input value: cp_ini: 1 2 3 4, and invoke the function with calexpma[cp_ini; 4]. The output seems OK.
  But once I input value with real Close Price, the output is not as same as the other softwares. 

  Do you have any suggestions? Thanks!

hzadonis
3 REPLIES 3

sandy_bradley
New Contributor

Hi,

 

Can you not do:

 

select macd: ( ema [ 2 % 26; close ] ) – ema [  2 % 71; close ] from priceCandleTable

 

 

 

 

 

From: hzadonis@gmail.com
Sent: Monday, 15 April 2019 10:07
To: Kdb+ Personal Developers
Subject: [personal kdb+] The MACD calculator

 

Hi, Dear all:

  I'm trying to draw the MACD curve based on Close Price. You know, MACD are based on the EMA short(12 periods) and EMA long(26 periods). So, I write a function to do the calculation.

  '''

    / calculate expma, cp: Close Price; n: n_period

    calexpma:{[cp;n]

      emaend: enlist 0f;    / declare a list to contain the ema value of every day based on Close Price

      expmaend: enlist 0f; / declare a list to contain the expma value of every day based on Close Price and previous ema value

      cnt_i: 0;

      while[cnt_i < n;

        emaend:emaend, (((2*cp[cnt_i])+(last emaend)*cnt_i)%cnt_i+2);    / calculate ema and append it into emaend list

                expmaend:expmaend, ((2*cp[cnt_i]%n+1) + (n-1)*(first -2#emaend)%n+1);    / calculate expma and append it into emaend list

        /break;

                cnt_i+:1;

       ];

      last expmaend           / return the last value of expmaend

  }

  '''

 

  I did test with a input value: cp_ini: 1 2 3 4, and invoke the function with calexpma[cp_ini; 4]. The output seems OK.

  But once I input value with real Close Price, the output is not as same as the other softwares. 

 

  Do you have any suggestions? Thanks!

 

hzadonis

--
You received this message because you are subscribed to the Google Groups "Kdb+ Personal Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to personal-kdbplus+unsubscribe@googlegroups.com.
To post to this group, send email to personal-kdbplus@googlegroups.com.
Visit this group at https://groups.google.com/group/personal-kdbplus.
For more options, visit https://groups.google.com/d/optout.

 

hzadonis
New Contributor
Hi, Sandy:
  It's amazing. I noticed there's a ema in Q, but don't know how to use it.
  With your sample, it works and matches with others. The parameter of ema function should be "2%13" and "2%27"

Thanks so much!
hzadonsi

在 2019年4月15日星期一 UTC+8下午4:07:44,hzad...@gmail.com写道:
Hi, Dear all:
  I'm trying to draw the MACD curve based on Close Price. You know, MACD are based on the EMA short(12 periods) and EMA long(26 periods). So, I write a function to do the calculation.
  '''
    / calculate expma, cp: Close Price; n: n_period
    calexpma:{[cp;n]
      emaend: enlist 0f;    / declare a list to contain the ema value of every day based on Close Price
      expmaend: enlist 0f; / declare a list to contain the expma value of every day based on Close Price and previous ema value
      cnt_i: 0;
      while[cnt_i < n;
        emaend:emaend, (((2*cp[cnt_i])+(last emaend)*cnt_i)%cnt_i+2);    / calculate ema and append it into emaend list
  expmaend:expmaend, ((2*cp[cnt_i]%n+1) + (n-1)*(first -2#emaend)%n+1);    / calculate expma and append it into emaend list
        /break;
  cnt_i+:1;
       ];
      last expmaend / return the last value of expmaend
  }
  '''

  I did test with a input value: cp_ini: 1 2 3 4, and invoke the function with calexpma[cp_ini; 4]. The output seems OK.
  But once I input value with real Close Price, the output is not as same as the other softwares. 

  Do you have any suggestions? Thanks!

hzadonis

Great stuff. You are welcome!

 

 

 

From: hzadonis@gmail.com
Sent: Tuesday, 16 April 2019 08:02
To: Kdb+ Personal Developers
Subject: [personal kdb+] Re: The MACD calculator

 

Hi, Sandy:

  It's amazing. I noticed there's a ema in Q, but don't know how to use it.

  With your sample, it works and matches with others. The parameter of ema function should be "2%13" and "2%27"

 

Thanks so much!

hzadonsi

2019415日星期一 UTC+8下午4:07:44hzad...@gmail.com写道:

Hi, Dear all:

  I'm trying to draw the MACD curve based on Close Price. You know, MACD are based on the EMA short(12 periods) and EMA long(26 periods). So, I write a function to do the calculation.

  '''

    / calculate expma, cp: Close Price; n: n_period

    calexpma:{[cp;n]

      emaend: enlist 0f;    / declare a list to contain the ema value of every day based on Close Price

      expmaend: enlist 0f; / declare a list to contain the expma value of every day based on Close Price and previous ema value

      cnt_i: 0;

      while[cnt_i < n;

        emaend:emaend, (((2*cp[cnt_i])+(last emaend)*cnt_i)%cnt_i+2);    / calculate ema and append it into emaend list

           expmaend:expmaend, ((2*cp[cnt_i]%n+1) + (n-1)*(first -2#emaend)%n+1);    / calculate expma and append it into emaend list

        /break;

           cnt_i+:1;

       ];

      last expmaend      / return the last value of expmaend

  }

  '''

 

  I did test with a input value: cp_ini: 1 2 3 4, and invoke the function with calexpma[cp_ini; 4]. The output seems OK.

  But once I input value with real Close Price, the output is not as same as the other softwares. 

 

  Do you have any suggestions? Thanks!

 

hzadonis

--
You received this message because you are subscribed to the Google Groups "Kdb+ Personal Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to personal-kdbplus+unsubscribe@googlegroups.com.
To post to this group, send email to personal-kdbplus@googlegroups.com.
Visit this group at https://groups.google.com/group/personal-kdbplus.
For more options, visit https://groups.google.com/d/optout.