Skip to content

Commit

Permalink
Added docstrings to Position.
Browse files Browse the repository at this point in the history
  • Loading branch information
mhallsmoore committed Nov 7, 2019
1 parent 6bb486e commit 9140a72
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions qstrader/broker/portfolio/position.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ def __init__(
current_price=0.0,
current_dt=None
):
"""
Initialise the Position object and calculate the
position direction (long or short), represented
by a +1 or -1.
"""
self.asset = asset
self.quantity = quantity
self.direction = np.copysign(1, self.quantity)
Expand All @@ -51,6 +46,11 @@ def __repr__(self):
"""
Provides a representation of the Position object to allow
full recreation of the object.
Returns
-------
`str`
The string representation of the Position.
"""
return "%s(asset=%s, quantity=%s, book_cost_pu=%s, " \
"current_price=%s)" % (
Expand All @@ -63,6 +63,11 @@ def market_value(self):
"""
Return the market value of the position based on the
current trade price provided.
Returns
-------
`float`
The current market value of the Position.
"""
return self.current_price * self.quantity

Expand All @@ -71,6 +76,11 @@ def unrealised_gain(self):
"""
Calculate the unrealised absolute gain in currency
of the position based solely on the market value.
Returns
-------
`float`
The unrealised gain of the Position.
"""
return self.market_value - self.book_cost

Expand All @@ -79,6 +89,11 @@ def unrealised_percentage_gain(self):
"""
Calculate the unrealised percentage gain of the
position based solely on the market value.
Returns
-------
`float`
The unrealised percentage gain of the Position.
"""
if self.book_cost == 0.0:
return 0.0
Expand All @@ -88,6 +103,13 @@ def update_book_cost_for_commission(self, asset, commission):
"""
Handles the adjustment to the position book cost due to
trading commissions.
Parameters
----------
asset : `str`
The asset symbol string.
commission : `float`
The commission to be applied to the book cost.
"""
if self.asset != asset:
raise ValueError(
Expand Down Expand Up @@ -117,6 +139,11 @@ def update(self, transaction):
"""
Calculates the adjustments to the Position that occur
once new units in an Asset are bought and sold.
Parameters
----------
transaction : `Transaction`
The Transaction to update the Position with.
"""
if self.asset != transaction.asset:
raise ValueError(
Expand Down Expand Up @@ -150,7 +177,8 @@ def update(self, transaction):
# else:
# # TODO: Implement this branch
# raise NotImplementedError(
# 'Opposing direction with transaction less than position quantity '
# 'Opposing direction with transaction less '
# 'than position quantity '
# 'is not yet implemented.'
# )

Expand Down

0 comments on commit 9140a72

Please sign in to comment.