kenlsk 5 posts msg #149886 - Ignore kenlsk modified |
12/14/2019 11:24:28 AM
Hi,
I'm trying to define my own Commodity Channel Index (CCI) for 14 days and constant of 0.03 instead of 0.015 based on the calculation given in this website:
https://school.stockcharts.com/doku.php?id=technical_indicators:commodity_channel_index_cci#calculation
Below is my code in stockfetcher:
----------------------------------------------------------------------
/*Derivation of CCI(14,0.03)*/
set{sum_high_low,high+low}
set{sum_range_close,sum_high_low+close}
set{typical_p,sum_range_close/3}
set{avg_p,sum(typical_p,14)/14}
set{fix_avg_p,DATE(20191212,avg_p)}
set{sub,typical_p-fix_avg_p}
set{ab_sub,abs(sub)}
set{mad,sum(ab_sub,3)}
set{nom,typical_p-ma(14)}
set{denom,0.03*mad}
set{cci14_0.03,nom/denom}
add column cci14_0.03
----------------------------------------------------------------------
I'm trying to make the variable fix_avg_p as a constant, so that when I offset this variable for several days (e.g. fix_avg_p 1 day ago, fix_avg_p 2 day ago, etc.), I will still get the same number. I attempted the function date(YYYYMMDD,indicator) to fix the date on 12 Dec 2019. However, I can't get the user defined CCI as expected.
How can I improve my code to implement this user defined indicator to my stockfetcher filter?
Your feedback is much appreciated.
Thanks.
Regards,
Kiem
|
xarlor 585 posts msg #149887 - Ignore xarlor |
12/14/2019 1:32:18 PM
kenlsk, your fix_avg_p is indeed a constant. The problem you are likely encountering is when you put fix_avg_p 2 days ago with date 20191212, it breaks. You can see with the add columns below. Note the fix_avg_p columns.
DATE 12/12/19 (2 days ago from today (12/14/19):
DATE 12/10/19, 4 days ago from today (12/14/19):
|
kenlsk 5 posts msg #149890 - Ignore kenlsk |
12/15/2019 1:40:26 AM
Hi xarlor,
Yes. This is exactly the issue that I had encountered. So I'm wondering whether anyone here have any workaround code for such scenario?
Thanks.
Regards,
Kiem
|
nibor100 1,042 posts msg #149892 - Ignore nibor100 |
12/15/2019 10:55:32 AM
@ kenlsk,
1. The simplest solution to getting an SF CCI based on 14 days and .03 instead of .015 is to divide the normal SF CCI(14) by 2
(this works because to increase the standard multiplier of .015 to .03 just requires the denominator to be multiplied by 2)
example filter:
2. However, when I look at the code you provided in your initial post you are doing a few thing different in your code than in the stock charts CCI calc example you provided.
a. this line
set{fix_avg_p,DATE(20191212,avg_p)}
based on the date of your original post is taking the typical price from 2 days ago instead of the current day's typical price. Is this on purpose?
b. this line
set{mad,sum(ab_sub,3)}
seems to be taking a 3 day sum of the absolute values instead of a 14 day sum?
c. this line
set{nom,typical_p-ma(14)}
seems to be setting the numerator to be a regular 14 day simple moving average of the closing prices subtracted from today's typical price instead of subtracting the 14 day simple moving average of typical price(or your variable "avg_p"?
d. which of course means that this line
set{denom,0.03*mad}
is setting the denominator to be .03 times your variable "mad" which is only a 3 day sum of the abosolute values???
Clearly if you really intend on all of those lines to be part of your calculation then my divide by 2 solution won't get you what you are looking for.
Thanks,
Ed S.
|
kenlsk 5 posts msg #149897 - Ignore kenlsk modified |
12/16/2019 9:11:45 AM
Hi nibor100,
It is a fantastic idea from you, divided the CCI(14,0.015) by 2 will be the number I need.
I had added my reply for your question below:
a. this line
set{fix_avg_p,DATE(20191212,avg_p)}
based on the date of your original post is taking the typical price from 2 days ago instead of the current day's typical price. Is this on purpose?
Me: Yes. Actually I prefer to have the fix_avg_p for today. But once I offset this value to a few days ago, the figure will change.
b. this line
set{mad,sum(ab_sub,3)}
seems to be taking a 3 day sum of the absolute values instead of a 14 day sum?
Me: It is my mistake here. It should be 14 day sum instead of 3 day sum.
c. this line
set{nom,typical_p-ma(14)}
seems to be setting the numerator to be a regular 14 day simple moving average of the closing prices subtracted from today's typical price instead of subtracting the 14 day simple moving average of typical price(or your variable "avg_p"?
Me: It is my mistake her too. You are right because the numerator should be (Typical Price - 20-period SMA of TP).
d. which of course means that this line
set{denom,0.03*mad}
is setting the denominator to be .03 times your variable "mad" which is only a 3 day sum of the abosolute values???
Me: Replied in part b.
Thanks.
Regards,
Kiem
|