-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
What is the structure of self.data ? #526
-
I defined a func to prepare for the indicator which will use both open, high, low, close columns as input. As my experience before, the package will first turn all pandas DataFrames into np.arrays. So I add a line to turn it back into pandas, so that I can use pd funcs to prepare the data. But it is throwing an error which indicates that DataFrame constructor failed.
`def slump(df):
df = pd.DataFrame(df)
big = ((df.High - df.Low) > 10).astype(int)
up = ((df.Open - df.Close) < 0).astype(int)
return big*up
...
class Big10(Strategy):
n = 52
def init(self):
self.big = self.I(slump, self.data, plot=True) `
and the error is:
Indicator "slump" errored with exception: DataFrame constructor not properly called!
I guess the problem lies in my calling self.data , as the input. As I see in the tutorials, self.data.Close works fine. So, I guess self.data has all the 5 columns ?
Beta Was this translation helpful? Give feedback.
All reactions
The docs explicitly say Strategy.data
is not a dataframe, so you can't just use it as if.
If you need data to become a DataFrame, you can use its .df accessor
(self.data.df
), or .s
accessor for individual series (self.data.Close.s
).
Finally, when in doubt, just look in the source:
backtesting.py/backtesting/_util.py
Lines 103 to 195 in 267d99f
Replies: 1 comment 1 reply
-
The docs explicitly say Strategy.data
is not a dataframe, so you can't just use it as if.
If you need data to become a DataFrame, you can use its .df accessor
(self.data.df
), or .s
accessor for individual series (self.data.Close.s
).
Finally, when in doubt, just look in the source:
backtesting.py/backtesting/_util.py
Lines 103 to 195 in 267d99f
😁
Beta Was this translation helpful? Give feedback.
All reactions
-
Cool ! It works. So I guess, it works, if I prepare all the conditions into one column and pass it onto OHLC data. Then, access it in a way, for example, when self.data.Signal == 1 buy, and when self.data.Signal == -1 close position ?
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1