-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathyahoo.py
More file actions
43 lines (33 loc) · 1.01 KB
/
Copy pathyahoo.py
File metadata and controls
43 lines (33 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import appdirs as ad
ad.user_cache_dir = lambda *args: "/tmp"
import yfinance as yf
import sys
country_map = {
'LN': 'L',
'NY': '',
'HK': 'HK',
'CA': '',
'SG': 'SI',
'EU': 'AQ' # arbitrary choice! in future could try to parse stock exchange field
}
def doit(args):
market_region = args['marketRegion']
country = country_map[market_region]
ticker = args['ticker'] if (country == '') else '.'.join([args['ticker'], country])
# print(ticker, file=sys.stderr)
t = yf.Ticker(ticker)
currency = t.info['currency']
# df = t.history(period="1mo")
df = t.history(period="max")
df['timestamp'] = df.index.strftime('%Y-%m-%d')
df['close'] = df['Close'].round(4)
df['currency'] = currency
ret = df.to_csv(columns=['timestamp', 'close', 'currency'], index=False)
return ret
def main():
ticker = sys.argv[1]
market_region = sys.argv[2]
args = {'ticker': ticker, 'marketRegion': market_region}
print(doit(args))
if __name__ == "__main__":
main()