prepare.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. from __future__ import annotations
  2. import collections
  3. import dataclasses
  4. import csv
  5. import json
  6. import sys
  7. import typing
  8. def main() -> None:
  9. (month,) = sys.argv[1:]
  10. with open(f'rawData/{month}.csv', 'r', newline='') as f:
  11. data = read_data(f)
  12. bases_data: dict[str, dict[str, int]] = {r.company_id: {'bases': r.num, 'rank': r.rank} for r in data['BASES']}
  13. with open(f'www/data/base-data-{month}.json', 'w', newline='') as f:
  14. json.dump(bases_data, f)
  15. ships_data: dict[str, dict[str, int]] = {r.company_id: {'ships': r.num, 'rank': r.rank} for r in data['SHIPS']}
  16. with open(f'www/data/ship-data-{month}.json', 'w') as f:
  17. json.dump(ships_data, f)
  18. with open(f'rawData/{month}-prices.json', 'r') as f:
  19. prices = get_prices(f)
  20. prod_data = get_prod_data(data, prices)
  21. with open(f'www/data/prod-data-{month}.json', 'w', newline='') as f:
  22. json.dump(prod_data, f)
  23. company_data = get_company_data(data, prices)
  24. with open(f'www/data/company-data-{month}.json', 'w', newline='') as f:
  25. json.dump(company_data, f)
  26. def read_data(f: typing.TextIO) -> dict[str, list[Row]]:
  27. data: dict[str, list[Row]] = collections.defaultdict(list)
  28. reader = csv.reader(f)
  29. for row in reader:
  30. data[row[0]].append(Row(int(row[1]), int(row[2]), row[3]))
  31. return data
  32. def get_prices(f: typing.TextIO) -> typing.Mapping[str, float]:
  33. raw_prices: typing.Sequence[Price] = json.load(f)
  34. volumes: dict[str, float] = collections.defaultdict(float)
  35. traded: dict[str, int] = collections.defaultdict(int)
  36. for price in raw_prices:
  37. if price['Traded30D'] is None:
  38. continue
  39. assert price['VWAP30D'] is not None
  40. volumes[price['MaterialTicker']] += price['VWAP30D'] * price['Traded30D']
  41. traded[price['MaterialTicker']] += price['Traded30D']
  42. prices = {ticker: volume / traded[ticker] for ticker, volume in volumes.items()}
  43. hardcoded_prices = {
  44. 'AFP': 116868,
  45. 'ANZ': 70601,
  46. 'BFP': 23408,
  47. 'BND': 230,
  48. 'BID': 47011,
  49. 'CRU': 169623,
  50. 'CQT': 378452,
  51. 'FUN': 124010,
  52. 'GCH': 18303,
  53. 'GNZ': 30361,
  54. 'HNZ': 93580,
  55. 'PFG': 2677222,
  56. 'PK': 869,
  57. 'RDS': 598170,
  58. 'SDM': 1721027,
  59. 'SST': 5863587,
  60. 'SU': 157860,
  61. 'TOR': 540169,
  62. 'VCB': 673713,
  63. 'WOR': 202000,
  64. }
  65. assert frozenset(prices).isdisjoint(hardcoded_prices)
  66. prices.update(hardcoded_prices)
  67. return prices
  68. def get_prod_data(data: dict[str, list[Row]], prices: typing.Mapping[str, float]) -> dict[str, ProdData]:
  69. prod: dict[str, ProdData] = {}
  70. for section, rows in data.items():
  71. if (ticker := get_production_ticker(section)) is None:
  72. continue
  73. price = prices.get(ticker)
  74. if price is None:
  75. continue
  76. amount = sum(row.num for row in rows) / 30
  77. prod[ticker] = {'amount': amount, 'volume': amount * price}
  78. return prod
  79. def get_company_data(data: dict[str, list[Row]], prices: typing.Mapping[str, float]) -> dict[str, typing.Any]:
  80. individual: dict[str, dict[str, CompanyTickerData]] = collections.defaultdict(dict)
  81. totals: dict[str, CompanyTotals] = collections.defaultdict(lambda: {'volume': 0.0})
  82. for section, rows in data.items():
  83. if (ticker := get_production_ticker(section)) is None:
  84. continue
  85. price = prices[ticker]
  86. for row in rows:
  87. amount = row.num / 30
  88. volume = amount * price
  89. individual[row.company_id][ticker] = {
  90. 'amount': amount,
  91. 'volume': volume,
  92. 'rank': row.rank,
  93. }
  94. totals[row.company_id]['volume'] += volume
  95. return {'totals': add_company_ranks(totals), 'individual': dict(individual)}
  96. def get_production_ticker(section: str) -> str | None:
  97. prefix = 'PRODUCTION_'
  98. suffix = '_DAYS_30'
  99. if not section.startswith(prefix) or not section.endswith(suffix):
  100. return None
  101. return section[len(prefix):-len(suffix)]
  102. def add_company_ranks(totals: dict[str, CompanyTotals]) -> dict[str, CompanyTotals]:
  103. ranked = sorted(totals.items(), key=lambda item: item[1]['volume'], reverse=True)
  104. for rank, (company_id, company_totals) in enumerate(ranked, start=1):
  105. company_totals['volumeRank'] = rank
  106. return totals
  107. @dataclasses.dataclass(frozen=True, slots=True, eq=False)
  108. class Row:
  109. rank: int
  110. num: int
  111. company_id: str
  112. class Price(typing.TypedDict):
  113. MaterialTicker: str
  114. VWAP30D: float | None
  115. Traded30D: int | None
  116. class ProdData(typing.TypedDict):
  117. amount: float
  118. volume: float
  119. class CompanyTickerData(typing.TypedDict):
  120. amount: float
  121. volume: float
  122. rank: int
  123. class CompanyTotals(typing.TypedDict, total=False):
  124. volume: float
  125. volumeRank: int
  126. if __name__ == '__main__':
  127. main()