0bps

Strategy vault / Volatility

Dynamic Support and Resistance Strategy Based on Historical Data

This strategy dynamically calculates support and resistance levels based on historical high, low and close prices, and generates trading signals accordingly. It is suitable for medium-to-long-term positions and can…

Author
ChaoZhang
Complexity
Starter · 55 lines
Parameters
5
Backtest bar
1h · intraday
Tested on
BTC/USDT · futures

IMG

Overview

This strategy dynamically calculates support and resistance levels based on historical high, low and close prices, and generates trading signals accordingly. It is suitable for medium-to-long-term positions and can effectively utilize support and resistance levels in the market for profit.

Strategy Logic

  1. Calculate the average of previous period's high, low and close prices as the pivot point (PP).

  2. Calculate 3 support lines: S1 = 2PP - highest price; S2 = PP - (R1-S1); S3 = lowest price - 2(highest price - PP).

  3. Calculate 3 resistance lines: R1 = 2PP - lowest price; R2 = PP + (R1-S1); R3 = highest price + 2(PP - lowest price).

  4. Take long position when price breaks through resistance lines, take short position when price breaks through support lines.

Advantage Analysis

  1. The dynamic support and resistance levels based on historical data can capture market structure changes timely.

  2. The multi-layer support and resistance settings allow better risk management optimization.

  3. Simple and intuitive trading signals and stop loss mechanism.

Risk Analysis

  1. The reference price levels provided by historical data may become invalid in high volatility scenarios.

  2. The switching between long and short positions should consider trading cost.

  3. Data quality needs to ensured to avoid calculation errors.

Optimization Directions

  1. Consider incorporating more historical data references like 100-day moving average etc.

  2. Optimize position sizing, e.g. adjust position size based on volatility.

  3. Add stop loss strategies like trailing stop loss or risk-based stop loss.

Summary

This strategy provides multi-layer support and resistance reference price levels based on history. It has simple and straightforward logic suitable for medium-to-long-term positions. Meanwhile, risks under high volatility market and trading costs should be monitored. Further optimizations can make the strategy robust under complex environments.

Parameters

NameDefaultWhat it controls
v_input_1DResolution
v_input_20Sell from : R1 | R2 | R3
v_input_30Buy from : S1 | S2 | S3
v_input_4truewidth
v_input_5falseTrade reverse

Source Pine Script

/*backtest
start: 2023-10-28 00:00:00
end: 2023-11-27 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4
////////////////////////////////////////////////////////////
//  Copyright by HPotter v1.0 09/06/2020
// Pivot points simply took the high, low, and closing price from the previous period and 
// divided by 3 to find the pivot. From this pivot, traders would then base their 
// calculations for three support, and three resistance levels. The calculation for the most 
// basic flavor of pivot points, known as 'floor-trader pivots', along with their support and 
// resistance levels.
//
// WARNING:
//  - For purpose educate only
//  - This script to change bars colors.
////////////////////////////////////////////////////////////
strategy(title="Pivot Point V2", shorttitle="Pivot Point V2", overlay = true)
res = input(title="Resolution", type=input.resolution, defval="D")
SellFrom = input(title="Sell from ", defval="R1", options=["R1", "R2", "R3"])
BuyFrom = input(title="Buy from ", defval="S1", options=["S1", "S2", "S3"])
width = input(1, minval=1)
reverse = input(false, title="Trade reverse")
xHigh  = security(syminfo.tickerid,res, high)
xLow   = security(syminfo.tickerid,res, low)
xClose = security(syminfo.tickerid,res, close)
vPP = (xHigh+xLow+xClose) / 3
vS1 = 2*vPP - xHigh 
vR1 = 2*vPP-xLow
vS2 = vPP - (vR1 - vS1)
vR2 = vPP + (vR1 - vS1)
vS3 = xLow - 2 * (xHigh - vPP)
vR3 = xHigh + 2 * (vPP - xLow) 
pos = 0
S = iff(BuyFrom == "S1", vS1, 
      iff(BuyFrom == "S2", vS2,
         iff(BuyFrom == "S3", vS3,0)))
B = iff(SellFrom == "R1", vR1, 
      iff(SellFrom == "R2", vR2,
         iff(SellFrom == "R3", vR3,0)))
pos := iff(close > B, 1,
       iff(close < S, -1, nz(pos[1], 0))) 
possig = iff(reverse and pos == 1, -1,
          iff(reverse and pos == -1 , 1, pos))	   
if (possig == 1) 
    strategy.entry("Long", strategy.long)
if (possig == -1)
    strategy.entry("Short", strategy.short)	 
if (possig == 0) 
    strategy.close_all()
barcolor(possig == -1 ? #b50404: possig == 1 ? #079605 : #0536b3 )

Published by ChaoZhang on the FMZ strategy square · last modified 2023-11-28. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.

Build this with AgenKit

A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.

in Claude Code, Cursor, Codex & 4 more
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Dynamic-Support-and-Resistance-Strategy-Based-on-Historical-Data.md
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. Get tests, backtest, risk controls and a paper-trading switch

Understand it first

Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.

Related specs

esc

Try , , or .

↑↓ move↵ open