aboutsummaryrefslogtreecommitdiffstats
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/book_bbg.py2
-rw-r--r--python/trade_dataclasses.py130
2 files changed, 76 insertions, 56 deletions
diff --git a/python/book_bbg.py b/python/book_bbg.py
index 33851193..45abe0d3 100644
--- a/python/book_bbg.py
+++ b/python/book_bbg.py
@@ -9,7 +9,7 @@ def get_bbg_id(s):
if m := re.match("(CDX|BOND)-[^_]*_([^$]*)", s):
return m.groups()
if "DEAL" in s:
- return "FXSWAP", s.split("_")[3]
+ return "FX", s.split("_")[3]
def run():
diff --git a/python/trade_dataclasses.py b/python/trade_dataclasses.py
index fbe294a9..7e91c265 100644
--- a/python/trade_dataclasses.py
+++ b/python/trade_dataclasses.py
@@ -295,7 +295,7 @@ class BbgDeal:
cls._bbg_sql_insert = (
f"INSERT INTO cds_tickets VALUES({','.join(['%s'] * 22)})"
)
- elif deal_type == DealType.FxSwap or deal_type == DealType.Spot:
+ elif deal_type in (DealType.Fx, DealType.Spot, DealType.FxSwap):
cls._bbg_sql_insert = (
f"INSERT INTO fx_tickets VALUES({','.join(['%s'] * 211)})"
)
@@ -696,11 +696,83 @@ class TerminationDeal(
return obj
+@dataclass
+class SpotDeal(
+ BbgDeal,
+ Deal,
+ deal_type=DealType.Spot,
+ table_name="spots",
+ insert_ignore=("id", "dealid"),
+):
+ folder: SpotStrat
+ portfolio: Portfolio
+ spot_rate: float
+ buy_currency: str
+ buy_amount: float
+ sell_currency: str
+ sell_amount: float
+ fund: Fund
+ cp_code: str
+ cash_account: str
+ commission_currency: str = "USD"
+ commission: float = None
+ id: int = field(default=None, metadata={"insert": False})
+ dealid: str = field(default=None, metadata={"insert": False})
+ trade_date: datetime.date = field(
+ default_factory=datetime.date.today(),
+ )
+ settle_date: datetime.date = field(
+ default_factory=datetime.date.today(),
+ )
+ bbg_ticket_id: str = None
+
+ @classmethod
+ def from_bbg_line(cls, line: dict):
+ if line["Side"] == "S":
+ key1, key2 = "buy", "sell"
+ else:
+ key1, key2 = "sell", "buy"
+
+ d = {
+ f"{key1}_currency": line["Currency 1"],
+ f"{key1}_amount": line["Amount Dealt"],
+ f"{key2}_currency": line["Currency 2"],
+ f"{key2}_amount": line["Counter Amount"],
+ }
+ for key in ("Comp Quote 1",):
+ if line[key] == "":
+ line[key] = None
+ cls._bbg_insert_queue.append(list(line.values()))
+ return cls(
+ folder="*",
+ portfolio="UNALLOCATED",
+ cp_code=_fx_cp[line["Counterparty Deal Code"]],
+ trade_date=datetime.datetime.strptime(line["Date Of Deal"], "%Y%m%d"),
+ settle_date=datetime.datetime.strptime(
+ line["Value Date Period 1 Currency 1"], "%Y%m%d"
+ ),
+ fund=_fx_funds[line["ALOC Account 1"]],
+ spot_rate=line["Exchange Rate Period 1"],
+ cash_account=_fx_accounts[line["ALOC Account 1"]],
+ bbg_ticket_id=line["bbg_ticket_id"],
+ **d,
+ )
+
+
_fx_cp = {"BAST": "BAMSNY", "MSST": "MSCSNY"}
_fx_funds = {"serenitas": "SERCGMAST", "bowdst": "BOWDST"}
_fx_accounts = {"serenitas": "V0NSCLMAMB", "bowdst": "751254"}
+class FxDeal(BbgDeal, Deal, table_name=None, deal_type=DealType.Fx):
+ @classmethod
+ def from_bbg_line(cls, line: dict):
+ if line["Deal Type"] == "4":
+ return SpotDeal.from_bbg_line(line)
+ else:
+ return FxSwapDeal.from_bbg_line(line)
+
+
@dataclass
class FxSwapDeal(
BbgDeal,
@@ -729,6 +801,7 @@ class FxSwapDeal(
cash_account: str
id: int = field(default=None, metadata={"insert": False})
dealid: str = field(default=None, metadata={"insert": False})
+ bbg_ticket_id: str = None
@classmethod
def from_bbg_line(cls, line: dict):
@@ -766,59 +839,6 @@ class FxSwapDeal(
near_rate=line["Exchange Rate Period 1"],
far_rate=line["Exchange Rate Period 2"],
cash_account=_fx_accounts[line["ALOC Account 1"]],
- **d,
- )
-
-
-@dataclass
-class SpotDeal(
- BbgDeal,
- Deal,
- deal_type=DealType.Spot,
- table_name="spots",
- insert_ignore=("id", "dealid"),
-):
- folder: str
- trade_date: datetime.date
- settle_date: datetime.date
- spot_rate: float
- buy_currency: str
- buy_amount: float
- sell_currency: str
- sell_amount: float
- fund: str
- cp_code: str
- cash_account: str
- portfolio: str
- action: str = field(default="NEW")
- commission_currency: str = field(default="USD")
- id: int = field(default=None, metadata={"insert": False})
- dealid: str = field(default=None, metadata={"insert": False})
-
- @classmethod
- def from_bbg_line(cls, line: dict):
- if line["Side"] == "S":
- key1, key2 = "sell", "buy"
- else:
- key1, key2 = "buy", "sell"
-
- d = {
- f"{key1}_currency": line["Currency 1"],
- f"{key1}_amount": line["Amount Dealt"],
- f"{key2}_currency": line["Currency 2"],
- f"{key2}_amount": line["Counter Amount"],
- }
- cls._bbg_insert_queue.append(list(line.values()))
- return cls(
- folder="*",
- portfolio="UNALLOCATED",
- cp_code=_fx_cp[line["Counterparty Deal Code"]],
- trade_date=datetime.datetime.strptime(line["Date Of Deal"], "%Y%m%d"),
- settle_date=datetime.datetime.strptime(
- line["Value Date Period 1 Currency 1"], "%Y%m%d"
- ),
- fund=_fx_funds[line["ALOC Account 1"]],
- spot_rate=line["Spot Basis Rate"],
- cash_account=_fx_accounts[line["ALOC Account 1"]],
+ bbg_ticket_id=line["bbg_ticket_id"],
**d,
)