Announcement

Collapse
No announcement yet.

Can I replace high() with a variable?

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

  • Can I replace high() with a variable?

    Is there a short, easy way that I can replace a function call like close() with a variable name like vOHLC() (as in example 1) or am I just going to have to use a longer test for the parameter setting (as in example 2)?

    Dale Sullivan


    PHP Code:

    //  ***  EXAMPLE 1  ***

        
    var fp1 = new FunctionParameter("vOHLC"FunctionParameter.STRING);
        
    fp1.setName("vOHLC");
        
    fp1.addOption("open");
        
    fp1.addOption("high");
        
    fp1.addOption("low");
        
    fp1.addOption("close");
        
    fp1.setDefault("close");

    }
    function 
    main(vOHLC) {

        
    vPrice1 close(vSymbol);
        
    vPrice2 vOHLC(vSymbol) + 4;

        return new Array(
    vPrice1vPrice2);


    PHP Code:

    //  ***  EXAMPLE 2  ***

        
    var fp1 = new FunctionParameter("vOHLC"FunctionParameter.STRING);
        
    fp1.setName("vOHLC");
        
    fp1.addOption("open");
        
    fp1.addOption("high");
        
    fp1.addOption("low");
        
    fp1.addOption("close");
        
    fp1.setDefault("close");

    }
    function 
    main(vOHLC) {


        
    vPrice1 close(vSymbol);

    switch (
    vOHLC) {
         case 
    "open" :
              
    vPrice2 open(vSymbol) + 4;
              break;
         case 
    "high" :
              
    vPrice2 high(vSymbol) + 4;
              break;
         case 
    "low" :
              
    vPrice2 low(vSymbol) + 4;
              break;
         case 
    "close" :
              
    vPrice2 close(vSymbol) + 4;
              break;
         default :
              
    vPrice2 close(vSymbol) + 4;
    }

        return new Array(
    vPrice1vPrice2); 
    Last edited by tasman; 11-24-2003, 10:41 AM.

  • #2
    Hello Dale,

    You can accomplish this with the use of eval(). It basically evaluates a string and turns it into a constant or literal. Not sure if that is the best way to describe it. The code example below should make its usage a little more clear. All you need to do is create one big string that would evaluate to the line of code you need and simply pass it to the eval() function. Another alternative is to use getValue() instead. There is an example of this in the code as well. This will allow you to avoid using the eval() function since vOHLC is already a string. Let me know if this helps.

    PHP Code:

    //  ***  EXAMPLE 1  ***

    function preMain() {
        
    setPriceStudy(true);
        
    setStudyTitle(" eval() example ");
        
    setCursorLabelName("Price 1",0);
        
    setCursorLabelName("Price 2",1);
        
        var 
    fp1 = new FunctionParameter("vOHLC"FunctionParameter.STRING);
        
    fp1.setName("vOHLC");
        
    fp1.addOption("open");
        
    fp1.addOption("high");
        
    fp1.addOption("low");
        
    fp1.addOption("close");
        
    fp1.setDefault("close");
    }

    function 
    main(vOHLC) {
        var 
    vSymbol getSymbol();
        var 
    str vOHLC "('" vSymbol "')"
        
        
    vPrice1 close(01vSymbol)*1;
        
    vPrice2 = eval(str) + 4;

        
    //Another alternative is to use getValue(), try:
        //vPrice2 = getValue(vOHLC, 0, 1, vSymbol)*1 + 4;

        
    return new Array(vPrice1vPrice2);


    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


    • #3
      Thanks a lot Jason. That's exactly what I was looking for.

      Dale

      Comment

      Working...
      X