Passed
Push — master ( 98cb16...1132f9 )
by Rafael S.
01:35
created

trade/occurrence.py (1 issue)

Severity
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
33
class Occurrence(object):
0 ignored issues
show
The variable __class__ seems to be unused.
Loading history...
34
    """A class to represent occurrences with subjects.
35
36
    Attributes:
37
        subject: A Subject instance.
38
        date: A string to represent the moment of the occurrence.
39
        details: A dict with the occurrence details.
40
    """
41
42
    def __init__(self, subject, date, details):
43
        self.subject = subject
44
        self.date = date
45
        self.details = details
46
47
    def update_holder(self, holder):
48
        """Udpate the Holder state according to the occurrence.
49
50
        This implementation is a example of how a Occurrence object
51
        can update the Holder state; this method should be overriden
52
        by classes that inherit from the Occurrence class.
53
54
        This sample implementation simply update the quantity and the average
55
        price of the Subject in the Holder's possession every time objects
56
        from this class are passed to Holder.trade().
57
58
        This sample implementation considers the following signature for
59
        the Holder.state dict:
60
            {
61
                "SUBJECT SYMBOL": {
62
                    "quantity": 0,
63
                    "value": 0
64
                },
65
                ...
66
            }
67
68
        And the following signature for the Occurrance.details dict:
69
            {
70
                "quantity": 0,
71
                "value": 0
72
            }
73
        """
74
75
        subject_symbol = self.subject.symbol
76
77
        # If the Holder already have a state regarding this Subject,
78
        # update that state
79
        if subject_symbol in holder.state:
80
81
            # If the Holder have zero units of this subject, the average
82
            # value paid/received for the subject is the value of the trade itself
83
            if not holder.state[subject_symbol]['quantity']:
84
                holder.state[subject_symbol]['value'] = self.details['value']
85
86
            # If the Holder owns units of this subject then the average value
87
            # paid/received for the subject may need to be updated with
88
            # this occurrence details
89
            elif same_sign(
90
                    holder.state[subject_symbol]['quantity'],
91
                    self.details['quantity']):
92
                holder.state[subject_symbol]['value'] = average_price(
93
                    holder.state[subject_symbol]['quantity'],
94
                    holder.state[subject_symbol]['value'],
95
                    self.details['quantity'],
96
                    self.details['value']
97
                )
98
99
            # Update the quantity of the subject in the Holder's posession
100
            holder.state[subject_symbol]['quantity'] += self.details['quantity']
101
102
        # If the Holder don't have a state with this occurrence's Subject,
103
        # then register this occurrence as the first state of the Subject
104
        # in the Holder's possession
105
        else:
106
            holder.state[subject_symbol] = {
107
                'quantity': self.details['quantity'],
108
                'value': self.details['value']
109
            }
110
111
        # If the Holder knows about this Subject but don't have any unit
112
        # of it, the paid value of the subject in the Holder state should
113
        # be zero.
114
        if not holder.state[subject_symbol]['quantity']:
115
            holder.state[subject_symbol]['value'] = 0
116
117
def average_price(quantity_1, price_1, quantity_2, price_2):
118
    """Calculates the average price between two asset states."""
119
    return (quantity_1 * price_1 + quantity_2 * price_2) / \
120
            (quantity_1 + quantity_2)
121
122
def same_sign(number_1, number_2):
123
    """Checks if two numbers have the same sign."""
124
    return (number_1 >= 0) ^ (number_2 < 0)
125