forked from robcarver17/pysystemtrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharctic_multiple_prices.py
82 lines (64 loc) · 2.57 KB
/
arctic_multiple_prices.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""
Read and write data from mongodb for 'multiple prices'
"""
import pandas as pd
from sysdata.arctic.arctic_connection import arcticData
from sysdata.futures.multiple_prices import (
futuresMultiplePricesData,
)
from sysobjects.multiple_prices import futuresMultiplePrices
from sysobjects.dict_of_named_futures_per_contract_prices import (
list_of_price_column_names,
contract_name_from_column_name,
)
from syslogging.logger import *
MULTIPLE_COLLECTION = "futures_multiple_prices"
class arcticFuturesMultiplePricesData(futuresMultiplePricesData):
"""
Class to read / write multiple futures price data to and from arctic
"""
def __init__(
self, mongo_db=None, log=get_logger("arcticFuturesMultiplePricesData")
):
super().__init__(log=log)
self._arctic = arcticData(MULTIPLE_COLLECTION, mongo_db=mongo_db)
def __repr__(self):
return repr(self._arctic)
@property
def arctic(self):
return self._arctic
def get_list_of_instruments(self) -> list:
return self.arctic.get_keynames()
def _get_multiple_prices_without_checking(
self, instrument_code: str
) -> futuresMultiplePrices:
data = self.arctic.read(instrument_code)
return futuresMultiplePrices(data)
def _delete_multiple_prices_without_any_warning_be_careful(
self, instrument_code: str
):
self.arctic.delete(instrument_code)
self.log.debug(
"Deleted multiple prices for %s from %s" % (instrument_code, str(self))
)
def _add_multiple_prices_without_checking_for_existing_entry(
self, instrument_code: str, multiple_price_data_object: futuresMultiplePrices
):
multiple_price_data_aspd = pd.DataFrame(multiple_price_data_object)
multiple_price_data_aspd = _change_contracts_to_str(multiple_price_data_aspd)
self.arctic.write(instrument_code, multiple_price_data_aspd)
self.log.debug(
"Wrote %s lines of prices for %s to %s"
% (len(multiple_price_data_aspd), instrument_code, str(self)),
instrument_code=instrument_code,
)
def _change_contracts_to_str(multiple_price_data_aspd):
for price_column in list_of_price_column_names:
multiple_price_data_aspd[price_column] = multiple_price_data_aspd[
price_column
].astype(float)
contract_column = contract_name_from_column_name(price_column)
multiple_price_data_aspd[contract_column] = multiple_price_data_aspd[
contract_column
].astype(str)
return multiple_price_data_aspd