forked from cleitonsouza01/trade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
73 lines (62 loc) · 1.46 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from trade.holder import Holder
from trade.occurrence import Occurrence
from trade.subject import Subject
# create a holder
holder = Holder()
# define some subject
some_asset = Subject('AST1')
# create an occurrence with that subject.
# In this example, a purchase of 100 units of the asset,
# for the value of $20.
some_occurrence = Occurrence(
some_asset,
'2018-01-02',
{
"quantity": 100,
"value": 20
}
)
# pass it to the holder
holder.trade(some_occurrence)
# check the holder state:
for subject, state in holder.state.items():
print(subject)
print(state)
# AST1
# {'value': 20.0, 'quantity': 100}
# create some other occurrence with that subject.
# In this example, a sale of 20 units of the asset,
# for the value of $30.
holder.trade(Occurrence(
some_asset,
'2018-01-03',
{
"quantity": -20,
"value": 30
}
))
# check the holder state. It should show a change in quantity
# and some profit:
for subject, state in holder.state.items():
print(subject)
print(state)
# AST1
# {'value': 20.0, 'quantity': 80}
# create some other occurrence with that subject.
# Now a purchase of 10 units of the asset, for the
# value of $20.
holder.trade(Occurrence(
some_asset,
'2018-01-04',
{
"quantity": 10,
"value": 25
}
))
# check the holder state. It should show a change in quantity
# and in the value of the subject:
for subject, state in holder.state.items():
print(subject)
print(state)
# AST1
# {'value': 20.555555555555557, 'quantity': 90}