導入數據庫怎么導入
重點 (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:
我將更深入地研究下面的邏輯和代碼,但這是該過程的高級概述:
- Import the historical data of every stock using yahoo finance. 使用Yahoo Finance導入每只股票的歷史數據。
Pull in over 32 technical indicators for each stock using the technical analysis library.
使用技術分析庫為每種股票提取32種以上的技術指標。
- Perform a logistic regression on each stock using 5, 30, and 60 day observation time periods. 使用5、30和60天的觀察時間段對每只股票執行邏輯回歸。
- 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.
Shutil , Glob和OS :訪問計算機上的文件夾/文件。
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 :要導入技術指標。
收集歷史庫存數據 (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.
我們還需要在本地計算機上刪除并創建一個文件夾來保存歷史庫存數據。 請注意,在首次運行此步驟之前,您將需要手動創建此文件夾。 此步驟很重要,因為它將清除過去迭代中的數據,并允許程序重新開始。
導入歷史庫存數據 (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文件中,該文件位于您預先創建的文件夾中。
導入技術指標 (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)。
運行邏輯回歸模型 (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:
對于那些不太熟悉運行統計模型的人,以下是以下代碼的功能的快速細分:
- Creates a loop to go through each stock that we saved. 創建一個循環以遍歷我們保存的每只股票。
- Replaces infinite values with null and then replaces null values with 0. 將無限值替換為null,然后將null值替換為0。
- Scales the data so that each variable is comparable. 縮放數據,以便每個變量都是可比較的。
- 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天的觀察值)。
- Saves the output to a CSV file so that we can interpret the results and find the best stocks to trade. 將輸出保存到CSV文件中,以便我們可以解釋結果并找到最適合交易的股票。
解釋結果 (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文件,其中包含對每只股票運行的邏輯回歸模型的結果。 以下是此輸出的示例(一小段摘錄):

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.
如果您想了解更多的股票分析自動化,機器學習和人工智能技術,請查閱我的新出版物《 動手投資》 。

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
導入數據庫怎么導入
本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。 如若轉載,請注明出處:http://www.pswp.cn/news/388382.shtml 繁體地址,請注明出處:http://hk.pswp.cn/news/388382.shtml 英文地址,請注明出處:http://en.pswp.cn/news/388382.shtml
如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!