Exemplo n.º 1
0
 def __init__(self):
     self.target_date = datetime.datetime(2016, 3, 1)
     self.forecast_date = datetime.datetime(2017, 5, 1)
     self.feature_date_range = 100
     self.profit_date_range = 3
     self.db = US_Database()
     self.feature_util = Feature_util()
Exemplo n.º 2
0
 def __init__(self):
     self.date_range = 15
     self.lr_range = 70
     self.db = US_Database()
     self.target_date = datetime.datetime(2017, 5, 4)
     self.up_volume = 1.2  #大阳线成交量比平均成交量的倍数
     self.up_pc = 5  #大阳线涨幅
     self.low_volume = 1.2  #大阴线倍数
     self.low_pc = 5  #大阴线涨幅
     self.flat_volume = 0.6  #底部成交量比平均倍数
     self.flat_pc = 2  #底部最好总体涨幅小于2%
Exemplo n.º 3
0
class Test_util(object):
    def __init__(self):
        self.db = US_Database()

    def test_profit_by_date(self, ticker, date):
        target_date_10 = date + datetime.timedelta(days=10)
        profit_10, profit_10_percent = self.db.get_profit_by_days(
            ticker, 9, target_date_10)
        profit_10_max, profit_10_max_percent = self.db.get_max_profit_by_days(
            ticker, 9, target_date_10)

        target_date_6 = date + datetime.timedelta(days=6)
        profit_6, profit_6_percent = self.db.get_profit_by_days(
            ticker, 5, target_date_6)
        profit_6_max, profit_6_max_percent = self.db.get_max_profit_by_days(
            ticker, 5, target_date_6)

        target_date_4 = date + datetime.timedelta(days=4)
        profit_4, profit_4_percent = self.db.get_profit_by_days(
            ticker, 4, target_date_4)
        profit_4_max, profit_4_max_percent = self.db.get_max_profit_by_days(
            ticker, 4, target_date_4)

        std_10 = self.db.get_std_by_days(ticker, 10, date)

        return {
            'ticker': ticker,
            'std_10': std_10,
            'profit_10': profit_10_percent,
            'profit_10_max': profit_10_max_percent,
            'profit_6': profit_6_percent,
            'profit_6_max': profit_6_max_percent,
            'profit_4': profit_4_percent,
            'profit_4_max': profit_4_max_percent,
        }
Exemplo n.º 4
0
class Deep_point_strategy(Strategy):
    def __init__(self):
        self.date_range = 15
        self.lr_range = 70
        self.db = US_Database()
        self.target_date = datetime.datetime(2017, 5, 4)
        self.up_volume = 1.2  #大阳线成交量比平均成交量的倍数
        self.up_pc = 5  #大阳线涨幅
        self.low_volume = 1.2  #大阴线倍数
        self.low_pc = 5  #大阴线涨幅
        self.flat_volume = 0.6  #底部成交量比平均倍数
        self.flat_pc = 2  #底部最好总体涨幅小于2%

    def deal_data(self, symbol):
        #find the biggest drop around 10 days
        start_date = self.target_date - datetime.timedelta(self.date_range)
        ticker_data = self.db.get_ticker_by_id_not_consecutive_date(
            symbol, start_date=start_date,
            end_date=self.target_date).reset_index()

        if ticker_data.empty:
            return

        ticker_data['delta'] = ticker_data['close'] - ticker_data['open']
        ticker_data['delta_pc'] = (ticker_data['close'] - ticker_data['open']
                                   ) * 100 / ticker_data['open']

        mean_volume = ticker_data['volume'].mean()
        mean_price = ticker_data.close.mean()

        # #then we need to confirm the trend of drop
        # pre_start_date = self.target_date
        # pre_target_date = pre_start_date - datetime.timedelta(days=self.lr_range)
        # pre_ticker_data = self.db.get_ticker_by_id_not_consecutive_date(symbol,start_date=pre_target_date,end_date=pre_start_date).reset_index()
        # X_train = np.array(pre_ticker_data.index)
        # y_train = np.array(pre_ticker_data.close)
        # xx = np.linspace(0,pre_ticker_data.index[-1],100)

        # quadratic_featurizer = PolynomialFeatures(degree=2)
        # X_train_quadratic = quadratic_featurizer.fit_transform(X_train.reshape(X_train.shape[0],1))
        # regressor_quadratic = LinearRegression()
        # regressor_quadratic.fit(X_train_quadratic, y_train)

        # xx_quadratic = quadratic_featurizer.transform(xx.reshape(xx.shape[0], 1))
        # yy_quadratic = regressor_quadratic.predict(xx_quadratic)

        # # y = ax2 + bx + c
        # coef_ = regressor_quadratic.coef_
        # a = coef_[-1]
        # b = coef_[1]
        # # 我们想让导数为零的点在今天附近或者之后,且二次函数a为正
        # # 导数为零点
        # d_p = (b * -1) / (2 * a)
        # d_range = len(X_train)
        # if a > 0 and (d_p > d_range - 20 and d_p < d_range + 10):
        #     print (symbol, d_p, d_range)
        #     df_orgin = pd.DataFrame({'x1': X_train, 'y1': y_train})
        #     df_predict_quadratic = pd.DataFrame({'x': xx, 'y': yy_quadratic})
        #     # sns.jointplot('x1','y1',df_orgin[['x1','y1']],kind = 'scatter',color='red')
        #     # sns.jointplot('x','y',df_predict_quadratic[['x','y']],kind = 'scatter',color='blue')
        #     # sns.plt.show()

        #判断有大跌
        lowest_df = ticker_data[ticker_data['delta_pc'] < (self.low_pc * -1)]

        if lowest_df.empty:
            return

        lowest_index = lowest_df.index[-1]
        lowest_volume = ticker_data.volume[lowest_index]

        if lowest_index <= 8 and (lowest_volume >
                                  (self.low_volume * mean_volume)):
            #volume is so few and the change of price after deep is slience
            after_point_df = ticker_data.iloc[lowest_index:].reset_index(
            ).drop('index', axis=1)

            #find the biggest grow in after series
            highest_df = after_point_df[
                after_point_df['delta_pc'] > self.up_pc]

            if highest_df.empty:
                return

            highest_index = highest_df.index[0]
            highest_volume = after_point_df.volume[highest_index]

            if highest_index > 3 and highest_volume > (self.up_volume *
                                                       mean_volume):
                #判断中间涨幅平稳切 成交量萎缩

                middle_range_df = after_point_df.iloc[1:highest_index]

                middle_volume_average = middle_range_df.volume.mean()
                middle_price_change_pct = (
                    middle_range_df.close[-1] -
                    middle_range_df.open[0]) / middle_range_df.open[0]

                if middle_volume_average < mean_volume * self.flat_volume:
                    if middle_price_change_pct < self.flat_pc:
                        print(symbol)
                        print(ticker_data)
                        print(after_point_df.iloc[:highest_index + 1])
                        print('=========')
Exemplo n.º 5
0
            if highest_index > 3 and highest_volume > (self.up_volume *
                                                       mean_volume):
                #判断中间涨幅平稳切 成交量萎缩

                middle_range_df = after_point_df.iloc[1:highest_index]

                middle_volume_average = middle_range_df.volume.mean()
                middle_price_change_pct = (
                    middle_range_df.close[-1] -
                    middle_range_df.open[0]) / middle_range_df.open[0]

                if middle_volume_average < mean_volume * self.flat_volume:
                    if middle_price_change_pct < self.flat_pc:
                        print(symbol)
                        print(ticker_data)
                        print(after_point_df.iloc[:highest_index + 1])
                        print('=========')


if __name__ == '__main__':
    db = US_Database()
    symbols = db.get_33_66_volume_by_day_symbol(20)
    dp = Deep_point_strategy()
    #loop
    # symbols.map(lambda x: dp.deal_data(x))
    for index in range(len(symbols)):
        # if index > 100 :
        #     break
        symbol = symbols[index]
        dp.deal_data(symbol)
Exemplo n.º 6
0
class Unicon_strategy(Strategy):
    def __init__(self):
        self.target_date = datetime.datetime(2016, 3, 1)
        self.forecast_date = datetime.datetime(2017, 5, 1)
        self.feature_date_range = 100
        self.profit_date_range = 3
        self.db = US_Database()
        self.feature_util = Feature_util()

    def pre_deal_data(self):
        symbols = self.db.get_symbol_from_db()

        data_X = []
        data_Y = []

        for index in range(len(symbols)):
            ticker = symbols[index]
            print(ticker, '==============================================')

            start_date = self.target_date - datetime.timedelta(
                days=(self.feature_date_range))
            end_date = self.target_date
            ticker_data = self.db.get_ticker_by_id(ticker, start_date,
                                                   end_date)

            if ticker_data.empty:
                continue
            profit, profit_percent = self.db.get_profit_by_days(
                ticker, self.profit_date_range, self.target_date +
                datetime.timedelta(days=self.profit_date_range))

            # if profit > 0:
            #     data_Y.append(1)
            # else:
            #     data_Y.append(-1)

            data_Y.append(float(profit))
            data_X.append(np.array(ticker_data['close']))

            # if(index > 100):
            #     break

        data_X = np.array(data_X)
        data_Y = np.array(data_Y)

        return (data_X, data_Y)

    def get_r2(self, X, y):
        #normalize
        X = self.feature_util.normalize(X)
        y = self.feature_util.normalize(y)

        print(X, y)

        train_x, test_x = cross_validation.train_test_split(X,
                                                            test_size=0.3,
                                                            random_state=0)
        train_y, test_y = cross_validation.train_test_split(y,
                                                            test_size=0.3,
                                                            random_state=0)

        models = [
            ('LR', LinearRegression()),
            ('RidgeR', Ridge(alpha=0.005)),
            ('lasso', Lasso(alpha=0.00001)),
            ('LassoLars', LassoLars(alpha=0.00001)),
            ('RandomForestRegression', RandomForestRegressor(2000)),
        ]

        best_r2 = (0, 0, None)

        print(train_x, train_y, train_x.shape, train_y.shape)

        for m in models:
            m[1].fit(train_x, train_y)
            pred_y = m[1].predict(test_x)
            r2 = r2_score(pred_y, test_y)
            print(m[0], r2, '=============')
            if r2 > best_r2[1]:
                best_r2 = (m[0], r2, m[1])

        print('the best regression is:', best_r2)

        return best_r2[2]

    def forecast(self, model):
        if model == None:
            print('model is None')

        self.forecast_date = datetime.datetime(2017, 5, 1)
        symbols = self.db.get_symbol_from_db()

        #build a empty list to be a content
        close_list = []
        profit_list = []

        for ticker in symbols:
            start_date = self.forecast_date - datetime.timedelta(
                days=(self.feature_date_range))
            ticker_data = self.db.get_ticker_by_id(ticker, start_date,
                                                   self.forecast_date)

            if ticker_data.empty:
                continue

            close = ticker_data['close']
            close_list.append(close)

            profit, profit_percent = self.db.get_profit_by_days(
                ticker, self.profit_date_range, self.forecast_date +
                datetime.timedelta(days=self.profit_date_range))
            # if profit > 0:
            #     profit_list.append(1)
            # else:
            #     profit_list.append(-1)
            print(profit_percent, '~~~~')
            profit_list.append(profit_percent)

        close_np = np.array(close_list)
        profit_np = np.array(profit_list)
        profit_np = self.feature_util.normalize(profit_np)

        #normalize
        close_np = self.feature_util.normalize(close_np)
        predict_np = model.predict(np.array(close_list))

        print('r2 is: ', r2_score(predict_np, profit_np))

        df = pd.DataFrame({'predict': predict_np, 'profit': profit_np})

        return df
Exemplo n.º 7
0
 def __init__(self):
     self.db = US_Database()
Exemplo n.º 8
0
			mini = min(Deviation)
			RS.append(maxi - mini)
			sigma = np.std(Range)

		RS = np.array(RS)
		ARS[r] = np.mean(RS[~np.isnan(RS)])

	lag = np.log10(lag)
	ARS = np.log10(ARS)
	hurst_exponent = np.polyfit(lag,ARS,1)
	hurst = hurst_exponent[0] * 2

	return hurst

if __name__ == '__main__':
	db = US_Database()
	symbols = db.get_symbol_from_db()
	# get_adf(symbols,db,datetime.datetime(2017,5,1))
	hurst_list = []
	for i in range(len(symbols)):
		symbol = symbols[i]
		hurst = get_hurst(symbol,db)
		print(i,hurst)
		hurst_list.append({
			'ticker': symbol,
			'hurst': hurst
			})


	df = pd.DataFrame(hurst_list).dropna()