Need help with my CCI indicator


New member
I'm a big fan of CCI indicator. Hence I took the code and change it to a CCI indicator instead. The test results look amazing. There are some coding which I don't really understand (e.g. def CCI = 1 * (ChgRatio + 1)). Any comments will be much appreciated.

#######################start code
input length = 5;
input over_Bought = 100;
input over_Sold = -100;
input price = close;
input averageType = AverageType.WILDERS;
input showBreakoutSignals = no;

def NetChgAvg = MovingAverage(averageType, price - price[1], length);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;

def CCI = 1 * (ChgRatio + 1);
def BULL = over_Sold;
def BEAR = over_Bought;
def UpSignal = if CCI crosses above BEAR then BEAR else Double.NaN;
def DownSignal = if CCI crosses below BULL then BULL else Double.NaN;

def Bearish = CCI crosses below 100;
def Bullish = CCI crosses above -100;

Alert(Bullish, ” CCI Possible SIGNAL UP ”, Alert.BAR, Sound.Ding);
Alert(Bearish, " CCI Possible SIGNAL DOWN ", Alert.BAR, Sound.Ding);

#For Orders
input TradeSize = 2;

def Ent = CCI(length)."CCI" crosses above -100 ;
def Ext = CCI(length)."CCI" crosses below 100 or CCI(length)."CCI" >= 85;

AddOrder(OrderType.BUY_TO_OPEN, Ent, close, TradeSize);
AddOrder(OrderType.SELL_TO_CLOSE, Ext, close, TradeSize);

#end code
Here's a CCI study on the upper price chart with buy/sell arrows you might like to check out

input length = 8;
input trigger = 45;
input arrowdisplacer = .00075; #Change by .001 increments to move arrows
input showallarrows = no; #show only one arrow per direction or all

def price = close + low + high;
def linDev = LinDev(price, length);
def CCI = if linDev == 0
          then 0
          else (price - Average(price, length)) / linDev / 0.015;

def Up = CCI[1] <= trigger and CCI > trigger;
def Down = CCI[1] >= -trigger and CCI < -trigger;

def count = if Up == 1 and count[1] == 0 then 1
            else if count[1] >= 1 and CCI >= -trigger then count[1] + 1
            else 0;
rec count1 = if Down == 1 and count1[1] == 0 then 1
             else if count1[1] >= 1 and CCI <= trigger then count1[1] + 1
             else 0;

plot ArrowUp = if (Up and showallarrows == no and count == 1) or (Up and showallarrows == yes)
               then low * (1 - arrowdisplacer) 
               else Double.NaN;

plot ArrowDn = if Down and count1 == 1
               then high * (1 + arrowdisplacer)
               else Double.NaN;

AddLabel(yes, "CCI: " + Round(CCI, 0) + if CCI[1] < CCI then " UP" else " DN",
    if CCI > 0 then if CCI[1] > CCI then Color.DARK_GREEN
    else Color.GREEN else if CCI[1] < CCI then Color.DARK_RED
    else  Color.RED);

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
322 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.