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

Errors with SmaCross #1168

Unanswered
Danz009 asked this question in Q&A
Discussion options

I am new to coding python and this is my first day doing backtesting, here is my code and my .csv is attached I think. Any help would be much appreciated.


from backtesting import Backtest, Strategy
from backtesting.lib import crossover
from backtesting.test import SMA, GOOG
import pandas as pd
from datetime import datetime
df = pd.read_csv('Gold Futures OHLC.csv')
df = df.set_index('Date')
df.index = pd.to_datetime(df.index)
class SmaCross(Strategy):
 n1 = 13
 n2 = 21
def init(self):
 close = self.data.Close
 self.sma1 = self.I(SMA, close, self.n1)
 self.sma2 = self.I(SMA, close, self.n2)
def next(self):
 if crossover(self.sma1, self.sma2):
 self.buy()
 elif crossover(self.sma2, self.sma1):
 self.sell()
bt = Backtest(df, SmaCross,
 cash=100_000_000, commission=.002,
 exclusive_orders=True)
output = bt.run()
bt.plot()

And here is the response.


C:\Users\claym\Documents\GitHub\Python\Trading\Backtesting\venv\Lib\site-packages\backtesting\test_init_.py:8: FutureWarning: The argument 'infer_datetime_format' is deprecated and will be removed in a future version. A strict version of it is now the default, see https://pandas.pydata.org/pdeps/0004-consistent-to-datetime-parsing.html. You can safely remove this argument.
return pd.read_csv(join(dirname(file), filename),
Traceback (most recent call last):
File "c:\Users\claym\Documents\GitHub\Python\Trading\Backtesting\kernc-github.py", line 27, in
bt = Backtest(df, SmaCross,
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\claym\Documents\GitHub\Python\Trading\Backtesting\venv\Lib\site-packages\backtesting\backtesting.py", line 1073, in init
if np.any(data['Close'] > cash):
^^^^^^^^^^^^^^^^^^^^
File "C:\Users\claym\Documents\GitHub\Python\Trading\Backtesting\venv\Lib\site-packages\pandas\core\ops\common.py", line 76, in new_method
return method(self, other)
^^^^^^^^^^^^^^^^^^^
File "C:\Users\claym\Documents\GitHub\Python\Trading\Backtesting\venv\Lib\site-packages\pandas\core\arraylike.py", line 56, in gt
return self._cmp_method(other, operator.gt)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\claym\Documents\GitHub\Python\Trading\Backtesting\venv\Lib\site-packages\pandas\core\series.py", line 6119, in _cmp_method
res_values = ops.comparison_op(lvalues, rvalues, op)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\claym\Documents\GitHub\Python\Trading\Backtesting\venv\Lib\site-packages\pandas\core\ops\array_ops.py", line 344, in comparison_op
res_values = comp_method_OBJECT_ARRAY(op, lvalues, rvalues)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\claym\Documents\GitHub\Python\Trading\Backtesting\venv\Lib\site-packages\pandas\core\ops\array_ops.py", line 129, in comp_method_OBJECT_ARRAY
result = libops.scalar_compare(x.ravel(), y, op)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "ops.pyx", line 107, in pandas._libs.ops.scalar_compare
TypeError: '>' not supported between instances of 'str' and 'int'


I can provide more clarification or upload more files if this is not enough information.

Gold Futures OHLC.csv

You must be logged in to vote

Replies: 2 comments

Comment options

hello

I reached out to you in order to have a better chat after seeing your article.
Are you still in need of assistance? If so, please let me know so that we may have a more in-depth conversation.
message me on discord: bushraa03

You must be logged in to vote
0 replies
Comment options

In the data, try converting the values ​​to the data type float
Rename column "Vol." to "Volume"
cash can be specified like this cash=100000000

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /