Skip to content

Is there a tutorial showing how I can use my own pandas dataframe? #182

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

Closed
sedna16 opened this issue Nov 15, 2020 · 9 comments
Closed

Is there a tutorial showing how I can use my own pandas dataframe? #182

sedna16 opened this issue Nov 15, 2020 · 9 comments
Labels
invalid This is not a (valid) bug report

Comments

@sedna16
Copy link

sedna16 commented Nov 15, 2020

I've checked the docs and there seems to be no way to implement or add my own pandas dataframe.

I needs to do this because I will be using that df in the next()

@kernc
Copy link
Owner

kernc commented Nov 15, 2020

add my own pandas dataframe

What do you mean? Backtesting.py only works with your data frames! The dataframe needs the OHLC(V) columns as prescribed, and any additional columns are optional. See: #64.

If you need back the df in next(), you can use its .df accessor:

    def next(self):
        df = self.data.df
        ...

@sedna16
Copy link
Author

sedna16 commented Nov 16, 2020

I tried doing it. Here's what I got with the result

Start                     2018-11-14 00:00:00
End                       2020-11-13 00:00:00
Duration                    730 days 00:00:00
Exposure Time [%]                           0
Equity Final [$]                        10000
Equity Peak [$]                         10000
Return [%]                                  0
Buy & Hold Return [%]                 70.2681
Max. Drawdown [%]                          -0
Avg. Drawdown [%]                         NaN
Max. Drawdown Duration                    NaN
Avg. Drawdown Duration                    NaN
# Trades                                    0
Win Rate [%]                              NaN
Best Trade [%]                            NaN
Worst Trade [%]                           NaN
Avg. Trade [%]                            NaN
Max. Trade Duration                       NaN
Avg. Trade Duration                       NaN
Profit Factor                             NaN
Expectancy [%]                            NaN
SQN                                       NaN
Sharpe Ratio                              NaN
Sortino Ratio                             NaN
Calmar Ratio                              NaN
_strategy                            SMACross
_equity_curve                          Equ...
_trades                   Empty DataFrame
...
dtype: object

The colums for dataframe are as follows:
Date (index)
Open
High
Low
Close
Volume
sma_20
sma_50

@sedna16
Copy link
Author

sedna16 commented Nov 16, 2020

Take note: the indicators are pre-added from outside the SMACross class

@kernc
Copy link
Owner

kernc commented Nov 16, 2020

Sorry, but without code/data, that's really not enough to go on. 👉 Zero trades were made.
Perhaps raise your cash/leverage.

@sedna16
Copy link
Author

sedna16 commented Nov 16, 2020

ok, I'll try to post the code here after work.

@sedna16
Copy link
Author

sedna16 commented Nov 17, 2020

I think I finally got it working (results, no charts yet)

	def next(self):
		# If sma1 crosses above sma2, close any existing
		# short trades, and buy the asset
		if crossover(self.data.df.sma_20, self.data.df.sma_50):
			self.position.close()
			self.buy()

		# Else, if sma1 crosses below sma2, close any existing
		# long trades, and sell the asset
		elif crossover(self.data.df.sma_50, self.data.df.sma_20):
			self.position.close()
			self.sell()`

@kernc
Copy link
Owner

kernc commented Nov 17, 2020

Note, you should find it far more performant not to convert to df:

if crossover(self.data.sma_20, self.data.sma_50):
    ...

So is there an issue here?

@sedna16
Copy link
Author

sedna16 commented Nov 17, 2020

I'll test it out.

@sedna16
Copy link
Author

sedna16 commented Nov 18, 2020

Note, you should find it far more performant not to convert to df:

if crossover(self.data.sma_20, self.data.sma_50):
    ...

So is there an issue here?

Tested this out, it worked. But somehow the first time I used it, it didn't behaved properly.

@kernc kernc closed this as completed Nov 18, 2020
@kernc kernc added the invalid This is not a (valid) bug report label Nov 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This is not a (valid) bug report
Projects
None yet
Development

No branches or pull requests

2 participants