1
|
|
|
"""Some sample events. |
2
|
|
|
|
3
|
|
|
trade: Financial Application Framework |
4
|
|
|
http://trade.readthedocs.org/ |
5
|
|
|
https://github.com/rochars/trade |
6
|
|
|
License: MIT |
7
|
|
|
|
8
|
|
|
This plugin provides a standard set of events for the trade module. |
9
|
|
|
Events are subclasses of the Occurrence class. They are passed to |
10
|
|
|
Accumulator and Porfolio objects to change asset data. |
11
|
|
|
|
12
|
|
|
It contains the definitions of: |
13
|
|
|
- Event |
14
|
|
|
- StockSplit |
15
|
|
|
- BonusShares |
16
|
|
|
|
17
|
|
|
Copyright (c) 2016 Rafael da Silva Rocha |
18
|
|
|
|
19
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy |
20
|
|
|
of this software and associated documentation files (the "Software"), to deal |
21
|
|
|
in the Software without restriction, including without limitation the rights |
22
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
23
|
|
|
copies of the Software, and to permit persons to whom the Software is |
24
|
|
|
furnished to do so, subject to the following conditions: |
25
|
|
|
|
26
|
|
|
The above copyright notice and this permission notice shall be included in |
27
|
|
|
all copies or substantial portions of the Software. |
28
|
|
|
|
29
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
31
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
32
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
33
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
34
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
35
|
|
|
THE SOFTWARE. |
36
|
|
|
""" |
37
|
|
|
|
38
|
|
|
from __future__ import absolute_import |
39
|
|
|
|
40
|
|
|
from . trade.occurrence import Occurrence |
41
|
|
|
from . trade.utils import average_price |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
class BaseEvent(Occurrence): |
45
|
|
|
"""Base class for events.""" |
46
|
|
|
|
47
|
|
|
def __init__(self, asset, date, factor): |
48
|
|
|
super(BaseEvent, self).__init__(asset, date) |
49
|
|
|
self.factor = factor |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
class StockSplit(BaseEvent): |
53
|
|
|
"""A stock split. |
54
|
|
|
|
55
|
|
|
This class represents both stock splits and reverse stock splits. |
56
|
|
|
Stock splits are represented by values greater than 1. |
57
|
|
|
Reverse stock splits are represented by values between 0 and 1. |
58
|
|
|
""" |
59
|
|
|
|
60
|
|
|
def update_accumulator(self, container): |
61
|
|
|
"""Performs a split or a reverse split on the stock.""" |
62
|
|
|
container.state['quantity'] = container.state['quantity'] * self.factor |
63
|
|
|
container.state['price'] = container.state['price'] / self.factor |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
class BonusShares(BaseEvent): |
67
|
|
|
"""Bonus shares.""" |
68
|
|
|
|
69
|
|
|
def update_accumulator(self, container): |
70
|
|
|
"""Add stocks received as bonus shares do the accumulator.""" |
71
|
|
|
new_quantity = container.state['quantity'] * self.factor |
72
|
|
|
container.state['price'] = average_price( |
73
|
|
|
container.state['quantity'], container.state['price'], |
74
|
|
|
new_quantity, 0 |
75
|
|
|
) |
76
|
|
|
container.state['quantity'] += new_quantity |
77
|
|
|
|