This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.

Price Action Trading Concepts – Part III

QuantInsti

Contributor:
QuantInsti
Visit: QuantInsti

See Part I for an overview of price action trading and the different types of charts and Part II to get insight on the concept of support and resistance.

Identifying support and resistance using Python

The first step in our approach is to import necessary libraries and get the asset data.

We will be focusing on the close price data of HINDUNILVR.NS for performing the support and resistance analysis. We fetch the asset’s OHLCV data from Yahoo! Finance and store it in a dataframe as shown below.

# Import Libraries
import numpy as np
import pandas as pd
import yfinance as yf
from math import sqrt
import matplotlib.pyplot as plt

# Set the start and end date
start = '2018-01-01°
end = ‘2021-07-12

# Set the ticker
symbol = "HINDUNTLVR.NS*

# Get the data from Yahoo! Finance
df = yf.dounload(symbol, start, end)

# Disply the data
df.tail()
Fetch data.py hosted with ❤ by GitHub

The last 5 rows of our data can be displayed using the df.tail() command.

Output:

DateOpenHighLowCloseAdj CloseVolume
2021-07-052499.0000002513.3999022485.0500492499.0500492499.050049761700
2021-07-062490.0000002497.9499512469.0000002472.5000002472.500000461782
2021-07-072458.0000002490.8000492445.8500982481.6499022481.649902623145
2021-07-082462.1999512469.7500002438.4499512447.5500492447.5500491114426
2021-07-092440.0000002464.5000002438.4499512451.4499512451.449951538094

To get a visual representation of the close price data, we will use various functions of the matplotlib library.

# Plot the close price data
series = df[‘Close’]
series. index = np.arange(series.shape[@])

plt. figure(figsize=(15, 7))
plt.title(symbol)

plt.xlabel ("Days")
plt.ylabel("Price’)
plt.plot(series, label-symbol)

plt.legend()
plt.show()
Plot.py hosted with ❤ by GitHub

Here’s the graph highlighting the close price of the asset:

Close price

Now since we have all our data in place, the next step is to identify the swing highs and the swing lows in the above price graph. An easier way to do this is to identify the local minima and local maxima points.

To identify the local minima and maxima points, we will first need to smoothen the price graph. This can be achieved using the savgol.filter function from the scipy.signal library. To learn more about this function, you can visit the documentation page here.

# Create smooth graph of close price data
from scipy.signal import savgol_filter

# To find amount of data in months
month_diff = series.shape[0] // 30
# We need value to be greater than 0
if month_diff == 0:
    month_diff = 1

# Algo to determine smoothness
smooth = int(2 * month diff + 3)

# Smooth price data
points = savgol_filter(series, smooth, 7)

# Plot the smooth price graph over default price graph
plt. figure(figsize=(15,7))

plt.title(symbol)

plt.xlabel ("Days")

plt.ylabel("Price’)

# Close price data
plt.plot(series, label-symbol)

# Smooth close price data
plt.plot(points, label=f'Smooth {symbol}")

plt.legend()
plt.show()
Smooth graph.py hosted with ❤ by GitHub

Stay tuned for the next installment of this series to learn how to plot and compare the normal close price graph with the smoothened close price graph

For additional insight on this topic visit QuantInsti blog: https://blog.quantinsti.com/price-action-trading/.

Disclosure: Interactive Brokers

Information posted on IBKR Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from QuantInsti and is being posted with permission from QuantInsti. The views expressed in this material are solely those of the author and/or QuantInsti and IBKR is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.

Disclosure: Post Contains Symbol

Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.

trading top