Exemplo n.º 1
0
 def _RSRS(self, code, dateStart, dateEnd, label=""):
     N = 18
     M = 600
     rsrsDara = getdata(code, dateStart, dateEnd, N, M)
     rsrsDara = rsrsDara.loc[2:]
     rsrsDara = rsrsDara.reset_index(drop=True)
     rsrsDara.head()
     # 斜率指标策略
     result = RSRS1(rsrsDara)
     num = result.flag.abs().sum() / 2
     nav = result.nav[result.shape[0] - 1]
     print('交易次数 = ', num)
     print('策略净值为= ', nav)
     # 标准分策略
     result2 = RSRS2(rsrsDara)
     num = result2.flag.abs().sum() / 2
     nav = result2.nav[result.shape[0] - 1]
     ret_year = (nav - 1)
     print('交易次数 = ', num)
     print('策略净值为= ', nav)
     # 修正标准分策略
     result3 = RSRS3(rsrsDara)
     num = result3.flag.abs().sum() / 2
     nav = result3.nav[result.shape[0] - 1]
     ret_year = (nav - 1)
     print('交易次数 = ', num)
     print('策略净值为= ', nav)
     N = 16
     M = 300
     rsrsDara = getdata(code, dateStart, dateEnd, N, M)
     rsrsDara = rsrsDara.loc[2:]
     rsrsDara = rsrsDara.reset_index(drop=True)
     rsrsDara.head()
     result4 = RSRS4(rsrsDara)
     num = result4.flag.abs().sum() / 2
     nav = result4.nav[result.shape[0] - 1]
     ret_year = (nav - 1)
     print('交易次数 = ', num)
     print('策略净值为= ', nav)
     xtick = np.arange(0, result.shape[0], int(result.shape[0] / 7))
     xticklabel = pd.Series(result.date[xtick])
     plt.figure(figsize=(25, 8))
     fig = plt.axes()
     plt.plot(np.arange(result.shape[0]), result.nav, label='RSRS1', linewidth=2)
     plt.plot(np.arange(result.shape[0]), result2.nav, label='RSRS2', linewidth=2)
     plt.plot(np.arange(result.shape[0]), result3.nav, label='RSRS3', linewidth=2)
     plt.plot(np.arange(result.shape[0]), result4.nav, label='RSRS4', linewidth=1)
     plt.plot(np.arange(result.shape[0]), result.close / result.close[0], color='yellow', label=label, linewidth=1.5)
     fig.set_xticks(xtick)
     fig.set_xticklabels(xticklabel, rotation=25)
     plt.legend()
     plt.show()
Exemplo n.º 2
0
 def setUpClass(cls) -> None:
     cls.code = '000300'
     dateStart = datetime.date(2005, 3, 1)
     dateEnd = datetime.date(2017, 3, 31)
     N = 18
     M = 600
     cls.hs300 = getdata(cls.code, dateStart, dateEnd, N, M)
Exemplo n.º 3
0
    def test_RSRS1_RSRS4(self):
        code = self.code
        data = self.hs300
        result = RSRS1(data)
        num = result.flag.abs().sum() / 2
        nav = result.nav[result.shape[0] - 1]

        print('交易次数 = ', num)
        print('策略净值为= ', nav)

        xtick = np.arange(0, result.shape[0], int(result.shape[0] / 7))
        xticklabel = pd.Series(result.date[xtick])
        plt.figure(figsize=(15, 4))
        fig = plt.axes()
        plt.plot(np.arange(result.shape[0]), result.nav, label='RSRS1', linewidth=2, color='red')
        plt.plot(np.arange(result.shape[0]), result.close / result.close[0], color='yellow', label='HS300', linewidth=2)

        fig.set_xticks(xtick)
        fig.set_xticklabels(xticklabel, rotation=45)
        plt.legend()
        plt.show()

        result2 = RSRS2(self.hs300)
        num = result2.flag.abs().sum() / 2
        nav = result2.nav[result.shape[0] - 1]
        ret_year = (nav - 1)
        print('交易次数 = ', num)
        print('策略净值为= ', nav)

        result3 = RSRS3(data)
        num = result3.flag.abs().sum() / 2
        nav = result3.nav[result.shape[0] - 1]
        ret_year = (nav - 1)
        print('交易次数 = ', num)
        print('策略净值为= ', nav)

        dateStart = datetime.date(2005, 3, 1)
        dateEnd = datetime.date(2017, 3, 31)
        N = 16
        M = 300
        HS300 = getdata(code, dateStart, dateEnd, N, M)
        HS300 = HS300.loc[2:]
        HS300 = HS300.reset_index(drop=True)

        result4 = RSRS4(self.hs300)
        num = result4.flag.abs().sum() / 2
        nav = result4.nav[result.shape[0] - 1]
        ret_year = (nav - 1)
        print('交易次数 = ', num)
        print('策略净值为= ', nav)

        xtick = np.arange(0, result.shape[0], int(result.shape[0] / 7))
        xticklabel = pd.Series(result.date[xtick])
        plt.figure(figsize=(15, 3))
        fig = plt.axes()
        plt.plot(np.arange(result.shape[0]), result.nav, label='RSRS1', linewidth=2)
        plt.plot(np.arange(result.shape[0]), result2.nav, label='RSRS2', linewidth=2)
        plt.plot(np.arange(result.shape[0]), result3.nav, label='RSRS3', linewidth=2)
        plt.plot(np.arange(result.shape[0]), result4.nav, label='RSRS4', linewidth=2)
        plt.plot(np.arange(result.shape[0]), result.close / result.close[0], color='yellow', label='HS300', linewidth=2)

        fig.set_xticks(xtick)
        fig.set_xticklabels(xticklabel, rotation=45)
        plt.legend()
        plt.show()