You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you are trading currencies, it makes sense to allow for a dollar and a quarter. So, why isn't it allowed?
Hence, the expected behavior when calling self.buy(size=1.1) is not an error, but rather the absence of any error. Printing output['_trades'] should result in this output:
Given that the size of a buy or sell order is a fraction.
Actual Behavior
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
Cell In[2], [line 27](vscode-notebook-cell:?execution_count=2&line=27)
[20](vscode-notebook-cell:?execution_count=2&line=20) self.sell(size=1.1) # changed to fractional
[23](vscode-notebook-cell:?execution_count=2&line=23) bt = Backtest(GOOG, SmaCross,
[24](vscode-notebook-cell:?execution_count=2&line=24) cash=10000, commission=.002,
[25](vscode-notebook-cell:?execution_count=2&line=25) exclusive_orders=True)
---> [27](vscode-notebook-cell:?execution_count=2&line=27) output = bt.run()
[28](vscode-notebook-cell:?execution_count=2&line=28) bt.plot()
File /opt/homebrew/lib/python3.10/site-packages/backtesting/backtesting.py:1170, in Backtest.run(self, **kwargs)
[1167](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/lib/python3.10/site-packages/backtesting/backtesting.py:1167) break
[1169](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/lib/python3.10/site-packages/backtesting/backtesting.py:1169) # Next tick, a moment before bar close
-> [1170](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/lib/python3.10/site-packages/backtesting/backtesting.py:1170) strategy.next()
[1171](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/lib/python3.10/site-packages/backtesting/backtesting.py:1171) else:
[1172](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/lib/python3.10/site-packages/backtesting/backtesting.py:1172) # Close any remaining open trades so they produce some stats
[1173](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/lib/python3.10/site-packages/backtesting/backtesting.py:1173) for trade in broker.trades:
Cell In[2], [line 20](vscode-notebook-cell:?execution_count=2&line=20)
[18](vscode-notebook-cell:?execution_count=2&line=18) self.buy(size=1.1) # changed to fractional
[19](vscode-notebook-cell:?execution_count=2&line=19) elif crossover(self.sma2, self.sma1):
---> [20](vscode-notebook-cell:?execution_count=2&line=20) self.sell(size=1.1)
File /opt/homebrew/lib/python3.10/site-packages/backtesting/backtesting.py:223, in Strategy.sell(self, size, limit, stop, sl, tp)
[212](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/lib/python3.10/site-packages/backtesting/backtesting.py:212) def sell(self, *,
[213](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/lib/python3.10/site-packages/backtesting/backtesting.py:213) size: float = _FULL_EQUITY,
[214](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/lib/python3.10/site-packages/backtesting/backtesting.py:214) limit: float = None,
[215](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/lib/python3.10/site-packages/backtesting/backtesting.py:215) stop: float = None,
[216](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/lib/python3.10/site-packages/backtesting/backtesting.py:216) sl: float = None,
[217](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/lib/python3.10/site-packages/backtesting/backtesting.py:217) tp: float = None):
[218](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/lib/python3.10/site-packages/backtesting/backtesting.py:218) """
[219](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/lib/python3.10/site-packages/backtesting/backtesting.py:219) Place a new short order. For explanation of parameters, see `Order` and its properties.
[220](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/lib/python3.10/site-packages/backtesting/backtesting.py:220)
[221](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/lib/python3.10/site-packages/backtesting/backtesting.py:221) See also `Strategy.buy()`.
[222](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/lib/python3.10/site-packages/backtesting/backtesting.py:222) """
--> [223](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/lib/python3.10/site-packages/backtesting/backtesting.py:223) assert 0 < size < 1 or round(size) == size, \
[224](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/lib/python3.10/site-packages/backtesting/backtesting.py:224) "size must be a positive fraction of equity, or a positive whole number of units"
[225](https://file+.vscode-resource.vscode-cdn.net/opt/homebrew/lib/python3.10/site-packages/backtesting/backtesting.py:225) return self._broker.new_order(-size, limit, stop, sl, tp)
AssertionError: size must be a positive fraction of equity, or a positive whole number of units
Fixes fractional shares issue.
This:
```
size = copysign(max(1, round(abs(self.__size) * portion)), -self.__size)
```
had to be changed to this:
```
size = copysign(max(1, abs(self.__size) * portion), -self.__size)
```
Otherwise numbers less than 0 are handled like percentages of total cash
Expected Behavior
Fractional shares are supported by numerous brokers. Here is a list of some:
Charles Schwab
Fidelity Investments
Interactive Brokers
Robinhood
E-Trade
Merrill Edge
Vanguard
Tastytrade
SoFi Active Investing
WellsTrade
If you are trading currencies, it makes sense to allow for a dollar and a quarter. So, why isn't it allowed?
Hence, the expected behavior when calling
self.buy(size=1.1)
is not an error, but rather the absence of any error. Printingoutput['_trades']
should result in this output:Given that the size of a buy or sell order is a fraction.
Actual Behavior
Steps to Reproduce
size
inself.buy()
and/orself.sell()
to a floating point numberAdditional info
The text was updated successfully, but these errors were encountered: