Announcement

Collapse
No announcement yet.

Integrating 2 indicators

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

  • Integrating 2 indicators

    Hello:

    I am trying to combine 2 indicators into 1: the 1st script is a standard price study that colors the bars red or green depending on the current trend, and is invoked on each trade, this script then attempts to call the DTI Directional Trend Index as a function (on BARSTATE_NEWBAR) that returns either a 1 or 2 depending on trend direction according to that indicator. I would then like to compare the results of the 2 indicators. The problem is that the vRef = ref(-1) from the DTI code is always undefined.

    Does the Ref(-1) command not work when called in a function?

    Also...Is "not-defined" the same thing as "null"?

  • #2
    mfmobley
    The most efficient (and easiest) way to call an external efs is to use the efsExternal() function which creates a series that you can then access using the .getValue() method (ie just as you would with any eSignal builtin study)
    In addition to the description and syntax provided in the linked article you can find further information and examples on how to use the efsExternal() function in this thread.
    Alex

    Comment


    • #3
      mfmobley
      To answer your questions

      Does the Ref(-1) command not work when called in a function?
      The ref() function only retrieves the values of the items included in the return statement of the efs in which it is being used

      Also...Is "not-defined" the same thing as "null"?
      Not necessarily. You could have a defined variable (a study for example) which will return null for the first n bars while it is priming.
      Alex

      Comment


      • #4
        integrating 2 indicators con't

        Thanks for your past reply. That helped, but as I've continued to work combining the 2 indicators I'm getting stuck.

        I need to be able to access a variable's value on the previous bar.

        Here is how I am trying to do it:

        preMain {
        .
        .
        .
        }

        var marktrend = null;

        main {
        .
        .
        .
        if marktrend == marktrend.getValue(-1) {

        do something;

        }

        but this code skips the rest of the commands in the loop and re-starts at the top, even if I only try a

        debugPrintln ("marktrend1= " + marktrend.getValue(-1));

        Does this getVaule(-1) work in a BARSTATE_NEWBAR condition only? Even if its in the loading mode for old data, not in the streaming mode getting new data?

        If it would make it easier I could send the entire code of the 2 indicators I'm trying to combine.

        Any help is certainly appreciated.

        Mark Mobley

        Comment


        • #5
          Re: integrating 2 indicators con't

          Hello mfmobley,

          Originally posted by mfmobley

          preMain {
          .
          .
          .
          }

          var marktrend = null;

          main {
          .
          .
          .
          if marktrend == marktrend.getValue(-1) {

          do something;

          }

          Are you initializing marktrend with efsExternal()? Please post your complete code for main() that you are working with in the above example for starters.
          Jason K.
          Project Manager
          eSignal - an Interactive Data company

          EFS KnowledgeBase
          JavaScript for EFS Video Series
          EFS Beginner Tutorial Series
          EFS Glossary
          Custom EFS Development Policy

          New User Orientation

          Comment


          • #6
            Hi Jason,

            Thanks for your reply, but I am still struggling with comparing the current bar’s color with the previous bar’s color,

            What is the proper syntax to make that possible?

            I have tried to do this with the global variable called marktrend, using similar syntax used in other parts of the code: marktrend(-1) and marktrend.getValue(-1).

            Thanks,
            Attached Files

            Comment


            • #7
              Mark
              Here is a basic example of how to determine the prior value of a variable that is not a series and that is not being returned by the script (hence preventing the use of the ref(() function).
              In order to do this the variables must be declared globally so that they retain their values on each iteration of the script. Also the transfer of values must occurr at BARSTATE_NEWBAR prior to the current variable being calculated.
              Alex

              PHP Code:
              function preMain(){
                  
              setPriceStudy(true);
              }
               
              var 
              Average null;
              var 
              CurrentColor Color.black;
              var 
              PreviousColor Color.black;
               
              function 
              main(){
               
                  if(
              Average==nullAverage sma(50);
                  
                  if(
              getBarState()==BARSTATE_NEWBAR){
                      
              PreviousColor CurrentColor;
                  }
                  
                  if(
              close(0) > Average.getValue(0)){
                      
              CurrentColor Color.lime;
                  } else {
                      
              CurrentColor Color.yellow;
                  }
                      
                  
              setBarBgColor(CurrentColor)
                  
                  if(
              PreviousColor==Color.lime && CurrentColor==Color.yellow){
                      
              drawShape(Shape.CIRCLEBelowBar1Color.redgetCurrentBarCount());
                  } else {
                      
              removeShape(getCurrentBarCount())
                  }
                  
                  return 
              Average.getValue(0);

              .

              Comment


              • #8
                Mark
                On a separate note I see that you included Blau's Directional Trend Index to your script.
                FYI I created a function that will calculate the DTI and requires just a few lines of code to implement. For more information see this post.
                Enclsed below is a basic example of William Blau's DTI indicator written using that function. Note that you must have installed the amBlauStudies function library to run the script
                Alex

                PHP Code:
                function preMain() {
                    
                setStudyTitle("Directional Trend Index");
                    
                setCursorLabelName("DTI",0);
                }
                 
                var 
                amLib addLibrary("amBlauStudies.efsLib");
                var 
                DTI null;
                 
                function 
                main(r,s,u) {
                 
                    if(
                r==null14;
                    if(
                s==null10;
                    if(
                u==null5;
                 
                    if(
                DTI==nullDTI amLib.amDTI(r,s,u);
                 
                    return 
                DTI.getValue(0);

                Comment

                Working...
                X