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

Hi,

<o:p> </o:p>

Can you not do:

<o:p> </o:p>

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

<o:p> </o:p>

<o:p> </o:p>

<o:p> </o:p>

<o:p> </o:p>

<o:p> </o:p>

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

<o:p> </o:p>

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

  }

  ‘’’

<o:p> </o:p>

  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. 

<o:p> </o:p>

  Do you have any suggestions? Thanks!

<o:p> </o:p>

hzadonis


Submitted via Google Groups

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!

<o:p> </o:p>

<o:p> </o:p>

<o:p> </o:p>

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

<o:p> </o:p>

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”

<o:p> </o:p>

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

  }

  ‘’’

<o:p> </o:p>

  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. 

<o:p> </o:p>

  Do you have any suggestions? Thanks!

<o:p> </o:p>

hzadonis


Submitted via Google Groups