-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommission.py
136 lines (95 loc) · 2.39 KB
/
commission.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python
from decimal import *
D = Decimal
pennies = D('0.01')
def percent (pct_value):
"""
returns a function which will apply a percent value commission to an input transaction value.
>>> x = percent(10)
>>> x(10, D('10.0'))
Decimal('10.00')
>>> x(-10, D('10.0'))
Decimal('10.00')
"""
pct_value = D(pct_value)
def commission (xact_qty, xact_price):
return (abs((pct_value / D('100.')) * xact_qty * xact_price)).quantize(pennies)
return commission
def percent_liquidate (pct_value):
pct_value = D(pct_value)
def liquidate (position_cost, position_qty):
return (position_cost / ((100 - pct_value) / D('100.') * position_qty)).quantize(pennies)
return liquidate
def ib ():
"""
returns a function which applies a fixed cost per share
>>> x = ib()
>>> x(1, 100)
Decimal('1')
>>> x(200, 100)
Decimal('1.00')
>>> x(199, 100)
Decimal('1')
>>> x(202, 100)
Decimal('1.01')
"""
def commission (xact_qty, xact_price):
"""
0.005 per share, one dollar min
"""
comm = xact_qty * D('0.005')
if comm < D(1):
return(D(1))
return comm.quantize(pennies)
return commission
def sb ():
"""
returns a function which applies a fixed cost per share
(JSCOTT SHAREBUILDER)
>>> x = ib()
>>> x(1, 100)
Decimal('1')
>>> x(200, 100)
Decimal('1.00')
>>> x(199, 100)
Decimal('1')
>>> x(202, 100)
Decimal('1.01')
"""
def commission (xact_qty, xact_price):
"""
0.005 per share, one dollar min
"""
comm = D('9.95')
return comm
return commission
def pershare (per_value):
"""
returns a function which applies a fixed cost per share
>>> x = pershare(D('0.05'))
>>> x(10, D('10.0'))
Decimal('0.50')
>>> x(-10, D('100.0'))
Decimal('0.50')
"""
per_value = D(per_value)
def commission (xact_qty, xact_price):
""" price not used """
return (abs(per_value * xact_qty)).quantize(pennies)
return commission
def pershare_liquidate (per_value):
per_value = D(per_value)
def liquidate (position_cost, position_qty):
return ((position_cost + (per_value * position_qty)) / position_qty).quantize(pennies)
return liquidate
def fidelity ():
def commission (xact_qty, xact_price):
return(D('7.95'))
return commission
def fidelity_liquidate ():
def liquidate (position_cost, position_qty):
return(((position_cost + D('7.95'))/position_qty).quantize(pennies))
return liquidate
if __name__ == "__main__":
import doctest
doctest.testmod()