forked from robcarver17/pysystemtrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_multiple_prices.py
76 lines (56 loc) · 2.53 KB
/
csv_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
from sysdata.futures.multiple_prices import (
futuresMultiplePricesData,
futuresMultiplePrices,
)
from syscore.fileutils import get_filename_for_package, files_with_extension_in_pathname
from syscore.pdutils import pd_readcsv
from syscore.genutils import str_of_int
from syslogdiag.log import logtoscreen
CSV_MULTIPLE_PRICE_DIRECTORY = "data.futures.multiple_prices_csv"
DATE_INDEX_NAME = "DATETIME"
class csvFuturesMultiplePricesData(futuresMultiplePricesData):
"""
Class for roll calendars write / to from csv
"""
def __init__(self, datapath=None, log=logtoscreen(
"csvFuturesMultiplePricesData")):
super().__init__()
if datapath is None:
datapath = CSV_MULTIPLE_PRICE_DIRECTORY
self._datapath = datapath
self.log = log
def __repr__(self):
return "csvFuturesMultiplePricesData accessing %s" % self._datapath
def get_list_of_instruments(self):
return files_with_extension_in_pathname(self._datapath, ".csv")
def _get_multiple_prices_without_checking(self, instrument_code):
filename = self._filename_given_instrument_code(instrument_code)
try:
instr_all_price_data = pd_readcsv(
filename, date_index_name=DATE_INDEX_NAME)
except OSError:
self.log.warning("Can't find multiple price file %s" % filename)
return futuresMultiplePrices.create_empty()
instr_all_price_data.CARRY_CONTRACT = instr_all_price_data.CARRY_CONTRACT.apply(
str_of_int)
instr_all_price_data.PRICE_CONTRACT = instr_all_price_data.PRICE_CONTRACT.apply(
str_of_int)
instr_all_price_data.FORWARD_CONTRACT = (
instr_all_price_data.FORWARD_CONTRACT.apply(str_of_int)
)
return futuresMultiplePrices(instr_all_price_data)
def _delete_multiple_prices_without_any_warning_be_careful(
instrument_code):
raise NotImplementedError(
"You can't delete multiple prices stored as a csv - Add to overwrite existing or delete file manually"
)
def _add_multiple_prices_without_checking_for_existing_entry(
self, instrument_code, multiple_price_data
):
filename = self._filename_given_instrument_code(instrument_code)
multiple_price_data.to_csv(filename, index_label=DATE_INDEX_NAME)
def _filename_given_instrument_code(self, instrument_code):
filename = get_filename_for_package(
self._datapath, "%s.csv" % (instrument_code)
)
return filename