In [6]:
# 首先将数据存入Mongodb, 请确保已打开mongod服务. 只需运行一次
from OnePy.builtin_module.mongodb_saver.tushare_saver import multi_tushare_to_mongodb
from OnePy.builtin_module.mongodb_saver.utils import MongoDBFunc


FREQUENCY = ["D","H1"]
START = '2010-01-01'
TICKER_LIST = ['000001','000002'] # 多线程导入数据

multi_tushare_to_mongodb(ticker_list=TICKER_LIST, 
                         period_list=FREQUENCY,
                         fromdate=START)

MongoDBFunc().drop_duplicates(TICKER_LIST, FREQUENCY, 'tushare')


Start!
<<000002, H1>> has completed, Total: 482
<<000001, H1>> has completed, Total: 482
<<000002, D>> has completed, Total: 1995
<<000001, D>> has completed, Total: 2080
['000001', '000002'],H1 all set!!!!!
<<000001, H1>> has been drop 482 duplicates!
<<000002, H1>> has been drop 482 duplicates!
<<000002, D>> has been drop 1995 duplicates!
<<000001, D>> has been drop 2080 duplicates!

In [1]:
%matplotlib inline
%config InlineBackend.figure_format = 'svg'

In [2]:
import OnePy as op
from OnePy.custom_module.cleaner_sma import SMA

class SmaStrategy(op.StrategyBase):

    def __init__(self):

        super().__init__()
        self.sma1 = SMA(3, 40).calculate
        self.sma2 = SMA(5, 40).calculate

    def handle_bar(self):
        for ticker in self.env.tickers:

            if self.sma1(ticker) > self.sma2(ticker):

                self.buy(100, ticker, takeprofit=15,
                         stoploss=100)
            else:
                self.sell(100, ticker)


TICKER_LIST = ['000001', '000002'] # 多品种
INITIAL_CASH = 20000
FREQUENCY = 'D'
START, END = '2012-08-07', '2018-08-07'

# 实例化策略,会自动添加到env.strategies中
SmaStrategy()


## 如果用CSV回测,注意csv文件名为name_frequency,同时要将下面MongoDB回测注释掉
# op.data_readers.CSVReader('../data/', file_name='000001', ticker='000001')
# go = op.backtest.stock([], FREQUENCY, INITIAL_CASH, START, END) # 设置好股票回测的基本配置


# 用MongoDB数据库回测
go = op.backtest.stock(TICKER_LIST, FREQUENCY, INITIAL_CASH, START, END)


# go.show_log(file=False) # 是否实时打印log
# go.save_original_signal() # 是否保存原始signal信息,便于盘后自定义分析

In [3]:
go.sunny(show_process=True)


正在初始化OnePy
=============== OnePy初始化成功! ===============
开始寻找OnePiece之旅~~~
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>[100%]

+--------------------------------+
|Fromdate           |  2012-08-07|
|Todate             |  2018-08-07|
|Initial_Value      |   $20000.00|
|Final_Value        |   $15261.53|
|Total_Return       |    -23.692%|
|Max_Drawdown       |     23.837%|
|Max_Duration       |   1434 days|
|Max_Drawdown_Date  |  2018-07-20|
|Sharpe_Ratio       |       -1.29|
+--------------------------------+

In [4]:
go.output.plot('000001')


/Users/chandler/anaconda/lib/python3.6/site-packages/matplotlib/figure.py:2362: UserWarning:

This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.