Skip to content

Commit d4c76c7

Browse files
committed
BUG: Handle _Array properties when pickling
1 parent 57ef461 commit d4c76c7

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

backtesting/_util.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ def __array_finalize__(self, obj):
5656
self.name = getattr(obj, 'name', '')
5757
self._opts = getattr(obj, '_opts', {})
5858

59+
# Make sure properties name and _opts are carried over
60+
# when (un-)pickling.
61+
def __reduce__(self):
62+
value = super().__reduce__()
63+
return value[:2] + (value[2] + (self.__dict__,),)
64+
65+
def __setstate__(self, state):
66+
self.__dict__.update(state[-1])
67+
super().__setstate__(state[:-1])
68+
5969
def __bool__(self):
6070
try:
6171
return bool(self[-1])

backtesting/test/_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import time
55
import unittest
66
import warnings
7+
from concurrent.futures.process import ProcessPoolExecutor
78
from contextlib import contextmanager
89
from glob import glob
910
from runpy import run_path
@@ -899,6 +900,11 @@ def next(self):
899900

900901
Backtest(GOOG.iloc[:20], S).run()
901902

903+
def test_indicators_picklable(self):
904+
with ProcessPoolExecutor() as executor:
905+
stats = executor.submit(Backtest.run, Backtest(SHORT_DATA, SmaCross)).result()
906+
assert stats._strategy._indicators[0]._opts, '._opts and .name were not unpickled'
907+
902908

903909
class TestDocs(TestCase):
904910
DOCS_DIR = os.path.join(os.path.dirname(__file__), '..', '..', 'doc')

0 commit comments

Comments
 (0)