-
Notifications
You must be signed in to change notification settings - Fork 0
/
test1.py
66 lines (41 loc) · 1.37 KB
/
test1.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import pandas as pd
import jqdatasdk as jq
from jqdatasdk.technical_analysis import RSI
from jqfactor_analyzer.utils import get_forward_returns_columns
import tushare as ts
import backtrader as bt
import datetime
current_date = datetime.datetime.now().date().strftime('%Y%m%d')
#current_date=current_date.replace("-","")
print("当前日期:", current_date)
pro=ts.pro_api('79c8ef91769f1ffe520edbb0c8d303c34fa7cc9588bfe32f8c564931')
data=pro.daily(ts_code='000001.SZ',start_date='20220401',end_date='20220401')
#print(df)
class TushareData(bt.feeds.PandasData):
params = (
('datetime', ),
('open', 1),
('high', 2),
('low', 3),
('close', 4),
('volume', 5),
('openinterest', None)
)
data = TushareData(dataname=data)
class MovingAverageStrategy(bt.Strategy):
params = (('pfast', 30), ('pslow', 100),)
def __init__(self):
sma1 = bt.ind.SMA(period=self.params.pfast)
sma2 = bt.ind.SMA(period=self.params.pslow)
self.crossover = bt.ind.CrossOver(sma1, sma2)
def next(self):
if not self.position:
if self.crossover > 0:
self.buy()
elif self.crossover < 0 :
self.close()
cerebro = bt.Cerebro()
cerebro.addstrategy(MovingAverageStrategy)
data_feed = bt.feeds.PandasData(dataname=data)
cerebro.adddata(data_feed)
cerebro.run()