Skip to content

Commit 21618b3

Browse files
author
rob
committed
working on robcarver17#754, removed csv warning, removed backup issue
1 parent 7b86f06 commit 21618b3

27 files changed

+695
-1301
lines changed

sysbrokers/broker_contract_position_data.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from syscore.constants import arg_not_supplied
44

5-
from sysdata.production.historic_positions_TO_DEPRECATE import contractPositionData
5+
from sysdata.production.historic_contract_positions import contractPositionData
66
from sysdata.data_blob import dataBlob
77
from sysobjects.production.positions import listOfContractPositions
88

sysdata/arctic/arctic_historic_contract_positions.py

-74
Original file line numberDiff line numberDiff line change
@@ -26,50 +26,6 @@ def __repr__(self):
2626
def arctic(self):
2727
return self._arctic
2828

29-
def _update_position_for_contract_object_with_date_supplied(
30-
self,
31-
contract_object: futuresContract,
32-
position: int,
33-
date_index: datetime.datetime,
34-
):
35-
new_position_series = pd.Series([position], index=[date_index])
36-
37-
try:
38-
current_series = self.get_position_as_series_for_contract_object(
39-
contract_object
40-
)
41-
self._update_position_for_contract_object_with_date_and_existing_data(
42-
contract_object=contract_object,
43-
current_series=current_series,
44-
new_position_series=new_position_series,
45-
)
46-
except missingData:
47-
## no existing data
48-
## no need to update, just write the new series
49-
self._write_updated_position_series_for_contract_object(
50-
contract_object=contract_object,
51-
updated_series=new_position_series,
52-
)
53-
54-
def _update_position_for_contract_object_with_date_and_existing_data(
55-
self,
56-
contract_object: futuresContract,
57-
current_series: pd.Series,
58-
new_position_series: pd.Series,
59-
):
60-
61-
try:
62-
assert new_position_series.index[0] > current_series.index[-1]
63-
except:
64-
error_msg = "Adding a position which is older than the last position!"
65-
self.log.critical(error_msg)
66-
raise Exception(error_msg)
67-
68-
updated_series = current_series.append(new_position_series)
69-
self._write_updated_position_series_for_contract_object(
70-
contract_object=contract_object, updated_series=updated_series
71-
)
72-
7329
def _write_updated_position_series_for_contract_object(
7430
self, contract_object: futuresContract, updated_series: pd.Series
7531
):
@@ -80,36 +36,6 @@ def _write_updated_position_series_for_contract_object(
8036

8137
self.arctic.write(ident=ident, data=updated_data_as_df)
8238

83-
def _delete_last_position_for_contract_object_without_checking(
84-
self, contract_object: futuresContract
85-
):
86-
try:
87-
current_series = self.get_position_as_series_for_contract_object(
88-
contract_object
89-
)
90-
self._delete_last_position_for_contract_object_without_checking_with_current_data(
91-
contract_object=contract_object, current_series=current_series
92-
)
93-
except missingData:
94-
## no existing data can't delete
95-
self.log.warn(
96-
"Can't delete last position for %s, as none present"
97-
% str(contract_object)
98-
)
99-
100-
def _delete_last_position_for_contract_object_without_checking_with_current_data(
101-
self, contract_object: futuresContract, current_series: pd.Series
102-
):
103-
updated_series = current_series.drop(current_series.index[-1])
104-
if len(updated_series) == 0:
105-
self._delete_position_series_for_contract_object_without_checking(
106-
contract_object
107-
)
108-
else:
109-
self._write_updated_position_series_for_contract_object(
110-
contract_object=contract_object, updated_series=updated_series
111-
)
112-
11339
def _delete_position_series_for_contract_object_without_checking(
11440
self, contract_object: futuresContract
11541
):

sysdata/arctic/arctic_historic_strategy_positions.py

-76
Original file line numberDiff line numberDiff line change
@@ -36,82 +36,6 @@ def get_list_of_instrument_strategies(self) -> listOfInstrumentStrategies:
3636

3737
return listOfInstrumentStrategies(list_of_instrument_strategies)
3838

39-
def _delete_last_position_for_instrument_strategy_object_without_checking(
40-
self, instrument_strategy: instrumentStrategy
41-
):
42-
try:
43-
current_series = self.get_position_as_series_for_instrument_strategy_object(
44-
instrument_strategy
45-
)
46-
self._delete_last_position_for_instrument_strategy_object_without_checking_with_current_data(
47-
instrument_strategy=instrument_strategy, current_series=current_series
48-
)
49-
except missingData:
50-
## no existing data can't delete
51-
self.log.warn(
52-
"Can't delete last position for %s, as none present"
53-
% str(instrument_strategy)
54-
)
55-
56-
def _delete_last_position_for_instrument_strategy_object_without_checking_with_current_data(
57-
self, instrument_strategy: instrumentStrategy, current_series: pd.Series
58-
):
59-
updated_series = current_series.drop(current_series.index[-1])
60-
if len(updated_series) == 0:
61-
self._delete_position_series_for_instrument_strategy_object_without_checking(
62-
instrument_strategy
63-
)
64-
else:
65-
self._write_updated_position_series_for_instrument_strategy_object(
66-
instrument_strategy=instrument_strategy,
67-
updated_series=updated_series,
68-
)
69-
70-
def _update_position_for_instrument_strategy_object_with_date(
71-
self,
72-
instrument_strategy: instrumentStrategy,
73-
position: int,
74-
date_index: datetime.datetime,
75-
):
76-
77-
new_position_series = pd.Series([position], index=[date_index])
78-
79-
try:
80-
current_series = self.get_position_as_series_for_instrument_strategy_object(
81-
instrument_strategy
82-
)
83-
self._update_position_for_instrument_strategy_object_with_date_and_existing_data(
84-
instrument_strategy=instrument_strategy,
85-
current_series=current_series,
86-
new_position_series=new_position_series,
87-
)
88-
except missingData:
89-
## no existing data
90-
## no need to update, just write the new series
91-
self._write_updated_position_series_for_instrument_strategy_object(
92-
instrument_strategy=instrument_strategy,
93-
updated_series=new_position_series,
94-
)
95-
96-
def _update_position_for_instrument_strategy_object_with_date_and_existing_data(
97-
self,
98-
instrument_strategy: instrumentStrategy,
99-
current_series: pd.Series,
100-
new_position_series: pd.Series,
101-
):
102-
103-
try:
104-
assert new_position_series.index[0] > current_series.index[-1]
105-
except:
106-
error_msg = "Adding a position which is older than the last position!"
107-
self.log.critical(error_msg)
108-
raise Exception(error_msg)
109-
110-
updated_series = current_series.append(new_position_series)
111-
self._write_updated_position_series_for_instrument_strategy_object(
112-
instrument_strategy=instrument_strategy, updated_series=updated_series
113-
)
114-
11539
def _write_updated_position_series_for_instrument_strategy_object(
11640
self, instrument_strategy: instrumentStrategy, updated_series: pd.Series
11741
):
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""
2+
Read and write data from mongodb for individual futures contracts
3+
4+
"""
5+
from typing import Union
6+
7+
from syscore.exceptions import missingData
8+
from sysdata.arctic.arctic_connection import arcticData
9+
from sysdata.production.optimal_positions import optimalPositionData
10+
from syslogdiag.log_to_screen import logtoscreen
11+
12+
from sysobjects.production.tradeable_object import (
13+
instrumentStrategy,
14+
listOfInstrumentStrategies,
15+
)
16+
17+
import pandas as pd
18+
19+
OPTIMAL_POSITION_COLLECTION = "optimal_positions"
20+
21+
22+
class arcticOptimalPositionData(optimalPositionData):
23+
def __init__(self, mongo_db=None, log=logtoscreen("arcticOptimalPositionData")):
24+
25+
super().__init__(log=log)
26+
27+
self._arctic_connection = arcticData(
28+
OPTIMAL_POSITION_COLLECTION, mongo_db=mongo_db
29+
)
30+
31+
def __repr__(self):
32+
return repr(self._arctic_connection)
33+
34+
@property
35+
def arctic_connection(self):
36+
return self._arctic_connection
37+
38+
def get_list_of_instrument_strategies_with_optimal_position(
39+
self,
40+
) -> listOfInstrumentStrategies:
41+
42+
raw_list_of_instrument_strategies = self.arctic_connection.get_keynames()
43+
list_of_instrument_strategies = [
44+
instrumentStrategy.from_key(key)
45+
for key in raw_list_of_instrument_strategies
46+
]
47+
48+
return listOfInstrumentStrategies(list_of_instrument_strategies)
49+
50+
def get_optimal_position_as_df_for_instrument_strategy(
51+
self, instrument_strategy: instrumentStrategy
52+
) -> pd.DataFrame:
53+
54+
try:
55+
ident = instrument_strategy.key
56+
df_result = self.arctic_connection.read(ident)
57+
except:
58+
raise missingData
59+
60+
return df_result
61+
62+
def write_optimal_position_as_df_for_instrument_strategy_without_checking(
63+
self,
64+
instrument_strategy: instrumentStrategy,
65+
optimal_positions_as_df: pd.DataFrame,
66+
):
67+
ident = instrument_strategy.key
68+
self.arctic_connection.write(ident, optimal_positions_as_df)

sysdata/csv/csv_contract_position_data.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pandas as pd
2-
from sysdata.production.historic_positions_TO_DEPRECATE import contractPositionData
2+
from sysdata.production.historic_contract_positions import contractPositionData
33
from sysobjects.contracts import futuresContract
44
from syscore.fileutils import resolve_path_and_filename_for_package
55
from syscore.constants import arg_not_supplied
@@ -28,9 +28,10 @@ def __init__(
2828
def __repr__(self):
2929
return "csvContractPositionData accessing %s" % self._datapath
3030

31-
def write_position_df_for_contract(
32-
self, contract: futuresContract, position_df: pd.DataFrame
31+
def _write_updated_position_series_for_contract_object(
32+
self, contract: futuresContract, update_series: pd.Series
3333
):
34+
position_df = pd.DataFrame(update_series)
3435
filename = self._filename_given_contract(contract)
3536
position_df.to_csv(filename, index_label=DATE_INDEX_NAME)
3637

sysdata/csv/csv_optimal_position.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pandas as pd
2-
from sysdata.production.optimal_positions_TO_DEPRECATE import optimalPositionData
2+
from sysdata.production.optimal_positions import optimalPositionData
33
from syscore.fileutils import resolve_path_and_filename_for_package
44
from syscore.constants import arg_not_supplied
55
from syslogdiag.log_to_screen import logtoscreen
@@ -28,11 +28,13 @@ def __init__(
2828
def __repr__(self):
2929
return "csvOptimalPositionData accessing %s" % self._datapath
3030

31-
def write_position_df_for_instrument_strategy(
32-
self, instrument_strategy: instrumentStrategy, position_df: pd.DataFrame
31+
def write_optimal_position_as_df_for_instrument_strategy_without_checking(
32+
self,
33+
instrument_strategy: instrumentStrategy,
34+
optimal_positions_as_df: pd.DataFrame,
3335
):
3436
filename = self._filename_given_instrument_strategy(instrument_strategy)
35-
position_df.to_csv(filename, index_label=DATE_INDEX_NAME)
37+
optimal_positions_as_df.to_csv(filename, index_label=DATE_INDEX_NAME)
3638

3739
def _filename_given_instrument_strategy(
3840
self, instrument_strategy: instrumentStrategy

sysdata/csv/csv_strategy_position_data.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pandas as pd
2-
from sysdata.production.historic_positions_TO_DEPRECATE import strategyPositionData
2+
from sysdata.production.historic_strategy_positions import strategyPositionData
33
from syscore.fileutils import resolve_path_and_filename_for_package
44
from syscore.constants import arg_not_supplied
55
from syslogdiag.log_to_screen import logtoscreen
@@ -28,9 +28,10 @@ def __init__(
2828
def __repr__(self):
2929
return "csvStrategyPositionData accessing %s" % self._datapath
3030

31-
def write_position_df_for_instrument_strategy(
32-
self, instrument_strategy: instrumentStrategy, position_df: pd.DataFrame
31+
def _write_updated_position_series_for_instrument_strategy_object(
32+
self, instrument_strategy: instrumentStrategy, updated_series: pd.Series
3333
):
34+
position_df = pd.DataFrame(updated_series)
3435
filename = self._filename_given_instrument_strategy(instrument_strategy)
3536
position_df.to_csv(filename, index_label=DATE_INDEX_NAME)
3637

sysdata/data_blob.py

-3
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,6 @@ def _get_csv_paths_for_class(self, class_object) -> str:
190190
class_name = get_class_name(class_object)
191191
csv_data_paths = self.csv_data_paths
192192
if csv_data_paths is arg_not_supplied:
193-
self.log.warn(
194-
"No datapaths provided for .csv, will use defaults (may break in production, should be fine in sim)"
195-
)
196193
return arg_not_supplied
197194

198195
datapath = csv_data_paths.get(class_name, "")

sysdata/futures/contracts.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -100,22 +100,25 @@ def add_contract_data(
100100
def get_list_of_contract_dates_for_instrument_code(
101101
self, instrument_code: str, allow_expired: bool = False
102102
) -> listOfContractDateStr:
103-
raise NotImplementedError(USE_CHILD_CLASS_ERROR)
103+
raise NotImplementedError
104104

105105
def get_all_contract_objects_for_instrument_code(
106106
self, instrument_code: str
107107
) -> listOfFuturesContracts:
108-
raise NotImplementedError(USE_CHILD_CLASS_ERROR)
108+
raise NotImplementedError
109109

110110
def _get_contract_data_without_checking(
111111
self, instrument_code: str, contract_date: str
112112
) -> futuresContract:
113-
raise NotImplementedError(USE_CHILD_CLASS_ERROR)
113+
raise NotImplementedError
114114

115115
def _delete_contract_data_without_any_warning_be_careful(
116116
self, instrument_code: str, contract_date: str
117117
):
118-
raise NotImplementedError(USE_CHILD_CLASS_ERROR)
118+
raise NotImplementedError
119+
120+
def get_list_of_all_instruments_with_contracts(self) -> list:
121+
raise NotImplementedError
119122

120123
def is_contract_in_data(self, instrument_code: str, contract_date_str: str) -> bool:
121124
raise NotImplementedError

sysdata/mongodb/mongo_futures_contracts.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,21 @@ def is_contract_in_data(self, instrument_code: str, contract_date_str: str) -> b
4343
key = contract_key_from_code_and_id(instrument_code, contract_date_str)
4444
return self.mongo_data.key_is_in_data(key)
4545

46-
def get_list_of_all_contract_keys(self) -> list:
46+
def _get_list_of_all_contract_keys(self) -> list:
4747
return self.mongo_data.get_list_of_keys()
4848

49+
def get_list_of_all_instruments_with_contracts(self) -> list:
50+
list_of_keys = self._get_list_of_all_contract_keys()
51+
list_of_split_keys = [
52+
get_code_and_id_from_contract_key(key) for key in list_of_keys
53+
]
54+
list_of_instruments = [
55+
instrument_code for instrument_code, _ in list_of_split_keys
56+
]
57+
unique_list_of_instruments = list(set(list_of_instruments))
58+
59+
return unique_list_of_instruments
60+
4961
def get_all_contract_objects_for_instrument_code(
5062
self, instrument_code: str
5163
) -> listOfFuturesContracts:
@@ -60,7 +72,7 @@ def get_all_contract_objects_for_instrument_code(
6072
return list_of_futures_contracts
6173

6274
def _get_all_contract_keys_for_instrument_code(self, instrument_code: str) -> list:
63-
list_of_all_contract_keys = self.get_list_of_all_contract_keys()
75+
list_of_all_contract_keys = self._get_list_of_all_contract_keys()
6476
list_of_relevant_keys = [
6577
contract_key
6678
for contract_key in list_of_all_contract_keys

0 commit comments

Comments
 (0)