0bps

Strategy vault / Volatility

Quantitative Trading Strategy Based on Double EMA and Price Volatility Index

This strategy is called "Moving Average Indicator and Price Volatility Combination Strategy". It combines the double exponential moving average (DEMA) and the price volatility index to generate a comprehensive…

Author
ChaoZhang
Complexity
Intermediate · 75 lines
Parameters
8
Backtest bar
1h · intraday
Tested on
BTC/USDT · futures

IMG

Overview

This strategy is called "Moving Average Indicator and Price Volatility Combination Strategy". It combines the double exponential moving average (DEMA) and the price volatility index to generate a comprehensive trading signal.

Principle

The strategy consists of two parts:

  1. DEMA indicator. This indicator calculates the 20-day and 2-day exponential moving averages. It generates trading signals when the price breaks through the 2-day line from above or breaks through the 20-day line from below.

  2. (Highest Price - Lowest Price)/Close Price Volatility Index. This index reflects the fluctuation range of prices within one period. Here we calculate the 16-day simple moving average of the volatility index over the past 20 bars. When the current bar's volatility is higher or lower than this average value, it generates trading signals.

The signals from the two parts are combined. If DEMA and volatility index give signals at the same time, the final long or short trading orders will be generated.

Advantage Analysis

The strategy has the following advantages:

  1. Combining multiple indicators can reduce false signals and improve signal reliability.

  2. The 20-day line can effectively identify medium-to-long term trends, and the 2-day line can capture short-term fluctuations, making the combination adaptable to different market environments.

  3. The volatility index can effectively reflect market volatility and trading opportunities.

  4. By adjusting parameters, it can adapt to different products and cycle markets.

Risk Analysis

The strategy also has some risks:

  1. In low volatility trends, the volatility index may generate wrong signals. Filtering with other liquidity indicators may help.

  2. In rapid one-way markets, double EMAs may lag. Shortening parameters appropriately or combining with other indicators may help.

  3. The increased complexity of multiple indicators also increases the risk of over-optimization. Comprehensive backtesting and parameter stability testing are required.

Optimization Directions

The strategy can also be optimized in the following aspects:

  1. Adding stop loss mechanisms can effectively control per order loss.

  2. Optimize parameters for different products and cycles to improve adaptability.

  3. Increasing liquidity and volatility indicators to improve signal quality.

  4. Adding machine learning algorithms to achieve dynamic parameter and weight adjustment.

Conclusion

By combining double EMAs and volatility indexes, this strategy can achieve good trading performance in both trending and volatile markets. There are also certain risks that require further optimization and improvement. But overall, the strategy idea is clear and has practical value.

Parameters

NameDefaultWhat it controls
v_input_int_114(?●═════ 2/20 EMA ═════●)Length
v_input_120(?●═════ H-L/C Histogram ═════●)Look Back
v_input_2false% change
v_input_316SMA Length
v_input_bool_1false(?●═════ MISC ═════●)Trade reverse
v_input_int_2true(?●═════ Time Start ═════●)From Day
v_input_int_3trueFrom Month
v_input_int_42005From Year

Source Pine Script

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

//@version=5
////////////////////////////////////////////////////////////
//  Copyright by HPotter v1.0 12/04/2022
// This is combo strategies for get a cumulative signal. 
//
// First strategy
// This indicator plots 2/20 exponential moving average. For the Mov 
// Avg X 2/20 Indicator, the EMA bar will be painted when the Alert criteria is met.
//
// Second strategy
//  This histogram displays (high-low)/close
//  Can be applied to any time frame.
//
//
// WARNING:
// - For purpose educate only
// - This script to change bars colors.
////////////////////////////////////////////////////////////
EMA20(Length) =>
    pos = 0.0
    xPrice = close
    xXA = ta.ema(xPrice, Length)
    nHH = math.max(high, high[1])
    nLL = math.min(low, low[1])
    nXS = nLL > xXA or nHH < xXA ? nLL : nHH
    iff_1 = nXS < close[1] ? 1 : nz(pos[1], 0)
    pos := nXS > close[1] ? -1 : iff_1
    pos


HLCH(input_barsback,input_percentorprice,input_smalength) =>
    pos = 0.0
    xPrice = (high-low)/close
    xPriceHL = (high-low)
    xPrice1 = input_percentorprice ? xPrice * 100: xPriceHL
    xPrice1SMA = ta.sma(math.abs(xPrice1), input_smalength)
    pos := xPrice1SMA[input_barsback] > math.abs(xPrice1) ? 1 :
    	     xPrice1SMA[input_barsback] < math.abs(xPrice1) ? -1 : nz(pos[1], 0)
    pos

strategy(title='Combo 2/20 EMA & (H-L)/C Histogram', shorttitle='Combo', overlay=true)
var I1 = '●═════ 2/20 EMA ═════●'
Length = input.int(14, minval=1, group=I1)
var I2 = '●═════ (H-L)/C Histogram  ═════●'
input_barsback = input(20, title="Look Back", group=I2)
input_percentorprice = input(false, title="% change", group=I2)
input_smalength = input(16, title="SMA Length", group=I2)
var misc = '●═════ MISC ═════●'
reverse = input.bool(false, title='Trade reverse', group=misc)
var timePeriodHeader = '●═════ Time Start ═════●'
d = input.int(1, title='From Day', minval=1, maxval=31, group=timePeriodHeader)
m = input.int(1, title='From Month', minval=1, maxval=12, group=timePeriodHeader)
y = input.int(2005, title='From Year', minval=0, group=timePeriodHeader)
StartTrade = time > timestamp(y, m, d, 00, 00) ? true : false
posEMA20 = EMA20(Length)
prePosHLCH = HLCH(input_barsback,input_percentorprice,input_smalength)
iff_1 = posEMA20 == -1 and prePosHLCH == -1 and StartTrade ? -1 : 0
pos = posEMA20 == 1 and prePosHLCH == 1 and StartTrade ? 1 : iff_1
iff_2 = reverse and pos == -1 ? 1 : pos
possig = reverse and pos == 1 ? -1 : iff_2
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-12-18. 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/Quantitative-Trading-Strategy-Based-on-Double-EMA-and-Price-Volatility-Index.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