Overview
This strategy is an adaptive trading system that dynamically identifies support and resistance levels based on price pivot points. It determines key price levels by calculating local highs and lows in real time and executes trades based on them. The core of this strategy is its dynamic nature, which allows it to adjust trading parameters in a timely manner according to changes in market conditions, and is suitable for both trending and volatile markets.
Strategy Principle
The core logic of the strategy is based on the following key elements:
- Dynamic Pivot Point Calculation: Uses an adjustable pivot length parameter (default is 2) to identify local highs and lows
- Support and resistance range: Set a percentage range (default 0.4%) based on the pivot point to define the effective trading area
- Trading signal generation: When the price breaks through the support level from below, a long signal is generated, and when it breaks through the resistance level from above, a short signal is generated.
- Risk control: Dynamic stop loss (10%) and profit (27%) settings are used, and the position size is automatically adjusted according to the account equity
Strategy Advantages
- Strong adaptability: The strategy can dynamically adjust the support and resistance positions according to the market status to avoid the lag caused by static levels
- Risk controllable: Through strict percentage stop loss and dynamic position management, the risk of each transaction is controlled within a reasonable range
- Scalability: Supports multiple time periods and parameter combinations to facilitate optimization according to different market environments
- High transparency: The trading logic is clear, and all signals and price levels can be intuitively displayed on the chart
Strategy Risks
- False breakout risk: Frequent false breakout signals may occur in a volatile market, which needs to be reduced by adjusting the support and resistance interval parameters.
- Slippage impact: In a market environment with poor liquidity, the actual transaction price may deviate greatly from the signal price.
- Trend dependence: The strategy performs better in a strong trending market, but may generate too many trading signals in a sideways phase
- Parameter sensitivity: Strategy performance is sensitive to parameter settings, and backtesting is required to determine the optimal parameter combination.
Strategy Optimization Direction
- Added market environment recognition module to automatically adjust parameters according to volatility
- Introduce volume and other technical indicators as auxiliary confirmation signals
- Optimize position management algorithms and make dynamic adjustments based on market volatility
- Add time filter to avoid generating trading signals during unfavorable periods
- Develop an adaptive stop loss algorithm to dynamically adjust the stop loss position according to market volatility
Summary
The strategy provides a reliable framework for trend following and reversal trading by dynamically identifying key price levels and combining strict risk control. Although there is a certain parameter sensitivity and market environment dependence, through continuous optimization and improvement, it can maintain stable performance in different market environments. The successful operation of the strategy requires traders to have a deep understanding of its principles and make appropriate parameter adjustments according to specific market conditions.
Strategy source code
/*backtest
start: 2019-12-23 08:00:00
end: 2025-01-08 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © felipemiransan
//@version=6
strategy("Dynamic Support and Resistance Pivot Strategy ", overlay=true)
// Strategy parameters
pivot_length = input.int(2, title="Pivot Length", tooltip="Pivot size to identify peaks and troughs")
support_resistance_distance = input.float(0.4, title="Support/Resistance Distance %", tooltip="Distance to consider a support or resistance level in %")
// Stop Loss and Take Profit parameters
stop_loss_pct = input.float(10.0, title="Stop Loss %", tooltip="Stop loss percentage", minval=0.1) / 100
take_profit_pct = input.float(26.0, title="Take Profit %", tooltip="Take profit percentage", minval=0.1) / 100
// Functions to identify high and low pivots
pivot_high = ta.pivothigh(high, pivot_length, pivot_length)
pivot_low = ta.pivotlow(low, pivot_length, pivot_length)
// Storing support and resistance levels
var float resistance_level = na
var float support_level = na
var float last_pivot_high = na
var float last_pivot_low = na
// Updating support and resistance based on pivots
if (not na(pivot_high))
resistance_level := high[pivot_length]
last_pivot_high := high[pivot_length]
if (not na(pivot_low))
support_level := low[pivot_length]
last_pivot_low := low[pivot_length]
// Function to check if the current price is near a support or resistance level
is_near_resistance = (not na(resistance_level)) and (close >= resistance_level * (1 - support_resistance_distance / 100)) and (close <= resistance_level * (1 + support_resistance_distance / 100))
is_near_support = (not na(support_level)) and (close >= support_level * (1 - support_resistance_distance / 100)) and (close <= support_level * (1 + support_resistance_distance / 100))
// Cross conditions variables
long_cross = ta.crossover(close, support_level) and not na(support_level)
short_cross = ta.crossunder(close, resistance_level) and not na(resistance_level)
// Entry conditions
long_condition = is_near_support and long_cross // Buy when crossing support from below
short_condition = is_near_resistance and short_cross // Sell when crossing resistance from above
// Order execution
if (long_condition)
strategy.entry("Long", strategy.long)
if (short_condition)
strategy.entry("Short", strategy.short)
// Stop Loss and Take Profit
if (strategy.opentrades > 0)
if (strategy.position_size > 0) // For long position
avg_price_long = strategy.position_avg_price
long_stop_level = avg_price_long * (1 - stop_loss_pct)
long_take_profit_level = avg_price_long * (1 + take_profit_pct)
strategy.exit("Exit Long", from_entry="Long", stop=long_stop_level, limit=long_take_profit_level)
if (strategy.position_size < 0) // For short position
avg_price_short = strategy.position_avg_price
short_stop_level = avg_price_short * (1 + stop_loss_pct)
short_take_profit_level = avg_price_short * (1 - take_profit_pct)
strategy.exit("Exit Short", from_entry="Short", stop=short_stop_level, limit=short_take_profit_level)
// Plotting support and resistance levels on the chart
plot(support_level, title="Support", color=color.green, linewidth=2, style=plot.style_line)
plot(resistance_level, title="Resistance", color=color.red, linewidth=2, style=plot.style_line)
// Adding labels to show pivot values
if (long_condition and not na(support_level))
label.new(bar_index, low[pivot_length], str.tostring(low[pivot_length]), style=label.style_label_up, color=color.green, textcolor=color.white, size=size.small)
if (short_condition and not na(resistance_level))
label.new(bar_index, high[pivot_length], str.tostring(high[pivot_length]), style=label.style_label_down, color=color.red, textcolor=color.white, size=size.small)
Strategy Parameters
The original address: Dynamic Support and Resistance Adaptive Pivot Trading Strategy