-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Optimization failure: Fatal Python error: GC object already tracked #103
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
Comments
The real error seems to be:
What backtesting.py version is this, and have you tried master? |
@kernc Backtesting==0.1.7 |
@kernc I tried with __version__ 0.1.8.dev7+g0739f4b
/home/skywalker/PycharmProjects/untitled/test.py:64: UserWarning: Searching best of 486 configurations.
return_heatmap=False)
Traceback (most recent call last):
File "/home/skywalker/PycharmProjects/untitled/test.py", line 64, in <module>
return_heatmap=False)
File "/home/skywalker/PycharmProjects/untitled/venv/lib/python3.7/site-packages/Backtesting-0.1.8.dev7+g0739f4b-py3.7.egg/backtesting/backtesting.py", line 846, in optimize
batch_index, values = future.result()
File "/usr/lib/python3.7/concurrent/futures/_base.py", line 428, in result
return self.__get_result()
File "/usr/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
raise self._exception
concurrent.futures.process.BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending.
Process finished with exit code 1 |
Well, the relevant traceback above ( backtesting.py/backtesting/backtesting.py Line 900 in 0739f4b
What pandas version is that? Have you tried upgrading or downgrading pandas? |
pandas~=1.0.4 |
The examples are covered in tests which do seem to pass without error, occasionally. |
@kernc Closing this issue as I figured out the correct way of doing it. This issue #90 helped! from backtesting import Backtest, Strategy
from backtesting.lib import crossover
from backtesting.test import SMA, GOOG, EURUSD
class Sma4Cross(Strategy):
n1 = 50
n2 = 100
n_enter = 20
n_exit = 10
def init(self):
self.sma1 = self.I(SMA, self.data.Close, self.n1)
self.sma2 = self.I(SMA, self.data.Close, self.n2)
self.sma_enter = self.I(SMA, self.data.Close, self.n_enter)
self.sma_exit = self.I(SMA, self.data.Close, self.n_exit)
def next(self):
if not self.position:
# On upwards trend, if price closes above
# "entry" MA, go long
# Here, even though the operands are arrays, this
# works by implicitly comparing the two last values
if self.sma1 > self.sma2:
if crossover(self.data.Close, self.sma_enter):
self.buy()
# On downwards trend, if price closes below
# "entry" MA, go short
else:
if crossover(self.sma_enter, self.data.Close):
self.sell()
# But if we already hold a position and the price
# closes back below (above) "exit" MA, close the position
else:
if (self.position.is_long and
crossover(self.sma_exit, self.data.Close)
or
self.position.is_short and
crossover(self.data.Close, self.sma_exit)):
self.position.close()
def prepare_optimization(cls: type) -> dict:
# Construct and return optimization kwargs
return {'n1': range(5, 30, 5),
'n2': range(10, 70, 5),
'n_enter': range(15, 35, 5),
'n_exit': range(10, 25, 5),
'constraint': lambda p: p.n_exit < p.n_enter < p.n1 < p.n2,
'maximize': 'Equity Final [$]'}
if __name__ == '__main__':
bt = Backtest(GOOG, Sma4Cross, commission=.002)
opt_kwargs = prepare_optimization(Sma4Cross)
stats, heatmap = bt.optimize(**opt_kwargs, return_heatmap=True)
print(stats)
print('*'*50)
print(heatmap)
print('*'*50)
print(heatmap.sort_values().iloc[-3:])
print('*'*50)
hm = heatmap.groupby(['n1', 'n2']).mean().unstack()
print(hm)
print('*'*50)
from backtesting.lib import plot_heatmaps
plot_heatmaps(heatmap, agg='mean')
# bt.plot() |
This is the same code from the
doc > examples
for parameter optimizationdoc/examples/Parameter Heatmap.ipynb
Running the above code results in BrokenProcessPool @kernc any ideas on what's wrong
P.S: Running Ubuntu 20.04 LTS / Python 3.7
The text was updated successfully, but these errors were encountered: