|
1
|
|
|
"""Occurrence |
|
2
|
|
|
|
|
3
|
|
|
A class to represent occurrences with subjects. |
|
4
|
|
|
|
|
5
|
|
|
https://github.com/rochars/trade |
|
6
|
|
|
License: MIT |
|
7
|
|
|
|
|
8
|
|
|
Copyright (c) 2015-2018 Rafael da Silva Rocha |
|
9
|
|
|
|
|
10
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy |
|
11
|
|
|
of this software and associated documentation files (the "Software"), to deal |
|
12
|
|
|
in the Software without restriction, including without limitation the rights |
|
13
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
14
|
|
|
copies of the Software, and to permit persons to whom the Software is |
|
15
|
|
|
furnished to do so, subject to the following conditions: |
|
16
|
|
|
|
|
17
|
|
|
The above copyright notice and this permission notice shall be included in |
|
18
|
|
|
all copies or substantial portions of the Software. |
|
19
|
|
|
|
|
20
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
21
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
22
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
23
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
24
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
25
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
26
|
|
|
THE SOFTWARE. |
|
27
|
|
|
""" |
|
28
|
|
|
|
|
29
|
|
|
from __future__ import absolute_import |
|
30
|
|
|
from __future__ import division |
|
31
|
|
|
|
|
32
|
|
|
import math |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
class Occurrence(object): |
|
|
|
|
|
|
36
|
|
|
"""A class to represent occurrences with subjects. |
|
37
|
|
|
|
|
38
|
|
|
Attributes: |
|
39
|
|
|
subject: A Subject instance. |
|
40
|
|
|
date: A string to represent the moment of the occurrence. |
|
41
|
|
|
details: A dictionary with the occurrence details. |
|
42
|
|
|
""" |
|
43
|
|
|
|
|
44
|
|
|
def __init__(self, subject, date, details): |
|
45
|
|
|
self.subject = subject |
|
46
|
|
|
self.date = date |
|
47
|
|
|
self.details = details |
|
48
|
|
|
|
|
49
|
|
|
def update_holder(self, holder): |
|
50
|
|
|
"""Udpate the Holder state according to the occurrence. |
|
51
|
|
|
|
|
52
|
|
|
This implementation is a example of how a Occurrence object |
|
53
|
|
|
can update the Holder state; this method should be overriden |
|
54
|
|
|
by classes that inherit from the Occurrence class. |
|
55
|
|
|
|
|
56
|
|
|
This sample implementation simply update the quantity and the average |
|
57
|
|
|
price of the Subject in the Holder's possession every time objects |
|
58
|
|
|
from this class are passed to Holder.trade(). |
|
59
|
|
|
|
|
60
|
|
|
This sample implementation considers the following signature for |
|
61
|
|
|
the Holder.state dictionary: |
|
62
|
|
|
{ |
|
63
|
|
|
"SUBJECT SYMBOL": { |
|
64
|
|
|
"quantity": 0, |
|
65
|
|
|
"value": 0 |
|
66
|
|
|
}, |
|
67
|
|
|
... |
|
68
|
|
|
} |
|
69
|
|
|
""" |
|
70
|
|
|
|
|
71
|
|
|
subject_symbol = self.subject.symbol |
|
72
|
|
|
|
|
73
|
|
|
# If the Holder already have a state regarding this Subject, |
|
74
|
|
|
# update that state |
|
75
|
|
|
if subject_symbol in holder.state: |
|
76
|
|
|
|
|
77
|
|
|
# If the Holder have zero units of this subject, the average |
|
78
|
|
|
# value paid/received for the subject is the value of the trade itself |
|
79
|
|
|
if not holder.state[subject_symbol]['quantity']: |
|
80
|
|
|
holder.state[subject_symbol]['value'] = self.details['value'] |
|
81
|
|
|
|
|
82
|
|
|
# If the Holder owns units of this subject then the average value |
|
83
|
|
|
# paid/received for the subject may need to be updated with |
|
84
|
|
|
# this occurrence details |
|
85
|
|
|
elif same_sign( |
|
86
|
|
|
holder.state[subject_symbol]['quantity'], |
|
87
|
|
|
self.details['quantity']): |
|
88
|
|
|
holder.state[subject_symbol]['value'] = average_price( |
|
89
|
|
|
holder.state[subject_symbol]['quantity'], |
|
90
|
|
|
holder.state[subject_symbol]['value'], |
|
91
|
|
|
self.details['quantity'], |
|
92
|
|
|
self.details['value'] |
|
93
|
|
|
) |
|
94
|
|
|
|
|
95
|
|
|
# Update the quantity of the subject in the Holder's posession |
|
96
|
|
|
holder.state[subject_symbol]['quantity'] += self.details['quantity'] |
|
97
|
|
|
|
|
98
|
|
|
# If the Holder don't have a state with this occurrence's Subject, |
|
99
|
|
|
# then register this occurrence as the first state of the Subject |
|
|
|
|
|
|
100
|
|
|
# in the Holder's possession |
|
101
|
|
|
else: |
|
102
|
|
|
holder.state[subject_symbol] = { |
|
103
|
|
|
'quantity': self.details['quantity'], |
|
104
|
|
|
'value': self.details['value'] |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
# If the Holder knows about this Subject but don't have any unit |
|
108
|
|
|
# of it, the paid value of the subject in the Holder state should |
|
109
|
|
|
# be zero. |
|
110
|
|
|
if not holder.state[subject_symbol]['quantity']: |
|
111
|
|
|
holder.state[subject_symbol]['value'] = 0 |
|
112
|
|
|
|
|
113
|
|
|
def average_price(quantity_1, price_1, quantity_2, price_2): |
|
114
|
|
|
"""Calculates the average price between two asset states.""" |
|
115
|
|
|
return (quantity_1 * price_1 + quantity_2 * price_2) / \ |
|
116
|
|
|
(quantity_1 + quantity_2) |
|
117
|
|
|
|
|
118
|
|
|
def same_sign(number_1, number_2): |
|
119
|
|
|
"""Checks if two numbers have the same sign.""" |
|
120
|
|
|
return (number_1 >= 0) ^ (number_2 < 0) |
|
121
|
|
|
|