Announcement

Collapse
No announcement yet.

Creating EFS to track ema on a single chart with multiple timeframes

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

  • Creating EFS to track ema on a single chart with multiple timeframes

    Hello,

    I hopefully have a simple request, I have found the following efs which represents the EMS(8) either going up(in green) or down(in red). This efs works great on any chart with any timeframe. I am trying to see if I can display multiple timeframes on one chart e.g. 1minute, 2minute, 3minute of the ema(8). Here is the existing efs I have found, any guidence or assistance is appreciated.


    var vMA = null;

    function preMain() {
    setPriceStudy(true);
    setStudyTitle("EMA");
    setCursorLabelName("EMA", 0);
    setDefaultBarFgColor(Color.blue, 0);
    setDefaultBarThickness(3,0);

    var fp1 = new FunctionParameter("Length", FunctionParameter.NUMBER);
    fp1.setLowerLimit(1);
    fp1.setDefault(8); //Edit this value to set a new default

    var fp2 = new FunctionParameter("Offset", FunctionParameter.NUMBER);
    fp2.setDefault(0); //Edit this value to set a new default

    var fp3 = new FunctionParameter("Source", FunctionParameter.STRING);
    fp3.setName("Source");
    fp3.addOption("Close");
    fp3.addOption("High");
    fp3.addOption("Low");
    fp3.addOption("Open");
    fp3.addOption("HL/2");
    fp3.addOption("HLC/3");
    fp3.addOption("OHLC/4");
    fp3.setDefault("Close"); //Edit this value to set a new default

    var fp4 = new FunctionParameter("Type", FunctionParameter.STRING);
    fp4.setName("Type");
    fp4.addOption("MAStudy.SIMPLE");
    fp4.addOption("MAStudy.EXPONENTIAL");
    fp4.addOption("MAStudy.WEIGHTED");
    fp4.addOption("MAStudy.VOLUMEWEIGHTED");
    fp4.setDefault("MAStudy.EXPONENTIAL"); //Edit this value to set a new default

    }

    function main(Length,Offset,Source,Type) {

    if (vMA == null) vMA = new MAStudy(Length, Offset, Source, eval(Type));

    /*****************************************
    Insert your code following this text block
    Use vMA.getValue(MAStudy.MA) for your code
    ******************************************/

    if (vMA.getValue(MAStudy.MA,0) > vMA.getValue(MAStudy.MA,-1) ) {
    setBarFgColor(Color.green);
    } else {
    setBarFgColor(Color.red);
    }

    return vMA.getValue(MAStudy.MA);
    }

  • #2
    Hello again hama123,
    I wrote this tonight where it will use the charts interval for MA1 and then I have two function parameters that default to 2 & 3 minutes that you can change to whatever you want. The 2 EMA's that don't use the chart's interval use the closing price for their calculations, but you can easily change that in the code. I will paste it here and also upload it in notepad...enjoy!

    var fpArray = [];
    var vMA1 = null;
    var xMA2 = null;
    var xMA3 = null;

    function preMain(){
    setPriceStudy(true);
    setStudyTitle("EMA");
    setCursorLabelName("EMA_1", 0);
    setDefaultBarFgColor(Color.blue, 0);
    setDefaultBarThickness(1, 0);
    setCursorLabelName("EMA_2", 1);
    setDefaultBarFgColor(Color.white, 1);
    setDefaultBarThickness(1, 1);
    setCursorLabelName("EMA_3", 2);
    setDefaultBarFgColor(Color.magenta, 2);
    setDefaultBarThickness(1, 2);

    var x=0;
    fpArray[x] = new FunctionParameter("Length", FunctionParameter.NUMBER);
    fpArray[x].setLowerLimit(1);
    fpArray[x++].setDefault(8);

    fpArray[x] = new FunctionParameter("Offset", FunctionParameter.NUMBER);
    fpArray[x++].setDefault(0);

    fpArray[x] = new FunctionParameter("IntervalTwo", FunctionParameter.NUMBER);
    fpArray[x].setLowerLimit(1);
    fpArray[x++].setDefault(2);

    fpArray[x] = new FunctionParameter("IntervalThree", FunctionParameter.NUMBER);
    fpArray[x].setLowerLimit(1);
    fpArray[x++].setDefault(3);

    fpArray[x] = new FunctionParameter("Source", FunctionParameter.STRING);
    with(fpArray[x++]){
    setName("Source");
    addOption("Close");
    addOption("High");
    addOption("Low");
    addOption("Open");
    addOption("HL/2");
    addOption("HLC/3");
    addOption("OHLC/4");
    setDefault("Close");
    }
    fpArray[x] = new FunctionParameter("Type", FunctionParameter.STRING);
    with(fpArray[x++]){
    setName("Type");
    addOption("MAStudy.SIMPLE");
    addOption("MAStudy.EXPONENTIAL");
    addOption("MAStudy.WEIGHTED");
    addOption("MAStudy.VOLUMEWEIGHTED");
    setDefault("MAStudy.EXPONENTIAL");
    }
    }

    function main(Length, Offset, IntervalTwo, IntervalThree, Source, Type){
    if(getBarState() == BARSTATE_ALLBARS){
    vMA1 = new MAStudy(Length, Offset, Source, eval(Type));
    xMA2 = ema(Length, close(inv(IntervalTwo)));
    xMA3 = ema(Length, close(inv(IntervalThree)));
    }
    if(vMA1.getValue(MAStudy.MA,0) > vMA1.getValue(MAStudy.MA,-1)) setBarFgColor(Color.green);
    else setBarFgColor(Color.red);

    return [vMA1.getValue(MAStudy.MA), xMA2.getValue(0), xMA3.getValue(0)];
    }
    Attached Files

    Comment


    • #3
      Hello,

      LetUsLearn, thank you so much for your assistence, I have used all of the information you have provided to help me along further.

      I have one other question, that may or may not be an easy one. I have tried everything I can think of but have not been able to get the results I am trying for.

      Do you know if it is possible to have a timeframe of less than 1min, within the charts you can select 5second, 10second, etc. When writing my efs I am used to using inv(1) for a 1 minute interval, I can not seem to find anywhere it is possible to have a time period of less than 1 minute. Can it be done, if yes, guidence on how would be appreciated.

      Comment


      • #4
        hama123
        As per this article in the EFS Kb (which I found by searching for "inv()")
        When passing a non-numeric interval value to inv(), a string value must be specified by enclosing the value in quotes (i.e. inv("D") or inv("W") ).
        Since any tick based interval is going to be a "non-numeric value" you just enter it in quotes e.g inv("30S") or inv("200T") etc
        Alex


        Originally posted by hama123 View Post
        Hello,

        LetUsLearn, thank you so much for your assistence, I have used all of the information you have provided to help me along further.

        I have one other question, that may or may not be an easy one. I have tried everything I can think of but have not been able to get the results I am trying for.

        Do you know if it is possible to have a timeframe of less than 1min, within the charts you can select 5second, 10second, etc. When writing my efs I am used to using inv(1) for a 1 minute interval, I can not seem to find anywhere it is possible to have a time period of less than 1 minute. Can it be done, if yes, guidence on how would be appreciated.

        Comment


        • #5
          Alex,

          Thank you so much

          Comment


          • #6
            hama123
            You are welcome
            Alex


            Originally posted by hama123 View Post
            Alex,

            Thank you so much

            Comment

            Working...
            X