CCI MTF Ob+Os

Author: ChaoZhang, Date: 2022-05-16 17:07:33
Tags: CCI

Hello Traders,

This is a simple Commodity Channel Index ( CCI ) indicator with multi-timeframe (MTF) overbought and oversold level.

It can detect overbought and oversold level up to 5 timeframes, which help traders spot potential reversal point more easily.

There are options to select 1-5 timeframes to detect overbought and oversold.

Green Background is “Oversold” , looking for “Long”. Red Background is “Overbought” , looking for “Short”.

Have fun :)

backtest

img


/*backtest
start: 2022-04-15 00:00:00
end: 2022-05-14 23:59:00
period: 12h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © thakon33
//    __  __        __             ____ ____
//   / /_/ /  ___ _/ /_____  ___  |_  /|_  /
//  / __/ _ \/ _ `/  '_/ _ \/ _ \_/_ <_/_ < 
//  \__/_//_/\_,_/_/\_\\___/_//_/____/____/ 

//@version=5
indicator("CCI MTF Ob+Os")

//------------------------------------------------------------------------------
// Input
var g_cci       = "[ CCI SETTING ]"
cciSrc          = input(title="Source", defval=hlc3, group=g_cci)
cciLength       = input.int(title="Length",     defval=20,   minval=1, group=g_cci)
cciOverbought   = input.int(title="Overbought", defval=100,  step=10,  group=g_cci)
cciOversold     = input.int(title="Oversold",   defval=-100, step=10,  group=g_cci)


var g_tf        = "[ SELECT TIMEFRAME ]"
cciTf1          = input.timeframe(title="Timeframe 1", defval="15",  group=g_tf, inline="tf1")
cciTf2          = input.timeframe(title="Timeframe 2", defval="30",  group=g_tf, inline="tf2")
cciTf3          = input.timeframe(title="Timeframe 3", defval="60",  group=g_tf, inline="tf3")
cciTf4          = input.timeframe(title="Timeframe 4", defval="120", group=g_tf, inline="tf4")
cciTf5          = input.timeframe(title="Timeframe 5", defval="240", group=g_tf, inline="tf5")
cciTf1_E        = input.bool(title="", defval=true, group=g_tf, inline="tf1")
cciTf2_E        = input.bool(title="", defval=true, group=g_tf, inline="tf2")
cciTf3_E        = input.bool(title="", defval=true, group=g_tf, inline="tf3")
cciTf4_E        = input.bool(title="", defval=true, group=g_tf, inline="tf4")
cciTf5_E        = input.bool(title="", defval=true, group=g_tf, inline="tf5")


//------------------------------------------------------------------------------
// Calculate CCI

Fsec(Sym, Tf, Exp) =>
    request.security(Sym, Tf, Exp[barstate.isrealtime ? 1 : 0], barmerge.gaps_off, barmerge.lookahead_off) [barstate.isrealtime ? 0 : 1]

cci1            = Fsec(syminfo.tickerid, cciTf1, ta.cci(cciSrc, cciLength))
cci2            = Fsec(syminfo.tickerid, cciTf2, ta.cci(cciSrc, cciLength))
cci3            = Fsec(syminfo.tickerid, cciTf3, ta.cci(cciSrc, cciLength))
cci4            = Fsec(syminfo.tickerid, cciTf4, ta.cci(cciSrc, cciLength))
cci5            = Fsec(syminfo.tickerid, cciTf5, ta.cci(cciSrc, cciLength))


//------------------------------------------------------------------------------
// CCI Overbought and Oversold detect

cci1_Ob         = not cciTf1_E or cci1 >= cciOverbought 
cci2_Ob         = not cciTf2_E or cci2 >= cciOverbought
cci3_Ob         = not cciTf3_E or cci3 >= cciOverbought
cci4_Ob         = not cciTf4_E or cci4 >= cciOverbought
cci5_Ob         = not cciTf5_E or cci5 >= cciOverbought

cci1_Os         = not cciTf1_E or cci1 <= cciOversold
cci2_Os         = not cciTf2_E or cci2 <= cciOversold
cci3_Os         = not cciTf3_E or cci3 <= cciOversold
cci4_Os         = not cciTf4_E or cci4 <= cciOversold
cci5_Os         = not cciTf5_E or cci5 <= cciOversold

cciOb           = cci1_Ob and cci2_Ob and cci3_Ob and cci4_Ob and cci5_Ob
cciOs           = cci1_Os and cci2_Os and cci3_Os and cci4_Os and cci5_Os


//------------------------------------------------------------------------------
// Drawing on chart
plot    (cciTf1_E ? cci1 : na, title="TF 1",            color=color.rgb(255, 205, 22, 20),  linewidth=1)
plot    (cciTf2_E ? cci2 : na, title="TF 2",            color=color.rgb(255, 22, 239, 20),  linewidth=1)
plot    (cciTf3_E ? cci3 : na, title="TF 3",            color=color.rgb(38, 22, 255, 0),    linewidth=1)
plot    (cciTf4_E ? cci4 : na, title="TF 4",            color=color.rgb(123, 253, 22, 20),  linewidth=1)
plot    (cciTf5_E ? cci5 : na, title="TF 5",            color=color.rgb(255, 255, 255, 50), linewidth=1)
hline   (cciOverbought,        title="CCI Overbought",  color=color.new(color.white, 0),   linestyle=hline.style_dashed, linewidth=1)
hline   (cciOversold,          title="CCI Overbought",  color=color.new(color.white, 0),   linestyle=hline.style_dashed, linewidth=1)

bgcolor (cciOb ? color.new(color.red, 0)  : na, title="Overbought")
bgcolor (cciOs ? color.new(color.lime, 0) : na, title="Oversold")


//------------------------------------------------------------------------------
// Alert
alertcondition(cciOb, title="CCI Overbought", message="CCI Overbought for {{ticker}} - Price = {{close}}")
alertcondition(cciOs, title="CCI Oversold",   message="CCI Oversold for {{ticker}} - Price = {{close}}")


//==============================================================================
//==============================================================================

if cciOb
    strategy.entry("Enter Long", strategy.long)
else if cciOs
    strategy.entry("Enter Short", strategy.short)

Related

More