Эх сурвалжийг харах

market: show outbid/undercut

raylu 3 долоо хоног өмнө
parent
commit
f94c230f8e
3 өөрчлөгдсөн 49 нэмэгдсэн , 6 устгасан
  1. 17 0
      config.py
  2. 29 0
      market.py
  3. 3 6
      supply.py

+ 17 - 0
config.py

@@ -0,0 +1,17 @@
+from __future__ import annotations
+
+import dataclasses
+import tomllib
+
+@dataclasses.dataclass(eq=False, frozen=True, slots=True)
+class Config:
+    username: str
+    fio_api_key: str
+
+    def __init__(self) -> None:
+        with open('config.toml', 'rb') as f:
+            config = tomllib.load(f)
+        for k, v in config.items():
+            object.__setattr__(self, k, v)
+
+config = Config()

+ 29 - 0
market.py

@@ -4,8 +4,11 @@ import dataclasses
 import typing
 
 import cache
+from config import config
 
 def main() -> None:
+	check_cxos()
+
 	raw_prices: list[RawPrice] = cache.get('https://refined-prun.github.io/refined-prices/all.json')
 	markets: list[Market] = []
 	for price in raw_prices:
@@ -27,6 +30,32 @@ def main() -> None:
 	for market in markets:
 		print(f'{market.full_ticker:>8} {market.bid:5} {market.ask:5} {market.spread*100: 5.0f}% {market.traded:7}')
 
+def check_cxos() -> None:
+	orders: typing.Sequence[ExchangeOrder] = cache.get('https://rest.fnar.net/cxos/' + config.username,
+			headers={'Authorization': config.fio_api_key})
+	summary: typing.Mapping[tuple[str, str], ExchangeSummary] = {
+		(summary['MaterialTicker'], summary['ExchangeCode']): summary
+		for summary in cache.get('https://rest.fnar.net/exchange/all')
+	}
+	for order in orders:
+		state = summary[order['MaterialTicker'], order['ExchangeCode']]
+		if order['OrderType'] == 'BUYING' and state['Bid'] is not None and state['Bid'] > order['Limit']:
+			print('outbid on', f'{order["MaterialTicker"]}.{order["ExchangeCode"]}')
+		elif order['OrderType'] == 'SELLING' and state['Ask'] is not None and state['Ask'] < order['Limit']:
+			print('undercut on', f'{order["MaterialTicker"]}.{order["ExchangeCode"]}')
+
+class ExchangeOrder(typing.TypedDict):
+	MaterialTicker: str
+	ExchangeCode: str
+	OrderType: typing.Literal['SELLING'] | typing.Literal['BUYING']
+	Limit: float
+
+class ExchangeSummary(typing.TypedDict):
+	MaterialTicker: str
+	ExchangeCode: str
+	Bid: float | None
+	Ask: float | None
+
 class RawPrice(typing.TypedDict):
 	FullTicker: str
 	Bid: float | None

+ 3 - 6
supply.py

@@ -4,12 +4,12 @@ import collections
 import dataclasses
 import json
 import math
-import tomllib
 import typing
 
 import tap
 
 import cache
+from config import config
 
 class Args(tap.Tap):
 	planets: tuple[str, ...]
@@ -106,11 +106,8 @@ def main() -> None:
 		}))
 
 def get_fio_burn(planet_names: typing.Sequence[str]) -> typing.Iterator[FIOBurn]:
-	with open('config.toml', 'rb') as f:
-		config = tomllib.load(f)
-
-	planets: list[FIOBurn] = cache.get('https://rest.fnar.net/fioweb/burn/user/' + config['username'],
-			headers={'Authorization': config['fio_api_key']})
+	planets: list[FIOBurn] = cache.get('https://rest.fnar.net/fioweb/burn/user/' + config.username,
+			headers={'Authorization': config.fio_api_key})
 	for name in planet_names:
 		name = name.casefold()
 		for planet_data in planets: