欢迎光临散文网 会员登陆 & 注册

股票量化软件:赫兹量化利用 CatBoost 算法寻找外汇市场的季节性变动

2023-10-12 15:07 作者:大牛啊呢  | 我要投稿

另外两篇关于季节性模式搜索的文章已经发表(1,2)。我想知道机器学习算法如何处理模式搜索任务。上述文章中的交易系统是在统计分析的基础上构建的。现在,只要简单地指示模型在一周中某一天的某个时间进行交易,就可以消除人为因素。模式搜索可以由单独的算法提供。


时间过滤器函数

通过添加过滤函数,可以很容易地扩展开发库。

def time_filter(data, count):    # filter by hour    hours=[15]    if data.index[count].hour not in hours:        return False    # filter by day of week    days = [1]    if data.index[count].dayofweek not in days:        return False    return True

函数检查其中指定的条件,可以实现其他附加条件(不仅仅是时间过滤器)。但由于本文专门讨论季节性模式,因此我将只使用时间过滤器。如果满足所有条件,函数将返回True,并将适当的样本添加到训练集中。例如,在这种特殊情况下,我们指示模型仅在周二15:00进行交易。“hours”和“days”列表可以包括其他小时和天。通过注释掉所有条件,您可以让算法无条件地工作,就像前一篇文章中的工作方式一样。

add_labels 函数现在接收这个条件作为输入。在Python中,函数是一级对象,因此可以将它们作为参数安全地传递给其他函数。

def add_labels(dataset, min, max, filter=time_filter):    labels = []    for i in range(dataset.shape[0]-max):        rand = random.randint(min, max)        curr_pr = dataset['close'][i]        future_pr = dataset['close'][i + rand]        if filter(dataset, i):            if future_pr + MARKUP < curr_pr:                labels.append(1.0)            elif future_pr - MARKUP > curr_pr:                labels.append(0.0)            else:                labels.append(2.0)        else:            labels.append(2.0)    dataset = dataset.iloc[:len(labels)].copy()    dataset['labels'] = labels    dataset = dataset.dropna()    dataset = dataset.drop(        dataset[dataset.labels == 2].index)    return dataset

一旦过滤器被传递给函数,它就可以用来标记买入或卖出交易。过滤器接收原始数据集和当前柱的索引。数据集中的索引表示为包含时间的“datetime index”。过滤器按第i个数字在数据帧的“datetime index”中搜索小时和天,如果未找到任何内容,则返回 False。如果满足条件,则交易标记为1或0,否则标记为2。最后,从训练数据集中删除所有2,因此只留下由过滤器确定的特定天数和小时的示例。

还应向自定义测试仪添加一个过滤器,以在特定时间启用交易打开(或根据此过滤器设置的任何其他条件)。

def tester(dataset, markup=0.0, plot=False, filter=time_filter):    last_deal = int(2)    last_price = 0.0    report = [0.0]    for i in range(dataset.shape[0]):        pred = dataset['labels'][i]        ind = dataset.index[i].hour        if last_deal == 2 and filter(dataset, i):            last_price = dataset['close'][i]            last_deal = 0 if pred <= 0.5 else 1            continue        if last_deal == 0 and pred > 0.5:            last_deal = 2            report.append(report[-1] - markup +                          (dataset['close'][i] - last_price))            continue        if last_deal == 1 and pred < 0.5:            last_deal = 2            report.append(report[-1] - markup +                          (last_price - dataset['close'][i]))    y = np.array(report).reshape(-1, 1)    X = np.arange(len(report)).reshape(-1, 1)    lr = LinearRegression()    lr.fit(X, y)    l = lr.coef_    if l >= 0:        l = 1    else:        l = -1    if(plot):        plt.plot(report)        plt.plot(lr.predict(X))        plt.title("Strategy performance")        plt.xlabel("the number of trades")        plt.ylabel("cumulative profit in pips")        plt.show()    return lr.score(X, y) * l

具体实现如下:当没有未平仓时使用数字2:last_deal=2。测试开始前没有未平的仓位,因此设置2。遍历整个数据集并检查是否满足筛选条件。如果条件满足,就开始买卖交易。过滤条件不应用于交易结束,因为它们可以在一周的另一个小时或一天结束。这些变化足以进行进一步的正确训练和测试。


股票量化软件:赫兹量化利用 CatBoost 算法寻找外汇市场的季节性变动的评论 (共 条)

分享到微博请遵守国家法律