aboutsummaryrefslogtreecommitdiffstats
path: root/python/book_bbg.py
blob: 831592a9d4b33836e08bf614863c8d2caf2383cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from serenitas.utils.remote import SftpClient
from trade_dataclasses import Deal, DealType, BbgDeal, FxswapDeal
from stat import S_ISREG
import re
import time


def run():
    sftp = SftpClient.from_creds("bbg")
    while True:
        try:
            for f in sftp.client.listdir_iter("/"):
                if S_ISREG(f.st_mode):
                    if m := re.match("(CDX|BOND)-[^_]*_([^$]*)", f.filename):
                        deal_type, bbg_id = m.groups()
                        if bbg_id not in BbgDeal._cache:
                            with sftp.client.open(f.filename) as fh:
                                Deal[DealType(deal_type)].process(fh, bbg_id)
                                BbgDeal._cache[bbg_id] = None
                    elif 'DEAL' in f.filename:
                        with sftp.client.open(f.filename) as fh:
                            FxswapDeal.process(fh, f.filename.split('_')[3])
        except OSError:
            sftp.client.close()
            sftp = SftpClient.from_creds("bbg")
        time.sleep(60)


if __name__ == "__main__":
    run()