market_stats.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. from __future__ import annotations
  2. import collections
  3. import datetime
  4. import json
  5. import typing
  6. import dulwich.repo
  7. import dulwich.objects
  8. if typing.TYPE_CHECKING:
  9. import market
  10. def main() -> None:
  11. repo = dulwich.repo.Repo('../refined-prices')
  12. today = datetime.datetime.now(tz=datetime.UTC).replace(hour=0, minute=0, second=0, microsecond=0)
  13. sunday = (today - datetime.timedelta(days=(today.weekday() + 1) % 7))
  14. for _ in range(52):
  15. analyze_markets(prices_on_day(repo, sunday))
  16. sunday -= datetime.timedelta(days=7)
  17. def prices_on_day(repo: dulwich.repo.Repo, day: datetime.datetime) -> typing.Sequence[market.RawPrice]:
  18. '''refined-prices for the earliest commit on the given day'''
  19. day_ts = int(day.timestamp())
  20. next_day_ts = int((day + datetime.timedelta(days=1)).timestamp())
  21. *_, entry = repo.get_walker(paths=[b'all.json'], since=day_ts, until=next_day_ts)
  22. dt = datetime.datetime.fromtimestamp(entry.commit.author_time, tz=datetime.UTC)
  23. print('loading refined-prices', dt, entry.commit.tree.decode())
  24. tree = typing.cast(dulwich.objects.Tree, repo[entry.commit.tree])
  25. _, blob = tree[b'all.json']
  26. return json.loads(typing.cast(dulwich.objects.Blob, repo[blob]).data.decode())
  27. def analyze_markets(prices: typing.Sequence[market.RawPrice]) -> None:
  28. markets: dict[str, list[market.RawPrice]] = collections.defaultdict(list)
  29. for price in prices:
  30. if price['ExchangeCode'].endswith('2'):
  31. continue
  32. markets[price['MaterialTicker']].append(price)
  33. ic1_lowest = 0
  34. for mat_prices in markets.values():
  35. (ic1_price,) = (price for price in mat_prices if price['ExchangeCode'] == 'IC1')
  36. lowest_traded = 1_000_000
  37. for price in mat_prices:
  38. if price['ExchangeCode'] == 'IC1':
  39. continue
  40. if price['Traded7D'] is None or price['Traded7D'] < lowest_traded:
  41. lowest_traded = price['Traded7D'] or 0
  42. if (ic1_price['Traded7D'] or 0) < lowest_traded:
  43. ic1_lowest += 1
  44. print('IC1 lowest', ic1_lowest, 'of', len(markets))
  45. if __name__ == '__main__':
  46. main()