深度学习机器人交易

There are so many articles on predicting stock prices, but this article provides two things to the reader, that no other article talks about:

关于预测股价的文章太多,但是本文向读者提供了两件事,而没有其他文章在谈论:

  • The use of confidence intervals in stock trading to determine stop-loss and take-profit在股票交易中使用置信区间确定止损和获利
  • The use of alpaca in stock trading to track profits and test trading strategies羊驼在股票交易中的使用以跟踪利润并测试交易策略

Both of which provide important tools to the next generation of machine learning trading algorithms.

两者都为下一代机器学习交易算法提供了重要的工具。

概念: (Concept:)

The program will consist of three main parts:

该计划将包括三个主要部分:

  • The Data Setup数据设置

The data will be accessed via the yfinance library, in daily intervals, The data will include the opening,high,low and closing price of the asset. The data will be normalized and then reshaped to fit the neural network

该数据将每天通过yfinance库进行访问,该数据将包括资产的开仓价,最高价,最低价和收盘价。 数据将被规范化,然后重塑以适合神经网络

  • The Neural Network神经网络

The neural network will be a convolutional LSTM network that can extract the feature and also access temporal features of the dataset. This network fits the data because some of the complex patterns are not only convolutional, they are also time based.

神经网络将是一个卷积LSTM网络,可以提取特征并访问数据集的时间特征。 该网络适合数据,因为某些复杂模式不仅是卷积的,而且它们都是基于时间的。

  • Creating Orders创建订单

The Neural Network will predict the daily opening and closing prices. If the opening prices is larger than the closing price, the network will short sell the stock. If the closing price is larger than the opening price, the network will buy the stock.

神经网络将预测每日开盘和收盘价。 如果开盘价大于收盘价,则网络将卖空股票。 如果收盘价大于开盘价,则网络将购买股票。

After training the network, I will compute the loss of the network and use this value as a a confidence interval, to determine the stop loss and take profit values. I will use requests to access the Alpaca API to make orders.

训练完网络后,我将计算网络的损耗并将此值用作一个置信区间,以确定止损并获利。 我将使用请求访问Alpaca API进行订单。

With the key concept in place let’s move to the code.

有了关键概念,让我们转到代码。

代码: (The Code:)

步骤1 | 先决条件: (Step 1| Prerequisites:)

from numpy import arrayfrom numpy import hstackfrom keras.models import Sequentialfrom keras.layers import LSTMfrom keras.layers import Densefrom keras import callbacksfrom sklearn.model_selection import train_test_splitfrom keras.layers import Flattenfrom keras.layers import TimeDistributedfrom keras.layers.convolutional import Conv1Dfrom keras.layers.convolutional import MaxPooling1Dfrom IPython.display import clear_outputimport datetimeimport statisticsimport time import osimport jsonimport yfinance as yffrom keras.models import model_from_jsonimport requestsfrom keras.models import load_modelfrom matplotlib import pyplot as plt

There are quite a lot of prerequisties of the program.They are spread out as so, to prevent importing the whole library and taking up space. Please note that importing clear_output from IPython.display is only for Jupyter notebooks. If you are using scripts it is not necessary to import this.

该程序有很多先决条件,它们因此分散开来,以防止导入整个库并占用空间。 请注意,从IPython.display导入clear_output仅适用于Jupyter笔记本。 如果使用脚本,则无需导入。

步骤2 | 访问数据: (Step 2| Access Data:)

def data_setup(symbol,data_len,seq_len):    end = datetime.datetime.today().strftime('%Y-%m-%d')    start = datetime.datetime.strptime(end, '%Y-%m-%d') - datetime.timedelta(days=(data_len/0.463))    orig_dataset = yf.download(symbol,start,end)    close = orig_dataset['Close'].values    open_ = orig_dataset['Open'].values    high = orig_dataset['High'].values    low = orig_dataset['Low'].values    dataset,minmax = normalize_data(orig_dataset)    cols = dataset.columns.tolist()    data_seq = list()    for i in range(len(cols)):        if cols[i] < 4:            data_seq.append(dataset[cols[i]].values)            data_seq[i] = data_seq[i].reshape((len(data_seq[i]), 1))    data = hstack(data_seq)    n_steps = seq_len    X, y = split_sequences(data, n_steps)    n_features = X.shape[2]    n_seq = len(X)    n_steps = seq_len    print(X.shape)    X = X.reshape((n_seq,1, n_steps, n_features))    true_y = []    for i in range(len(y)):        true_y.append([y[i][0],y[i][1]])    return X,array(true_y),n_features,minmax,n_steps,close,open_,high,low

This function takes data from yfinance and splits it into its respective sections. It also reshapes data into the form:

此功能从yfinance提取数据并将其拆分为各个部分。 它还将数据重塑为以下形式:

(n_seq,1, n_steps, n_features)

A four-dimensional array to fit the Convolutional LSTM network.

适应卷积LSTM网络的四维数组。

步骤3 | 准备数据: (Step 3| Prepare Data:)

Accessing the data is only half of the challenge. The rest is putting the data into the correct format, and splitting the data into training and testing datasets.

访问数据仅是挑战的一半。 剩下的就是将数据放入正确的格式,并将数据拆分为训练和测试数据集。

def split_sequences(sequences, n_steps):        X, y = list(), list()        for i in range(len(sequences)):            end_ix = i + n_steps            if end_ix > len(sequences)-1:                break            seq_x, seq_y = sequences[i:end_ix, :], sequences[end_ix, :]            X.append(seq_x)            y.append(seq_y)        return array(X), array(y)

This function splits the sequence into time series data, by splitting the sequence into chunks of size n_steps.

此函数通过将序列拆分为大小为n_steps的块,将序列拆分为时间序列数据。

def normalize_data(dataset):        cols = dataset.columns.tolist()        col_name = [0]*len(cols)        for i in range(len(cols)):            col_name[i] = i        dataset.columns = col_name        dtypes = dataset.dtypes.tolist()#         orig_answers = dataset[attr_row_predict].values        minmax = list()        for column in dataset:            dataset = dataset.astype({column: 'float32'})        for i in range(len(cols)):            col_values = dataset[col_name[i]]            value_min = min(col_values)            value_max = max(col_values)            minmax.append([value_min, value_max])        for column in dataset:            values = dataset[column].values            for i in range(len(values)):                values[i] = (values[i] - minmax[column][0]) / (minmax[column][1] - minmax[column][0])            dataset[column] = values        dataset[column] = values        return dataset,minmax

This function changes all data into a value between 0 and 1. This is as many stocks have skyrocketed or nosedived. Without normalizing, the neural network would learn from datapoints with higher values. This could create a blind spot and therefore affect predictions. The normalizing is done as so:

此功能会将所有数据更改为0到1之间的值。这是因为许多股票暴涨或暴跌。 如果不进行标准化,则神经网络将从具有更高值的数据点学习。 这可能会造成盲点,从而影响预测。 规范化是这样完成的:

value = (value - minimum) / maximum

Where minimum and maximum are the minimum and maximum values of the feature.

其中最小值和最大值是特征的最小值和最大值。

def enviroment_setup(X,y):        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)        return X_train, X_test, y_train, y_test

This function uses sklearn’s train_test_split function to shuffle the data and divide into training and testing datasets.

此函数使用sklearn的train_test_split函数对数据进行混洗,并分为训练和测试数据集。

步骤4 | 创建神经网络: (Step 4| Create Neural Network:)

def initialize_network(n_steps,n_features,optimizer):    model = Sequential()    model.add(TimeDistributed(Conv1D(filters=64, kernel_size=1, activation='relu'), input_shape=(None, n_steps, n_features)))    model.add(TimeDistributed(MaxPooling1D(pool_size=2)))    model.add(TimeDistributed(Flatten()))    model.add(LSTM(50, activation='relu'))    model.add(Dense(2))    model.compile(optimizer=optimizer, loss='mse')    return model

This is the basic architecture of the Convolutional LSTM network. The optimizer that I found that works best with this network is Adam.

这是卷积LSTM网络的基本架构。 我发现最适合此网络的优化器是Adam。

步骤5 | 训练神经网络: (Step 5| Train Neural Network:)

def train_model(X_train,y_train,model,epochs):    dirx = 'something directory'    os.chdir(dirx)    h5='Stocks'+'_best_model'+'.h5'    checkpoint = callbacks.ModelCheckpoint(h5, monitor='val_loss', verbose=0, save_best_only=True, save_weights_only=True, mode='auto', period=1)    earlystop = callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=epochs * 1/4, verbose=0, mode='auto', baseline=None, restore_best_weights=True)    callback = [earlystop,checkpoint]     json = 'Stocks'+'_best_model'+'.json'    model_json = model.to_json()    with open(json, "w") as json_file:        json_file.write(model_json)    history = model.fit(X_train, y_train, epochs=epochs, batch_size=len(X_train)//4, verbose=2,validation_split = 0.3, callbacks = callback)    return history

For the training function, I used the criminally underused Model Checkpoint callback to save the best weights of the model. Change the dirx variable to where you want to store your model.

对于训练功能,我使用了未被充分利用的Model Checkpoint回调,以节省模型的最佳权重。 将dirx变量更改为要存储模型的位置。

步骤6 | 评估与预测: (Step 6| Evaluation and Prediction:)

def load_keras_model(dataset,model,loss,optimizer):    dirx = 'something directory'    os.chdir(dirx)    json_file = open(dataset+'_best_model'+'.json', 'r')    loaded_model_json = json_file.read()    json_file.close()    model = model_from_json(loaded_model_json)    model.compile(optimizer=optimizer, loss=loss, metrics = None)    model.load_weights(dataset+'_best_model'+'.h5')    return modeldef evaluation(exe_time,X_test, y_test,X_train, y_train,history,model,optimizer,loss):    model = load_keras_model('Stocks',model,loss,optimizer)    test_loss = model.evaluate(X_test, y_test, verbose=0)    train_loss = model.evaluate(X_train, y_train, verbose=0)    eval_test_loss = round(100-(test_loss*100),1)    eval_train_loss = round(100-(train_loss*100),1)    eval_average_loss = round((eval_test_loss + eval_train_loss)/2,1)    print("--- Training Report ---")    plot_loss(history)    print('Execution time: ',round(exe_time,2),'s')    print('Testing Accuracy:',eval_test_loss,'%')    print('Training Accuracy:',eval_train_loss,'%')    print('Average Network Accuracy:',eval_average_loss,'%')    return model,eval_test_loss

After saving the best weights, load the model again to ensure that you are using the best weights. The program then evaluates the program, based on data that it has not seen before. It then prints a set of variables to give comprehensive insight on the training of the network.

保存最佳权重后,再次加载模型以确保您使用的是最佳权重。 然后,程序将根据之前从未见过的数据对程序进行评估。 然后,它会打印一组变量,以全面了解网络的培训。

def market_predict(model,minmax,seq_len,n_features,n_steps,data,test_loss):    pred_data = data[-1].reshape((len(data[-1]),1, n_steps, n_features))    pred = model.predict(pred_data)[0]    appro_loss = list()    for i in range(len(pred)):        pred[i] = pred[i] * (minmax[i][1] - minmax[i][0]) + minmax[i][0]        appro_loss.append(((100-test_loss)/100) * (minmax[i][1] - minmax[i][0]))    return pred,appro_loss

This is the function that makes the prediction of the program. We have to perform the inverse of the normalization function to get the value in terms of USD.

这是预测程序的功能。 我们必须执行标准化函数的反函数才能获得以美元为单位的价值。

步骤7 | 创建订单: (Step 7| Create Order:)

BASE_URL = 'https://paper-api.alpaca.markets'API_KEY = 'XXXXXXXX'SECRET_KEY = 'XXXXXXXX'ORDERS_URL = '{}/v2/orders'.format(BASE_URL)HEADERS = {'APCA-API-KEY-ID':API_KEY,'APCA-API-SECRET-KEY':SECRET_KEY}

These are the basic parameters and endpoints to make alpaca orders. You can get your own API key and secret key here.

这些是制作羊驼毛定单的基本参数和端点。 您可以在此处获得自己的API密钥和秘密密钥。

def create_order(pred_price,company,test_loss,appro_loss):    open_price,close_price = pred_price[0],pred_price[1]    if open_price > close_price:        side = 'sell'    elif open_price < close_price:        side = 'buy'    if side == 'buy':        order = {            'symbol':company,            'qty':round(20*(test_loss/100)),            'type':'stop_limit',            'time_in_force':'day',            'side': 'buy',            'take_profit': close_price + appro_loss,            'stop_loss': close_price - appro_loss                }    elif side == 'sell':        order = {            'symbol':company,            'qty':round(20*(test_loss/100)),            'type':'stop_limit',            'time_in_force':'day',            'side': 'sell',            'take_profit':close_price - appro_loss,            'stop_loss':close_price + appro_loss                }    r = requests.post(ORDERS_URL, json = order,headers = HEADERS)    print(r.content)

This function applies the take profit and stop loss idea:

此功能应用了止盈和止损的想法:

Take profit and prevent loss as the close price fluctuates along the predicted closing price.

当收盘价沿着预测收盘价波动时,请获利并防止亏损。

The length between the borders of the red area and the center is the loss value. The borders act as the stop loss and take profit value, as these are the value that the program predicts the price will fluctuate within.

红色区域和中心的边界之间的长度是损失值。 边界充当止损并获取利润值,因为这些值是程序预测价格将在其中波动的值。

结论: (Conclusion:)

Machine Learning and Stock Trading come hand in hand, as both are the prediction of complex patterns.

机器学习和股票交易齐头并进,因为两者都是对复杂模式的预测。

I hope that more people will use the Alpaca API and confidence intervals when it comes to algorithmic trading.

我希望有更多的人在算法交易中使用Alpaca API和置信区间。

Thank you for reading my article!

感谢您阅读我的文章!

翻译自: https://medium.com/analytics-vidhya/using-deep-learning-to-create-a-stock-trading-bot-a96e6351d31c

深度学习机器人交易


http://www.taodudu.cc/news/show-4138468.html

相关文章:

  • 深度学习用于股票预测_用于自动股票交易的深度强化学习
  • python神经网络预测股票组合_神经网络预测股票市场
  • 【Python量化】使用机器学习预测股票交易信号
  • 股票市场交易中的强化学习
  • CKEditor编辑器的详细使用
  • CSDN 「Markdown」编辑器的优点、不足、使用技巧和新增功能|CSDN编辑器测评
  • java常用的编辑器之kindeditor
  • SpringBoot接入Ueditor编辑器
  • html在线编辑器源代码,最完整的html在线编辑器 - WEB源码|其它源码|源代码 - 源码中国...
  • 产品周报第26期|富文本编辑器新增预览功能;博客首页增加上次阅读频道记录……
  • xheditor编辑器的使用
  • ueditor 编辑器的使用(编辑)
  • thinkphp使用编辑器kindeditor
  • vscode连接安卓模拟器上autojs
  • Mac下用android studio创建安卓模拟器
  • 2021 年 15 款适用于 PC 和 Mac 的最佳 Android 模拟器
  • android模拟器转方向,如何旋转Android模拟器显示?
  • Mac M2芯片Arm64安卓模拟器7.0安装xposed
  • Mac/Windows下如何使用安卓模拟器开发UniApp
  • RxSwift 学习:基础使用篇 - 序列 订阅 销毁
  • RxSwift取消定时
  • ①、iOS-RxSwift基础控件的使用、RxSwift-Tableview的使用、RxSwift-SectionTableview结合RxDataSources使用、RxSwift 网络请求封装使用
  • RxSwift学习记录
  • RxSwift学习(三)— Observer、Binder、Subjects、BehaviorRelay
  • RxSwift-内存管理
  • RxSwift核心逻辑
  • RxSwift+Moya之项目实战
  • RxSwift极简入门
  • iOS Swift之RxSwift初探
  • RxSwift学习笔记

深度学习机器人交易_使用深度学习创建股票交易机器人相关推荐

  1. 深度学习 情感分析_使用深度学习进行情感分析

    深度学习 情感分析 介绍 (Introduction) The growth of the internet due to social networks such as Facebook, Twit ...

  2. 深度学习实现象棋_使用深度学习创建国际象棋人工智能

    深度学习实现象棋 When Gary Kasparov was dethroned by IBM's Deep Blue chess algorithm, the algorithm did not ...

  3. 深度学习多模态融合_多模态深度学习综述.PDF

    多模态深度学习综述.PDF 37 6 Vol. 37 No. 6 第 卷第 期 计算机应用研究 录用定稿 Application Research of Computers Accepted Pape ...

  4. 深度学习基础知识_数学基础(学习笔记)

    线性代数 向量.矩阵.矩阵计算,矩阵的秩,范数 了解更多可以参考: 哔哩哔哩: 晓之车高山老师 https://space.bilibili.com/138962930/channel/collect ...

  5. 深度相机 物体三维重建_基于深度相机进行室内完整场景三维重建的方法及系统_2017100513665_说明书_专利查询_专利网_钻瓜专利网...

    S121:采用Kintinuous框架,进行视觉里程计估计,得到每帧深度图像下的相机位姿信息. S122:根据相机位姿信息,将由每帧深度图像对应的点云数据反投影到初始坐标系下,用投影后得到的深度图像与 ...

  6. 深度相机 物体三维重建_基于深度相机的实时物体三维重建方法与流程

    本发明涉及三维成像领域,特别是一种能够实时地对物体或人体进行三维重建的方法. 背景技术: 三维重建技术一直是计算机图形学和计算机视觉领域的热点课题.三维重建就是从输入数据中建立3D模型.随着各种面向普 ...

  7. 云鲸扫拖一体机器人说明书_云鲸扫拖一体机器人开箱测评。拖地机器人的天花板是什么样的?...

    云鲸我已经使用了好多天了,这个评测早就想写了,但是一直没想好该怎么写. 这是一台打破常规的扫地机器人,并不是说它仅仅是会自己洗抹布这么简单,而是这台机器完全的打破了之前所有扫地机器人的使用逻辑,走了一 ...

  8. 写字机器人软件_中小学开学临近 “补作业机器人”现身

    2月18日上午,首都图书馆内,几名中学生正忙着写寒假作业.新京报记者 黄哲程 摄 某电商平台出售的写字机器人,售价超千元,据称写字速度为每分钟30-40个字,还支持随机字体.网络截图 图书馆.咖啡厅成 ...

  9. 索尼koov机器人比赛_索尼KOOV可编程教育机器人评测 这无疑不是教育界的黑科技...

    没有创造力的人生略苍白 到了某个年纪,越发的觉得创新是多么重要,没有想象力的人生多少有些苍白,甚至无法凸显自己的个性.从小是个积木爱好者,给我一堆木头我分分钟还你一个城堡,最"孤独" ...

  10. 小学生机器人挑战赛_创意放飞梦想 宝鸡中小学生青少年机器人竞赛显身手

    宝鸡新闻网讯(记者 张超)自动售蛋机.智能灭火车.避震课桌--孩子们的奇思妙想变成了真实的作品.3月23日,第五届宝鸡市青少年机器人竞赛在渭滨区石鼓小学举办,不少解决生活实际困难的小创意让人眼前一亮. ...

最新文章

  1. .CN域名总量达1090.6万个:8月份共净增13.8万个
  2. 深入浅出SharePoint——数据库维护
  3. sql server express 并发数的限制_阿里数据库性能诊断的利器——SQL执行干预
  4. 浪潮云发布全新“1231”业务战略,打造“一朵分布式云”
  5. 调用$.ajax不成功,jquery中ajax请求后台数据成功后既不执行success也不执行error的完美解决方法...
  6. ubuntu软件安装、卸载
  7. linux 多线程(一)条件变量
  8. windows2003 IIS6 部署MVC4程序等等
  9. 物业管理系统源码java_Java小区物业管理系统 源码报告下载
  10. 计算机原理寻址方式试题,计算机组成原理试题和答案
  11. word文字上下间距怎么调_word文档上下行间距怎么调整
  12. 胡润研究院发布《2018胡润区块链富豪榜》
  13. 从实习生到算法专家,他只用了2年!
  14. MPlayer安装和使用指南(转)
  15. Linux-常用快捷键
  16. 计算机课程中常用的高中数学公式
  17. 【第六章】使用jQuery操作表单和表格2
  18. static定义静态方法
  19. Mac苹果电脑调整磁盘区域大小
  20. mysql 建表语句 及完整案例

热门文章

  1. emv交易流程介绍,简易波动指标EMV基础知识介绍:EMV的计算公式_EMV应用法则
  2. FinalShell Mac OS版安装
  3. mysql mtq_MySQL调优学习笔记(一、MySQL基础)
  4. Java面试题及答案2020,安卓java编程软件app
  5. 2018.10.27 bzoj1984: 月下“毛景树”(树链剖分)
  6. cocos creator使用anysdk接入admob广告教程
  7. 【谷歌浏览器打不开Axure解决办法~】
  8. 第四篇:UE4视角切换节点,Possess和Set View Target With Blend的区别
  9. 关于新手必须要理解的几个名词,cookie、session和token
  10. python iter函数用法