Exemple #1
0
        self.if_monkey(longest_period=longest_period)
        self.long_or_short()
        self._equity()
        self._win_lose()
        self._drawdown()
        self._return_rate()
        self.long_short_count()
        self._beta()
        print('最大回撤:' + str(max(self.drawdown) * 100) + '%')
        print('买入信号统计:' + str(self.long_count))
        print('卖出信号统计:' + str(self.short_count))
        print('全仓平仓亏损:' + str(self.lost_rate))
        print('全仓平仓赢: ' + str(self.win_rate))
        print('最大回报:' + str((self._df['return'].max() - 1) * 100) + '%')
        print('最大亏损:' + str((self._df['return'].min() - 1) * 100) + '%')
        print('总回报:' + str(self._df[len(self._df) - 1:len(self._df)]['return']))
        return self._df

if __name__=='__main__':
    df = get_his_data('513100',ma=[3,60,120,250])
    print(df)
    #df = get_his_data('162411',ma=[4,5,13,12,20,30,60,120,18,14])
    print(df)
    test = NSDK(df)
    #print(test._df)
    pgraph = test.backtest()
    print('start date: ' + test.start_date)
    test._df.to_csv('test3.csv')
    #test.coef_find()
    pgraph = get_rid_unchanged_equity(pgraph)
    plot_return_beta(pgraph)
        '''
        # 牛市---------->
        if self._df.bull[i] == 1 and \
                (self._df.ma_5_close[i] < self._df.ma_16_close[i] and self._df.ma_5_close[i - 1] > self._df.ma_16_close[
                        i - 1]):
            self._df.loc[i:i + 1, 'short'] = 1
            return True, 1, 1
        # 熊市---------->
        # 猴市---------->
        elif self._df.monkey[i] == 1 and \
                        self._df.ma_12_close[i] < self._df.ma_12_close[i-1] and \
                        self._df.ma_12_close[i - 1] > self._df.ma_12_close[i - 2]:
            self._df.loc[i:i + 1, 'short'] = 1
            return True, 1, 1
        else:
            return False, 1, 1


if __name__ == '__main__':
    df = read_csv_excel('/Users/leotao/Downloads/黄金ETF回测.xlsx', '择时分析-创')
    #df = ts.get_hist_data('160719')
    #print(df)
    df = MA_CALCULATOR(df)
    df = df.get_ma(ll=[5, 12, 13, 16, 18, 20, 30, 60, 120])
    test = GOLD_ETF(df)
    result = test.backtest()
    print('start date: ' + test.start_date)
    pgraph = get_rid_unchanged_equity(result)
    plot_return_beta(pgraph)
    #test.today_signal(120)
Exemple #3
0
        elif self._df.bear[i] ==1 and \
            self._df.ma_5_close[i] < self._df.ma_5_close[i - 1] and \
            self._df.ma_5_close[i - 1] > self._df.ma_5_close[i - 2]:
            self._df.loc[i:i + 1, 'short'] = 1
            return True, 1, 1
        # 猴市---------->
        elif self._df.monkey[i] ==1 and \
            self._df.ma_5_close[i] < self._df.ma_13_close[i] and \
            self._df.ma_5_close[i-1] > self._df.ma_13_close[i-1]:
            self._df.loc[i:i + 1, 'short'] = 1
            return True, 1, 1
        else:
            return False, 1, 1


if __name__ == '__main__':
    df = get_his_data('hs300',
                      start='2014-01-01',
                      ma=[5, 12, 13, 18, 20, 30, 60, 120],
                      index=True)
    #print(df)
    #df = read_csv_excel('/Users/leotao/Downloads/沪深300回测.xlsx', '择时分析-创')
    #df = MA_CALCULATOR(df)
    #df = df.get_ma(ll = [5,12,13,18,20,30,60,120])
    test = hs_300_strategy(df)
    pgraph = test.backtest()
    print('start date: ' + test.start_date)
    #test._df.to_csv('test2.csv')
    #test.coef_find()
    pgraph = get_rid_unchanged_equity(pgraph)
    plot_return_beta(pgraph)
        # 牛市---------->
        if self._df.bull[i] == 1 and \
                (self._df.ma_5_close[i] < self._df.ma_16_close[i] and self._df.ma_5_close[i - 1] > self._df.ma_16_close[
                        i - 1]):
            self._df.loc[i:i + 1, 'short'] = 1
            return True, 1, 1
        # 熊市---------->
        # 猴市---------->
        elif self._df.monkey[i] == 1 and \
                        self._df.ma_12_close[i] < self._df.ma_12_close[i-1] and \
                        self._df.ma_12_close[i - 1] > self._df.ma_12_close[i - 2]:
            self._df.loc[i:i + 1, 'short'] = 1
            return True, 1, 1
        else:
            return False, 1, 1

if __name__=='__main__':
    df = read_csv_excel('/Users/leotao/Downloads/黄金ETF回测.xlsx','择时分析-创')
    #df = ts.get_hist_data('160719')
    #print(df)
    df = MA_CALCULATOR(df)
    df = df.get_ma(ll=[5,12,13,16,18,20,30,60,120])
    test = GOLD_ETF(df)
    result = test.backtest()
    print('start date: ' + test.start_date)
    pgraph = get_rid_unchanged_equity(result)
    plot_return_beta(pgraph)
    #test.today_signal(120)