fx.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from __future__ import annotations
  2. import dataclasses
  3. import sys
  4. import typing
  5. import cache
  6. import roi
  7. def main() -> None:
  8. from_cx, to_cx = sys.argv[1:]
  9. raw_prices: list[RawPrice] = cache.get('https://refined-prun.github.io/refined-prices/all.json')
  10. materials: dict[str, roi.Material] = {m['Ticker']: m for m in cache.get('https://api.prunplanner.org/data/materials')}
  11. bids: dict[str, float] = {}
  12. for price in raw_prices:
  13. if price['ExchangeCode'] != to_cx or price['Bid'] is None:
  14. continue
  15. assert price['Bid'] is not None
  16. bids[price['MaterialTicker']] = price['Bid']
  17. rates: list[FX] = []
  18. for price in raw_prices:
  19. if price['ExchangeCode'] != from_cx or price['Ask'] is None or (to_price := bids.get(price['MaterialTicker'])) is None:
  20. continue
  21. rate = (price['Ask'] - to_price) / to_price
  22. if rate < 0.01:
  23. rates.append(FX(price['MaterialTicker'], to_price, rate, price['AskCount']))
  24. rates.sort()
  25. print('mat rate per shipping max converted')
  26. for rate in rates:
  27. mat = materials[rate.ticker]
  28. if mat['Weight'] >= mat['Volume']:
  29. if (per_t := rate.to_price / mat['Weight']) < 1000:
  30. continue
  31. per_shipping = f'{per_t:9,.0f}/t '
  32. else:
  33. if (per_m3 := rate.to_price / mat['Volume']) < 1000:
  34. continue
  35. per_shipping = f'{per_m3:9,.0f}/m³'
  36. print(f'{rate.ticker:4} {rate.rate:8.5f} {per_shipping} {rate.depth * rate.to_price:12,.0f}')
  37. class RawPrice(typing.TypedDict):
  38. MaterialTicker: str
  39. ExchangeCode: str
  40. Ask: float | None
  41. Bid: float | None
  42. AskCount: int
  43. @dataclasses.dataclass(eq=False, frozen=True, slots=True)
  44. class FX:
  45. ticker: str
  46. to_price: float
  47. rate: float
  48. depth: int
  49. def __lt__(self, other: FX) -> bool:
  50. return self.rate < other.rate
  51. if __name__ == '__main__':
  52. main()