How to use Python for Algorithmic Trading on the Stock Exchange Part 2

How to use Python for Algorithmic Trading on the Stock Exchange Part 2

We continue publishing the adaptation of the DataCamp manual on using Python to develop financial applications.

Thefirst part of the story told about the structure of financial markets, stocks and trading strategies, data of time series, as well as what will be needed to start the development.

Stock Exchange

The structure of the manual:

The first part is intended for beginners in the market, it will deal with the design of financial markets, stocks and trading strategies, time series data, and what will be needed to start the development.

The second part introduces an introduction to working with time series data and financial analysis tools, such as calculating volatility and moving averages, using the Pandas Python library.

Then we proceed to the immediate development of a simple impulse trading strategy.

In the fourth part, we will talk about how to conduct backtest strategies on historical data.

In the end, the questions of strategy optimization will be touched upon to increase its productivity, as well as to evaluate its performance and reliability.

Now that you know more about data requirements, understand the concept of time series, and become acquainted with pandas, it’s time to go deeper into the topic of financial analysis that is necessary to create a trading strategy.

With a simple calculation of the daily percent change, for example, dividends and other factors are not taken into account simply a percentage change in the price of the acquired shares is noted compared to the previous trading day. You can easily calculate such changes using the:

Usingpct_change () is convenient, but in that case, it is difficult to understand exactly how the daily income is calculated. Therefore, as an alternative, you can use the Pandas function called shift (). Then you need to separate the daily_close values by daily_close.shift (1) -1. If this function is used, NA-values will be located at the beginning of the resulting data frame.

For reference, the calculation of the daily change in the value of shares is calculated by the formula:

The result looks symmetrical and normally distributed: the daily price change is in the area of bin 0.00. It should be understood that in order to correctly interpret the results of the histogram, you need to use the describe ()function applied to daily_pct_c. In this case, it will be seen that the mean is also close to bin 0.00, and the standard deviation is 0.02. You also need to study percentiles to understand how much data is out of bounds -0.0101672, 0.001677 and 0.014306.

The indicator of the total daily rate of return is useful for determining the cost of investments in regular segments. To calculate the total daily rate of profit, you can use the values of daily changes in the price of assets in percent, adding to them 1 and calculate the final values:

Knowing how to calculate income is a useful skill, but in practice, these values rarely carry valuable information unless you compare them with the performance of other stocks. That’s why in two different instances two or more shares are often compared.

To do this too, you first need to download more data – in our case, c Yahoo! Finance. To do this, you can create a function that will use ticker shares, as well as the start and end dates of the trading period. In the example below, the data () function takes a ticker to retrieve data, starting from start date to end date and returns the result of the get () function. Data is marked with correct tickets, resulting in a data frame containing this information.

In the code below, the shares of Apple, Microsoft, IBM, and Google are loaded into one common data frame:

Note:this code was also used in the Pandas manual for finance, and it was later finalized. Also, because there are currently problems downloading data from Yahoo! Finance, for correct work, you may have to download the fix_yahoo_finance package – installation instructions can be found here or in the Repository of this manual.

Another useful graph for financial analysis is the scattering matrix. You can get it using the panda’s library. It will be necessary to add the function scatter_matrix () to the code. The arguments are passed daily_pct_change, and the diagonal is set to a value of choice – so as to obtain a kernel density estimation graph (Kernel Density Estimate, KDE). Also, with the alpha argument, you can set transparency, and use fig size to resize the graph.

Paul Brook is a professional full stack developer & IT content marketer. Developer by day and a content Marketer by night. He is the lead Developer and content marketer at www.smartspate.com. His passion for helping people in all aspects IT and Marketing flows through in the expert industry coverage he provides. In addition to IT, also provide technical SEO and copywriting services for business of all sizes.

Find Us At:

error: This Content is Protected! All Rights belong to Smart Spate Ltd.

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. For information on a cookie and how it impacts on users, you can visit our Privacy Policy and Cookie Policy.AcceptCloseRead more