JoJ February 2012 Article

From J Wiki
Jump to navigation Jump to search
read=. 1!:1
write=. 1!:2
tonumbers=. ".
tochars=. ":
NUMBERS=. ?100#100
(tochars NUMBERS) write <'data.txt'
DATA=. tonumbers read <'data.txt'
DATA -: NUMBERS
sum=. +/
count=. #
mean=. sum % count
variance=. mean@:*: - *:@:mean
stdev=. %: @ variance
rolling=. 2 : 'v\' NB. Ignoring the left parameter for mnemonics

mean DATA
stdev DATA
20 observation rolling mean DATA
20 observation rolling stdev DATA
20 observation rolling (mean % stdev) DATA

load 'plot'
pd 'reset'
pd 20 observation rolling mean DATA
pd 20 observation rolling stdev DATA
pd 20 observation rolling (mean % stdev) DATA
pd 'show'

load 'dd'
CON=. ddcon 'dsn=mydatabase'
DATA=. ddfet ('select * from mytable' ddsel CON) , _1
dddis CON

3 : 0''
 if. IFWIN do.
  LIBCURL=: '"C:\Users\Thomas\Downloads\libcurl.dll" '
 else.
  LIBCURL=: 'libcurl.dll '
 end.
)

curl_init=. [:{.(LIBCURL,' curl_easy_init * ') & cd
curl_cleanup=. (LIBCURL,' curl_easy_cleanup n * ') & cd
curl_setopt=. (LIBCURL,' curl_easy_setopt n * i *') & cd
curl_perform=. (LIBCURL,' curl_easy_perform i * ') & cd
CURLOPT_URL=. 10002
CURLOPT_FILE=. 10001

open_file=. 1!:21
close_file=. 1!:22
yahoo_url=. 'http://ichart.finance.yahoo.com/table.csv?=%5E',,&'&d=0&e=9&f=2012&g=d&a=11&b=23&c=1980&ignore=.csv'

download_history=. verb define
F=. open_file <y
NB. Create a file with the same name as the symbol
C=. curl_init ''
curl_setopt C;CURLOPT_URL;yahoo_url y
NB. Set the URL curl will fetch
curl_setopt C;CURLOPT_FILE;<<F
NB. Tell curl which file to download to
curl_perform <C
NB. Perform the HTTP request
curl_cleanup <C
close_file F
NB. Close the file
)

SYMBOLS=. 'GSPC';'DJA';'IXIC'
download_history each SYMBOLS
append_comma=. ,&','
cut=. ;._2
cut_csv=. [:}. < cut @ append_comma cut
fetch=. &{::
eachrow=. "1
get_close=. tonumbers @: (4 fetch eachrow)
NB. The closing price is the 4th column of data
log=. ^.
return=. log @ %/ NB. Continuously compounded return
RETURNS=. (2 observation rolling return ]) @ get_close @ cut_csv @ read SYMBOLS