1
|
|
|
"""trade.Context |
2
|
|
|
|
3
|
|
|
https://github.com/rochars/trade |
4
|
|
|
|
5
|
|
|
Copyright (c) 2015-2018 Rafael da Silva Rocha |
6
|
|
|
|
7
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy |
8
|
|
|
of this software and associated documentation files (the "Software"), to deal |
9
|
|
|
in the Software without restriction, including without limitation the rights |
10
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
copies of the Software, and to permit persons to whom the Software is |
12
|
|
|
furnished to do so, subject to the following conditions: |
13
|
|
|
|
14
|
|
|
The above copyright notice and this permission notice shall be included in |
15
|
|
|
all copies or substantial portions of the Software. |
16
|
|
|
|
17
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
23
|
|
|
THE SOFTWARE. |
24
|
|
|
""" |
25
|
|
|
|
26
|
|
|
from __future__ import absolute_import |
27
|
|
|
from __future__ import division |
28
|
|
|
|
29
|
|
|
import copy |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
class Context(object): |
33
|
|
|
"""A class to group occurrences and contextualize them. |
34
|
|
|
|
35
|
|
|
To contextualize a group of occurrences could mean: |
36
|
|
|
- To identify daytrades and separate them into different occurrences |
37
|
|
|
- Group occurrences with the same subject into one occurrence |
38
|
|
|
- Apply costs and other deductions to the value to the occurrences |
39
|
|
|
- Calculate taxes based on the occurrences in the context |
40
|
|
|
|
41
|
|
|
Objects from this class are created with both a list of occurrences and a |
42
|
|
|
list of functions (called "context tasks"). |
43
|
|
|
|
44
|
|
|
The context tasks are run by calling Context.apply(). Context tasks |
45
|
|
|
run in the order they are listed. |
46
|
|
|
|
47
|
|
|
Every context task function receives one argument: the context itself. It |
48
|
|
|
is then free to manipulate the raw occurrence data stored in the |
49
|
|
|
Context.occurrences list. |
50
|
|
|
|
51
|
|
|
The contextualized occurrence data is then stored in the |
52
|
|
|
Context.contextualized list. |
53
|
|
|
|
54
|
|
|
The occurrences in Context.contextualized should be informed to a Holder |
55
|
|
|
as regular occurrences. |
56
|
|
|
|
57
|
|
|
The original Occurrence list is not modified. |
58
|
|
|
|
59
|
|
|
Attributes: |
60
|
|
|
occurrences: A list of Occurrence objects. |
61
|
|
|
tasks: A list of functions. |
62
|
|
|
contextualized: A list of Occurrence objects. |
63
|
|
|
""" |
64
|
|
|
|
65
|
|
|
def __init__(self, occurrences, tasks): |
66
|
|
|
self.occurrences = occurrences |
67
|
|
|
self.tasks = tasks |
68
|
|
|
self.contextualized = [] |
69
|
|
|
|
70
|
|
|
def apply(self): |
71
|
|
|
"""Apply the rules of the context to its occurrences. |
72
|
|
|
|
73
|
|
|
This method executes all the functions defined in |
74
|
|
|
self.tasks in the order they are listed. |
75
|
|
|
|
76
|
|
|
Every function that acts as a context task receives the |
77
|
|
|
Context object itself as its only argument. |
78
|
|
|
|
79
|
|
|
The contextualized occurrences are then stored in |
80
|
|
|
Context.contextualized. |
81
|
|
|
|
82
|
|
|
The original Occurrence instances are not modified. |
83
|
|
|
""" |
84
|
|
|
raw_operations = copy.deepcopy(self.occurrences) |
85
|
|
|
for task in self.tasks: |
86
|
|
|
task(self) |
87
|
|
|
self.occurrences = raw_operations |
88
|
|
|
|