-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Order/Trade/Position API #47
Conversation
57c47a3
to
7e229d6
Compare
776e55a
to
b8da2c3
Compare
Awesome framework! This looks like a great PR too. If I wanted to disable the NFA compliant FIFO functionality, would that be as simple as commenting out lines 824 to 842 in backtesting.py? This rule only applies to brokers in the USA, so perhaps this could even be a configuration option? |
one bug found:
should the order be removed as it fails fro insufficient cash?
|
for the previous size = order.size
if -1 < size < 1:
#close order do not check margin_available
if np.sign(self.position.size) + np.sign(order.size) == 0:
size=np.sign(size)*round(abs(size)*abs(self.position.size))
else:
size = np.sign(size) * int((self.margin_available * self._leverage * abs(size))
// adjusted_price)
# Not enough cash/margin even for a single unit
if not size:
continue |
self.buy(size=.1)
# ...
self.sell(size=.05) # order.size = 0.5 This also likely wouldn't work for hedging traders that run long and short trades in parallel (see #47 (comment)). I think a better approach would be to fix |
would it be more clear to distiguish order as open(long or short) orders and close orders(long or short)? an open order leverages available cash(percentage) , and close orders count on holding positions( percentage)? |
how does resample work to comply various open hours (start time unalign and segmented hours)? |
what if adding optional data1,data2,data3 to Strategy, cause only strategy might use extra timeframe data (also Backtest class to pass data to strategy). brokers are not impacted.
and self.I should reindex the extra timeframe to base timeframe. in Backtest.run(), steps still count on data(base timeframe), when data's time meet data1..data3's time, signal strategy e.g. strategy.next_on_data1( )
|
one more change is to enable strategy choose brokers ,thus can split backtest for future/us stock /forex etc to standalone brokers, instead of mixing up everrthing in one process/function, expect to be easier for implementation |
@kernc hi, are you going to use dataframe for trades and maybe orders,positions?As I am building realtrading , would like to align with you to get minimum difference from the backtest
|
The only place where Backtesting.py does resampling is in backtesting.py/backtesting/_plotting.py Line 367 in b460092
Everywhere else we just use whatever bars are available in user-provided |
Not in favor of extra data parameters. Resampling and data shaping is the domain of and easy enough in pandas. Note, |
If you mean the new mentioned dataframes |
@sdmovie And dataframes, compared to simple dicts, namedtuples, or thin dataclasses are also painfully slow. I was considering it for a time, but the few advantages are minor. 😅 |
@arunavo4 Thanks. I fixed the geometric mean of negative numbers by adding +1 to contain all returns around 1 instead of 0. Seems to work and is the correct approach as returns of 1 (× 1.0 × 1.0 × ...) maintain the identity. backtesting.py/backtesting/backtesting.py Line 1288 in e56f758
|
@sdmovie I fixed the stale orders issue, but the |
@qacollective I added |
Anybody happen to have an idea why this fails on Python 3.5? 😕
backtesting.py/backtesting/backtesting.py Lines 925 to 935 in b123fba
|
Trailing commas in function declarations in combination with * were only allowed from Python version 3.6 onwards. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️
…n new orders And thus approximates previous (0.1.x) behavior.
e8223d8
to
e1600b6
Compare
As a simple performance benchmark, 0.1.8 CI tests take 2min 40sec versus this PR 3min 1sec. It's the same ballpark figure, so its fine. |
Much thanks to @qacollective, @sdmovie, and @arunavo4 for reviews and help! |
Hi, It confused me at the begining. |
refs #8
fixes #28
closes #77
closes #83
closes #92
New features:
Strategy.orders
list of active orders,Strategy.trades
list of active trades,Strategy.closed_trades
list of settled trades,Order
class, representing an active order,Trade
class, representing an active or already settled trade,stats
keys (fixes Way to access equity values, individual trade returns, ... #4, fixes Track the actions of the trade strategy #29):_equity_curve
, equity, drawdown, and drawdown duration,_trades
, DataFrame of executed trades.Breakages:
Strategy.position.close()
between subsequentStrategy.buy/sell()
calls to approximate previous behavior.Strategy.buy(price=)
→Strategy.buy(limit=)
Strategy.buy/sell()
only takes keyword parameters.Position.open_price
→Trade.entry_price
Position.open_time
→Trade.entry_time
Strategy.orders
→Strategy.orders[i]
Backtest.plot(omit_missing=)
is gone.backtesting.lib.SignalStrategy
.