Strategy · Pine Script v5 · Own pane

CCI Level Zone

by hasnocool· code credited to Naruephat

This strategy plots the Commodity Channel Index of the OHLC4 price in a separate pane and uses fixed CCI zones to allow long or short trades.

View on GitHub

GitHub stars106on the repository
Pine versionv5runs as is
Lines of code117
Settings22inputs
LicenceGPL-3.0
Last update2 years ago18 Sep 2024

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

117 lines of Pine Script v5 from CCI Level Zone.pine, published under GPL-3.0. Keep the author's credit and licence lines when you reuse it.

CCI Level Zone.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Naruephat
//@version=5
strategy("CCI Level Zone", overlay= false  ,pyramiding = 0,  default_qty_type = strategy.cash,initial_capital = 100, default_qty_value =100 , commission_value=0.1)
use_sma = input.bool( false ,  'use sma( cci , len ) ' ,inline = 'u' )
len_sma = input.int( 200 , 'len' ,inline = 'u' )
GL = '<<<<[ Long ]>>>>>'
trad_Long = input.bool(true  ,  'Open Trad Long' ,inline = 'T' , group = GL)
level_CCI_ZON_L = input.int(30 , 'level_CCI_ZON' , step = 10,inline = 'CR', group = GL)
len_RSI_L = input.int(9 , 'len RSI Long' ,inline = 'CR', group = GL)
level_RSI_LongB = input.int(54 , 'cross B' ,inline = 'L', group = GL)
level_RSI_LongS = input.int(28 , 'cross S' ,inline = 'L', group = GL)
cross_invert_L = input.bool(true , 'invert' , inline  =   'L' , group = GL)
GS = '<<<<[ Short ]>>>>>'
trad_Short = input.bool(true ,  'Open Trad Short' ,inline = 'T' , group = GS)
level_CCI_ZON_S = input.int(-70 , 'level_CCI_ZON' , step = 10,inline = 'CR', group = GS)
len_RSI_S = input.int(13 , 'len RSI Long',inline = 'CR', group = GS)
level_RSI_ShortB = input.int(51 , 'cross B' , inline  =   'S' , group = GS)
level_RSI_ShortS = input.int(39 , 'cross S' , inline  =   'S' , group = GS)
cross_invert_S = input.bool(false , 'invert' , inline  =   'S' , group = GS)
/////////////////////////////////////////////////////////[  Set Test Date  ]///////////////////////////////////////////////////////////////////////////////////////////////////// 
G0 = 'Set Test Date'
i_dateFilter    = input.bool(false,  "═════ Date Range Filtering ═════" ,group = G0)
i_fromYear      = input.int(2020,   "From Year ",    minval = 1900 ,inline = 'd1',group = G0)
i_fromMonth     = input.int(1,      "From Month",   minval = 1, maxval = 12,inline = 'd2',group = G0)
i_fromDay       = input.int(15,      "From Day ",     minval = 1, maxval = 31,inline = 'd3',group = G0)
i_toYear        = input.int(2023,   "To Year ",      minval = 1900,inline = 'd1',group = G0)
i_toMonth       = input.int(12,      "To Month",     minval = 1, maxval = 12,inline = 'd2',group = G0)
i_toDay         = input.int(31,      "To Day ",       minval = 1, maxval = 31,inline = 'd3',group = G0)
fromDate        = timestamp(i_fromYear, i_fromMonth, i_fromDay, 00, 00)
toDate          = time < timestamp(i_toYear, i_toMonth, i_toDay, 23, 59) ? time :  timestamp(i_toYear, i_toMonth, i_toDay, 23, 59)
// เอา f_tradeDateIsAllowed() ไปใช้
f_tradeDateIsAllowed() => not i_dateFilter or (time >= fromDate and time <= toDate)
Win_Loss_Show = input.bool(true,'Show win loss', inline = 's')
// indicator 
cci = ta.cci(ohlc4 , 1000)
sma_CCi = ta.sma(cci,len_sma)
rsi_Long = ta.rsi(close,len_RSI_L)
rsi_Short = ta.rsi(close,len_RSI_S)
sto = ta.stoch(close, high, low, 14)
// condition zone
zoneB_Long = cci > level_CCI_ZON_L and ( use_sma ?  cci > sma_CCi : true ) 
zoneB_Short = cci < level_CCI_ZON_S  and ( use_sma ?  cci < sma_CCi : true )  
/////////////////////////////////////////////////[ condition Long  ]/////////////////////////////////////////////////
Con_Long       = false
Con_close_Long = false
if cross_invert_L
    Con_Long        :=  zoneB_Long and ta.crossunder(rsi_Long , level_RSI_LongB)
    Con_close_Long  := ta.crossover(rsi_Long , level_RSI_LongS)
else
    Con_Long        :=  zoneB_Long and ta.crossover(rsi_Long , level_RSI_LongB)
    Con_close_Long  := ta.crossunder(rsi_Long , level_RSI_LongS)
    
// If conditions pass, remember this first. Find the opportunity to open the next order with stoch.
var con_L_perss = 0
if Con_Long and con_L_perss == 0
    con_L_perss := 1
// clear con_L_perss
if strategy.position_avg_price != 0 or  Con_close_Long
    con_L_perss := 0 
// END Condition Long
Open_Long = con_L_perss == 1 and ta.crossover(sto ,20)
/////////////////////////////////////////////////[ condition Short  ]/////////////////////////////////////////////////
Con_Short       = false
Con_close_Short = false
if cross_invert_S
    Con_Short       := zoneB_Short and  ta.crossover(rsi_Short , level_RSI_ShortB)
    Con_close_Short :=  ta.crossunder(rsi_Short , level_RSI_ShortS)
else
    Con_Short       := zoneB_Short and  ta.crossunder(rsi_Short , level_RSI_ShortB)
    Con_close_Short :=  ta.crossover(rsi_Short , level_RSI_ShortS)
// If conditions pass, remember this first. Find the opportunity to open the next order with stoch.
var con_S_perss = 0
if Con_Short and con_S_perss == 0
    con_S_perss := 1
//  clear con_S_perss
if strategy.position_avg_price != 0 or  Con_close_Short
    con_S_perss := 0 
// END Condition
Open_Short = con_S_perss == 1 and ta.crossunder(sto, 65)
///////////////////////////////////////////////////////////////////////////////////////////////[ 
// this chang Alert message for u
textAlert_Long          = 'textAlert Long '
textAlert_close_Long    = 'textAlert Close Long '
textAlert_Short         = 'textAlert_Short'
textAlert_Close_Short   = 'textAlert_Close_Short'
if trad_Long
    strategy.entry('L' ,strategy.long , when =f_tradeDateIsAllowed()  and  Open_Long , alert_message = textAlert_Long )
    strategy.close('L' ,when = Con_close_Long , alert_message = textAlert_Long)
    
if trad_Short
    strategy.entry('S', strategy.short,when =f_tradeDateIsAllowed() and  Con_Short , alert_message = textAlert_Short)
    strategy.close('S' ,when = Con_close_Short , alert_message = textAlert_Close_Short )
plot(cci , 'line cci', color = zoneB_Short ? color.purple :  zoneB_Long ? color.green : color.new(color.gray, 50))
plot(use_sma ? sma_CCi : na, 'line hma CCi ', color = zoneB_Short ? color.purple :  zoneB_Long ? color.green : color.new(color.gray, 50))
hline(350 ,'line level'   , linestyle = hline.style_dotted ,color = color.new(color.green  , 65 ))
hline(200 ,'line level'   , linestyle = hline.style_dotted ,color = color.new(color.green  , 65 ))
hline(100 ,'line level'   , linestyle = hline.style_dotted ,color = color.new(color.green  , 65 ))
hline(0 ,'line level'     , linestyle = hline.style_dotted ,color = color.new(color.white , 50 ))
hline(-350 ,'line level'  , linestyle = hline.style_dotted ,color = color.new(color.red  ,65 ))
hline(-200 ,'line level'  , linestyle = hline.style_dotted ,color = color.new(color.red  ,65 ))
hline(-100 ,'line level'  , linestyle = hline.style_dotted ,color = color.new(color.red  ,65 ))
/////////////////////////////////////////////////////////[  Plot  ]///////////////////////////////////////////////////////////////////////////////////////////////////// 
var buy= 0.   
var sell  = 0. 
if(ta.change(strategy.position_size) and  strategy.position_size == 0)
    
    buy := nz(strategy.closedtrades.entry_price(strategy.closedtrades-1))
    sell  := nz(strategy.closedtrades.exit_price(strategy.closedtrades-1))
var buy_SELL = 2
buy_SELL := strategy.position_size > 0 ? 0 : strategy.position_size < 0 ? 1 : 2
//plot(buy_SELL[1])
plotchar(  Win_Loss_Show ? nz(buy_SELL[1]) == 1 and ta.change(strategy.position_size) and  strategy.position_size == 0 and buy > sell : na ,location  = location.top ,color = color.new(color.green, 0), char='☺'  , size  = size.small)     
plotchar(  Win_Loss_Show ? nz(buy_SELL[1]) == 1 and ta.change(strategy.position_size) and  strategy.position_size == 0 and buy < sell : na ,location  = location.top ,  color = color.new(color.red, 0), char='☹'  , size  = size.tiny)   
plotchar(  Win_Loss_Show ? nz(buy_SELL[1]) ==0 and ta.change(strategy.position_size) and  strategy.position_size == 0 and buy < sell : na ,location  = location.top ,color = color.new(color.green, 0), char='☺'  , size  = size.small)     
plotchar(  Win_Loss_Show ? nz(buy_SELL[1]) ==0 and ta.change(strategy.position_size) and  strategy.position_size == 0 and buy > sell : na ,location  = location.top ,  color = color.new(color.red, 0), char='☹'  , size  = size.tiny)   
Expand (162 lines)

Overview

Longs open when the CCI is above its long level, optionally above an SMA of the CCI, and the long RSI crosses a buy level; they close when that RSI crosses a sell level. Shorts use the opposite CCI zone and a separate RSI. Invert switches can swap those crosses. A date filter and a win-loss display can be enabled. There is no stop loss.

Details

Published by

hasnocool on GitHub

Code credit

Naruephat, as written in the script's header

Type

Strategy, Pine Script v5

Draws

Separate pane below the chart

Category

Oscillators & momentum

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • The strategy enters trades without a stop loss

Topics

analysisbtcusdtcryptocurrencydatapandaspythonsimulationtradingtradingview

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
use sma( cci , len ) boolfalseuse_sma
lenint200len_sma
Open Trad Longbooltruetrad_Long
level_CCI_ZONint30level_CCI_ZON_L
len RSI Longint9len_RSI_L
cross Bint54level_RSI_LongB
cross Sint28level_RSI_LongS
invertbooltruecross_invert_L
Open Trad Shortbooltruetrad_Short
level_CCI_ZONint-70level_CCI_ZON_S
len RSI Longint13len_RSI_S
cross Bint51level_RSI_ShortB
Show all 22 settings
SettingTypeDefaultVariable
cross Sint39level_RSI_ShortS
invertboolfalsecross_invert_S
═════ Date Range Filtering ═════boolfalsei_dateFilter
From Year int2020i_fromYear
From Monthint1i_fromMonth
From Day int15i_fromDay
To Year int2023i_toYear
To Monthint12i_toMonth
To Day int31i_toDay
Show win lossbooltrueWin_Loss_Show
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

330 stars

MACD + Divergences [Alorse] is a momentum strategy that calculates the MACD oscillator and identifies regular and hidden divergences using pivot high and low points. It filters trade signals with dual Exponential Moving Averages (EMA 150 and 600) to ensure tra

Strategy v4 Oscillators & momentumDivergence Repaints
Updated 2 months ago · None stated GitHub repository of MACD + Divergences [Alorse]

Q-Trend + KVO Strategy

by TradersPost
218 stars

This strategy combines the ATR-banded Q-Trend indicator with the Klinger Volume Oscillator (KVO) to execute confluence trades. It enters long when a Q-Trend buy trigger aligns with a positive KVO value and a bullish candle confirmation, taking short trades und

Strategy v6 TrendVolume
Updated 3 months ago · None stated GitHub repository of Q-Trend + KVO Strategy

MACD Crossover (tutorial)

by pineforge-4pass
193 stars

This strategy calculates the Moving Average Convergence Divergence indicator and plots the MACD line, signal line, and histogram in a separate pane. It enters long positions when the MACD line crosses above the signal line and enters short positions when it cr

Strategy v6 Oscillators & momentumMoving averages
Updated today · Apache-2.0 GitHub repository of MACD Crossover (tutorial)
114 stars

This strategy estimates price behavior using ordinary least squares regression between higher-timeframe data and a generated dataset, smoothed by a Karobein oscillator. It enters long positions when the oscillator turns upward and short positions when it turns

Strategy v4 Oscillators & momentumAI / machine learning Repaints
65 stars

This strategy calculates a 20-period Relative Strength Index (RSI) and generates entry orders based on crossovers and crossunders of the 50 level. When RSI crosses above 50, it places a long stop entry at the current high price with a custom alert message. Whe

Strategy v4 Oscillators & momentum
Updated 4 days ago · GPL-3.0 GitHub repository of Strategy Using Alert Message

BTC Futures MACD

by iamrichardD
57 stars

This strategy plots a MACD oscillator in a separate pane and trades Bitcoin futures from zero-line and signal-line crosses. Optional early momentum signals, a volume filter, and a trend moving-average filter can confirm entries. It sizes positions as fixed con

Strategy v6 Oscillators & momentumMoving averages Repaints
Updated 1 year ago · Apache-2.0 GitHub repository of BTC Futures MACD

More strategys

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers