-
Notifications
You must be signed in to change notification settings - Fork 9
/
two.py
30 lines (27 loc) · 1.37 KB
/
two.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import backtrader as bt
import pandas as pd
from datetime import datetime
if __name__ == '__main__':
cerebro = bt.Cerebro()
#获取数据
# stock_hfq_df = pd.read_csv("./data/600000.csv", index_col="datetime", parse_dates=True, usecols=["datetime", "open", "high", "low", "close", "volume"])
start_date = datetime(2021, 1, 4) # 回测开始时间
end_date = datetime(2021, 12, 4) # 回测结束时间
data = bt.feeds.GenericCSVData(dataname="./data/600000.csv",
fromdate=start_date,
todate=end_date,
dtformat="%Y/%m/%d",
datetime=0,
open=4,
high=2,
low=3,
close=1,
volume=5,
openinterest=-1,
reverse=True) #加载数据
# data = bt.feeds.PandasData(dataname=stock_hfq_df, fromdate=start_date, todate=end_date) # 加载数据
cerebro.adddata(data) # 将数据传入回测系统
cerebro.broker.setcash(100000.0)
print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
cerebro.run()
print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())