Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added fractional shares. Closes issue #1159 👍 #1160

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions backtesting/backtesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,7 @@ def buy(self, *,

See also `Strategy.sell()`.
"""
assert 0 < size < 1 or round(size) == size, \
"size must be a positive fraction of equity, or a positive whole number of units"
assert size > 0, 'size must be positive.'
return self._broker.new_order(size, limit, stop, sl, tp, tag)

def sell(self, *,
Expand All @@ -228,8 +227,7 @@ def sell(self, *,
If you merely want to close an existing long position,
use `Position.close()` or `Trade.close()`.
"""
assert 0 < size < 1 or round(size) == size, \
"size must be a positive fraction of equity, or a positive whole number of units"
assert size > 0, "size must be positive."
return self._broker.new_order(-size, limit, stop, sl, tp, tag)

@property
Expand Down Expand Up @@ -555,7 +553,7 @@ def _copy(self, **kwargs):
def close(self, portion: float = 1.):
"""Place new `Order` to close `portion` of the trade at next market price."""
assert 0 < portion <= 1, "portion must be a fraction between 0 and 1"
size = copysign(max(1, round(abs(self.__size) * portion)), -self.__size)
size = copysign(max(1, abs(self.__size) * portion), -self.__size)
order = Order(self.__broker, size, parent_trade=self, tag=self.__tag)
self.__broker.orders.insert(0, order)

Expand Down