Announcement

Collapse
No announcement yet.

Help with Global Vars

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

  • Help with Global Vars

    Could someone help me with global vars

    I wish to have a efs running on one longer timeframe that set global vars after each bar close.
    On a shorter Timeframe, I would like it to read the relevant global var and action if met

    Below is the script for the longer tf, that set the global vars

    var vLastAlert = -1;

    function preMain() {
    setColorPriceBars(true);
    setPriceStudy(true);
    setStudyTitle("Long TF Chart");
    setGlobalValue ("30low",0);
    setGlobalValue ("30high",0);
    setComputeOnClose(true)
    }
    function main() {

    if (
    (close(-1) > open(-1))

    ) onAction1()

    else if (
    (close(-1) < open(-1))
    ) onAction2();

    return null;

    }
    function postMain() {
    }
    function onAction1() {
    setPriceBarColor(Color.RGB(155,0,0));
    var low = open(-1)
    var high = close(-1)
    setGlobalValue ("30low",low);
    setGlobalValue ("30high",high);
    vLastAlert = 1;
    }

    function onAction2() {
    setPriceBarColor(Color.RGB(0,255,0));
    var low = close()
    var high = open()
    setGlobalValue ("30low",low);
    setGlobalValue ("30high",high);


    vLastAlert = 2;
    }

    This script is for the slower timefram

    var vLastAlert = -1;

    function preMain() {
    //setComputeOnClose(true)
    setPriceStudy(true);
    setStudyTitle("Lower TF");
    setCursorLabelName("30low", 0);
    setCursorLabelName("30high", 1);
    setDefaultBarStyle(PS_SOLID, 0);
    setDefaultBarStyle(PS_SOLID, 1);
    setDefaultBarFgColor(Color.red, 0);
    setDefaultBarFgColor(Color.red, 1);
    setDefaultBarThickness(1, 0);
    setDefaultBarThickness(1, 1);
    setPlotType(PLOTTYPE_LINE, 0);
    setPlotType(PLOTTYPE_LINE, 1);

    }

    function main() {
    if (
    close(-1) > getGlobalValue("30high")&&
    close(-1) > getGlobalValue("30low")
    ) onAction1()

    else if (
    close(-1) < getGlobalValue("30high")&&
    close(-1) < getGlobalValue("30low")
    ) onAction2();

    return new Array(
    null,
    null
    );
    }

    function postMain() {

    }
    function onAction1() {
    var vHL = high() - low();
    var vVar = vHL * 0.25;
    var vAddVar = vVar * 0.35;
    var buy = "buy" + getGlobalValue("30high")
    drawTextRelative(0, high() + (vVar + vAddVar +vVar ), buy, Color.black, Color.red, Text.BOLD | Text.ONTOP, null, null, "buyTxt" + getValue("time"));
    vLastAlert = 1;
    }

    function onAction2() {
    vLastAlert = 2;
    var vHL = high() - low();
    var vVar = vHL * 0.25;
    var vAddVar = vVar * 0.35;
    var sell = "Sell + getGlobalValue("30low")
    drawTextRelative(0, high() + (vVar + vAddVar +vVar ), sell, Color.black, Color.red, Text.BOLD | Text.ONTOP, null, null, "buyTxt" + getValue("time"));
    }


    Apreciate any help, I have a feeling it something to do with time

  • #2
    Found a small error on the offset of bars. this is the script i have now and need help with

    thanks

    var vLastAlert = -1;

    function preMain() {
    setColorPriceBars(true);
    setPriceStudy(true);
    setStudyTitle("Long TF Chart");
    setGlobalValue ("30low",0);
    setGlobalValue ("30high",0);
    //setComputeOnClose(true)
    }
    function main() {

    if (
    (close(-1) > open(-1))

    ) onAction1()

    else if (
    (close(-1) < open(-1))
    ) onAction2();

    return null;

    }
    function postMain() {
    }
    function onAction1() {
    setPriceBarColor(Color.RGB(155,0,0));
    var low = open(-1)
    var high = close(-1)
    setGlobalValue ("30low",low);
    setGlobalValue ("30high",high);
    vLastAlert = 1;
    }

    function onAction2() {
    setPriceBarColor(Color.RGB(0,255,0));
    var low = close(-1)
    var high = open(-1)
    setGlobalValue ("30low",low);
    setGlobalValue ("30high",high);


    vLastAlert = 2;
    }

    This script is for the slower timefram

    var vLastAlert = -1;

    function preMain() {
    //setComputeOnClose(true)
    setPriceStudy(true);
    setStudyTitle("Lower TF");
    setCursorLabelName("30low", 0);
    setCursorLabelName("30high", 1);
    setDefaultBarStyle(PS_SOLID, 0);
    setDefaultBarStyle(PS_SOLID, 1);
    setDefaultBarFgColor(Color.red, 0);
    setDefaultBarFgColor(Color.red, 1);
    setDefaultBarThickness(1, 0);
    setDefaultBarThickness(1, 1);
    setPlotType(PLOTTYPE_LINE, 0);
    setPlotType(PLOTTYPE_LINE, 1);

    }

    function main() {
    if (
    close(-1) > getGlobalValue("30high")&&
    close(-1) > getGlobalValue("30low")
    ) onAction1()

    else if (
    close(-1) < getGlobalValue("30high")&&
    close(-1) < getGlobalValue("30low")
    ) onAction2();

    return new Array(
    null,
    null
    );
    }

    function postMain() {

    }
    function onAction1() {
    var vHL = high() - low();
    var vVar = vHL * 0.25;
    var vAddVar = vVar * 0.35;
    var buy = "buy" + getGlobalValue("30high")
    drawTextRelative(0, high() + (vVar + vAddVar +vVar ), buy, Color.black, Color.red, Text.BOLD | Text.ONTOP, null, null, "buyTxt" + getValue("time"));
    vLastAlert = 1;
    }

    function onAction2() {
    vLastAlert = 2;
    var vHL = high() - low();
    var vVar = vHL * 0.25;
    var vAddVar = vVar * 0.35;
    var sell = "Sell + getGlobalValue("30low")
    drawTextRelative(0, high() + (vVar + vAddVar +vVar ), sell, Color.black, Color.red, Text.BOLD | Text.ONTOP, null, null, "buyTxt" + getValue("time"));
    }


    Apreciate any help, I have a feeling it something to do with time

    Comment


    • #3
      I am taking a look at what you have, you have several errors where you have not completed quotes and don't have semicolons at the end of the lines. This will kill you when it comes to troubleshooting.

      Take a look at what you have and we can compare notes

      Comment


      • #4
        Hi

        fixed a few on this bigger tf script

        var vLastAlert = -1;

        function preMain() {
        setColorPriceBars(true);
        setPriceStudy(true);
        setStudyTitle("Long TF Chart");
        setGlobalValue ("30low",0);
        setGlobalValue ("30high",0);
        //setComputeOnClose(true)
        }
        function main() {

        if (
        (close(-1) > open(-1))

        ) onAction1();

        else if (
        (close(-1) < open(-1))
        ) onAction2();

        return null;

        }
        function postMain() {
        }
        function onAction1() {
        setPriceBarColor(Color.RGB(155,0,0));
        var low = open(-1);
        var high = close(-1);
        setGlobalValue ("30low",low);
        setGlobalValue ("30high",high);
        vLastAlert = 1;
        }

        function onAction2() {
        setPriceBarColor(Color.RGB(0,255,0));
        var low = close(-1);
        var high = open(-1);
        setGlobalValue ("30low",low);
        setGlobalValue ("30high",high);
        vLastAlert = 2;
        }


        this is the mod script for short tf


        var vLastAlert = -1;

        function preMain() {
        //setComputeOnClose(true)
        setPriceStudy(true);
        setStudyTitle("Lower TF");
        setCursorLabelName("30low", 0);
        setCursorLabelName("30high", 1);
        setDefaultBarStyle(PS_SOLID, 0);
        setDefaultBarStyle(PS_SOLID, 1);
        setDefaultBarFgColor(Color.red, 0);
        setDefaultBarFgColor(Color.red, 1);
        setDefaultBarThickness(1, 0);
        setDefaultBarThickness(1, 1);
        setPlotType(PLOTTYPE_LINE, 0);
        setPlotType(PLOTTYPE_LINE, 1);

        }

        function main() {
        if (
        close(-1) > getGlobalValue("30high")&&
        close(-1) > getGlobalValue("30low")
        ) onAction1();

        else if (
        close(-1) < getGlobalValue("30high")&&
        close(-1) < getGlobalValue("30low")
        ) onAction2();

        return new Array(
        null,
        null
        );
        }

        function postMain() {

        }
        function onAction1() {
        var vHL = high() - low();
        var vVar = vHL * 0.25;
        var vAddVar = vVar * 0.35;
        var buy = "buy" + getGlobalValue("30high");
        drawTextRelative(0, high() + (vVar + vAddVar +vVar ), buy, Color.black, Color.red, Text.BOLD | Text.ONTOP, null, null, "");
        vLastAlert = 1;
        }

        function onAction2() {
        vLastAlert = 2;
        var vHL = high() - low();
        var vVar = vHL * 0.25;
        var vAddVar = vVar * 0.35;
        var sell = "Sell" + getGlobalValue("30low");
        drawTextRelative(0, high() + (vVar + vAddVar +vVar ), sell, Color.black, Color.red, Text.BOLD | Text.ONTOP, null, null, "");
        }

        Comment


        • #5
          biswar
          I have done something similar and here is the code. What I do, is indentify a "trend" in a "longer term" chart timeframe, and store the value in the Global Variable Pool.
          Then, an other EFS, on a shorter timeframe chart, reads this Variable Pool, and depending on the value of trend (if any), looks at an oscillator to determine if overbought/oversold, etc. So, I get trend from a longer term timeframe, and look for extremes in a shorter timeframe, and seek to trade within that trrend...that is my thinking, anyway.
          After identifying a value for UPTREND and DOWNTREND (which can be up, down or neutral), I store the values as follows:
          var UpVal = ""+symbol+"TrendIsUp";
          var DownVal = ""+symbol+"TrendIsDown";
          setGlobalValue(UpVal,TrendIsUp); setGlobalValue(DownVal,TrendIsDown);
          where symbol =getSymbol().

          The next EFS - attached to the shorter term timeframe chart for that same symbol has this code:
          var TrendIsUp = false
          var TrendIsDown = false;
          TrendIsUp=getGlobalValue(UpVal);TrendIsDown=getGlo balValue(DownVal);

          I am curious to know what you are trying to do.
          George

          Comment


          • #6
            redone

            With the thought of this being a cpu hog, ive changed it to compute on close


            var vLastAlert = -1;

            function preMain() {
            setColorPriceBars(true);
            setPriceStudy(true);
            setStudyTitle("Long TF Chart");
            setGlobalValue ("30low",0);
            setGlobalValue ("30high",0);
            setComputeOnClose(true);
            }
            function main() {

            if (
            (close() > open())

            ) onAction1();

            else if (
            (close() < open())
            ) onAction2();

            return null;

            }
            function postMain() {
            }
            function onAction1() {
            setPriceBarColor(Color.RGB(155,0,0));
            var low = open();
            var high = close();
            setGlobalValue ("30low",low);
            setGlobalValue ("30high",high);
            vLastAlert = 1;
            }

            function onAction2() {
            setPriceBarColor(Color.RGB(0,255,0));
            var low = close();
            var high = open();
            setGlobalValue ("30low",low);
            setGlobalValue ("30high",high);
            vLastAlert = 2;
            }

            the shorter is also setto compute on close

            var vLastAlert = -1;

            function preMain() {

            setPriceStudy(true);
            setStudyTitle("Lower TF");
            setCursorLabelName("30low", 0);
            setCursorLabelName("30high", 1);
            setDefaultBarStyle(PS_SOLID, 0);
            setDefaultBarStyle(PS_SOLID, 1);
            setDefaultBarFgColor(Color.red, 0);
            setDefaultBarFgColor(Color.red, 1);
            setDefaultBarThickness(1, 0);
            setDefaultBarThickness(1, 1);
            setPlotType(PLOTTYPE_LINE, 0);
            setPlotType(PLOTTYPE_LINE, 1);
            setComputeOnClose(true)
            }

            function main() {
            if (
            close() > getGlobalValue("30high")&&
            close() > getGlobalValue("30low")
            ) onAction1();

            else if (
            close() < getGlobalValue("30high")&&
            close() < getGlobalValue("30low")
            ) onAction2();

            return new Array(
            null,
            null
            );
            }

            function postMain() {

            }
            function onAction1() {
            var vHL = high() - low();
            var vVar = vHL * 0.25;
            var vAddVar = vVar * 0.35;
            var buy = "buy" + getGlobalValue("30high");
            drawTextRelative(0, high() + (vVar + vAddVar +vVar ), buy, Color.black, Color.red, Text.BOLD | Text.ONTOP, null, null, "");
            vLastAlert = 1;
            }

            function onAction2() {
            vLastAlert = 2;
            var vHL = high() - low();
            var vVar = vHL * 0.25;
            var vAddVar = vVar * 0.35;
            var sell = "Sell" + getGlobalValue("30low");
            drawTextRelative(0, high() + (vVar + vAddVar +vVar ), sell, Color.black, Color.red, Text.BOLD | Text.ONTOP, null, null, "");
            }

            Comment


            • #7
              Bob,

              Try this:

              PHP Code:
              var vLastAlert = -1;

              function 
              preMain() {
                  
              setColorPriceBars(true);
                  
              setPriceStudy(true);
                  
              setStudyTitle("Long TF Chart");
                  
              setGlobalValue ("flag",0);
                  
              setGlobalValue ("30low",0);
                  
              setGlobalValue ("30high",0);
                  
              setComputeOnClose(true);
              }
              function 
              main() {
                  
                  if ((
              close() > open())) onAction1();
                  
                  else if ((
              close() < open())) onAction2();
                  
                  return 
              null;

              }
              function 
              postMain() {
              }
              function 
              onAction1() {
                  
              setPriceBarColor(Color.RGB(155,0,0));
                  var 
              low open();
                  var 
              high close();
                  
              setGlobalValue ("30low",low());
                  
              setGlobalValue ("30high",high());
                  
              setGlobalValue ("flag",getValue("rawtime",0););
                  
              vLastAlert 1;
              }

              function 
              onAction2() {
                  
              setPriceBarColor(Color.RGB(0,255,0));
                  var 
              low close();
                  var 
              high open();
                  
              setGlobalValue ("30low",low());
                  
              setGlobalValue ("30high",high()); 
                  
              setGlobalValue ("flag",getValue("rawtime",0));
                  
              vLastAlert 2;
              }

              the shorter is also setto compute on close (Receiving efs)

              var 
              vLastAlert = -1;
              var 
              vHi vLo vsTime vlTime 0;
              function 
              preMain() {
                  
                  
              setPriceStudy(true);
                  
              setStudyTitle("Lower TF");
                  
              setCursorLabelName("30low"0);
                  
              setCursorLabelName("30high"1);
                  
              setDefaultBarStyle(PS_SOLID0);
                  
              setDefaultBarStyle(PS_SOLID1);
                  
              setDefaultBarFgColor(Color.red0);
                  
              setDefaultBarFgColor(Color.red1);
                  
              setDefaultBarThickness(10);
                  
              setDefaultBarThickness(11);
                  
              setPlotType(PLOTTYPE_LINE0);
                  
              setPlotType(PLOTTYPE_LINE1);
                  
              setComputeOnClose(true);
                  
              setGlobalValue ("flag",0);
                  
              setGlobalValue ("30low",0);
                  
              setGlobalValue ("30high",0);
              }

              function 
              main() {

                  var    
              vTime = new Date();//the date object slows things down a tiny bit
                  
              vsTime =  vTime.getTime();
                  if (
              getGlobalValue("flag")){
                      
              vlTime getGlobalValue("flag");
                      
              setGlobalValue ("flag",0);
                      
              vHi =getGlobalValue("30high");
                      
              vLo getGlobalValue("30low");
                  }


                  
              debugPrintln("time difference = "+(vsTime-vlTime));

                  if (
              close() > vHi && close() > vLoonAction1();

                  else if (
              close() < vHi && close() < vLoonAction2();

                  return new Array(
              null,null);
              }

              function 
              postMain() {

              }
              function 
              onAction1() {
                  var 
              vHL high() - low();
                  var 
              vVar vHL 0.25;
                  var 
              vAddVar vVar 0.35;
                  var 
              buy "buy" vHi;
                  
              drawTextRelative(0high() + (vVar vAddVar +vVar ), buyColor.blackColor.redText.BOLD Text.ONTOPnullnull""); 
                  
              vLastAlert 1;
              }

              function 
              onAction2() {
                  
              vLastAlert 2;
                  var 
              vHL high() - low();
                  var 
              vVar vHL 0.25;
                  var 
              vAddVar vVar 0.35;
                  var 
              sell "Sell" vLo;
                  
              drawTextRelative(0high() + (vVar vAddVar +vVar ), sellColor.blackColor.redText.BOLD Text.ONTOPnullnull"");

              I put call to the date object in the recieving efs to get the milliseconds. This is slightly slower than getValue("rawtime",0) in the sending efs which should take care of sequencing issues. To check to see if you are getting impacted by sequencing issues, I put a debug Printlnstatement in there.

              Regards,

              Comment

              Working...
              X