Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb157c48

Browse files
committed
REF: AddStraetgy.data.<array>.to_series() method
1 parent7711732 commitb157c48

File tree

6 files changed

+16
-14
lines changed

6 files changed

+16
-14
lines changed

‎backtesting/_util.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
fromnumbersimportNumber
33

44
importnumpyasnp
5+
importpandasaspd
56

67

78
def_as_str(value):
@@ -52,6 +53,9 @@ def __float__(self):
5253
exceptIndexError:
5354
returnsuper().__float__()
5455

56+
defto_series(self):
57+
returnpd.Series(self,index=self._opts['data'].index,name=self.name)
58+
5559

5660
class_Indicator(_Array):
5761
pass

‎backtesting/backtesting.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,9 @@ def data(self) -> _Data:
231231
`backtesting.backtesting.Backtest` internally),
232232
the last array value (e.g. `data.Close[-1]`)
233233
is always the _most recent_ value.
234-
234+
* If you need data arrays (e.g. `data.Close`) to be indexed
235+
Pandas series, you can call their `.to_series()` method
236+
(e.g. `data.Close.to_series()`).
235237
"""
236238
returnself._data
237239

‎backtesting/lib.py‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
from .backtestingimportStrategy
2424
from ._plottingimportplot_heatmapsas_plot_heatmaps
25-
from ._utilimport_Indicator,_as_str
25+
from ._utilimport_Array,_Indicator,_as_str
2626

2727

2828
OHLCV_AGG=OrderedDict((
@@ -175,9 +175,7 @@ class System(Strategy):
175175
def init(self):
176176
# Strategy exposes `self.data` as raw NumPy arrays.
177177
# Let's convert closing prices back to pandas Series.
178-
close = pd.Series(self.data.Close,
179-
index=self.data.index,
180-
name='Close')
178+
close = self.data.Close.to_series()
181179
182180
# Resample to daily resolution. Aggregate groups
183181
# using their last value (i.e. closing price at the end
@@ -200,9 +198,9 @@ def SMA(series, n):
200198
201199
"""
202200
ifnotisinstance(series,pd.Series):
203-
series=pd.Series(series,
204-
index=series._opts['data'].index,
205-
name=series.name)
201+
assertisinstance(series,_Array), \
202+
'resample_apply() takes either a `pd.Series` or a `Strategy.data.*` array'
203+
series=series.to_series()
206204

207205
resampled=series.resample(rule,label='right').agg('last').dropna()
208206
resampled.name=_as_str(series)+'['+rule+']'

‎backtesting/test/_test.py‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ def test_plot_heatmaps(self):
456456
deftest_SignalStrategy(self):
457457
classS(SignalStrategy):
458458
definit(self):
459-
sma=pd.Series(self.data.Close).rolling(10).mean()
459+
sma=self.data.Close.to_series().rolling(10).mean()
460460
self.set_signal(self.data.Close>sma,
461461
self.data.Close<sma)
462462

@@ -469,9 +469,7 @@ def init(self):
469469
super().init()
470470
self.set_atr_periods(40)
471471
self.set_trailing_sl(3)
472-
self.sma=self.I(
473-
lambda:pd.Series(self.data.Close,
474-
index=self.data.index).rolling(10).mean())
472+
self.sma=self.I(lambda:self.data.Close.to_series().rolling(10).mean())
475473

476474
defnext(self):
477475
super().next()

‎doc/examples/Quick Start User Guide.ipynb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@
563563
"\n",
564564
"In `init()`, the whole series of points was available, whereas **in `next()`, the length of `self.data` and any declared indicator arrays is adjusted** on each `next()` call so that `array[-1]` (e.g. `self.data.Close[-1]` or `self.sma1[-1]`) always contains the most recent value, `array[-2]` the previous value, etc. (ordinary Python indexing of ascending-sorted 1D arrays).\n",
565565
"\n",
566-
"**Note**: `self.data` and any indicators wrapped with `self.I` (e.g. `self.sma1`) are **NumPy arrays** for performance reasons. If you need `pandas.Series`, use,e.g., `pd.Series(self.data.Close, index=self.data.index)`.\n",
566+
"**Note**: `self.data` and any indicators wrapped with `self.I` (e.g. `self.sma1`) are **NumPy arrays for performance reasons**. If you need `pandas.Series`, use `.to_series()` method (e.g. `self.data.Close.to_series()`) or construct the series manually (e.g. `pd.Series(self.data.Close, index=self.data.index)`).\n",
567567
"\n",
568568
"Let's see how our strategy performs on historical Google data. We begin with 10,000 units of cash and set broker's commission to realistic 0.2%."
569569
]

‎doc/examples/Quick Start User Guide.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def next(self):
127127
#
128128
# In `init()`, the whole series of points was available, whereas **in `next()`, the length of `self.data` and any declared indicator arrays is adjusted** on each `next()` call so that `array[-1]` (e.g. `self.data.Close[-1]` or `self.sma1[-1]`) always contains the most recent value, `array[-2]` the previous value, etc. (ordinary Python indexing of ascending-sorted 1D arrays).
129129
#
130-
# **Note**: `self.data` and any indicators wrapped with `self.I` (e.g. `self.sma1`) are **NumPy arrays** for performance reasons. If you need `pandas.Series`, use,e.g., `pd.Series(self.data.Close, index=self.data.index)`.
130+
# **Note**: `self.data` and any indicators wrapped with `self.I` (e.g. `self.sma1`) are **NumPy arrays for performance reasons**. If you need `pandas.Series`, use `.to_series()` method (e.g. `self.data.Close.to_series()`) or construct the series manually (e.g. `pd.Series(self.data.Close, index=self.data.index)`).
131131
#
132132
# Let's see how our strategy performs on historical Google data. We begin with 10,000 units of cash and set broker's commission to realistic 0.2%.
133133

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp