Announcement

Collapse
No announcement yet.

Merging formulas

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

  • Merging formulas

    Please, I need some help to merge the two formulas below (MACD + MOVTREND (Rafter) to work togheter setting the bars colors, blue (buy) and red (sell):
    If MACD>0 and if(wt>wt_1)
    setBarFgColor(Color.blue);
    and
    If MACD <0 and if(wt<wt_1)
    setBarFgColor(Color.red);


    /************************************************** *******
    Alexis C. Montenegro © July 2003
    Use and/or modify this code freely. If you redistribute it
    please include this and/or any other comment blocks and a
    description of any changes you make.
    ************************************************** ********/

    var vMACD = null;

    function preMain() {
    setStudyTitle("MACD");
    setCursorLabelName("MACDHist",0);
    setCursorLabelName("MACD",1);
    setCursorLabelName("MACDSig",2);
    setDefaultBarFgColor(Color.magenta,0);
    setDefaultBarFgColor(Color.blue,1);
    setDefaultBarFgColor(Color.red,2);
    setDefaultBarThickness(1,0);
    setDefaultBarThickness(1,1);
    setDefaultBarThickness(1,2);
    setPlotType(PLOTTYPE_HISTOGRAM);

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

    var fp2 = new FunctionParameter("Slow", FunctionParameter.NUMBER);
    fp2.setLowerLimit(1);
    fp2.setDefault(26); //Edit this value to set a new default

    var fp3 = new FunctionParameter("Smoothing", FunctionParameter.NUMBER);
    fp3.setLowerLimit(1);
    fp3.setDefault(9); //Edit this value to set a new default

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

    var fp5 = new FunctionParameter("TypeOsc", FunctionParameter.BOOLEAN);
    fp5.setName("SMA (Oscillator)");
    fp5.setDefault(false); //Edit this value to set a new default

    var fp5 = new FunctionParameter("TypeSig", FunctionParameter.BOOLEAN);
    fp5.setName("SMA (Signal)");
    fp5.setDefault(true); //Edit this value to set a new default

    }

    function main(Fast, Slow, Smoothing, Source, TypeOsc, TypeSig) {

    if (vMACD == null) vMACD = new MACDStudy(Fast, Slow, Smoothing, Source, TypeOsc, TypeSig);

    /*********************************************
    Insert your code following this text block
    Use vMACD.getValue(MACDStudy.HIST) and
    vMACD.getValue(MACDStudy.MACD) and
    vMACD.getValue(MACDStudy.SIGNAL) for your code
    **********************************************/



    return new Array (vMACD.getValue(MACDStudy.HIST),vMACD.getValue(MAC DStudy.MACD),vMACD.getValue(MACDStudy.SIGNAL));

    }

    PLUS



    function preMain()
    {
    setStudyTitle("Movtrend");
    setCursorLabelName("Movtrend", 0);
    setDefaultBarFgColor(Color.white, 0);
    setPlotType(PLOTTYPE_INSTANTCOLORLINE);
    setDefaultBarThickness(2, 0);
    setPriceStudy(true);

    }

    var wt = null;
    var wt_1 = null;

    function main(n) {

    if(n == null)
    n = 34;
    var sum = 0;
    var i = 0;

    if(getBarState()==BARSTATE_NEWBAR){
    wt_1 = wt;
    }

    for(i = n; i > 0; i--)
    sum += (i - (n + 1) / 3) * close(i - n);
    wt = 6 / (n * (n + 1)) * sum;

    if(wt>wt_1)
    setBarFgColor(Color.blue);
    if(wt<wt_1)
    setBarFgColor(Color.red);


    return wt;
    }

  • #2
    Barros
    Rather than merging the formulas you may find it easier to use the efsExternal() function to call the external formula.
    To do that you would use the following in the main function of your primary efs
    PHP Code:
    var myVar efsExternal("/path_to_efs/name_of_efs.efs"nn);//nn is the length parameter required by the called efs 
    At that point you can use the getValue() method to retrieve the specific values of the study ie
    PHP Code:
    myVar.getValue(0);//is the current value
    myVar.getValue(-1);//is the value of one bar ago
    //etc 
    For more information and examples on how to use efsExternal() see this thread and the definitions provided in this article in the EFS KnowledgeBase
    That said Rafter's Movtrend is none other than the LSMA calculated in a different way and you may find it easier to just use the amLSMA() function which is included in the amStudies.efsLib function library. For information on the amStudies library see this thread
    Alex

    Comment


    • #3
      Thanks

      Alex,
      Thank you very much for your attention.
      I will try to do the task, because I only have know how to use the Wizard, not these kind of formulas.
      Regards,
      Barros

      Comment


      • #4
        Alexis,
        Please, for me is difficult to make the work.
        Is it possible you merge the formulas and the work will be a example for new formulas?
        Thank you very much.
        Barros.

        Comment


        • #5
          Barros
          Rather than trying to merge the two formulas into one the better solution is to use the efsExternal() function as I suggested in my prior reply. This will provide you with a method that can be implemented with virtually any efs and not just this specific case. Also this solution is considerably simpler as all you need to do is write one simple line in your primary formula (ie the MACD formula).
          In the MACD formula you will see that there is a comment section which says "Insert your code following this text block..."
          Add the following code after that text block
          PHP Code:
          var myVar efsExternal("/path_to_efs/name_of_efs.efs"nn); 
          Then in that line of code replace
          path_to_efs with the name of the folder in which Rafter Movtrend efs is located (for example Library)
          name_of_efs.efs with the name of the formula (for example movtrend.efs)
          nn with the length for that indicator (for example 34)
          Once you have done that it will be as if you had merged the two scripts and you will have accomplished this without having to rewrite all the code.
          If you now want to use the Rafter Movtrend study for your conditions inside the MACD formula all you need to do is use the getValue() method to retrieve the appropriate value. For example myVar.getValue(0) is the value of the Rafter Movtrend at the current bar, myVar.getValue(-1) is the value at the prior bar, etc.
          So your original condition would be
          PHP Code:
          if(vMACD.getValue(MACDStudy.MACD) > && myVar.getValue(0) > myVar.getValue(-1)){
              
          setPriceBarColor(Color.blue);//this colors the price bars
              
          setBarFgColor(Color.blue,0);//this colors the histogram
              
          setBarFgColor(Color.blue,1);//this colors the MACD
              
          setBarFgColor(Color.blue,2);//this colors the Signal
          }
          //etc 
          If you are still having some issues please post the code as you have modified it and I or someone else will help you in fixing it so that in the process you can learn how to do it.
          If on the other hand you are not interested in trying this yourself I would suggest that you have one of the EFS Consultants develop the script for you
          Alex

          Comment


          • #6
            Dear Alexis,
            I wrote:

            /************************************************** *******
            Alexis C. Montenegro © July 2003
            Use and/or modify this code freely. If you redistribute it
            please include this and/or any other comment blocks and a
            description of any changes you make.
            ************************************************** ********/

            var vMACD = null;

            function preMain() {
            setStudyTitle("MACD");
            setCursorLabelName("MACDHist",0);
            setCursorLabelName("MACD",1);
            setCursorLabelName("MACDSig",2);
            setDefaultBarFgColor(Color.magenta,0);
            setDefaultBarFgColor(Color.blue,1);
            setDefaultBarFgColor(Color.red,2);
            setDefaultBarThickness(1,0);
            setDefaultBarThickness(1,1);
            setDefaultBarThickness(1,2);
            setPlotType(PLOTTYPE_HISTOGRAM);

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

            var fp2 = new FunctionParameter("Slow", FunctionParameter.NUMBER);
            fp2.setLowerLimit(1);
            fp2.setDefault(26); //Edit this value to set a new default

            var fp3 = new FunctionParameter("Smoothing", FunctionParameter.NUMBER);
            fp3.setLowerLimit(1);
            fp3.setDefault(9); //Edit this value to set a new default

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

            var fp5 = new FunctionParameter("TypeOsc", FunctionParameter.BOOLEAN);
            fp5.setName("SMA (Oscillator)");
            fp5.setDefault(false); //Edit this value to set a new default

            var fp5 = new FunctionParameter("TypeSig", FunctionParameter.BOOLEAN);
            fp5.setName("SMA (Signal)");
            fp5.setDefault(true); //Edit this value to set a new default

            }

            function main(Fast, Slow, Smoothing, Source, TypeOsc, TypeSig) {

            if (vMACD == null) vMACD = new MACDStudy(Fast, Slow, Smoothing, Source, TypeOsc, TypeSig);

            /*********************************************
            Insert your code following this text block
            Use vMACD.getValue(MACDStudy.HIST) and
            vMACD.getValue(MACDStudy.MACD) and
            vMACD.getValue(MACDStudy.SIGNAL) for your code
            **********************************************/
            var myVar = efsExternal(MaRsi/rafter(color)3.efs,34; LINE66
            if(vMACD.getValue(MACDStudy.MACD) > 0 && myVar.getValue(0) > myVar.getValue(-1)){
            setPriceBarColor(Color.navy);
            if(vMACD.getValue(MACDStudy.MACD) > 0 && myVar.getValue(0) > myVar.getValue(-1)){
            setPriceBarColor(Color.red);


            return new Array (vMACD.getValue(MACDStudy.HIST),vMACD.getValue(MAC DStudy.MACD),vMACD.getValue(MACDStudy.SIGNAL));

            }

            And there is an error=
            Training\buitinMACDexternal.efs, line66:
            SyntaxError: missing expoent:
            var myVar = efsExternal(MaRsi/rafter(color)3,11

            Could you help me?

            Barros.
            Last edited by Barros; 02-21-2006, 06:08 PM.

            Comment


            • #7
              Barros
              The path and file name should be enclosed in quotes as in the example I provided ie "MaRsi/rafter(color)3.efs"
              Also there is a missing parenthesis at the end of line 66 (again please see my example)
              Alex

              Comment

              Working...
              X