forked from robcarver17/pysystemtrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspot_fx_prices.py
108 lines (76 loc) · 2.65 KB
/
spot_fx_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
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
from collections import namedtuple
import pandas as pd
from syscore.pandas.merge_data_keeping_past_data import (
merge_newer_data,
SPIKE_IN_DATA,
)
from syscore.pandas.full_merge_with_replacement import full_merge_of_existing_data
class fxPrices(pd.Series):
"""
adjusted price information
"""
def __init__(self, data):
super().__init__(data)
data.index.name = "index"
data.name = ""
@classmethod
def create_empty(fxPrices):
"""
Our graceful fail is to return an empty, but valid, dataframe
"""
empty_data = pd.Series()
fx_prices = fxPrices(empty_data)
return fx_prices
@classmethod
def from_data_frame(fxPrices, data_frame):
return fxPrices(data_frame.T.squeeze())
def merge_with_other_prices(
self, new_fx_prices, only_add_rows=True, check_for_spike=True
):
"""
Merges self with new data.
If only_add_rows is True,
Otherwise: Any Nan in the existing data will be replaced (be careful!)
:param new_fx_prices:
:return: merged fx prices: doesn't update self
"""
if only_add_rows:
return self.add_rows_to_existing_data(
new_fx_prices, check_for_spike=check_for_spike
)
else:
return self._full_merge_of_existing_data(new_fx_prices)
def _full_merge_of_existing_data(self, new_fx_prices):
"""
Merges self with new data.
Any Nan in the existing data will be replaced (be careful!)
We make this private so not called accidentally
:param new_fx_prices: the new data
:return: updated data, doesn't update self
"""
merged_data = full_merge_of_existing_data(self, new_fx_prices)
return fxPrices(merged_data)
def add_rows_to_existing_data(self, new_fx_prices, check_for_spike=True):
"""
Merges self with new data.
Only newer data will be added
:param new_fx_prices:
:return: merged fxPrices
"""
merged_fx_prices = merge_newer_data(
self, new_fx_prices, check_for_spike=check_for_spike
)
if merged_fx_prices is SPIKE_IN_DATA:
return SPIKE_IN_DATA
merged_fx_prices = fxPrices(merged_fx_prices)
return merged_fx_prices
currencyValue = namedtuple("currencyValue", "currency, value")
class listOfCurrencyValues(list):
pass
# by convention we always get prices vs the dollar
DEFAULT_CURRENCY = "USD"
def get_fx_tuple_from_code(code):
assert len(code) == 6
currency1 = code[:3]
currency2 = code[3:]
return currency1, currency2