Click here to download the PDF file of this post

**This guest post was written by Troy Bombardia. He shares free market analysis, data and trading algorithms at Bull Markets.**

This is part 4 of this Python for commercial series. Parts 1, 2 and 3 can be found here, here and here. In this tutorial, I will teach you how to code an easy-to-use strategy for recent public (IPO) trading stocks. These promotions are highly variable, which makes them great candidates for quick strategies.

This strategy uses 1 index and 1 ETF, but for real trading you can use the ETF that corresponds to the index:

- Renaissance IPO Index (Renaissance IPO ETF: IPO)
- ETF Treasury Bonds: TLT

You can download the data for this algorithm and the code for this algorithm here as a CSV.

About how to code this algorithmic trading strategy.

### Import some libraries

First, import the Pandas and Numpy libraries. These Python libraries allow us to use pre-created functions instead of creating our own functions.

The code line “% Matplotlib inline” allows us to use matplotlib to create a table in Python.

*importing pandas as pd**Import numpy as np**% matplotlib series*

### Read your CSV data as a database

Next, we “read” the CSV file data and convert the data into a Dataframe called the “df” variable:

*df = pd.read_csv (‘C: \ Users \ vaish \ Desktop \ pythoncode \ y_new \ ipo_strategy.csv’)**df*

### Coding strategy

Our strategy for selling IPO shares in the near future is simple and effective: buy an IPO when the 50-day moving average of the IPO index is rising and otherwise a long TLT (Treasury Commitment).

*df[‘IPO Trend’] = df[‘IPO Index’].rolling (play = 50) .mean ()> = df[‘IPO Index’].rolling (play = 50) .mean (). shift (1)**df[‘TLT Long’] = (df[‘IPO Trend’] == lie)*

*df*

### Calculate the IPO index and daily price change TLT

Next, we need to calculate the IPO index and the daily% change in TLT. To do this, we use the .pct_change () function.

*df[‘IPO Change’] = df[‘IPO Index’].pct_change ()**df[‘TLT Change’] = df[‘TLT’].pct_change ()*

*df*

### Calculate the effectiveness of the strategy

Finally, it is time to calculate how the strategy will work over time.

*df[‘Buy and hold IPO’] = 100 * (1 + df.)[‘IPO Change’]) .cumprod ()**df[‘Strategy’] = 100 * (1 + df.)[‘IPO Trend’].shift (1) * df[‘IPO Change’]+ df[‘TLT Long’].shift (1) * df[‘TLT Change’]) .cumprod ()*

*df*

The first line of the code calculates the purchase and retention of the IPO index.

The second line of code calculates how this trading algorithm works over time. Every day the algorithm checks whether yesterday’s “IPO purchase” condition is TRUE or NEW. If the condition was TRUE, then the strategy will continue the IPO index today for a long time. If the condition is FALSE, today’s strategy is a long-term TLT (Treasury Commitment).

### How well this trading strategy works

To determine how well this trading algorithm works, we can write a few lines of code.

*start_portfolio = df[‘Strategy’].lok[50]**end_portfolio = df[‘Strategy’].lok[-1]**year = (2959-50) / 252**average_return = (end_portfolio / start_portfolio) ** (1 / year) -1*

*print (‘Average trading algorithm profitability’, average_return, ‘per year’)*

*ipo_start = df[‘IPO Index’].lok[50]**ipo_end = df[‘IPO Index’].lok[-1]**ipo_average_return = (ipo_end / ipo_start) ** (1 / year) -1**print (‘Average purchase and hold IPO return’, ipo_average_return, ‘per annum’)*

- The first line of the code determines the initial value of this trading algorithm (start_portfolio)
- The second line of the code determines the final value of this trading algorithm (end_portfolio)
- The third line of the code calculates the years (years) used in this strategy
- The fourth line of code calculates the average annual revenue of this trading algorithm (as a decimal number instead of%)

This trading algorithm yields an average of 16.19% per year

- The “Ipo_start” variable determines the initial value of the buy and hold
- The “Ipo_end” variable determines the final value of the purchase and hold
- The variable “Ipo_average_return” calculates the average annual return on purchases and holdings

Buy and hold the IPO index, the average annual return is 17.9%

As you can see, this strategy reduces the ability to buy and hold a bit. However, the main advantage is that this strategy should be less variable than buying and holding – the shots are smaller. Recent IPO shares can be highly volatile, so it is important to reduce the decline.

This 2 line code allows us to determine the effectiveness of the strategy:

**Thank you for reading this guide. For trading series, I will share more trading algorithms in future parts of this Python. **

**Previous posts:**

Commercial Series Python – Part 1

Commercial Series Python – Part 2

Commercial Series Python – Part 3

**You can follow Troy Bombardia on Twitter @BullMarketsco. ****or on its website** www.bullmarkets.co.