Browse Source

market_stats

raylu 2 weeks ago
parent
commit
0c788808d0
3 changed files with 57 additions and 0 deletions
  1. 1 0
      market.py
  2. 55 0
      market_stats.py
  3. 1 0
      pyproject.toml

+ 1 - 0
market.py

@@ -250,6 +250,7 @@ class RawPrice(typing.TypedDict):
 	HighYesterday: float | None
 	LowYesterday: float | None
 	AverageTraded7D: float | None # averaged daily traded volume over last 7 days
+	Traded7D: int | None
 	Traded30D: int | None
 
 class PriceChartPoint(typing.TypedDict):

+ 55 - 0
market_stats.py

@@ -0,0 +1,55 @@
+from __future__ import annotations
+
+import collections
+import datetime
+import json
+import typing
+
+import dulwich.repo
+import dulwich.objects
+
+if typing.TYPE_CHECKING:
+	import market
+
+def main() -> None:
+	repo = dulwich.repo.Repo('../refined-prices')
+
+	today = datetime.datetime.now(tz=datetime.UTC).replace(hour=0, minute=0, second=0, microsecond=0)
+	sunday = (today - datetime.timedelta(days=(today.weekday() + 1) % 7))
+	for _ in range(52):
+		analyze_markets(prices_on_day(repo, sunday))
+		sunday -= datetime.timedelta(days=7)
+
+def prices_on_day(repo: dulwich.repo.Repo, day: datetime.datetime) -> typing.Sequence[market.RawPrice]:
+	'''refined-prices for the earliest commit on the given day'''
+	day_ts = int(day.timestamp())
+	next_day_ts = int((day + datetime.timedelta(days=1)).timestamp())
+	*_, entry = repo.get_walker(paths=[b'all.json'], since=day_ts, until=next_day_ts)
+	dt = datetime.datetime.fromtimestamp(entry.commit.author_time, tz=datetime.UTC)
+	print('loading refined-prices', dt, entry.commit.tree.decode())
+	tree = typing.cast(dulwich.objects.Tree, repo[entry.commit.tree])
+	_, blob = tree[b'all.json']
+	return json.loads(typing.cast(dulwich.objects.Blob, repo[blob]).data.decode())
+
+def analyze_markets(prices: typing.Sequence[market.RawPrice]) -> None:
+	markets: dict[str, list[market.RawPrice]] = collections.defaultdict(list)
+	for price in prices:
+		if price['ExchangeCode'].endswith('2'):
+			continue
+		markets[price['MaterialTicker']].append(price)
+
+	ic1_lowest = 0
+	for mat_prices in markets.values():
+		(ic1_price,) = (price for price in mat_prices if price['ExchangeCode'] == 'IC1')
+		lowest_traded = 1_000_000
+		for price in mat_prices:
+			if price['ExchangeCode'] == 'IC1':
+				continue
+			if price['Traded7D'] is None or price['Traded7D'] < lowest_traded:
+				lowest_traded = price['Traded7D'] or 0
+		if (ic1_price['Traded7D'] or 0) < lowest_traded:
+			ic1_lowest += 1
+	print('IC1 lowest', ic1_lowest, 'of', len(markets))
+
+if __name__ == '__main__':
+	main()

+ 1 - 0
pyproject.toml

@@ -4,6 +4,7 @@ version = '0'
 requires-python = '>=3.13'
 dependencies = [
 	'cbor2',
+	'dulwich',
 	'h2',
 	'httpx',
 	'typed-argument-parser',