Announcement

Collapse
No announcement yet.

Nth bar MA

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

  • Nth bar MA

    Is there a script to do a Moving Average of every nth bar?
    When looking at intraday charts of stats, like $TVOL, I want an MA of the values for 3rd (or 5th or ???) intraday bar for the last 7 days.

    I want the MA to be every nth bar but instead it is plotting every bar
    Last edited by pj909; 05-05-2022, 10:54 AM.

  • #2
    PHP Code:

    /************************************************** ***************
    Provided By:
    plot a moving average of the nth bar.
    for use on an intraday non-price chart like $TVOL i.e.:
    show the 11am cumulative average volume for the last nLen days
    ************************************************** ****************/

    var bInit false;
    function 
    preMain() {
    setPriceStudy(true);
    setStudyTitle("nth Bar MA");
    setCursorLabelName("MA"0);

    var 
    fp1 = new FunctionParameter("nLen"FunctionParameter.NUMBER);
    fp1.setName("nLen");
    fp1.setLowerLimit(1);
    fp1.setDefault(7);
    var 
    fp2 = new FunctionParameter("nthBar"FunctionParameter.NUMBER);
    fp2.setName("nthBar");
    fp2.setLowerLimit(1);
    fp2.setDefault(3);
    }


    function 
    main(nLennthBar) {

    nBarsPerDay 390 getInterval();

    if (!
    bInit){
    nCnt nLen nBarsPerDay
    bInit 
    true;
    }

    if (
    close(-nCnt) == null) return;

    var 
    vSum 0;
    for(var 
    0nCnt+1; ) {
    i+nBarsPerDay
    vSum 
    += close(-i);
    // only sum the nth bar of the day
    }

    return (
    vSum nLen);

    Last edited by pj909; 05-05-2022, 10:53 AM.

    Comment

    Working...
    X