Daten aus Ordner ziehen und bearbeiten
Verfasst: Dienstag 22. September 2009, 15:49
Hi,
habe folgendes Problem, habe folgenden Code (ich poste mal alles damit auch nix fehlt):
'''
from GoogleFinance import GoogleFinance
import sys
import datetime
import pytz
import os
import time
def script(j):
# US Eastern time zone
uset = pytz.timezone("US/Eastern")
# google finance account
# for i in range(1,11):
# list of US holidays
# see: http://www.rightline.net/calendar/market-holidays.html
holidays = [datetime.date(2009,1,1),datetime.date(2009,1,19), \
datetime.date(2009,2,16),datetime.date(2009,4,10), \
datetime.date(2009,5,25), datetime.date(2009,7,3), \
datetime.date(2009,9,7), datetime.date(2009,11,26), \
datetime.date(2009,12,25)]
# US markets start and stop times
startTime = datetime.time(9,30)
stopTime = datetime.time(16,00)
# Check that it is trading time
def CheckTime(nowdatetime):
if ((nowdatetime.time() >= startTime) and \
(nowdatetime.time() <= stopTime)) and \
(nowdatetime.date().weekday() <= 4) and \
not (nowdatetime.date() in holidays):
return True
else:
return False
# this loop runs forever
while 1:
# if CheckTime(datetime.datetime.now(uset)):
# time.sleep(30)
for i in range(1,11):
email = '******' + str(i) + '@gmail.com'
password = '******'
dt = str(datetime.datetime.now(uset).date())
path = '' + dt + '/'
if not(os.path.exists(path)):
os.mkdir(path)
# log into Google Finance account and get portfolios
# record the datetime of fetch
# keep trying until there is no error
while 1:
try:
gfi = GoogleFinance(email, password)
# keep fetching data until markets close
# while CheckTime(datetime.datetime.now(uset)):
# get list of portfolios
# keep trying to success
while 1:
try:
pfl_list = gfi.portfolios.get()
except:
pass
else:
break
# loop through all portfolios
for pfl in pfl_list:
for pos in pfl.positions:
filename = path + pos.ticker_id.split(':')[1] + '_' + dt + '.csv'
if not(os.path.exists(filename)):
f = open(filename,'w')
f.write('DateTime,Price\n')
else:
f = open(filename,'a')
f.write(str(pos.updated) + ',' + str(pos.market_value/pos.shares) + '\n')
f.close()
# pause
# time.sleep(120)
except:
pass
else:
break
time.sleep(30)
if __name__ == '__main__':
script(1)
Wie ziehe ich jetzt die Preise meiner Positionen meiner Portfolios aus den Ordnern, in denen sie im obigen Code ja abgespeichert werden, heraus und berechne Bsp den aktuellen oder den Tagesreturn?
Vielen Dank für die Mühe, würde nicht sowas spezielles hier reinposten wenns nicht dringend und wichtig wäre.
Beste Grüße
Basti
habe folgendes Problem, habe folgenden Code (ich poste mal alles damit auch nix fehlt):
'''
from GoogleFinance import GoogleFinance
import sys
import datetime
import pytz
import os
import time
def script(j):
# US Eastern time zone
uset = pytz.timezone("US/Eastern")
# google finance account
# for i in range(1,11):
# list of US holidays
# see: http://www.rightline.net/calendar/market-holidays.html
holidays = [datetime.date(2009,1,1),datetime.date(2009,1,19), \
datetime.date(2009,2,16),datetime.date(2009,4,10), \
datetime.date(2009,5,25), datetime.date(2009,7,3), \
datetime.date(2009,9,7), datetime.date(2009,11,26), \
datetime.date(2009,12,25)]
# US markets start and stop times
startTime = datetime.time(9,30)
stopTime = datetime.time(16,00)
# Check that it is trading time
def CheckTime(nowdatetime):
if ((nowdatetime.time() >= startTime) and \
(nowdatetime.time() <= stopTime)) and \
(nowdatetime.date().weekday() <= 4) and \
not (nowdatetime.date() in holidays):
return True
else:
return False
# this loop runs forever
while 1:
# if CheckTime(datetime.datetime.now(uset)):
# time.sleep(30)
for i in range(1,11):
email = '******' + str(i) + '@gmail.com'
password = '******'
dt = str(datetime.datetime.now(uset).date())
path = '' + dt + '/'
if not(os.path.exists(path)):
os.mkdir(path)
# log into Google Finance account and get portfolios
# record the datetime of fetch
# keep trying until there is no error
while 1:
try:
gfi = GoogleFinance(email, password)
# keep fetching data until markets close
# while CheckTime(datetime.datetime.now(uset)):
# get list of portfolios
# keep trying to success
while 1:
try:
pfl_list = gfi.portfolios.get()
except:
pass
else:
break
# loop through all portfolios
for pfl in pfl_list:
for pos in pfl.positions:
filename = path + pos.ticker_id.split(':')[1] + '_' + dt + '.csv'
if not(os.path.exists(filename)):
f = open(filename,'w')
f.write('DateTime,Price\n')
else:
f = open(filename,'a')
f.write(str(pos.updated) + ',' + str(pos.market_value/pos.shares) + '\n')
f.close()
# pause
# time.sleep(120)
except:
pass
else:
break
time.sleep(30)
if __name__ == '__main__':
script(1)
Wie ziehe ich jetzt die Preise meiner Positionen meiner Portfolios aus den Ordnern, in denen sie im obigen Code ja abgespeichert werden, heraus und berechne Bsp den aktuellen oder den Tagesreturn?
Vielen Dank für die Mühe, würde nicht sowas spezielles hier reinposten wenns nicht dringend und wichtig wäre.
Beste Grüße
Basti