Announcement

Collapse
No announcement yet.

Moving Average startig from Flat Line

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

  • Moving Average startig from Flat Line

    I have been trying to develop code that will give me a moving average starting from a given time and price that I can specify.
    The moving average would be flat up until the trigger time and then it would be based on the close price as per normal.
    xFlat Start EMA v3.efs

    I seem to be doing something wrong.
    I would appreciate any help with this.

    Thanks

    Peter

  • #2
    I have done some more work on the indicator and reached a point of confusion. I am trying to make a dummy series in which the value at each bar is the same ( set manually ) up until a trigger time ( again set manually ) at which point the next and subsequent bars are set at the close price. The EMA output would be a straight line up until the trigger bar, and then move away slowly as the EMA is effected by each new close.
    In the attached code, the EMA is horizontal at the manual set level up to the trigger bar, but then reverts to the EMA calculated on the closes of all bars before the trigger.
    How do I get an EMA of the dummy series ?


    // xFlatStartEMAv11
    // A series is created in which each bar has a constant value ( manually defined ) up until a start time
    // at which point all bars behond that time are set at the bar close.
    // An EMA of that new series is then plotted.

    function preMain() {

    var xStudy = null;

    setPriceStudy(true);

    setCursorLabelName("FlatStart_Close", 0);
    setCursorLabelName("FlatStart_EMA", 1);
    setDefaultBarFgColor(Color.red, 0);
    setDefaultBarFgColor(Color.blue, 1);

    setDefaultBarBgColor(Color.white);

    var fp1 = new FunctionParameter("Input1", FunctionParameter.NUMBER);
    fp1.setName("Start Year");
    fp1.setLowerLimit(2000);
    fp1.setDefault(2015); // Year in std format eg. 2012
    var fp2 = new FunctionParameter("Input2", FunctionParameter.NUMBER);
    fp2.setName("Start Month");
    fp2.setLowerLimit(1);
    fp2.setDefault(4); // 1 = January... 12 = December
    var fp3 = new FunctionParameter("Input3", FunctionParameter.NUMBER);
    fp3.setName("Start Day");
    fp3.setLowerLimit(1);
    fp3.setDefault(22); // Day of the Month 1 to 31
    var fp4 = new FunctionParameter("Input4", FunctionParameter.NUMBER);
    fp4.setName("Start Hour");
    fp4.setLowerLimit(1);
    fp4.setDefault(9); // Hour 1 to 23
    var fp5 = new FunctionParameter("Input5", FunctionParameter.NUMBER);
    fp5.setName("Start Minute");
    fp5.setLowerLimit(0);
    fp5.setDefault(49); // Minute 1 to 59
    var fp6 = new FunctionParameter("Input6", FunctionParameter.NUMBER);
    fp6.setName("Open");
    fp6.setLowerLimit(1);
    fp6.setDefault(5859); // SPI Open at 9:49
    }

    function main( Input1, Input2, Input3, Input4, Input5, Input6 )
    {
    var vYear = Input1;
    var vMonth = Input2;
    var vDay = Input3;
    var vHour = Input4;
    var vMinute = Input5;
    var vOpen = Input6;

    var xYear = getYear(); // Year in std format eg. 2012
    var xMonth = getMonth(); // 1 = January... 12 = December
    var xDay = getDay(); // Day of the Month 1 to 31
    var xHour = getHour();
    var xMinute = getMinute();

    target_time = vYear*100000000 + vMonth*1000000 + vDay*10000 + vHour*100 + vMinute;
    thisbar_time = xYear*100000000 + xMonth*1000000 + xDay*10000 + xHour*100 + xMinute;


    var flag;

    flag = (thisbar_time < target_time);

    if(flag) setBarBgColor(Color.paleyellow);

    xStudy = efsInternal( "DummyClose", flag, vOpen, close() );
    xema = ema(60,xStudy);

    return new Array (xStudy.getValue(0), xema);
    }


    function DummyClose( f_flag, vOpenVal, source ) {

    if(f_flag) {
    return vOpenVal;
    }
    else {
    return source.getValue(0);
    }
    }xFlatStartEMAv11.efs

    Comment

    Working...
    X