Browse Source

mmnode-ticker: get historical financial data from Yahoo Finance

The MMGen Project 6 months ago
parent
commit
af7c14fe36

+ 42 - 1
mmgen_node_tools/Ticker.py

@@ -48,6 +48,7 @@ class DataSource:
 			'cc': 'coinpaprika'
 		}, {
 			'fi': 'yahoospot',
+			'hi': 'yahoohist',
 		}
 	]
 
@@ -248,7 +249,9 @@ class DataSource:
 				'symbol': sym.upper(),
 				'price_usd': price_usd,
 				'price_btc': price_usd / btcusd,
-				'percent_change_7d': None,
+				'percent_change_1y': data['pct_chg_1y'],
+				'percent_change_30d': data['pct_chg_4wks'],
+				'percent_change_7d': data['pct_chg_1wk'],
 				'percent_change_24h': data['regularMarketChangePercent']['raw'] * 100,
 				'last_updated': data['regularMarketTime'],
 			}
@@ -272,6 +275,7 @@ class DataSource:
 
 			kwargs = {
 				'formatted': True,
+				'asynchronous': True,
 				'proxies': { 'https': cfg.proxy2 },
 			}
 
@@ -297,6 +301,33 @@ class DataSource:
 				id     = s.lower(),
 				source = 'fi' )
 
+	class yahoohist(yahoospot):
+
+		json_fn_basename = 'ticker-finance-history.json'
+		data_desc = 'historical financial data'
+		net_data_type = 'json'
+		period = '1y'
+		interval = '1wk'
+
+		def process_network_data(self,ticker):
+			return ticker.history(
+				period   = self.period,
+				interval = self.interval).to_json(orient='index')
+
+		def postprocess_data(self,data):
+			def gen():
+				keys = set()
+				for key,val in data.items():
+					if m := re.match(r"\('(.*?)', datetime\.date\((.*)\)\)$",key):
+						date = '{}-{:>02}-{:>02}'.format(*m[2].split(', '))
+						if (sym := m[1]) in keys:
+							d[date] = val
+						else:
+							keys.add(sym)
+							d = {date:val}
+							yield (sym,d)
+			return dict(gen())
+
 def assets_list_gen(cfg_in):
 	for k,v in cfg_in.cfg['assets'].items():
 		yield ''
@@ -378,6 +409,16 @@ def gen_data(data):
 			if id in wants['id']:
 				if id in found['id']:
 					die(1,dup_sym_errmsg(id))
+				if m := data['hi'].get(k):
+					spot = v['regularMarketPrice']['raw']
+					hist = tuple(m.values())
+					v['pct_chg_1wk'], v['pct_chg_4wks'], v['pct_chg_1y'] = (
+						(spot / hist[-2]['close'] - 1) * 100,
+						(spot / hist[-5]['close'] - 1) * 100, # 4 weeks ≈ 1 month
+						(spot / hist[0]['close'] - 1) * 100,
+					)
+				else:
+					v['pct_chg_1wk'] = v['pct_chg_4wks'] = v['pct_chg_1y'] = None
 				yield ( id, conv_func(id,v,btcusd) )
 				found['id'].add(id)
 				wants['id'].remove(id)

+ 1 - 1
mmgen_node_tools/data/version

@@ -1 +1 @@
-3.2.dev3
+3.2.dev4

+ 1 - 1
setup.cfg

@@ -23,7 +23,7 @@ python_requires = >=3.8
 include_package_data = True
 
 install_requires =
-	mmgen>=14.0.dev9
+	mmgen>=14.0.dev11
 	pyyaml
 	yahooquery
 

+ 2 - 1
test/cmdtest_py_d/ct_misc.py

@@ -102,6 +102,7 @@ class CmdTestScripts(CmdTestBase):
 		self.spawn('',msg_only=True)
 		shutil.copy2(os.path.join(refdir,'ticker.json'),self.tmpdir)
 		shutil.copy2(os.path.join(refdir,'ticker-finance.json'),self.tmpdir)
+		shutil.copy2(os.path.join(refdir,'ticker-finance-history.json'),self.tmpdir)
 		shutil.copy2(os.path.join(refdir,'ticker-btc.json'),self.tmpdir)
 		return 'ok'
 
@@ -146,7 +147,7 @@ class CmdTestScripts(CmdTestBase):
 				'BITCOIN',
 				r'ETHEREUM 1,659.66 1,559.5846 131,943.14 0.07138094 \+36.41 \+29.99 \+21.42 \+1.82',
 				r'MONERO 158.97 149.3870 12,638.36 0.00683732 \+12.38 \+10.19 \+7.28 \+1.21 2022-08-02 18:25:59',
-				r'S&P 500 4,320.06 4,059.5604 343,444.77 0.18580285 -- -- -- -0.23',
+				r'S&P 500 4,320.06 4,059.5604 343,444.77 0.18580285 -1.71 \+12.93 \+9.05 -0.23',
 				r'INDIAN RUPEE 0.01 0.0118 1.00 0.00000054 -- -- -- --',
 			])
 

File diff suppressed because it is too large
+ 0 - 0
test/ref/ticker/ticker-finance-history.json


Some files were not shown because too many files changed in this diff