how solve this error
-
Traceback (most recent call last):
File "D:/program/pycharm_code/stock_data_deal/双动量策略/部分数据测试/输出收盘价.py", line 62, in <module>
cerebro.run()
File "D:\program\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 1127, in run
runstrat = self.runstrategies(iterstrat)
File "D:\program\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 1212, in runstrategies
data.preload()
File "D:\program\Anaconda3\lib\site-packages\backtrader\feed.py", line 438, in preload
while self.load():
File "D:\program\Anaconda3\lib\site-packages\backtrader\feed.py", line 479, in load
_loadret = self._load()
File "D:\program\Anaconda3\lib\site-packages\backtrader\feeds\pandafeed.py", line 255, in _load
line[0] = self.p.dataname.iloc[self._idx, colindex]
File "D:\program\Anaconda3\lib\site-packages\backtrader\linebuffer.py", line 222, in setitem
self.array[self.idx + ago] = value
TypeError: must be real number, not strmy code:
from __future__ import (absolute_import, division, print_function, unicode_literals) import datetime # For datetime objects import os.path # To manage paths import sys # To find out the script name (in argv[0]) import pandas as pd # Import the backtrader platform import backtrader as bt # Create a Stratey class TestStrategy(bt.Strategy): def log(self, txt, dt=None): ''' Logging function for this strategy''' # 用了默认参数并用了短路原则,如果没有输入参数,就输出数据的日期,一个函数多用<br> # 通过数据对象的属性方式取出线对象 dt = dt or self.datas[0].datetime.date(0)# 生成ISO 8601日期 print('%s, %s' % (dt.isoformat(), txt)) def __init__(self): # Keep a reference to the "close" line in the data[0] dataseries<br> # 一般情况下,通过self.datas[0]选出数据线集合的对象,通过属性选定指定的数据线,就是lines里面的一个对象 self.dataclose = self.datas[0].close def next(self): # Simply log the closing price of the series from the reference<br> # 这里将执行每个数据点位状态的输出,所以具体的逻辑后续也在这里写 self.log('Close, %.2f' % self.dataclose[0]) if __name__ == '__main__': # Create a cerebro entity cerebro = bt.Cerebro() # Add a strategy cerebro.addstrategy(TestStrategy) dataframe = pd.read_csv('D:\program\pycharm_code\stock_data_deal\双动量策略\新数据\clean_ACWI.csv', index_col=0, parse_dates=True) # Datas are in a subfolder of the samples. Need to find where the script is # because it could have been called from anywhere # modpath = os.path.dirname(os.path.abspath(sys.argv[0])) # datapath = os.path.join(modpath, '../../datas/orcl-1995-2014.txt') dataframe['close'].astype('float') print(dataframe) # Create a Data Feed data = bt.feeds.PandasData( dataname=dataframe, # Do not pass values before this date fromdate=datetime.datetime(2011, 1, 1), # Do not pass values before this date todate=datetime.datetime(2011, 12, 31), # Do not pass values after this date ) # Add the Data Feed to Cerebro cerebro.adddata(data) # Set our desired cash start cerebro.broker.setcash(100000.0) # Print out the starting conditions print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue()) # Run over everything cerebro.run() # Print out the final result print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
i just want to output close,but fail,How can I solve this problem
-
open close high low volume openinterest changes
date
2008-03-28 50.0999 50.10 50.1 50.0999 200 0 0.0000
2008-03-31 50.0000 49.32 50 49.32 400 0 -1.5569
2008-04-01 50.2500 50.65 50.65 50.16 584 0 2.6967
2008-04-02 51.1200 51.21 51.4 51.05 10699 0 1.0977
2008-04-03 59.7000 51.50 59.7 50.95 29013 0 0.5742
... ... ... ... ... ... ... ...this is my data,I think it's all numerical
-
strange. Your code works for me with a different csv file.
I would assume that the csv file combined with that line is at fault:dataframe['close'].astype('float')
Best
-
@jack-will said in how solve this error:
this is my data,I think it's all numerical
Just guessing but did you convert your date to datetime?
-
@Jonny8 thank you. i made this on purpose,If i delete this, it will still make an error