Skip to content
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

buy() method does not work. #510

Open
arturnawrot opened this issue Oct 22, 2021 · 4 comments
Open

buy() method does not work. #510

arturnawrot opened this issue Oct 22, 2021 · 4 comments

Comments

@arturnawrot
Copy link

arturnawrot commented Oct 22, 2021

When I'm importing data from Coinbase API (you don't need any API keys to reproduce the issue, just copy-paste the snippet below) self.buy() and self.sell() do not work at all. Although self.plot() works flawlessly and the data is normally plotted, trades are not executed at all. Possibly there's something wrong with data formatting but it does not return any warning nor errors, so I'm not sure whether it's an issue with my data or the backtesting.py library.

When I used Forex data for this script then everything worked.

You may need to install cbpro pip install cbpro

import pandas as pd
from backtesting import Strategy
from backtesting import Backtest
import pandas as pd
import cbpro

public_client = cbpro.PublicClient()

data = public_client.get_product_historic_rates('ETH-USD', granularity=900)

# data = [
#   [1634928300, 3964.4, 3975.74, 3968.12, 3965.88, 624.23484212],
#   [1634927400, 3940.56, 3973.95, 3942.74, 3968.12, 3112.89691445],
#   [1634926500, 3915.02, 3948.26, 3931.91, 3942.69, 2164.6179708] ... and more
# ]

data = pd.DataFrame(data)
data.columns= ["Date","Low","High","Open","Close","Volume"]
data['Date'] = pd.to_datetime(data['Date'], unit='s')
data.set_index('Date', inplace=True)

# data
# Date                 Low      High     Open     Close    Volume
# 2021-10-22 18:45:00  3956.44  3975.74  3968.12  3962.28  1056.597933
# 2021-10-22 18:30:00  3940.56  3973.95  3942.74  3968.12  3112.896914
# 2021-10-22 18:15:00  3915.02  3948.26  3931.91  3942.69  2164.617971
# ... and more

class SmaCross(Strategy):
    
    def init(self):
        pass
        
    def next(self):
        self.buy(limit=4100, tp=4500, sl=3900) 
       # even self.buy() without any parameters does not work, but with Forex data it executes at the very beginning and ends
       # on the last candle.

bt = Backtest(data, SmaCross, cash=1000)

stats = bt.run()

print(stats)

bt.plot(resample=False)

# stats
# Start                     2021-10-19 16:15:00
# End                       2021-10-22 19:00:00
# Duration                      3 days 02:45:00
# Exposure Time [%]                         0.0
# Equity Final [$]                       1000.0
# Equity Peak [$]                        1000.0
# Return [%]                                0.0
# Buy & Hold Return [%]                4.345899
# Return (Ann.) [%]                         0.0
# Volatility (Ann.) [%]                     0.0
# Sharpe Ratio                              NaN
# Sortino Ratio                             NaN
# Calmar Ratio                              NaN
# Max. Drawdown [%]                        -0.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
# _strategy                            SmaCross
# _equity_curve                             ...
# _trades                   Empty DataFrame
# ...
# dtype: object
  • Backtesting version: 0.3.2
@kernc
Copy link
Owner

kernc commented Oct 23, 2021

Do you see any warnings issued?

@arturnawrot
Copy link
Author

No

@kernc
Copy link
Owner

kernc commented Oct 23, 2021

I believe you should have seen this warning printed:

if np.any(data['Close'] > cash):
warnings.warn('Some prices are larger than initial cash value. Note that fractional '
'trading is not supported. If you want to trade Bitcoin, '
'increase initial cash, or trade μBTC or satoshis instead (GH-134).',
stacklevel=2)

See #134.

@stellenberger
Copy link

For anyone having this issue; the size of the buy needs to be set to 1, and you need to give it enough cash to buy whole bitcoins. Just times the BTC price by 10.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants