2017-09-04

Get Technical Analysis Indicators from BigObject In-Data Computing

Get Technical Analysis Indicators from BigObject In-Data Computing

BigObject In-Data Computing allows users to write their own program with LUA programming language. Similar to the store procedure concept of the SQL server, but a LUA script can do whatever users want to do together with the data stored in the BigObject. Like technical analysis applications of the financial market, BigObject can generate indicators like SMA,EMA,..etc. by walking thru data of the table with a LUA function ta().
The definition of SMA(simple moving average) is:
SMA(p,N)= ∑i=1,N pi / N

And the EMA(exponential moving average) is

EMA(p,N)i = α x pi + (1-α) x EMA(p,N)i-1
α=2/(N-1)

Convert these formulas into LUA we can get the attached sample code.

When we want to run this TA function, we simply use the APPLY command to call it up like:
APPLY ta('sma','stock','Close','ma', 120, 0,0,0,0)
APPLY ta('ema','stock','Close','ma', 120, 0,0,0,0)
APPLY ta('var','stock','Close','ma', 120, 0,0,0,0)
APPLY ta('sd','stock','Close','ma', 120, 0,0,0,0)


Source Code:

function ta(fun,v1,v2,v3,v4,v5,v6,v7,v8)
function sma(bt, col, rt_col, n)
t = bo.getTable(bt)
rt = t:getColumnValue(col);
for i = 2, #rt do
s=0.0
cnt=0
for j = 0, n-1 do
if (i-j>0) then
s = s + rt[i-j]
cnt=cnt+1.0
end
end
rt[i] = s/cnt
end
t:setColumnValue(rt_col, rt)
end

function ema(bt, col, rt_col, n)
t = bo.getTable(bt)
rt = t:getColumnValue(col);
alpha=2/(N+1)
for i = 2, #rt do
rt[i] = alpha*rt[i]+(1-alpha)*rt[i-1]
end
t:setColumnValue(rt_col, rt)
end

function var(bt, col, rt_col, n)
t = bo.getTable(bt)
rt = t:getColumnValue(col);
for i = 2, #rt do
s=0.0
cnt=0
for j = 0, n-1 do
if (i-j>0) then
s = s + rt[i+j]
cnt=cnt+1.0
end
end
av = s/cnt
s=0.0
cnt=0
for j = 0, n-1 do
if (i-j>0) then
s = s + math.pow((rt[i-j] - av),2)
cnt=cnt+1.0
end
end
rt[i] = s/cnt

end
t:setColumnValue(rt_col, rt)
end

function sd(bt, col, rt_col, n)
t = bo.getTable(bt)
rt = t:getColumnValue(col);
for i = 2, #rt do
s=0.0
cnt=0
for j = 0, n-1 do
if (i-j>0) then
s = s + rt[i-j]
cnt=cnt+1.0
end
end
av = s/cnt
s=0.0
cnt=0
for j = 0, n-1 do
if (i-j>0) then
s = s + math.pow((rt[i-j] - av),2)
cnt=cnt+1.0
end
end
rt[i] = math.pow(s/cnt,0.5)

end
t:setColumnValue(rt_col, rt)
end

if (fun=='sma') then
sma(v1,v2,v3,v4)
elseif (fun==’ema’) then
var(v1,v2,v3,v4)
elseif (fun=='var') then
var(v1,v2,v3,v4)
elseif (fun=='sd') then
sd(v1,v2,v3,v4)
end
end




沒有留言:

張貼留言