From the course: Practical Python for Algorithmic Trading

Download and export data

- [Instructor] In this tutorial, I will show you how to download and export data tables with Python. You may observe the Microsoft daily stock prices which we download by using the Yahoo Finance Library. So we import YFinance, we abbreviated as YF, and we access the function download. The first parameter they ask is tickers, therefore tickers equals to the code for the Microsoft stock. If we go to Yahoo Finance, we search for Microsoft and we see the ticker MSFT. We go to Python, MSFT, we execute. And there we have the historical daily prices for Microsoft. Let's save the data frame into a variable that we will call df_msft, and to make the code more legible, we will substitute a variable that we create up here, ticker equals MSFT. Now, if we would like to download any other ticket, such as the Bitcoin, we apply the same thinking process. Going to Yahoo Finance, looking for Bitcoin, we see the ticker is BTC_USD. So in the Python code BTC_USD, we execute, and there we have the historical daily prices for the Bitcoin. One thing is to visualize the data in a table and another very different in a chart. We will use the Plotly library that makes interactive charts as the one that you see here, where you can select a date range and a specifically observe the information for a given day. We copy this snippet of code, we paste it in our Python, and now we need to adapt this code to our problem as we want to plot the Microsoft stock. The data frame that we are using is df_msft. Therefore, instead of reading a new CSV file, we just put our data frame. And now they are using the ticker in the names of the columns, but we don't have the name of the column, therefore we remove this. And one last modification is the date, which is not really a column because it's on another level of the data frame which is the index. Therefore the df.index. We don't need these two libraries. We execute. And here we can see the candlestick for the Microsoft data. The same process would apply for the Bitcoin that we can execute and see the variation of the stock over time. Now, what if you would like to export the data into a CSV file or an Excel? We can create a new folder. Let's find out in the following lines of code. We will drop the adjust close price to not confuse it with the close. And the first thing we need to do is create a folder called data. So we see the notebooks course we create a folder called data. And then with the data frame, df_MSFT, we access the functions with a dot. We look for a function name, Excel. There we have it, to Excel. And now we pass the name of the Excel. So within the folder data, we will call the Excel, Microsoft Stock Price Historical Daily, and the Excel format. We execute and then we see in the folder, we see the Excel. Now, if we would like to use a CSV, which occupies less memory, we apply the same thinking process. We look for CSV function, and there we have it. And now the name of the file, instead of being in format of an Excel, in the Excel format, it should be on a CSV. We execute. And there we see the CSV file. Wrapping up everything step by step. First, let's do it with the Bitcoin. We define the ticker, we download the data frame, we create the file path, we create the file path with a ticker, then we export it into an Excel, and into a CSV. And there we see the two files. So with this snippet of code, you can access any stock data from Yahoo Finance and export it into an Excel or a CSV. Let's say that instead of Bitcoin you would like to put the Tesla. There you have it. Now let's move on to the following tutorial, where I will show you how to pre-process the data to calculate the columns that we will use to decide when to buy or sell the stock.

Contents