Trouble Calculating CCI Slope

TimTee

New member
I am trying to determine in the last hour of trading, if the slope of the CCI indicator is going Up or Down. I wanted "Slope UP" or "Slope Down" to show-up on a bubble in the last hour. I thought I had it until I looked at the actual CCI and saw that my calculation was hit or miss on being accurate. So I added some extra lines so that I could try to troubleshoot. I have verified that the numbers I'm using in the up/down calculation match what is in the chart, but the calculation doesn't work. Below is the code I've been working on. Any ideas what I am overlooking? Thanks.


declare lower;

input cciLength = 20;
input cciAvgLength = 15;
input price = close;
input length = 3;
input displace = 0;


def AvgExp = (ExpAverage(price[-displace], length));


plot CCI = CCI(length = cciLength);
plot CCIAvg = Average(CCI, cciAvgLength);
CCI.SetDefaultColor(GetColor(9));
CCIAvg.SetDefaultColor(GetColor(8));


def CCISlope = if CCI > CCI[-1] then 1 else 0 ; # 1 is UP, 0 is DOWN

AddChartBubble(SecondsFromTime(1500) >= 0 and SecondsFromTime(1500) <= 59 , high, if CCISLope == 1 then "CCI - Slope UP" else "CCI - Slope DOWN", if CCISlope == 1 then Color.GREEN else Color.RED);

#For testing/verification
AddChartBubble(SecondsFromTime(1500) >= 0 and SecondsFromTime(1500) <= 59 , high, "CCI Fast: " + CCI, Color.GREEN);
AddChartBubble(SecondsFromTime(1500) >= 0 and SecondsFromTime(1500) <= 59, high, "Previous CCI Fast: " + CCI[1], Color.GREEN);



cci-example.jpg
 
Last edited:
Solution
I am trying to determine in the last hour of trading, if the slope of the CCI indicator is going Up or Down. I wanted "Slope UP" or "Slope Down" to show-up on a bubble in the last hour. I thought I had it until I looked at the actual CCI and saw that my calculation was hit or miss on being accurate. So I added some extra lines so that I could try to troubleshoot. I have verified that the numbers I'm using in the up/down calculation match what is in the chart, but the calculation doesn't work. Below is the code I've been working on. Any ideas what I am overlooking? Thanks.


declare lower;

input cciLength = 20;
input cciAvgLength = 15;
input price = close;
input length = 3;
input displace = 0;


def AvgExp =...
I am trying to determine in the last hour of trading, if the slope of the CCI indicator is going Up or Down. I wanted "Slope UP" or "Slope Down" to show-up on a bubble in the last hour. I thought I had it until I looked at the actual CCI and saw that my calculation was hit or miss on being accurate. So I added some extra lines so that I could try to troubleshoot. I have verified that the numbers I'm using in the up/down calculation match what is in the chart, but the calculation doesn't work. Below is the code I've been working on. Any ideas what I am overlooking? Thanks.


declare lower;

input cciLength = 20;
input cciAvgLength = 15;
input price = close;
input length = 3;
input displace = 0;


def AvgExp = (ExpAverage(price[-displace], length));


plot CCI = CCI(length = cciLength);
plot CCIAvg = Average(CCI, cciAvgLength);
CCI.SetDefaultColor(GetColor(9));
CCIAvg.SetDefaultColor(GetColor(8));


def CCISlope = if CCI > CCI[-1] then 1 else 0 ; # 1 is UP, 0 is DOWN

AddChartBubble(SecondsFromTime(1500) >= 0 and SecondsFromTime(1500) <= 59 , high, if CCISLope == 1 then "CCI - Slope UP" else "CCI - Slope DOWN", if CCISlope == 1 then Color.GREEN else Color.RED);

#For testing/verification
AddChartBubble(SecondsFromTime(1500) >= 0 and SecondsFromTime(1500) <= 59 , high, "CCI Fast: " + CCI, Color.GREEN);
AddChartBubble(SecondsFromTime(1500) >= 0 and SecondsFromTime(1500) <= 59, high, "Previous CCI Fast: " + CCI[1], Color.GREEN);



cci-example.jpg

Here is a minor modification (See bold area) that I think will then produce what you are looking to get. Your orignial code, CCI[-1], referenced a future rather than a previous bar's CCI.

Code:
def CCISlope = if CCI > CCI[1] then 1 else 0 ; # 1 is UP, 0 is DOWN

Here is the full code with all modifications with testing bubbles not displayed.
Capture.jpg
Ruby:
declare lower;

input cciLength = 20;
input cciAvgLength = 15;
input price = close;
input length = 3;
input displace = 0;


def AvgExp = (ExpAverage(price[-displace], length));


plot CCI = CCI(length = cciLength);
plot CCIAvg = Average(CCI, cciAvgLength);
CCI.SetDefaultColor(GetColor(9));
CCIAvg.SetDefaultColor(GetColor(8));


def CCISlope = if CCI > CCI[1] then 1 else 0 ; # 1 is UP, 0 is DOWN

AddChartBubble(SecondsFromTime(1500) >= 0 and SecondsFromTime(1559) <= 0 , cci, if CCISLope == 1 then
#"CCI - Slope
"UP" else
#"CCI - Slope
"DOWN", if CCISlope == 1 then Color.GREEN else Color.RED);

#For testing/verification
input testing = yes;
AddChartBubble(testing and SecondsFromTime(1500) >= 0 and SecondsFromTime(1559) <= 0 ,cci, "CCI Fast: " + CCI, Color.GREEN);
AddChartBubble(testing and SecondsFromTime(1500) >= 0 and SecondsFromTime(1559) <= 0, cci, "Previous CCI Fast: " + CCI[1], Color.GREEN);
 
Solution

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

SleepyZ - Thanks so much for the help. I can't believe it was such a small change. I don't want to think how much time I spent trying to figure that out before I made the post. Thanks again!
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
366 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.
Back
Top