buy.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from __future__ import annotations
  2. import collections
  3. import concurrent.futures
  4. import dataclasses
  5. import typing
  6. import cache
  7. from config import config
  8. import supply
  9. if typing.TYPE_CHECKING:
  10. import market
  11. def main() -> None:
  12. with concurrent.futures.ThreadPoolExecutor() as executor:
  13. futures = [
  14. executor.submit(get_raw_prices),
  15. executor.submit(get_total_buy), # what we need to buy
  16. executor.submit(supply.warehouse_inventory), # what we have
  17. executor.submit(get_bids), # what we already are bidding for
  18. ]
  19. raw_prices, buy, warehouse, (bids, orders) = (f.result() for f in futures)
  20. executor.shutdown()
  21. # what's left to buy
  22. materials: list[Material] = []
  23. for mat, amount in buy.items():
  24. remaining = amount - bids[mat] - warehouse.get(mat, 0)
  25. if remaining <= 0:
  26. continue
  27. price = raw_prices[mat]
  28. if price['Bid'] is None or price['Ask'] is None:
  29. print(mat, 'has no bid/ask')
  30. continue
  31. spread = price['Ask'] - price['Bid']
  32. materials.append(Material(mat, amount=amount, bids=bids[mat], warehouse=warehouse.get(mat, 0),
  33. spread=spread, savings=spread * remaining))
  34. materials.sort(reverse=True)
  35. print('mat want bids have buy savings')
  36. for m in materials:
  37. print(f'{m.ticker:4} {m.amount:>5} {m.bids:>5} {m.warehouse:>5} {m.amount - m.bids - m.warehouse:>5} {m.savings:8.0f}')
  38. # deposits of current bids
  39. orders.sort(key=lambda order: order['Limit'] * order['Amount'], reverse=True)
  40. print('\ncurrent bid deposits:')
  41. for order in orders:
  42. print(f"{order['MaterialTicker']:4} {order['Limit'] * order['Amount']:7,.0f}")
  43. def get_raw_prices() -> typing.Mapping[str, market.RawPrice]:
  44. return {p['MaterialTicker']: p
  45. for p in cache.get('https://refined-prun.github.io/refined-prices/all.json') if p['ExchangeCode'] == 'IC1'}
  46. def get_total_buy() -> typing.Mapping[str, int]:
  47. planets = [supply.Planet(fio_burn) for fio_burn in cache.get('https://rest.fnar.net/fioweb/burn/user/' + config.username,
  48. headers={'Authorization': config.fio_api_key})]
  49. buy: dict[str, int] = collections.defaultdict(int)
  50. for planet in planets:
  51. for mat, amount in planet.supply_for_days(7).items():
  52. buy[mat] += amount
  53. return buy
  54. def get_bids() -> tuple[typing.Mapping[str, int], list[market.ExchangeOrder]]:
  55. orders: typing.Sequence[market.ExchangeOrder] = cache.get('https://rest.fnar.net/cxos/' + config.username,
  56. headers={'Authorization': config.fio_api_key})
  57. orders = [order for order in orders
  58. if order['OrderType'] == 'BUYING' and order['Status'] != 'FILLED' and order['ExchangeCode'] == 'IC1']
  59. bids = collections.defaultdict(int)
  60. for order in orders:
  61. bids[order['MaterialTicker']] += order['Amount']
  62. return bids, orders
  63. @dataclasses.dataclass(eq=False, slots=True)
  64. class Material:
  65. ticker: str
  66. amount: int
  67. bids: int
  68. warehouse: int
  69. spread: float
  70. savings: float
  71. def __lt__(self, o: Material) -> bool:
  72. return self.savings< o.savings
  73. if __name__ == '__main__':
  74. main()