导入数据库怎么导入_导入必要的库

导入数据库怎么导入

重点 (Top highlight)

With the increasing popularity of machine learning, many traders are looking for ways in which they can “teach” a computer to trade for them. This process is called algorithmic trading (sometimes called algo-trading).

随着机器学习的日益普及,许多交易者正在寻找可以“教”计算机进行交易的方式。 此过程称为算法交易(有时称为算法交易)。

Algorithmic trading is a hands off strategy for buying and selling stocks that leverages technical indicators instead of human intuition.

算法交易是一种利用技术指标而不是凭直觉来买卖股票的不干预策略。

In order to implement an algorithmic trading strategy though, you have to narrow down a list of stocks you want to analyze. This walk-through provides an automated process (using python and logistic regression) for determining the best stocks to algo-trade.

但是,为了实施算法交易策略,您必须缩小要分析的股票列表。 本演练提供了自动流程(使用python和逻辑回归),用于确定要进行算法交易的最佳库存。

I will dive deeper into the logic and code below, but here is a high-level overview of the process:

我将更深入地研究下面的逻辑和代码,但这是该过程的高级概述:

  1. Import the historical data of every stock using yahoo finance.

    使用Yahoo Finance导入每只股票的历史数据。
  2. Pull in over 32 technical indicators for each stock using the technical analysis library.

    使用技术分析库为每种股票提取32种以上的技术指标。

  3. Perform a logistic regression on each stock using 5, 30, and 60 day observation time periods.

    使用5、30和60天的观察时间段对每只股票执行逻辑回归。
  4. Interpret the results.

    解释结果。

导入必要的库 (Import Necessary Libraries)

Running this program in python requires these libraries:

在python中运行此程序需要以下库:

Libraries:

库:

  • Yfinance: Gather the historical data of each stock.

    Yfinance :收集每只股票的历史数据。

  • Pandas: Work with large datasets.

    熊猫 :处理大型数据集。

  • Shutil, Glob, and OS: Access folders/files on your computer.

    ShutilGlobOS :访问计算机上的文件夹/文件。

  • Time: Forcing the program to pause for a period of time.

    时间 :强制程序暂停一段时间。

  • Get_All_Tickers: Filter through all stocks to get the list you desire.

    Get_All_Tickers :筛选所有股票以获得所需的列表。

  • Numpy: Work with arrays.

    numpy :使用数组。

  • Sklearn: To run our logistic regression model.

    Sklearn :运行我们的逻辑回归模型。

  • TA: To import the technical indicators.

    TA :要导入技术指标。

Necessary libraries.
必要的库。

收集历史库存数据 (Collecting Historical Stock Data)

Obtaining historical data on the stocks that we want to observe is a two-step process.

获取我们要观察的股票的历史数据是一个两步过程。

  • Narrow down a list of stocks that we want to observe.

    缩小我们要观察的股票清单。
  • Make individual calls to the Yfinance API in order to import data about each company.

    单独调用Yfinance API ,以导入有关每个公司的数据。

Choosing the Stocks We Want to Target

选择我们要定位的股票

The library get-all-tickers allows us to compile a list of stock tickers by filtering companies on aspects like market cap or exchange. For this example, I am looking at companies that have a market cap between $150,000 and $10,000,000 (in millions).

全收录行情允许我们通过过滤公司的市值或交易所等方面来编制股票行情清单。 对于此示例,我正在研究市值在150,000美元到10,000,000美元(百万美元)之间的公司。

You will notice that I also included a line of code to print the number of tickers we are using. This is very important. You will need to be sure that you are not targeting more than 2,000 tickers, because the Yfinance API has a 2,000 API calls per hour limit.

您会注意到,我还提供了一行代码来打印我们正在使用的股票代号。 这个非常重要。 您需要确保定位的目标报价没有超过2,000个,因为Yfinance API每小时的API调用限制为2,000个。

We also need to remove and create a folder on our local machine to hold historical stock data. Note that before this step runs for the first time, you will need to manually create this folder. This step is important because it will get rid of the data from past iterations and allow the program to start new.

我们还需要在本地计算机上删除并创建一个文件夹来保存历史库存数据。 请注意,在首次运行此步骤之前,您将需要手动创建此文件夹。 此步骤很重要,因为它将清除过去迭代中的数据,并允许程序重新开始。

Initialize the tickers list and necessary folders.
初始化代码列表和必要的文件夹。

导入历史库存数据 (Importing Historical Stock Data)

We will now obtain the historical price data of each stock in our tickers list by making independent calls to Yahoo Finance. After receiving the data, the program will save each company’s information in a new CSV file that will be located in the folder you created beforehand.

现在,通过独立致电Yahoo Finance,我们将在报价清单中获取每只股票的历史价格数据。 收到数据后,程序会将每个公司的信息保存在一个新的CSV文件中,该文件位于您预先创建的文件夹中。

Code to collect and save the historical stock data of each observed stock.
编码以收集并保存每个观察到的库存的历史库存数据。

导入技术指标 (Importing Technical Indicators)

In order to run a logistic regression on each stock, we need to substantiate the independent variables. These 32+ technical indicators will act as predictor variables for the dependent variable.

为了对每种股票进行逻辑回归,我们需要证实自变量。 这32多个技术指标将用作因变量的预测变量。

To bring in the technical indicators we make use of the technical analysis library.

为了引入技术指标,我们利用技术分析库

You will also notice that we are creating three extra columns. These will act as the value we are trying to predict (dependent variable) with the model. The three variables are 5, 30, and 60 day observations of the closing prices of each stock (1 if it increased and 0 if it did not).

您还将注意到,我们将创建三个额外的列。 这些将充当我们试图用模型预测的值(因变量)。 三个变量是每只股票收盘价的5天,30天和60天观察值(如果增加,则为1,如果没有,则为0)。

Bring in the technical indicators for each stock.
引入每种股票的技术指标。

运行逻辑回归模型 (Run the Logistic Regression Model)

At this point, we have the historical data as well as over 32 technical indicators for each stock we chose to observe. We also created three different time-interval observations for which we want to predict the stock’s future price.

至此,我们已经有了历史数据以及我们选择观察的每种股票的32多项技术指标。 我们还创建了三个不同的时间间隔观察值,以用于预测股票的未来价格。

Now, all that is left is to clean the data (remove infinite and null values), run the logistic regression model, and interpret the results.

现在,剩下的就是清理数据(删除无限和空值),运行逻辑回归模型并解释结果。

For those who are less familiar with running statistical models, here is a quick breakdown of what the below code does:

对于那些不太熟悉运行统计模型的人,以下是以下代码的功能的快速细分:

  1. Creates a loop to go through each stock that we saved.

    创建一个循环以遍历我们保存的每只股票。
  2. Replaces infinite values with null and then replaces null values with 0.

    将无限值替换为null,然后将null值替换为0。
  3. Scales the data so that each variable is comparable.

    缩放数据,以便每个变量都是可比较的。
  4. Creates a loop to run the logistic regression three times for each stock (for 5, 30, and 60 day observations).

    创建一个循环以对每只股票运行Logistic回归3次(针对5天,30天和60天的观察值)。
  5. Saves the output to a CSV file so that we can interpret the results and find the best stocks to trade.

    将输出保存到CSV文件中,以便我们可以解释结果并找到最适合交易的股票。
Runs the logistic regression on each stock.
对每只股票运行逻辑回归。

解释结果 (Interpreting the Results)

We now have a CSV file with the results of the logistic regression model that we ran on each individual stock. Below is an example of this output (a small excerpt):

现在,我们有了一个CSV文件,其中包含对每只股票运行的逻辑回归模型的结果。 以下是此输出的示例(一小段摘录):

Image for post
Output in CSV format.
以CSV格式输出。

Here are some assumptions we can make from these results:

我们可以从这些结果中得出一些假设:

  • Which observation period is the best to use for each stock (i.e has the highest model accuracy).

    哪种观察期最适合每种股票使用(即模型精度最高)。
  • Which stocks are best predicted by their technical indicators.

    通过技术指标可以最好地预测哪些股票。
  • If the model is better at predicting increases in price or decreases.

    如果模型更适合预测价格上涨或下跌。

It is definitely possible to derive more from this code, for example, you could calculate sensitivity and specificity from the confusion matrices.

绝对有可能从此代码中获取更多信息,例如,您可以从混淆矩阵中计算出灵敏度和特异性。

To learn more about logistic regression in python, check out this article. To get my full code, download it from GitHub here.

要了解有关python中逻辑回归的更多信息,请查看本文 。 要获取我的完整代码,请从GitHub 此处下载。

If you’re interested in learning more stock analysis automation, machine learning, and artificial intelligence techniques, check out my new publication, Hands-Off Investing.

如果您想了解更多的股票分析自动化,机器学习和人工智能技术,请查阅我的新出版物《 动手投资》

Image for post
Hands-Off Investing 放手投资

If you enjoyed this article, please give it a like and let me know what you thought. I would love to hear some feedback!

如果您喜欢这篇文章,请给我一个赞,让我知道您的想法。 我希望听到一些反馈!

Also, connect with me on LinkedIn here. I’m always happy to make some new connections!

另外,请在此处通过LinkedIn与我联系。 我总是很高兴结识一些新朋友!

** Disclaimer: The content of this article is not financial advice.

**免责声明:本文内容并非财务建议。

翻译自: https://medium.com/automated-trading/creating-an-algorithmic-trading-strategy-using-python-and-logistic-regression-3b93562d9493

导入数据库怎么导入

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值