forked from robcarver17/pysystemtrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharctic_connection.py
64 lines (45 loc) · 1.84 KB
/
arctic_connection.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import pandas as pd
from arctic import Arctic
from sysdata.mongodb.mongo_connection import mongoDb, clean_mongo_host
"""
IMPORTANT NOTE: Make sure you have a mongodb running eg mongod --dbpath /home/yourusername/pysystemtrade/data/futures/arctic
This connection won't fail if mongo missing, but will hang
"""
class arcticData(object):
"""
All of our ARCTIC mongo connections use this class (not static data which goes directly via mongo DB)
"""
def __init__(self, collection_name, mongo_db=None):
if mongo_db is None:
mongo_db = mongoDb()
database_name = mongo_db.database_name
hostname = mongo_db.host
client = mongo_db.client
# Arctic doesn't accept a port
store = Arctic(client)
self.database_name = database_name
self.collection_name = collection_name
self.host = hostname
self.store = store
self.library = self._setup_lib(store, database_name, collection_name)
def __repr__(self):
return (
f"Arctic connection: host {clean_mongo_host(self.host)}, "
f"db {self.database_name}, collection {self.collection_name}"
)
def read(self, ident) -> pd.DataFrame:
item = self.library.read(ident)
return pd.DataFrame(item.data)
def write(self, ident: str, data: pd.DataFrame):
self.library.write(ident, data)
def get_keynames(self) -> list:
return self.library.list_symbols()
def has_keyname(self, keyname) -> bool:
return self.library.has_symbol(keyname)
def delete(self, ident: str):
self.library.delete(ident)
def _setup_lib(self, store: Arctic, db_name, coll_name):
lib_name = db_name + "." + coll_name
if lib_name not in store.list_libraries():
store.initialize_library(lib_name)
return store[lib_name]