-
Notifications
You must be signed in to change notification settings - Fork 855
/
Copy pathmargin.py
67 lines (51 loc) · 2.17 KB
/
margin.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
import pandas as pd
import datetime
from syscore.exceptions import missingData
TOTAL_MARGIN = "_TOTAL_MARGIN"
class seriesOfMargin(pd.Series):
def final_value(self) -> float:
if len(self) == 0:
raise missingData
return self.values[-1]
class marginData(object):
def get_series_of_total_margin(self) -> seriesOfMargin:
total_margin = self.get_series_of_strategy_margin(TOTAL_MARGIN)
return total_margin
def get_current_total_margin(self) -> float:
current_margin = self.get_current_strategy_margin(TOTAL_MARGIN)
return current_margin
def add_total_margin_entry(self, margin_entry: float):
self.add_strategy_margin_entry(
margin_entry=margin_entry, strategy_name=TOTAL_MARGIN
)
def get_list_of_strategies_with_margin(self) -> list:
list_of_strategies = self._get_list_of_strategies_with_margin_including_total()
try:
list_of_strategies.remove(TOTAL_MARGIN)
except:
# missing no sweat
pass
return list_of_strategies
def get_current_strategy_margin(self, strategy_name: str) -> float:
series_of_margin = self.get_series_of_strategy_margin(
strategy_name=strategy_name
)
return series_of_margin.final_value()
def add_strategy_margin_entry(self, margin_entry: float, strategy_name: str):
new_val = pd.Series([margin_entry], index=[datetime.datetime.now()])
existing_series = self.get_series_of_strategy_margin(strategy_name)
if existing_series.empty:
new_series = new_val
else:
new_series = pd.concat([existing_series, new_val])
self._write_series_of_strategy_margin(
strategy_name, series_of_margin=seriesOfMargin(new_series)
)
def get_series_of_strategy_margin(self, strategy_name: str) -> seriesOfMargin:
raise NotImplementedError
def _get_list_of_strategies_with_margin_including_total(self) -> list:
raise NotImplementedError
def _write_series_of_strategy_margin(
self, strategy_name: str, series_of_margin: seriesOfMargin
):
raise NotImplementedError