trade.occurrence   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 36
dl 0
loc 142
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A average_price() 0 4 1
A same_sign() 0 3 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A Occurrence.__init__() 0 4 1
B Occurrence.update_holder() 0 89 6
1
"""trade.Occurrence
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
30
class Occurrence(object):
31
    """A class to represent occurrences with subjects.
32
33
    Attributes:
34
        subject: A Subject instance.
35
        date: A string to represent the moment of the occurrence.
36
        details: A dict with the occurrence details.
37
    """
38
39
    def __init__(self, subject, date, details):
40
        self.subject = subject
41
        self.date = date
42
        self.details = details
43
44
    def update_holder(self, holder):
45
        """Udpate the Holder state according to the occurrence.
46
47
        This implementation is a example of how a Occurrence object
48
        can update the Holder state; this method should be overriden
49
        by classes that inherit from the Occurrence class.
50
51
        This sample implementation simply update the quantity and the average
52
        price of the Subject in the Holder's possession every time objects
53
        from this class are passed to Holder.trade().
54
55
        This sample implementation considers the following signature for
56
        the Holder.state dict:
57
        
58
        .. code:: python
59
60
            {
61
                "SUBJECT SYMBOL": {
62
                    "quantity": 0,
63
                    "value": 0
64
                }
65
            }
66
67
        And the following signature for the Occurrance.details dict:
68
        
69
        .. code:: python
70
71
            {
72
                "quantity": 0,
73
                "value": 0
74
            }
75
        """
76
77
        subject_symbol = self.subject.symbol
78
79
        # If the Holder already have a state regarding this Subject,
80
        # update that state
81
        if subject_symbol in holder.state:
82
83
            # If the Holder have zero units of this subject, the average
84
            # value paid/received for the subject is the value of the trade itself
85
            if not holder.state[subject_symbol]['quantity']:
86
                holder.state[subject_symbol]['value'] = self.details['value']
87
88
            # If the Holder owns units of this subject then the average value
89
            # paid/received for the subject may need to be updated with
90
            # this occurrence details
91
92
            # If the occurrence have the same sign as the quantity in the Holder
93
            # state, a new average value needs to be calculated for the subject
94
            elif same_sign(
95
                    holder.state[subject_symbol]['quantity'],
96
                    self.details['quantity']):
97
                holder.state[subject_symbol]['value'] = average_price(
98
                    holder.state[subject_symbol]['quantity'],
99
                    holder.state[subject_symbol]['value'],
100
                    self.details['quantity'],
101
                    self.details['value']
102
                )
103
104
            # If the occurrence does not have the same sign of the quantity in the
105
            # Holder state, then do other stuff.
106
            # A trade app would normally implement some sort of profit/loss logic
107
            # here.
108
            # This sample implementation only checks if the average value
109
            # of the subject needs to be updated and then update it as needed.
110
            else:
111
                if same_sign(
112
                        self.details['quantity'],
113
                        holder.state[subject_symbol]['quantity'] + self.details['quantity']):
114
                    holder.state[subject_symbol]['value'] = self.details['value']
115
116
            # Update the quantity of the subject in the Holder's posession
117
            holder.state[subject_symbol]['quantity'] += self.details['quantity']
118
119
        # If the Holder don't have a state with this occurrence's Subject,
120
        # then register this occurrence as the first state of the Subject
121
        # in the Holder's possession
122
        else:
123
            holder.state[subject_symbol] = {
124
                'quantity': self.details['quantity'],
125
                'value': self.details['value']
126
            }
127
128
        # If the Holder knows about this Subject but don't have any unit
129
        # of it, the paid value of the subject in the Holder state should
130
        # be zero.
131
        if not holder.state[subject_symbol]['quantity']:
132
            holder.state[subject_symbol]['value'] = 0
133
134
def average_price(quantity_1, price_1, quantity_2, price_2):
135
    """Calculates the average price between two asset states."""
136
    return (quantity_1 * price_1 + quantity_2 * price_2) / \
137
            (quantity_1 + quantity_2)
138
139
def same_sign(number_1, number_2):
140
    """Checks if two numbers have the same sign."""
141
    return (number_1 >= 0) ^ (number_2 < 0)
142