Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CCI Indicator - Price Oscillator #5

Open
QRMarketing opened this issue Mar 15, 2018 · 2 comments
Open

CCI Indicator - Price Oscillator #5

QRMarketing opened this issue Mar 15, 2018 · 2 comments

Comments

@QRMarketing
Copy link

This is just a feeler to see if anyone has (or something hackable - though at this level a bit far for me) the CCI or a Price Oscillator. It is fairly standard on most packages and often requested. Seems with all the other fancy things here this would be one that perhaps has been over looked.

@QRMarketing
Copy link
Author

QRMarketing commented Mar 25, 2018

Ok, here is a hack. I couldn't figure out all the snaking around in the base code, so I wrote a standalone cci indicator function:

export function cci(window, data) {

/*
CCI = (Typical Price  -  window-period SMA of TP) / (.015 x Mean Deviation)

Typical Price (TP) = (High + Low + Close)/3

Constant = .015

There are four steps to calculating the Mean Deviation: 
First, subtract the most recent 20-period average of the typical price from each period's typical price. 
Second, take the absolute values of these numbers. 
Third, sum the absolute values. 
Fourth, divide by the total number of periods (window). 
*/
    
var tmpData = [];

for(var i=0; i<data.length; i++) {  
    data[i].cci = 0; 
    tmpData.push({TypicalPrice: 0, TypicalPriceAVG: 0});
}

var TypicalPrice, MeanDeviation;
var k, p;

for(k=window-1; k<data.length; k++) {  
    
    TypicalPrice = 0;
    
    for(p=0; p<window; p++) {
        tmpData[k - p].TypicalPrice = (data[k - p].high + data[k - p].low + data[k - p].close) / 3;
        TypicalPrice = TypicalPrice + tmpData[k - p].TypicalPrice;
    }
    
    tmpData[k].TypicalPriceAVG = (TypicalPrice / window);
    
    MeanDeviation = 0;
    
    for(p=0; p<window; p++) {
        MeanDeviation = MeanDeviation + (Math.abs(tmpData[k - p].TypicalPrice - tmpData[k].TypicalPriceAVG)); 
    }
    MeanDeviation = MeanDeviation / window;
    
    data[k].cci = (tmpData[k].TypicalPrice  - tmpData[k].TypicalPriceAVG) / (.015 * MeanDeviation);
    
}

return data;

};

I integrated as follows (excerpts only):

render:

cciData = cci(window, initialData);
constTemp = ema1(sma2(ema2(sma1(bb(cciData)))));

return:

<LineSeries yAccessor={d => d.cci} stroke={"#8e8e8e"}/>
<StraightLine yValue={-100} stroke={"#ff9b9b"} />
<StraightLine yValue={0} stroke={"#8e8e8e"} />
<StraightLine yValue={100} stroke={"#92f8bd"} />

<SingleValueTooltip
yAccessor={d => d.cci}
origin={[2, 12]}
yDisplayFormat={format(".2f")}
fontFamily={'lato'}
textFill={StrokeTheme}
labelFill={StrokeTheme}
fontSize={10}
labelStroke={ "#8e8e8e" }
yLabel={"CCI (" + window + ")"}
/>

Perhaps someone can do it properly. I did check the results via an MT4 CCI indicator and it shows correct.

@QRMarketing
Copy link
Author

And an indicator for Standard deviation:

`export function stddev(window, data) {

/*
To Calculate Standard Deviation:
    1. Get the Mean (sma close)
    2. Get the deviations (subtract the current mean from each of the numbers)
    3. Square these
    4. Add the squares
    5. Divide by total number
    6. Square root of result is Standard Deviation
*/
        
for(var i=0; i<data.length; i++) {  
    data[i].stddev = 0; 
}

var SMA, Square, SquareTotal;
var k, p;

for(k=window-1; k<data.length; k++) {  
    
    SMA = 0;
    
    for(p=0; p<window; p++) {
        SMA = SMA + data[k - p].close;
    }
    
    SMA = SMA / window;
            
    SquareTotal = 0;
    
    for(p=0; p<window; p++) {
        
        Square = (data[k - p].close - SMA) * (data[k - p].close - SMA);
        SquareTotal = SquareTotal + Square;
        
    }

    data[k].stddev = Math.sqrt(SquareTotal / window);
            
}

return data;

};`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant