# Customizing my CCI script

#### mayerrs

##### New member
hey all,

i would like to make script but i see now i am not able to make it.. i actually did one, but that was pretty easy. not like thisone.. so, i am asking for the help.. what is it about..

i trade CCI. now i would like to change my trading and i would like to start use scan. so, i am looking for the trend. if i have CCI i can simple see the trend but i was wondering to try scan. it should work like this:

a) uptrend - 90 bars from 100 past bars is greater then 0 on the cci
b) downtrend - 90 bars from 100 past bars is less then 0 on the cci

would be perfect if i would be able to change these days

do you thing it is possible? thanks a lot

def Upbar = if cci > 0 then 1 else 0 ;
def Upbar1 = if cci[1] > 0 then 1 else 0 ;
def Upbar2 = if cci[2] > 0 then 1 else 0 ;
continue the above pattern 97 more times...

def TotalUPBars = Upbar + Upbar1 + Upbar2 + ;

Plot SignalUP = TotalBars>90 ;

The above code says
add 1 if the candle is greater than zero
add 1 if the previous candle is greater than zero.
add 1 if the candle from 2 ago is greater than zero. So on and so on....
After you have defined current bar through 100 bars past.
Then TotalBars adds current bar through 100 bars past.
Then in the scanner you scan for SignalUp is True which is defined as at least 90 bars are greater than 0.

When you are ALL done with that, copy and paste ALL the 100 lines of code and change every > to < and change every UP to DOWN to define your downtrend

This is the low tech way cause I am a low tech sort-of coder. If you have a programming background, you might be able to use the function 'fold' to skip manually writing out the definition of 200 bars.
HTH

Last edited:
thank you, i try it

once again..

there should be some parametr - CCI 14

where should i write this?

@mayerrs Once you get proficient at coding you could probably accomplish this same task with a loop using fold and a counter state variable... Food for thought...

@mayerrs The above post was only the syntax for what you requested. You stated that you are trading with the CCI indicator. If you are using a custom study the above script would be typed into the bottom of the study. If you are trading w/ the TOS CCI indicator then your 1st lines in the new custom study that you are creating are:
Ruby:
``````#
# TD Ameritrade IP Company, Inc. (c) 2017-2020
#

declare lower;

input length = 14;
input over_sold = -100;
input over_bought = 100;
input showBreakoutSignals = no;

def price = close + low + high;
def linDev = lindev(price, length);
plot CCI = if linDev == 0 then 0 else (price - Average(price, length)) / linDev / 0.015;
plot OverBought = over_bought;
plot ZeroLine = 0;
plot OverSold = over_sold;``````

Then type in the script as explained above...
HTH

Another approach, perhaps a little shorter to implement. Per your first post, and assuming that you don't care where in the 100 bars the 90 up bars come...
Code:
``````input length = 100;
input threshold = 90;

def val = CCI(length = 14);
def up_series = if val > 0 then 1 else 0;
def dn_series = if val < 0 then 1 else 0;

plot uptrend = if sum(up_series, length) > threshold then 1 else double.nan;
plot downtrend = if sum(dn_series, length) > threshold then 1 else double.nan;``````

comment out (using #) one of the two plots or the scanner will complain about expecting exactly one plot.

-mashume

Another approach, perhaps a little shorter to implement. Per your first post, and assuming that you don't care where in the 100 bars the 90 up bars come...
Code:
``````input length = 100;
input threshold = 90;

def val = CCI(length = 14);
def up_series = if val > 0 then 1 else 0;
def dn_series = if val < 0 then 1 else 0;

plot uptrend = if sum(up_series, length) > threshold then 1 else double.nan;
plot downtrend = if sum(dn_series, length) > threshold then 1 else double.nan;``````

comment out (using #) one of the two plots or the scanner will complain about expecting exactly one plot.

-mashume
looks also great.. and what about when i would prefer last 100bars and no matter where 90bars? or your code is about last 100bars?

looks also great.. and what about when i would prefer last 100bars and no matter where 90bars? or your code is about last 100bars?
maybe better idea.. the best way would be to set offset like -1 or -2 and so on..

tried it and i guess it works pretty well

thank you!!

Last edited:
looks also great.. and what about when i would prefer last 100bars and no matter where 90bars? or your code is about last 100bars?
This one creates an array of 1 and 0 and simply adds them up. It does not care where in the last 100 bars the 90 are. If the latest 10 are all zero, it still passes the test.

To make sure your trend is strong, you could look at something like
Code:
``````plot uptrend = if sum(up_series, length) > threshold
AND sum(up_series, floor(length / 10)) > (threshold / 10)
then 1 else double.nan;``````
That would find stocks who had positive CCI for 90 of the last 100 and 9 of the last 10. the 'floor' function makes sure you have an integer length for the sum function. Of course you can adjust values as you see fit. 10 and 5 would give you 90 of the last 100 and 4.5 of the last 10, so 5 would pass but 4 would not.
-mashume

This one creates an array of 1 and 0 and simply adds them up. It does not care where in the last 100 bars the 90 are. If the latest 10 are all zero, it still passes the test.

To make sure your trend is strong, you could look at something like
Code:
``````plot uptrend = if sum(up_series, length) > threshold
AND sum(up_series, floor(length / 10)) > (threshold / 10)
then 1 else double.nan;``````
That would find stocks who had positive CCI for 90 of the last 100 and 9 of the last 10. the 'floor' function makes sure you have an integer length for the sum function. Of course you can adjust values as you see fit. 10 and 5 would give you 90 of the last 100 and 4.5 of the last 10, so 5 would pass but 4 would not.
-mashume
thank you, it works really well..

can i ask you for one less study?

i would like to scan High and Low.. i am wondering to scan on the weekly chart and there should be script the last bar is crossing H/L 50weeks or 100weeks and so on - just what i need to.

could you help me also with this?

@mayerrs You need to post your request in the thread for the Support-Resistance study you are referencing, you need to provide screenshots so we can see what you are asking. Here are some guidelines to follow that will help @mashume and everyone else understand what you are requesting.
HTH

@mashume hi,

i would like to asky you to help with this:

if i use your script for threshold for example 90 and than 80 and than 70 and 75, i always have different stocks. i was expecting when i go threshold 70, i will get all up to 70 (70, 71, 72...100). but it seems i always get the "threshold number" not ">threshold". i try to do it but i do not know how.

and what i am wondering about is to set the script to find out last x bars is less than 0 or something like this

thank you

87k+ Posts
269 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?