0bps

Strategy vault / Mean reversion

Dual Color K line Tracking Strategy

This strategy observes the change of K-line colors to determine the market trend and establish long and short positions accordingly. The strategy principle is simple and straightforward, aiming to capture short-term…

Author
ChaoZhang
Complexity
Intermediate · 46 lines
Parameters
9
Backtest bar
2h · intraday
Tested on
BTC/USDT · spot

Summary

This strategy observes the change of K-line colors to determine the market trend and establish long and short positions accordingly. The strategy principle is simple and straightforward, aiming to capture short-term trends.

Principle

The strategy judges the color of K-lines based on the relationship between closing price and opening price. Closing price greater than opening price is a red K-line, and vice versa is a green K-line.

When there are specified consecutive K-lines of the same color ( adjustable), take actions accordingly:

  • If red, go long.

  • If green, go short.

When the K-line color changes, close all positions.

Advantages

  • The principle is clear and simple, easy to understand and implement.

  • Can track relatively short-term trends and trade frequently.

  • The number of K-lines is customizable to adjust strategy sensitivity.

  • Can go long or short only to reduce trading frequency.

  • Trading time frame can be set to avoid certain periods.

Risks

  • Unable to determine trend direction, risk of being whipsawed.

  • Unable to determine optimal entry timing, risks of premature entry or missing opportunities.

  • Risks of reversal as color changes do not necessarily represent real trend changes.

  • Chasing short-term may lead to excessive trading and commission pressure.

  • Inappropriate parameter settings may lead to poor strategy performance.

Optimization

  • Consider adding trend judgment indicators to avoid reverse entry, e.g. MACD, KD.

  • Set up trailing stop loss to reduce loss risk.

  • Relax exit criteria appropriately to avoid over-frequent exits.

  • Combine other factors to optimize entry timing, e.g. surging volume, breaking previous high.

  • Use market orders to reduce slippage impact.

Conclusion

This strategy originates from the simplest K-line technical analysis, achieving basic trend tracking by judging K-line colors. The advantages are simplicity, high trading frequency, and flexible parameter adjustment. But it also has some blindness, unable to determine the quality of the trend. It can be improved by adding trend judgment indicators while keeping it simple, thereby enhancing strategy performance.

Parameters

NameDefaultWhat it controls
v_input_1trueLong
v_input_2trueShort
v_input_32Bars Q
v_input_42018From Year
v_input_52018To Year
v_input_6trueFrom Month
v_input_712To Month
v_input_8trueFrom Day
v_input_931To Day

Source Pine Script

/*backtest
start: 2023-09-10 00:00:00
end: 2023-10-10 00:00:00
period: 2h
basePeriod: 15m
exchanges: [{"eid":"Binance","currency":"BTC_USDT"}]
*/

//2018
//Noro

//@version=2
strategy("Noro's Candles Strategy v1.0", shorttitle = "Candles str 1.0", overlay=true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100.0, pyramiding = 0)

//Settings
needlong = input(true, defval = true, title = "Long")
needshort = input(true, defval = true, title = "Short")
bq = input(2, defval = 2, minval = 2, maxval = 6, title = "Bars Q")

fromyear = input(2018, defval = 2018, minval = 1900, maxval = 2100, title = "From Year")
toyear = input(2018, defval = 2018, minval = 1900, maxval = 2100, title = "To Year")
frommonth = input(01, defval = 01, minval = 01, maxval = 12, title = "From Month")
tomonth = input(12, defval = 12, minval = 01, maxval = 12, title = "To Month")
fromday = input(01, defval = 01, minval = 01, maxval = 31, title = "From Day")
today = input(31, defval = 31, minval = 01, maxval = 31, title = "To Day")

//Bars Q
bar = close > open ? 1 : close < open ? -1 : 0
gb = bar == 1
rb = bar == -1
redbars = bq == 2 and rb and rb[1] ? 1 : bq == 3 and rb and rb[1] and rb[2] ? 1 : bq == 4 and rb and rb[1] and rb[2] and rb[3] ? 1 : bq == 5 and rb[1] and rb[2] and rb[3] and rb[4] ? 1 : bq == 6 and rb[1] and rb[2] and rb[3] and rb[4] and rb[5] ? 1 : 0
greenbars = bq == 2 and gb and gb[1] ? 1 : bq == 3 and gb and gb[1] and gb[2] ? 1 : bq == 4 and gb and gb[1] and gb[2] and gb[3] ? 1 : bq == 5 and gb[1] and gb[2] and gb[3] and gb[4] ? 1 : bq == 6 and gb[1] and gb[2] and gb[3] and gb[4] and gb[5] ? 1 : 0

//Signals
up1 = redbars == 1
dn1 = greenbars == 1
exit = bar != bar[1]

if up1
    strategy.entry("Long", strategy.long, needlong == false ? 0 : na)

if dn1
    strategy.entry("Short", strategy.short, needshort == false ? 0 : na)
    
if time > timestamp(toyear, tomonth, today, 00, 00) or exit
    strategy.close_all()

Published by ChaoZhang on the FMZ strategy square · last modified 2023-10-11. 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/Dual-Color-K-line-Tracking-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