あなたの問題は、単に株価を差し引くよりも複雑だと思います。日付も保存する必要があります (ファイル名から推測できる一貫した期間がない場合)。
ただし、データ量はそれほど多くありません。過去 30 年間、毎日、毎秒、毎秒、300 ストックのデータがあるとしても、5Tb の非圧縮データに相当するため、ハイエンドの家庭用コンピューター (MAC Pro など) にすべてを保存することができます。 .
毎日YahooでIBM株を追跡し、それを「通常」(調整された終値のみ)に保存し、あなたが言及した「差分法」を使用してから、gzipを使用してそれらを圧縮する、迅速で汚いスクリプトを作成しました。節約できます: 16K 対 10K。問題は、日付を保存しなかったことです。どの値がどの日付に対応しているかわかりません。もちろん、これを含める必要があります。
幸運を。
import urllib as ul
import binascii as ba
# root URL
url = 'http://ichart.finance.yahoo.com/table.csv?%s'
# dictionary of options appended to URL (encoded)
opt = ul.urlencode({
's':'IBM', # Stock symbol or ticker; IBM
'a':'00', # Month January; index starts at zero
'b':'2', # Day 2
'c':'1978', # Year 2009
'd':'10', # Month November; index starts at zero
'e':'30', # Day 30
'f':'2009', # Year 2009
'g':'d', # Get daily prices
'ignore':'.csv', # CSV format
})
# get the data
data = ul.urlopen(url % opt)
# get only the "Adjusted Close" (last column of every row; the 7th)
close = []
for entry in data:
close.append(entry.strip().split(',')[6])
# get rid of the first element (it is only the string 'Adj Close')
close.pop(0)
# write to file
f1 = open('raw.dat','w')
for element in close:
f1.write(element+'\n')
f1.close()
# simple function to convert string to scaled number
def scale(x):
return int(float(x)*100)
# apply the previously defined function to the list
close = map(scale,close)
# it is important to store the first element (it is the base scale)
base = close[0]
# normalize all data (difference from nom)
close = [ close[k+1] - close[k] for k in range(len(close)-1)]
# introduce the base to the data
close.insert(0,base)
# define a simple function to convert the list to a single string
def l2str(list):
out = ''
for item in list:
if item>=0:
out += '+'+str(item)
else:
out += str(item)
return out
# convert the list to a string
close = l2str(close)
f2 = open('comp.dat','w')
f2.write(close)
f2.close()
次に、「生データ」(raw.dat)と提案する「圧縮形式」(comp.dat)を比較します。
:sandbox jarrieta$ ls -lh
total 152
-rw-r--r-- 1 jarrieta staff 23K Nov 30 09:28 comp.dat
-rw-r--r-- 1 jarrieta staff 47K Nov 30 09:28 raw.dat
-rw-r--r-- 1 jarrieta staff 1.7K Nov 30 09:13 stock.py
:sandbox jarrieta$ gzip --best *.dat
:sandbox jarrieta$ ls -lh
total 64
-rw-r--r-- 1 jarrieta staff 10K Nov 30 09:28 comp.dat.gz
-rw-r--r-- 1 jarrieta staff 16K Nov 30 09:28 raw.dat.gz
-rw-r--r-- 1 jarrieta staff 1.7K Nov 30 09:13 stock.py