forked from robcarver17/pysystemtrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmargin.py
64 lines (47 loc) · 2.29 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
import pandas as pd
import datetime
from syscore.objects import missing_data
TOTAL_MARGIN = "_TOTAL_MARGIN"
class seriesOfMargin(pd.Series):
def final_value(self)-> float:
if len(self)==0:
return missing_data
return self.values[-1]
def add_value(self, value: float,
dateref = datetime.datetime.now()):
return seriesOfMargin(self.append(pd.Series([value],
index=[dateref])))
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):
existing_series = self.get_series_of_strategy_margin(strategy_name)
new_series = existing_series.add_value(margin_entry)
self._write_series_of_strategy_margin(strategy_name,
series_of_margin=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