In [1]:
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']
INITIAL_CASH = 20000
FREQUENCY = 'D'
START, END = '2012-08-07', '2018-08-07'

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

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

# go.sunny() # 注意,fordward analysis之前不能运行go.sunny(), 不然会报错

In [2]:
go.forward_analysis.run(fromdate=START,
                        length_month=3,
                        rolling_month=2,
                        times=2,
                        show_summary=True,
                        warning=False)


Begin Forward Analysis!
+----------------------------------------+ 
Fromdate: 2012-08-07, Todate: 2013-01-07
Timescale: 3 Months.
Rollingscale: 2 Months.
Total roll 2 times.
+----------------------------------------+
CRITICAL:OnePy:正在初始化OnePy
CRITICAL:OnePy:正在初始化OnePy
CRITICAL:OnePy:=============== OnePy初始化成功! ===============
CRITICAL:OnePy:开始寻找OnePiece之旅~~~
CRITICAL:OnePy:=============== OnePy初始化成功! ===============
CRITICAL:OnePy:开始寻找OnePiece之旅~~~



+--------------------------------+
|Fromdate           |  2012-08-07|
|Todate             |  2012-11-07|
|Initial_Value      |   $20000.00|
|Final_Value        |   $19900.69|
|Total_Return       |     -0.497%|
|Max_Drawdown       |      0.732%|
|Max_Duration       |     66 days|
|Max_Drawdown_Date  |  2012-10-29|
|Sharpe_Ratio       |       -1.85|
+--------------------------------+
当前是第 1 次, 剩余 0.00 mins
+--------------------------------+
|Fromdate           |  2012-10-07|
|Todate             |  2013-01-07|
|Initial_Value      |   $20000.00|
|Final_Value        |   $19957.34|
|Total_Return       |     -0.213%|
|Max_Drawdown       |      0.836%|
|Max_Duration       |     54 days|
|Max_Drawdown_Date  |  2012-11-30|
|Sharpe_Ratio       |       -0.65|
+--------------------------------+
当前是第 2 次, 剩余 0.00 mins
Done!

In [ ]: