forked from robcarver17/pysystemtrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotfx.py
225 lines (165 loc) · 6.95 KB
/
spotfx.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
"""
Spot fx prices
"""
import numpy as np
import pandas as pd
import datetime
from sysdata.base_data import baseData
from syscore.merge_data import spike_in_data
from sysobjects.spot_fx_prices import fxPrices, get_fx_tuple_from_code, DEFAULT_CURRENCY
DEFAULT_DATES = pd.date_range(
start=datetime.datetime(1970, 1, 1), freq="B", end=datetime.datetime.now()
)
DEFAULT_RATE_SERIES = pd.Series(np.full(len(DEFAULT_DATES), 1.0), index=DEFAULT_DATES)
USE_CHILD_CLASS_ERROR = "You need to use a child class of fxPricesData"
class fxPricesData(baseData):
"""
Read and write data class to get fx prices
We'd inherit from this class for a specific implementation
"""
def __repr__(self):
return USE_CHILD_CLASS_ERROR
def keys(self):
return self.get_list_of_fxcodes()
def __getitem__(self, code):
return self.get_fx_prices(code)
def get_fx_prices(self, fx_code: str) -> fxPrices:
"""
Get a historical series of FX prices
:param fx_code: currency code, in the form EURUSD
:return: fxData object
"""
currency1, currency2 = get_fx_tuple_from_code(fx_code)
if currency1 == currency2:
# Trivial, just a bunch of 1's
fx_data = DEFAULT_RATE_SERIES
elif currency2 == DEFAULT_CURRENCY:
# We ought to have data
fx_data = self._get_standard_fx_prices(fx_code)
elif currency1 == DEFAULT_CURRENCY:
# inversion
fx_data = self._get_fx_prices_for_inversion(fx_code)
else:
# Try a cross rate
fx_data = self._get_fx_cross(fx_code)
return fx_data
def _get_standard_fx_prices(self, fx_code: str) -> fxPrices:
currency1, currency2 = get_fx_tuple_from_code(fx_code)
assert currency2 == DEFAULT_CURRENCY
fx_data = self._get_fx_prices_vs_default(currency1)
return fx_data
def _get_fx_prices_for_inversion(self, fx_code: str) -> fxPrices:
"""
Get a historical series of FX prices, must be USDXXX
:param currency2
:return: fxData
"""
currency1, currency2 = get_fx_tuple_from_code(fx_code)
assert currency1 == DEFAULT_CURRENCY
raw_fx_data = self._get_fx_prices_vs_default(currency2)
if raw_fx_data.empty:
self.log.warn(
"Data for %s is missing, needed to calculate %s"
% (currency2 + DEFAULT_CURRENCY, DEFAULT_CURRENCY + currency2)
)
return raw_fx_data
inverted_fx_data = 1.0 / raw_fx_data
return inverted_fx_data
def _get_fx_cross(self, fx_code: str) -> fxPrices:
"""
Get a currency cross rate XXXYYY, eg not XXXUSD or USDXXX or XXXXXX
:return: fxPrices
"""
currency1, currency2 = get_fx_tuple_from_code(fx_code)
currency1_vs_default = self._get_fx_prices_vs_default(currency1)
currency2_vs_default = self._get_fx_prices_vs_default(currency2)
if currency1_vs_default.empty or currency2_vs_default.empty:
return fxPrices.create_empty()
(aligned_c1, aligned_c2) = currency1_vs_default.align(
currency2_vs_default, join="outer"
)
fx_rate_series = aligned_c1.ffill() / aligned_c2.ffill()
return fx_rate_series
def _get_fx_prices_vs_default(self, currency1: str) -> fxPrices:
"""
Get a historical series of FX prices, must be XXXUSD
:param code: currency code, in the form EUR
:return: fxData object
"""
code = currency1 + DEFAULT_CURRENCY
fx_data = self._get_fx_prices(code)
return fx_data
def _get_fx_prices(self, code: str) -> fxPrices:
if not self.is_code_in_data(code):
self.log.warn("Currency %s is missing from list of FX data" % code)
return fxPrices.create_empty()
data = self._get_fx_prices_without_checking(code)
return data
def delete_fx_prices(self, code: str, are_you_sure=False):
self.log.label(fx_code=code)
if are_you_sure:
if self.is_code_in_data(code):
self._delete_fx_prices_without_any_warning_be_careful(code)
self.log.terse("Deleted fx price data for %s" % code)
else:
# doesn't exist anyway
self.log.warn("Tried to delete non existent fx prices for %s" % code)
else:
self.log.warn("You need to call delete_fx_prices with a flag to be sure")
def is_code_in_data(self, code: str) -> bool:
if code in self.get_list_of_fxcodes():
return True
else:
return False
def add_fx_prices(
self, code: str, fx_price_data: fxPrices, ignore_duplication: bool = False
):
self.log.label(fx_code=code)
if self.is_code_in_data(code):
if ignore_duplication:
pass
else:
self.log.warn(
"There is already %s in the data, you have to delete it first, or set ignore_duplication=True, or use update_fx_prices"
% code
)
return None
self._add_fx_prices_without_checking_for_existing_entry(code, fx_price_data)
self.log.terse("Added fx data for code %s" % code)
def update_fx_prices(
self, code: str, new_fx_prices: fxPrices, check_for_spike=True
) -> int:
"""
Checks existing data, adds any new data with a timestamp greater than the existing data
:param code: FX code
:param new_fx_prices: fxPrices object
:return: int, number of rows added
"""
new_log = self.log.setup(fx_code=code)
old_fx_prices = self.get_fx_prices(code)
merged_fx_prices = old_fx_prices.add_rows_to_existing_data(
new_fx_prices, check_for_spike=check_for_spike
)
if merged_fx_prices is spike_in_data:
return spike_in_data
rows_added = len(merged_fx_prices) - len(old_fx_prices)
if rows_added == 0:
if len(old_fx_prices) == 0:
new_log.msg("No new or old prices for %s" % code)
else:
new_log.msg(
"No additional data since %s for %s"
% (str(old_fx_prices.index[-1]), code)
)
return 0
self.add_fx_prices(code, merged_fx_prices, ignore_duplication=True)
new_log.msg("Added %d additional rows for %s" % (rows_added, code))
return rows_added
def get_list_of_fxcodes(self):
raise NotImplementedError(USE_CHILD_CLASS_ERROR)
def _add_fx_prices_without_checking_for_existing_entry(self, code, fx_price_data):
raise NotImplementedError(USE_CHILD_CLASS_ERROR)
def _delete_fx_prices_without_any_warning_be_careful(self, code):
raise NotImplementedError(USE_CHILD_CLASS_ERROR)
def _get_fx_prices_without_checking(self, code):
raise NotImplementedError(USE_CHILD_CLASS_ERROR)