Esempio n. 1
0
    def run_model(self, n, n_cluster, n_effective, step, threshold):
        data = self.input_data
        data = data.values.reshape(1, -1)
        index = round(len(data[0]) / 3)
        p1 = data[0, 0:index]
        p2 = data[0, index:2 * index]
        p_eval = data[0, 2 * index:]

        data_pro = ProcessData(p1, n, n_cluster, n_effective)
        effective = data_pro.select_effective_clusters()

        test_model = Prediction(effective, p2, n, p_eval)
        p = test_model.predict_delta_p()

        bench = np.random.randn(100, 1)
        hold = np.random.randn(1, 100)

        eval_result = Evaluation(p_eval, max(n), p, step, threshold, bench,
                                 hold, 100, True, 5000, 5000, 4)
        returns = eval_result.periodic_return()[0]
        market = eval_result.periodic_return()[1]
        temp = Calculate_index(returns, market, 0.05, 0.04, 1, 500, 4)
        sharpe = temp.sharpe_ratio()
        return sharpe, eval_result.visual_account(
            threshold)[0], eval_result.visual_account(threshold)[2]
Esempio n. 2
0
    def run_model(self, n, n_cluster, n_effective, step, threshold,
                  is_ultimate):
        """
        :param n: 一组设定时间序列长度的数组
        :param n_cluster: int, 聚类个数
        :param n_effective: 有效聚类个数
        :param step: 步长
        :param threshold: 阈值
        :param is_ultimate: 波尔数,是否运行最终结果
        :return:夏普指数,账户余额
        """
        data = self.input_data
        data = data.values.reshape(1, -1)
        index = round(len(data[0]) / 3)
        p1 = data[0, 0:index]
        p2 = data[0, index:2 * index]
        p_eval = data[0, 2 * index:]

        data_pro = ProcessData(p1, n, n_cluster, n_effective)
        effective = data_pro.select_effective_clusters()

        test_model = Prediction(effective, p2, n, p_eval)
        p = test_model.predict_delta_p()

        bench = np.random.randn(100, 1)
        hold = np.random.randn(1, 100)

        eval_result = Evaluation(p_eval, max(n), p, step, threshold, bench,
                                 hold, 100, True, 5000, 5000, 4)
        drawdown = eval_result.calculate_max_drawdown()
        #returns = eval_result.periodic_return()[0]
        #market = eval_result.periodic_return()[1]
        #temp = Calculate_index(returns, market, 0.05, 0.04, 1, 500, 4)
        # sharpe = temp.sharpe_ratio()

        if is_ultimate:
            rate = eval_result.correct_rate()
            print("Correct rate:", rate)
            eval_result.plot_price_and_profit()

        return drawdown, eval_result.visual_account(
            threshold)[0], eval_result.visual_account(threshold)[2]
Esempio n. 3
0
        best_w4 = total_w4 / 10
        return best_w0, best_w1, best_w2, best_w3, best_w4

    def error(self):
        pre = Prediction(self.s, self.prices, self.n, self.price_3)
        predicted_dp = pre.predict_delta_p()
        print(predicted_dp)
        """
        actual_dp = [0]
        variance = 0
        for i in range(max(self.n), len(self.price_3)-1):
            actual_dp.append(self.price_3[i] - self.price_3[i - 1])
            variance += (predicted_dp[i - max(self.n)] - actual_dp[i - max(self.n)]) ** 2
        error = variance / (2 * len(predicted_dp))
        
        return error
        """

if __name__ == '__main__':
    p1 = pd.read_csv(".//price4.csv")
    p2 = pd.read_csv(".//price5.csv")
    p3 = pd.read_csv(".//price6.csv")
    price_reshaped_1 = p1.values.reshape((1, -1))[0, :]
    price_reshaped_2 = p2.values.reshape((1, -1))[0, :]
    price_reshaped_3 = p3.values.reshape((1, -1))[0, :]
    n = [90, 180, 360, 720]
    process = ProcessData(price_reshaped_1, n, 100, 20)
    s = process.select_effective_clusters()
    temp = Train(s, price_reshaped_2, n, price_reshaped_3)
    result = temp.error()
    print(result)
Esempio n. 4
0
import numpy as np
import matplotlib.pyplot as plt

p1 = pd.read_csv('.//p1.csv')
p2 = pd.read_csv('.//p2.csv')
p3 = pd.read_csv('.//p3.csv')
# def read_data():
#p1 = pd.read_csv('.//price4.csv')
#p2 = pd.read_csv('.//price5.csv')
#p3 = pd.read_csv('.//price6.csv')
n = [90, 180, 360, 720]
price_reshaped_1 = p1.values.reshape((1, -1))[0, :]
price_reshaped_2 = p2.values.reshape((1, -1))[0, :]
price_reshaped_3 = p3.values.reshape((1, -1))[0, :]
data_pro = ProcessData(price_reshaped_1, n, 100, 20)
effective = data_pro.select_effective_clusters()
test_model = Prediction(effective, price_reshaped_2, n, price_reshaped_3)
p = test_model.predict_delta_p()
print(p)
actual_dp = []
for i in range(len(price_reshaped_3) - 1):
    actual_dp.append(price_reshaped_3[i + 1] - price_reshaped_3[i])
# print(actual_dp)
eval = Evaluation(price_reshaped_3, 720, p, 2, 0.07, True, 5000, 5000, 100)
eval.plot_price_and_profit()
eval.correct_rate()
#eval.calculate_max_drawdown()
print(eval.sharpe_ratio())
# delta = eval.visual_account()

returns = eval.periodic_return()[0]