Announcement

Collapse
No announcement yet.

Smoothed CCI

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Smoothed CCI

    I am trying to produce a smoothed CCI (called MACCI) which uses hlc3 as it's data instead of Close.
    I have taken the CCI efs code from the Library and modified it, but the plot is clearly wrong when compared to the CCI it is smoothing, and it doesn't extend up to the current bar or plot the +/- 100 Bands.

    (I also tried using the EFS Wizard. It generated code but wouldn't plot anything and anyway I assume it's based on Close rather than hlc3)

    Basically it's a 9 Sma of a 20 CCI using hlc3.
    Can anyone help to identify where I have gone wrong please ?
    Here is the code:-

    function preMain() {
    setPriceStudy(false);
    setStudyTitle("MACCI");
    setCursorLabelName("MACCI", 0);
    setDefaultBarFgColor(Color.blue, 0);
    setPlotType(PLOTTYPE_LINE,0);
    setDefaultBarThickness(1,0);
    addBand(100, PS_SOLID, 1, Color.black,"Upper");
    addBand(-100, PS_SOLID, 1, Color.black,"Lower");
    }

    var xPrice = null;
    var xAvgPrice = null;

    function main(Length){

    if(Length==null) Length = 20;

    if(xPrice==null) xPrice = hlc3();
    if(xAvgPrice==null) xAvgPrice = sma(Length,xPrice);

    if(xPrice.getValue(-Length)==null||xAvgPrice.getValue(-Length)==null) return;

    var Sum = 0;

    for (i=0; i<Length; i++){
    Sum += Math.abs(xPrice.getValue(-i)-xAvgPrice.getValue(0));
    }

    var MeanDevPrice = Sum/Length;
    var CCI = (xPrice.getValue(0)-xAvgPrice.getValue(0))/(MeanDevPrice*0.015);
    var MACCI = sma(9,CCI);
    return MACCI;
    }

    Thanks for any advice.
    tgmjf

  • #2
    Re: Smoothed CCI

    tgmjf
    The reason your script is not working correctly is because the variable CCI is a value and not a series which the sma() function instead requires.
    In order to do what you want you will need to first calculate CCI in a separate function (or efs) and then call that function or efs using either the efsInternal() or efsExternal() functions. This will create the series which you can then use as a source for the sma() functions used to calculate MACCI
    For a more detailed explanation and solution see this post in reply to a similar question. In that reply you will also find links to other examples on creating studies on studies using efs2 functions
    Having said that you may want to implement a simpler solution using the efs2 functions. In this case the 9 period of the 20 period CCI based on (H+L+C)/3 would be written as sma(9, cci(20, hlc3()))
    Alex


    Originally posted by tgmjf18
    I am trying to produce a smoothed CCI (called MACCI) which uses hlc3 as it's data instead of Close.
    I have taken the CCI efs code from the Library and modified it, but the plot is clearly wrong when compared to the CCI it is smoothing, and it doesn't extend up to the current bar or plot the +/- 100 Bands.

    (I also tried using the EFS Wizard. It generated code but wouldn't plot anything and anyway I assume it's based on Close rather than hlc3)

    Basically it's a 9 Sma of a 20 CCI using hlc3.
    Can anyone help to identify where I have gone wrong please ?
    Here is the code:-

    function preMain() {
    setPriceStudy(false);
    setStudyTitle("MACCI");
    setCursorLabelName("MACCI", 0);
    setDefaultBarFgColor(Color.blue, 0);
    setPlotType(PLOTTYPE_LINE,0);
    setDefaultBarThickness(1,0);
    addBand(100, PS_SOLID, 1, Color.black,"Upper");
    addBand(-100, PS_SOLID, 1, Color.black,"Lower");
    }

    var xPrice = null;
    var xAvgPrice = null;

    function main(Length){

    if(Length==null) Length = 20;

    if(xPrice==null) xPrice = hlc3();
    if(xAvgPrice==null) xAvgPrice = sma(Length,xPrice);

    if(xPrice.getValue(-Length)==null||xAvgPrice.getValue(-Length)==null) return;

    var Sum = 0;

    for (i=0; i<Length; i++){
    Sum += Math.abs(xPrice.getValue(-i)-xAvgPrice.getValue(0));
    }

    var MeanDevPrice = Sum/Length;
    var CCI = (xPrice.getValue(0)-xAvgPrice.getValue(0))/(MeanDevPrice*0.015);
    var MACCI = sma(9,CCI);
    return MACCI;
    }

    Thanks for any advice.
    tgmjf

    Comment


    • #3
      Alex
      Thank you for your comprehensive and quick reply.

      Regards
      tgmjf

      Comment


      • #4
        tgmjf
        You are most welcome
        Alex


        Originally posted by tgmjf18
        Alex
        Thank you for your comprehensive and quick reply.

        Regards
        tgmjf

        Comment

        Working...
        X