Sitemap
InsiderFinance Wire

InsiderFinance news wire — Top articles on data-driven trading, investing, analyses, tools, and strategies to achieve financial freedom

Moving average strategy using Python and yfinance api

--

Algo Trading, Python

Introduction of Moving Average
(MA) is a stock indicator that is commonly used in technical analysis. The reason for calculating the moving average of a stock is to help smooth out the price data by creating a constantly updated average price.
Let's dive in to find out how it can help us get more returns with a simple long-term investment.

Source : Investopedia

Let’s Start

We check a good momentum stock - PYPL

Step 1 — Get the data

# Import libraries and initialize our data parameters

We have considered the following parameters

Stock — Paypal
Initial Capital — $1000

Import Data from Yfinance
We now get the data in a DF, rename the columns, and also set an index to the date column.

Calculate the short moving average and long moving average

Get Amol Chawathe’s stories in your inbox

Join Medium for free to get updates from this writer.

Time to get the short moving average and long moving average, we use a simple crossover strategy to find the points where the short average is crossing the long average.

Finding the buy and sell signals
Let’s calculate the buying and selling signals and profit and loss .

We can check the results

Press enter or click to view image in full size

Backtesting —

Let’s backtest the result with an initial capital of $1000

Let’s look at the result.

Press enter or click to view image in full size

As you can see, we actually lose some gains when doing a moving average strategy. We hope to discuss more ideas and strategies which will enhance our potential to earn more income.

Graphs — Let’s plot some graphs

Let’s run the script

Result — we have a $2,799.78 profit based on Moving average strategy.

Press enter or click to view image in full size

As we can see, there is a potential of generating more better income strategies. We tried to learn the basics here and get our results. Please find the attached link to my complete source code.

I help retail investors trade better, in case you are interested to learn about algo-trading or have any ideas to implement, please connect with me on telegram here t.me/anandnavathe

--

--