Creating a Support & Resistance Scanner Using Python
Written on
Understanding Support & Resistance Levels
As we prepare for the trading day, having efficient scanners and interpreters is essential for quick market analysis. Some traders utilize comprehensive signal scanners and trading algorithms, while others opt for simpler tools, such as technical scanners. This article presents a scanner that analyzes market data to identify support and resistance levels through a well-known and objective method.
I have recently published a book titled "Contrarian Trading Strategies in Python," featuring a range of advanced contrarian indicators and strategies, along with a GitHub repository for ongoing updates. If you're interested, you can purchase the PDF version for €9.99 via PayPal. Please include your email address in the payment note to ensure you receive it properly. Afterward, remember to download it through Google Drive.
What are Support & Resistance Levels?
In its simplest form, a support level acts as a barrier preventing prices from declining further. As the market approaches this area, traders typically adopt a bullish outlook. Conversely, a resistance level serves as a ceiling, deterring price increases. When approaching a resistance area, traders generally adopt a bearish perspective.
The chart below illustrates support (green) and resistance (blue) lines, drawn based on empirical observations.
Identifying support and resistance can be achieved through various methods:
- Charting: A subjective approach relying on past price reactions to establish future levels, drawing trend lines as well as horizontal support and resistance lines. However, this method can vary significantly among traders.
- Fibonacci: A less subjective technique that employs the Fibonacci sequence for retracement levels, helping to pinpoint reactionary levels.
- Moving Averages: An objective method that uses moving averages as dynamic support and resistance levels. However, the multitude of moving averages available can impact reliability.
- Pivot Points: This time-tested method calculates future implied support and resistance levels, offering a quick and effective analysis tool.
For an in-depth look at market positioning and future projections across major markets, consider checking out my weekly market sentiment report.
The Role of Price Action Trading
Price action trading focuses solely on current price movements rather than relying on technical indicators. This approach has the advantage of being non-lagging, allowing traders to detect patterns and identify support and resistance levels. One effective method for this is through the use of Pivot Points.
Pivot points are straightforward calculations used to determine implied support and resistance levels. Many intraday traders calculate them daily, while others may do so monthly or annually. For instance, on January 1st, a trader might calculate projected levels based on the previous year's data.
Note: In this article, we will compute daily pivot points based on midnight to midnight Paris time. While this is just a demonstration and may not be the most optimal approach, time zone considerations are crucial in the FX market.
What is a Pivot Point and How is it Used?
Essentially, pivot points can act as magnets for the market, encouraging it to revert back when deviations occur. They can also serve as profit targets after initiating a contrarian position based on resistance or support levels.
Calculating Pivot Points for USDCAD
The following analysis examines the USDCAD hourly data from the previous day. By gathering the high, low, and closing values, we can compute today's support and resistance levels.
The previous day's data was as follows:
- High = 1.20950
- Low = 1.20275
- Close = 1.20650
Using the formulas, the Pivot Point is calculated at 1.2062, with the first support at 1.2030 and the first resistance at 1.2100. As the trading day begins, we monitor these levels to inform our trading decisions.
Let’s develop the scanner code to visualize these levels effectively.
The first video titled "Bambu Studio tutorial: How to create automatic or manual supports" provides an insightful guide on setting up support and resistance levels in trading.
Crafting the Asset List Function
To begin, we need to refine the asset_list function to include various currency pairs:
def asset_list(asset_set):
if asset_set == 'FX':
# A selected group of currency pairs
assets = ['EURUSD', 'USDCHF', 'GBPUSD', 'AUDUSD', 'NZDUSD',
'USDCAD', 'EURCAD', 'EURGBP', 'EURCHF', 'AUDCAD']elif asset_set == 'CRYPTO':
# Cryptocurrency examples
assets = ['BTCUSD', 'ETHUSD', 'XRPUSD', 'LTCUSD', 'DSHUSD',
'XMRUSD', 'ETHBTC']elif asset_set == 'COMMODITIES':
# Commodity examples
assets = ['XAUUSD', 'XAGUSD', 'XPTUSD', 'XPDUSD']
return assets
Next, we select the asset type, focusing on FX:
assets = asset_list('FX')
Now, we define the get_quotes function using today’s date.
def get_quotes(time_frame, year=2021, month=8, day=10, asset="EURUSD"):
# Connect to MetaTrader 5
if not mt5.initialize():
print("initialize() failed, error code =", mt5.last_error())
quit()
timezone = pytz.timezone("Europe/Paris")
utc_from = datetime.datetime(year, month, day, tzinfo=timezone)
utc_to = datetime.datetime(now.year, now.month, now.day, tzinfo=timezone)
rates = mt5.copy_rates_range(asset, time_frame, utc_from, utc_to)
rates_frame = pd.DataFrame(rates)
return rates_frame
The next step is to gather daily data rather than short-term data for simplicity.
Calculating Pivot Points
We will now define the pivot point calculation function so the algorithm can determine support and resistance levels:
def pivot_point_calculator(Data, n, type='Classic'):
levels = []
pivot = (Data[-1, 1] + Data[-1, 2] + Data[-1, 3]) / 3
pivot = round(pivot, 4)
levels = np.append(levels, pivot)
first_support = ((pivot * 2) - Data[-1, 1])
first_support = round(first_support, 4)
levels = np.append(levels, first_support)
first_resistance = ((pivot * 2) - Data[-1, 2])
first_resistance = round(first_resistance, 4)
levels = np.append(levels, first_resistance)
print(assets[n])
print('Pivot = ', pivot)
print('First Support = ', first_support)
print('First Resistance = ', first_resistance)
print('----')
return levels
Selecting only six currency pairs allows for a more focused analysis.
Visualizing the Results
We’ll create a plotting function to visualize the results clearly.
The second video titled "Cura 3D Model Support Generator: Auto-generate, Tree supports, and Custom supports" demonstrates techniques for creating various supports, which can be analogous in trading strategies.
Conclusion
In summary, my goal is to contribute to the domain of objective technical analysis, promoting transparent techniques that undergo rigorous back-testing prior to deployment. This way, we can enhance the credibility of technical analysis as a reliable trading method.
It's essential to approach any trading strategy with a critical mindset, ensuring that you back-test under realistic conditions, optimize potential strategies, and include transaction costs and risk management in your evaluations. Always remain vigilant, as market dynamics can shift, potentially affecting the profitability of any strategy.
For those interested in exploring algorithmic trading further, I recommend checking out Lumiwealth for detailed courses covering a variety of topics, including machine learning and blockchain.
For the paperback version of my book, please refer to the following link: