Heikin-Ashi

Hello I have a table with several instruments trading data like this:

instrument granularity time open high low close

----------------------------------------------------------------------------------------

AUD_USD D 2015.08.25D21:00:00.000000000 0.71278 0.715685 0.706985 0.712205

AUD_USD D 2015.08.26D21:00:00.000000000 0.71195 0.718155 0.7099 0.716595

AUD_USD D 2015.08.27D21:00:00.000000000 0.716555 0.720585 0.712075 0.71722

AUD_USD D 2015.08.30D21:00:00.000000000 0.71533 0.71638 0.70827 0.71136

AUD_USD D 2015.08.31D21:00:00.000000000 0.71115 0.71541 0.70145 0.701855

EUR_USD D 2015.09.29D04:00:00.000000000 1.12556 1.128145 1.119415 1.1258

EUR_USD D 2015.09.30D04:00:00.000000000 1.125865 1.126065 1.11458 1.11476

EUR_USD D 2015.10.01D04:00:00.000000000 1.11475 1.120695 1.113515 1.117975

EUR_USD D 2015.10.02D04:00:00.000000000 1.118 1.131855 1.115035 1.121205

EUR_USD D 2015.10.04D04:00:00.000000000 1.12207 1.124725 1.120765 1.122575

GBP_USD D 2015.08.25D21:00:00.000000000 1.56881 1.572025 1.545315 1.54633

GBP_USD D 2015.08.26D21:00:00.000000000 1.54624 1.550905 1.53702 1.540215

GBP_USD D 2015.08.27D21:00:00.000000000 1.54058 1.544335 1.53354 1.53912

GBP_USD D 2015.08.30D21:00:00.000000000 1.54145 1.54372 1.53393 1.534415

GBP_USD D 2015.08.31D21:00:00.000000000 1.534465 1.54085 1.53001 1.53051

I want to get the Heikin-Ashi values:

CandlesList:update HAclose:0.25*open+close+high+low by instrument from CandlesList;

CandlesList:update HAhigh:high|HAopen|HAclose by instrument from CandlesList;

CandlesList:update HAlow:low&HAopen&HAclose by instrument from CandlesList;

Probably is very easy, but I’cant write correctly the q code for HAopen:

To calculate HAopen the formula is:

First row of the table:

HAopen:0.5*open+close

Remaining rows of the table

HAopen:0.5*(prev HAopen)+(prev HAclose)

Can you help me, please?

Cheers

Francisco

One way to do it is

CandlesList:update HAopen:{0.5*x+0^y}\[(first open)+first close;prev HAclose] by instrument from CandlesList

it applies {0.5*x+0^y}\ (scan: http://code.kx.com/wiki/Reference/BackSlash) to an initial argument (first open)+first close and a list of previous HAclose values resulting in

HAopen_0: 0.5*(first open)+(first close) + 0^0n / (prev HAclose is 0n for the first row hence the coalesce operator)

HAopen_1: 0.5*HAopen_0+HAclose_0

etc.

Many Thanks. The explanation is also very appreciated.

Francisco