roi.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. from __future__ import annotations
  2. import dataclasses
  3. import typing
  4. import cache
  5. def main() -> None:
  6. recipes: list[Recipe] = cache.get('https://api.prunplanner.org/data/recipes')
  7. buildings: dict[str, Building] = {m['Ticker']: m for m in cache.get('https://api.prunplanner.org/data/buildings')}
  8. materials: dict[str, Material] = {m['Ticker']: m for m in cache.get('https://api.prunplanner.org/data/materials')}
  9. raw_prices: list[RawPrice] = cache.get('https://refined-prun.github.io/refined-prices/all.json')
  10. prices: dict[str, Price] = {
  11. p['MaterialTicker']: Price(p['VWAP7D'], p['AverageTraded7D']) for p in raw_prices # pyright: ignore[reportArgumentType]
  12. if p['ExchangeCode'] == 'IC1' and p['VWAP7D'] is not None
  13. }
  14. profits = []
  15. for recipe in recipes:
  16. if profit := calc_profit(recipe, buildings, materials, prices):
  17. profits.append(profit)
  18. profits.sort(reverse=True)
  19. for p in profits:
  20. print(f'{p.output:5} \033[32m{p.profit_per_area: 10,.0f}\033[31m', end='')
  21. if p.low_volume:
  22. print(' low volume', end='')
  23. if p.heavy_logistics:
  24. print(' heavy logistics', end='')
  25. if p.high_opex:
  26. print(' high opex', end='')
  27. print(f'\n\033[30m{p.recipe:30} \033[0m{p.expertise:19} \033[33m{p.capex:7,.0f} \033[35m{p.cost_per_day:7,.0f}\033[0m')
  28. def calc_profit(recipe: Recipe, buildings: typing.Mapping[str, Building], materials: typing.Mapping[str, Material],
  29. prices: typing.Mapping[str, Price]) -> Profit | None:
  30. try:
  31. (output,) = recipe['Outputs']
  32. except ValueError: # skip recipes that don't have exactly 1 output
  33. return
  34. try:
  35. output_price = prices[output['Ticker']]
  36. cost = sum(prices[input['Ticker']].vwap * input['Amount'] for input in recipe['Inputs'])
  37. except KeyError: # skip recipes with thinly traded materials
  38. return
  39. revenue = output_price.vwap * output['Amount']
  40. building = buildings[recipe['BuildingTicker']]
  41. capex = sum(bm['Amount'] * prices[bm['CommodityTicker']].vwap
  42. for bm in building['BuildingCosts'])
  43. profit_per_run = revenue - cost
  44. runs_per_day = 24 * 60 * 60 * 1000 / recipe['TimeMs']
  45. cost_per_day = cost * runs_per_day
  46. output_per_day = output['Amount'] * runs_per_day
  47. logistics_per_day = max(
  48. sum(materials[input['Ticker']]['Weight'] * input['Amount'] for input in recipe['Inputs']),
  49. sum(materials[input['Ticker']]['Volume'] * input['Amount'] for input in recipe['Inputs']),
  50. materials[output['Ticker']]['Weight'] * output['Amount'],
  51. materials[output['Ticker']]['Volume'] * output['Amount'],
  52. ) * runs_per_day
  53. return Profit(output['Ticker'], recipe['RecipeName'],
  54. expertise=building['Expertise'].replace('_', ' ').lower(),
  55. profit_per_area=profit_per_run * runs_per_day / building['AreaCost'],
  56. capex=capex,
  57. cost_per_day=cost_per_day,
  58. low_volume=output_price.average_traded < output_per_day * 20,
  59. heavy_logistics=logistics_per_day > 100)
  60. class Recipe(typing.TypedDict):
  61. RecipeName: str
  62. BuildingTicker: str
  63. Inputs: list[RecipeMat]
  64. Outputs: list[RecipeMat]
  65. TimeMs: int
  66. class RecipeMat(typing.TypedDict):
  67. Ticker: str
  68. Amount: int
  69. class Building(typing.TypedDict):
  70. Ticker: str
  71. Expertise: str
  72. AreaCost: int
  73. BuildingCosts: list[BuildingMat]
  74. class BuildingMat(typing.TypedDict):
  75. CommodityTicker: str
  76. Amount: int
  77. class Material(typing.TypedDict):
  78. Ticker: str
  79. Weight: float
  80. Volume: float
  81. class RawPrice(typing.TypedDict):
  82. MaterialTicker: str
  83. ExchangeCode: str
  84. PriceAverage: int
  85. VWAP7D: float | None # volume-weighted average price over last 7 days
  86. AverageTraded7D: float | None # averaged daily traded volume over last 7 days
  87. @dataclasses.dataclass(eq=False, frozen=True, slots=True)
  88. class Price:
  89. vwap: float
  90. average_traded: float
  91. @dataclasses.dataclass(eq=False, slots=True)
  92. class Profit:
  93. output: str
  94. recipe: str
  95. expertise: str
  96. profit_per_area: float
  97. capex: float
  98. cost_per_day: float
  99. low_volume: bool
  100. heavy_logistics: bool
  101. high_opex: bool = dataclasses.field(init=False)
  102. score: float = dataclasses.field(init=False)
  103. def __post_init__(self) -> None:
  104. self.high_opex = self.cost_per_day > 50_000
  105. self.score = self.profit_per_area
  106. if self.low_volume:
  107. self.score *= 0.2
  108. def __lt__(self, other: Profit) -> bool:
  109. return self.score < other.score
  110. if __name__ == '__main__':
  111. main()