Calculating Pivot Points and Support and Resistance Levels
Summary
This code defines two pivot-point indicators that calculate a central pivot, two support levels, and two resistance levels from high, low, and close prices. The first uses the previous bar’s prices, a common way to derive levels for the next period without using that period’s completed data. Its pivot is the prior high-low-close average; the first support and resistance reflect the pivot’s distance from the prior high or low, while the second pair extend the pivot by the prior trading range.
The second indicator applies the same formulas to the current bar’s high, low, and close, so the resulting levels change as that bar develops. The document includes implementation only: it offers no entry or exit rules, backtest, or evidence that these levels predict reversals or breakouts. Users must choose the appropriate timing and avoid treating levels calculated from incomplete bars as fixed in advance. The code also depends on the Backtrader indicator framework.
Key ideas
- The indicator calculates a central pivot from high, low, and close prices.
- It derives first support and resistance from the pivot and the period’s extremes.
- Second support and resistance extend the pivot by the period’s full high-low range.
- One implementation uses the previous bar, while the other uses the current bar’s developing prices.
- The document supplies no trading rules or performance evidence for the levels.
Tags
Full text
# pivotpoint.py
```py
#!/usr/bin/env python
# -*- coding: utf-8; py-indent-offset:4 -*-
###############################################################################
#
# Copyright (C) 2015-2023 Daniel Rodriguez
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################
from __future__ import (absolute_import, division, print_function,)
# unicode_literals)
import backtrader as bt
class PivotPoint1(bt.Indicator):
lines = ('p', 's1', 's2', 'r1', 'r2',)
def __init__(self):
h = self.data.high(-1) # previous high
l = self.data.low(-1) # previous low
c = self.data.close(-1) # previous close
self.lines.p = p = (h + l + c) / 3.0
p2 = p * 2.0
self.lines.s1 = p2 - h # (p x 2) - high
self.lines.r1 = p2 - l # (p x 2) - low
hilo = h - l
self.lines.s2 = p - hilo # p - (high - low)
self.lines.r2 = p + hilo # p + (high - low)
class PivotPoint(bt.Indicator):
lines = ('p', 's1', 's2', 'r1', 'r2',)
plotinfo = dict(subplot=False)
def __init__(self):
h = self.data.high # current high
l = self.data.low # current high
c = self.data.close # current high
self.lines.p = p = (h + l + c) / 3.0
p2 = p * 2.0
self.lines.s1 = p2 - h # (p x 2) - high
self.lines.r1 = p2 - l # (p x 2) - low
hilo = h - l
self.lines.s2 = p - hilo # p - (high - low)
self.lines.r2 = p + hilo # p + (high - low)
```Shown in full with attribution under the source's licence. Licence: GPL-3.0
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.