[GO] A super beginner who does not know the basics of Python tried to graph the realized profit and loss data of Rakuten Securities in Python

I tried to graph the realized profit and loss data of Rakuten Securities.

Realized gains/losses are gains/losses that are fixed by selling or settling the shares you hold . Until then, it is called unrealized profit/loss , and the profit/loss will be fixed by cashing.

In other words, by checking the realized profit and loss, you can see whether you have gained or lost. Let's look back on past results and make use of them in future investments so as not to make the same mistakes.

Get csv file

First, get the realized profit / loss data (csv file) from Rakuten Securities. Log in to Rakuten Securities and open Account Management → P & L / Tax History → Realized P & L.

When the following screen is displayed, click "Save in CSV format" at the bottom right of the screen to save.

picture_pc_55f4822bae2299441937f6c4fed09926.jpg

As shown below, I was able to obtain a csv file of realized profit and loss from Rakuten Securities. (It's hard to see, but it's 99% lost.)

picture_pc_25da6fe28172d07769885719567f609d.jpg

* Notes on csv files

Although it is a csv file brought from Rakuten Securities, in fact, all data types other than "brand code" are "object type". (Check the data type with dtypes)

picture_pc_3c47c2f12ae2093c2c9852c5bfbec692.jpg

For data types, see the following articles.

Check data types with Pandas and perform type conversion with dtype/dtypes astype Check data types with Pandas and perform type conversion astype | Samurai Blog --Site for programming beginners

Since the calculation cannot be performed as it is, it is necessary to convert "object type" to "int type". Let's convert the type using Python.

I wanted to say, but I couldn't do it. So, this time, I took the painstaking measure of converting directly to "int type" in the spreadsheet.

Open the file in a spreadsheet and set the display format to "Automatic".

picture_pc_da0560848e8a140622d964f7e8be242d.jpg

You have now converted the "object type" to the "int type".

This is a future issue.

If there is a kind person who says "I can do it this way!", I would appreciate it if you could let me know.

Creation procedure

Now let's read the csv file and graph the realized profit and loss.

Click here for the completed code.

from google.colab import drive
drive.mount('/content/drive')

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('drive/My Drive/csv/rakuten-int.csv', encoding='utf-8')

df.head()

df.dtypes

df['Realized profit / loss[Circle]'].cumsum().plot(figsize=(8,6),fontsize=18)
plt.xlabel("Number of transactions",size="12")
plt.ylabel("Realized profit/loss",size="12")
plt.grid(True)
plt.show()

First of all, let Google Colab read the csv file.

However, since Google Colab runs on the Internet, it is necessary to devise to read the csv file.

Execute the following code

from google.colab import drive
drive.mount('/content/drive')

It means to mount Google Drive in a folder called "drive" under the "count" directory. Google Drive files are stored in the "drive/My Drive" folder under the folder "drive" specified here. When you execute this code, the authentication URL will appear, so please proceed to the update as described in the following article.

[Google Colab] How to read a file [Can be done in 3 minutes]

After the update, import pandas and matplotlib to load the csv file.

Import pandas and matplotlib

import pandas as pd
import matplotlib.pyplot as plt

Substitute csv file for df

df = pd.read_csv('drive/My Drive/csv/rakuten-int.csv', encoding='utf-8
')

read_csv : Used when reading csv files 'drive/My Drive/csv/rakuten-int.csv' : Relative path to csv file encoding = ’utf-8’ : Character code

Is there a problem with the character code of the csv file brought from Rakuten Securities? Because there is, an error occurs. Be sure to enter "encoding ='utf-8'" after the relative path. Please check the following article for details.

Active engineers explain how to avoid unicode decode error in Python [for beginners]

Check if the data was acquired

df.head()

picture_pc_d4b3682ba02ef6ffbe1d270a755ce078.jpg

I was able to get the data.

Graphing realized profit and loss data

df['Realized profit / loss[Circle]'].cumsum().plot(figsize=(8,6),fontsize=18)
plt.xlabel("Number of transactions",size="12")
plt.ylabel("Realized profit/loss",size="12")
plt.grid(True)
plt.show()

df ['Realized P/L [Yen]'] : ['Realized P/L [Yen]'] in df cumsum () : Cumulative sum (the sum of the sequence from the first term to the nth term) For example, it is a calculation that outputs the cumulative sales data up to that day from the daily sales data.

Plot x-axis labels

plt.xlabel("Number of transactions",size="12")

Number of transactons : x-axis name (“number of transactions” in Japanese) size : Font size

Plot the y-axis label

plt.ylabel("Realized profit/loss",size="12")

Realized profit/loss : y-axis name (“realized profit/loss” in Japanese)

The label of the axis is written in English, but if it is written in Japanese, the characters will be garbled. I really wanted to write it in Japanese, but it's going to be long, so I'll take another opportunity.

plt.grid (True) : Display the grid on the graph plt.show () : Display the created graph on the screen

Here is the execution result so far.

picture_pc_ce65da45eb230cccde0c497dd42ae0cf.jpg

I was able to graph the realized profit and loss data.

It is poor from the stunning W bottom. It is very easy to understand if you visualize it with a graph.

At one point, it went to nearly minus 200,000 yen, but it recovered sharply and recovered to nearly plus 150,000 yen, and ended up with a minus.

By the way, the final realized profit/loss is -5,091 yen. (Ended on April 30, 2020)

I lost about 5,000 yen over a year. It's cheap if you think of it as a study fee.

Future tasks

I would like to acquire more Python skills so that I can perform detailed analysis. And we will continue to do our best in asset management so that we can show you transaction data that is not embarrassing.

Recommended Posts

A super beginner who does not know the basics of Python tried to graph the realized profit and loss data of Rakuten Securities in Python
A super beginner who does not know the basics of Python tried to graph the stock price of GAFA
A Python beginner first tried a quick and easy analysis of weather data for the last 10 years.
I tried to display the altitude value of DTM in a graph
Let's use Python to represent the frequency of binary data contained in a data frame in a single bar graph.
Use libsixel to output Sixel in Python and output a Matplotlib graph to the terminal.
Not being aware of the contents of the data in python
[Super easy! ] How to display the contents of dictionaries and lists including Japanese in Python
I tried to create a Python script to get the value of a cell in Microsoft Excel
Build a Python environment and transfer data to the server
[Introduction to Data Scientists] Basics of Python ♬ Functions and classes
I want to know the features of Python and pip
I tried to find out the difference between A + = B and A = A + B in Python, so make a note
[Python] The role of the asterisk in front of the variable. Divide the input value and assign it to a variable
[Kenchon book to Python] "Train your problem-solving skills! Algorithms and data structures" I tried to rewrite the posted code in Python! -table of contents-
How to execute Linux commands on Windows by people who do not know Linux (ubuntu) [Until checking the operation of R and python]
processing to use notMNIST data in Python (and tried to classify it)
How to know the internal structure of an object in Python
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
A beginner who has been programming for 2 months tried to analyze the real GDP of Japan in time series with the SARIMA model.
A special Python codec that seems to know but does not know
[Introduction to Data Scientists] Basics of Python ♬ Conditional branching and loops
[Introduction to Data Scientists] Basics of Python ♬ Functions and anonymous functions, etc.
[Python / Jupyter] Translate the comment of the program copied to the clipboard and insert it in a new cell
I tried to open the latest data of the Excel file managed by date in the folder with Python
A super introduction to Django by Python beginners! Part 2 I tried using the convenient functions of the template
I tried to get and analyze the statistical data of the new corona with Python: Data of Johns Hopkins University
Calculate the shortest route of a graph with Dijkstra's algorithm and Python
How to send a visualization image of data created in Python to Typetalk
I tried to verify and analyze the acceleration of Python by Cython
A beginner of machine learning tried to predict Arima Kinen with python
I made a program to check the size of a file in Python
I tried to implement a card game of playing cards in Python
[Introduction to Data Scientists] Basics of scientific calculation, data processing, and how to use the graph drawing library ♬ Environment construction
[Python & SQLite] I tried to analyze the expected value of a race with horses in the 1x win range ①
(Diary 1) How to create, reference, and register data in the SQL database of Microsoft Azure service with python
An engineer who has noticed the emo of cryptography is trying to implement it in Python and defeat it
I wanted to know the number of lines in multiple files, so I tried to get it with a command
I didn't know the basics of Python
Easily graph data in shell and Python
The basics of running NoxPlayer in Python
[Introduction to Data Scientists] Basics of Python ♬
Various ways to read the last line of a csv file in Python
How to pass the execution result of a shell command in a list in Python
[Python] From morphological analysis of CSV data to CSV output and graph display [GiNZA]
A programming beginner tried to find out the execution time of sorting etc.
I tried to automate the 100 yen deposit of Rakuten horse racing (python / selenium)
What to do when the graph does not appear in jupyter (ipython) notebook
I tried to refactor the code of Python beginner (junior high school student)
Examples and solutions that the Python version specified in pyenv does not run
Get the stock price of a Japanese company with Python and make a graph
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
Acquire the data of Mitsubishi UFJ International Investment Trust eMAXIS with Python and make a graph with the beginning of the term as 100