0bps

Strategy vault / Machine learning

Percent Change Bar Chart Backtest Strategy

This strategy calculates the percentage change of the current bar's closing price relative to the closing price N bars ago, and displays different colored histogram bars to determine the trend. It uses trend lines to…

Author
ChaoZhang
Complexity
Starter · 42 lines
Parameters
6
Backtest bar
1d · swing
Tested on
BTC/USDT · futures

IMG

Overview

This strategy calculates the percentage change of the current bar's closing price relative to the closing price N bars ago, and displays different colored histogram bars to determine the trend. It uses trend lines to determine entries and exits.

Strategy Logic

  1. Set strategy parameters via input, including bar width, display price change or percentage change, lookback period, buy/sell thresholds etc.

  2. Calculate the price difference or percentage difference between the current bar's closing price and the closing price N bars ago.

  3. Set buy and sell threshold lines.

  4. Display histogram bars in different colors based on percentage change.

  5. Set to long when percentage change is greater than buy threshold, and set to short when lower than sell threshold.

  6. Color histogram bars according to position direction.

  7. Enter and exit based on position direction.

Advantages

  1. Intuitive display of price change trends for decision making.

  2. Clear entry and exit signals in combination with trend indicators.

  3. Parameters can be optimized for different products and timeframes.

  4. Simple and clear logic, easy to understand and modify.

  5. Good visualization for quick trend judgment.

Risks

  1. Prone to false signals, improper entry selection may lead to losses.

  2. Parameters need adjustment for high volatility products, otherwise increasing loss probability.

  3. Unaccounted for sudden events like significant bearish news.

  4. Short backtest period may be unable to determine robustness of parameters.

  5. Missing reversal opportunities without stop time.

Risks can be controlled via parameter optimization, signal filtering with other indicators, stop loss, expanding backtest period etc.

Optimization Directions

  1. Consider combining with other indicators like trend and volatility indicators to confirm signals.

  2. Introduce machine learning algorithms to optimize parameter settings.

  3. Set dynamic stop loss to control single loss amount.

  4. Incorporate sentiment indicators and news to avoid sudden event impacts.

  5. Add trading time or session filters.

  6. Optimize backtest period with longer timeframe.

Summary

This strategy displays price change percentage in real-time with histogram bars and uses trendlines for decision making, forming clear trading signals. The logic is simple for easy operation. But risks exist and need to be controlled via optimization, filtering, stop loss etc. With continuous optimizations, it can become an easy to grasp and practical trend following strategy.

Parameters

NameDefaultWhat it controls
v_input_14Bar Width
v_input_2falsePrice Change
v_input_3trueLook Back
v_input_4-0.33SellZone
v_input_50.33BuyZone
v_input_6falseTrade reverse

Source Pine Script

/*backtest
start: 2022-11-08 00:00:00
end: 2023-11-14 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=2
////////////////////////////////////////////////////////////
//  Copyright by HPotter v3.0 27/07/2018
//
//  This histogram displays price or % change from previous bar. 
//
// You can change long to short in the Input Settings
// WARNING:
// - For purpose educate only
// - This script to change bars colors.
////////////////////////////////////////////////////////////
strategy(title="Percent change bar chart Backtest", precision = 2)
input_barwidth = input(4, title="Bar Width")
input_percentorprice = input(false, title="Price Change")
input_barsback = input(1, title="Look Back")
SellZone = input(-0.33, minval=0.01, step = 0.01)
BuyZone = input(0.33, minval=0.01, step = 0.01)
reverse = input(false, title="Trade reverse")
hline(0, color=blue, linestyle=line)
hline(BuyZone, color=green, linestyle=line)
hline(SellZone, color=red, linestyle=line)
xPrice = close
xPrice1 = iff(input_percentorprice, xPrice - xPrice[input_barsback], ((xPrice - xPrice[input_barsback]) * 100)/ xPrice[input_barsback])
colorg = iff(xPrice1 < 0, red, green)
pos = iff(xPrice1 > BuyZone, 1,
       iff(xPrice1 < SellZone, -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)	   	    
barcolor(possig == -1 ? red: possig == 1 ? green : blue ) 
plot(xPrice1, color=colorg, style = histogram, linewidth = input_barwidth, title="Change")

Published by ChaoZhang on the FMZ strategy square · last modified 2023-11-15. 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/Percent-Change-Bar-Chart-Backtest-Strategy.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