Announcement

Collapse
No announcement yet.

Help with my formula please

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

  • Help with my formula please

    var study = new MAStudy(5, 0, "Close", MAStudy.SIMPLE);

    function preMain() {
    setPriceStudy(true);
    }

    function main() {
    var v = study.getValue(MAStudy.MA);
    var vLastAlert = -1;

    if(v == null)
    return;


    if(close() >= v) {
    if(!Strategy.isLong()) {
    Strategy.doLong("Crossing Up", Strategy.MARKET, Strategy.THISBAR);
    }

    } else {
    if(!Strategy.isShort()) {
    Strategy.doShort("Crossing Down", Strategy.MARKET, Strategy.THISBAR);
    }
    }

    if(Strategy.isLong()) {
    setBarBgColor(Color.green);
    if (vLastAlert != 1) Alert.addToList(getSymbol(), "Es geht hoch!", Color.RGB(0,0,0), Color.RGB(0,255,0));
    if (vLastAlert != 1) Alert.email("Es geht hoch!", "Es geht hoch!");
    vLastAlert = 1;

    } else if(Strategy.isShort()) {
    setBarBgColor(Color.red);
    if (vLastAlert != 2) Alert.addToList(getSymbol(), "Es geht runter!", Color.RGB(0,0,0), Color.RGB(195,0,0));
    if (vLastAlert != 2) Alert.email("Es geht runter!", "Es geht runter!");
    vLastAlert = 2;
    }


    return v;
    }
    when i am running this formula i get a signal when a candle is changing up or down but i want a signal only at the time the trend is changing (upwards or downwards)

    can anybody help me ?
    regards eric

  • #2
    Eric
    The enclosed should now do what you want. Changes are commented.
    Alex

    PHP Code:
    var study = new MAStudy(50"Close"MAStudy.SIMPLE);
    vLastAlert = -1//moved here

    function preMain() {
        
    setPriceStudy(true);
    }

    function 
    main() {
    var 
    study.getValue(MAStudy.MA);
        
        
    if(
    == null)
    return;


    if(
    close() >= v) {
    if(!
    Strategy.isLong()) {
    Strategy.doLong("Crossing Up"Strategy.MARKETStrategy.THISBAR);
    }

    } else {
    if(!
    Strategy.isShort()) {
    Strategy.doShort("Crossing Down"Strategy.MARKETStrategy.THISBAR);
    }
    }

    if(
    Strategy.isLong()) {
            if (
    vLastAlert != 1)setBarBgColor(Color.green); //added if(vLastAlert !=1)
            
    if (vLastAlert != 1Alert.addToList(getSymbol(), "Es geht hoch!"Color.RGB(0,0,0), Color.RGB(0,255,0));
            if (
    vLastAlert != 1Alert.email("Es geht hoch!""Es geht hoch!");
            
    vLastAlert 1;

    } else if(
    Strategy.isShort()) {
            if (
    vLastAlert != 2)setBarBgColor(Color.red); //added if(vLastAlert !=2)
            
    if (vLastAlert != 2Alert.addToList(getSymbol(), "Es geht runter!"Color.RGB(0,0,0), Color.RGB(195,0,0));
            if (
    vLastAlert != 2Alert.email("Es geht runter!""Es geht runter!");
            
    vLastAlert 2;
    }


    return 
    v;

    Comment


    • #3
      hmmm ok i will test this ...
      but i only see that you changed that the bg color isnt always there and i mean that the signals sometimes repeat in one candlestick 2 signals or something like that

      regards eric

      Comment


      • #4
        Eric
        I think I see what you are saying ie that you are getting multiple signals when the condition is true.
        Part of the issue is that when the close() is crossing the MA it may do so repeatedly.
        You would need to do one of two things; either use setComputeOnClose() in preMain() so that the condition is verified only on a new bar or use if(getBarState()==BARSTATE_NEWBAR) in main() which does essentially the same thing.
        With the latter the conditions may have to be set slightly differently.
        Let me know if either solution works for you.
        Alex

        Comment


        • #5
          var study = new MAStudy(5, 0, "Close", MAStudy.SIMPLE);
          vLastAlert = -1; //moved here

          function preMain() {
          setPriceStudy(true);
          setComputeOnClose()
          }

          function main() {
          var v = study.getValue(MAStudy.MA);
          if(getBarState()==BARSTATE_NEWBAR);

          if(v == null)
          return;


          if(close() >= v) {
          if(!Strategy.isLong()) {
          Strategy.doLong("Crossing Up", Strategy.MARKET, Strategy.THISBAR);
          }

          } else {
          if(!Strategy.isShort()) {
          Strategy.doShort("Crossing Down", Strategy.MARKET, Strategy.THISBAR);
          }
          }

          if(Strategy.isLong()) {
          if (vLastAlert != 1)setBarBgColor(Color.green); //added if(vLastAlert !=1)
          if (vLastAlert != 1) Alert.addToList(getSymbol(), "Es geht hoch!", Color.RGB(0,0,0), Color.RGB(0,255,0));
          if (vLastAlert != 1) Alert.email("Es geht hoch!", "Es geht hoch!");
          vLastAlert = 1;

          } else if(Strategy.isShort()) {
          if (vLastAlert != 2)setBarBgColor(Color.red); //added if(vLastAlert !=2)
          if (vLastAlert != 2) Alert.addToList(getSymbol(), "Es geht runter!", Color.RGB(0,0,0), Color.RGB(195,0,0));
          if (vLastAlert != 2) Alert.email("Es geht runter!", "Es geht runter!");
          vLastAlert = 2;
          }


          return v;
          }
          if i use the code below i get a very unuseful study because the chart isnt full colored and thats irritating me... because i think when it looks like that there wont be cool signals.

          perhabs you dont understand what i mean so i try to tell you the problem again.

          With the efs file i want to get:
          -a background colored with the trend (red - down ,, green - up)
          -a signal on my cellphone ( but only one time <- and there is the problem i get more than one signal - i dont understand that because i used if (vLastAlert != 2) ... and so on)
          -a signal on my screen (alertlist) < this signal also appears 2 times

          ok i hope i told you this problem better that the post before

          regards eric

          Comment


          • #6
            if u wonder about the 2 tags that i copyed into my text
            i forgot the //
            i only used one of them

            setComputeOnClose()
            or
            if(getBarState()==BARSTATE_NEWBAR);

            but the chart resultat isnt like in my imagion

            regards eric

            Comment


            • #7
              Eric
              Here is a revised version of the script.
              Alex

              PHP Code:
              var study = new MAStudy(50"Close"MAStudy.SIMPLE);
              vLastAlert = -1;

              function 
              preMain() {
                  
              setPriceStudy(true);
              }

              function 
              main() {

              var 
              study.getValue(MAStudy.MA);
              var 
              v1 study.getValue(MAStudy.MA,-1);

                  
              if(
              == null)
              return;

                  if(
              getBarState()==BARSTATE_NEWBAR){
                  
                      if(
              Strategy.isLong()==false && close(-1) >= v1){ 
                          
              Strategy.doLong("Crossing Up"Strategy.MARKETStrategy.THISBAR);
                      }
                      if(
              Strategy.isShort()==false && close(-1) < v1){ 
                          
              Strategy.doShort("Crossing Down"Strategy.MARKETStrategy.THISBAR);
                      }
                  }
                 
                  if(
              Strategy.isLong()==true){
                      
              setBarBgColor(Color.green);
                      if (
              vLastAlert != 1Alert.addToList(getSymbol(), "Es geht hoch!"Color.RGB(0,0,0), Color.RGB(0,255,0));
                      if (
              vLastAlert != 1Alert.email("Es geht hoch!""Es geht hoch!");
                      
              vLastAlert 1;
                      }
                  if(
              Strategy.isShort()==true){
                      
              setBarBgColor(Color.red);  
                      if (
              vLastAlert != 2Alert.addToList(getSymbol(), "Es geht runter!"Color.RGB(0,0,0), Color.RGB(195,0,0));
                      if (
              vLastAlert != 2Alert.email("Es geht runter!""Es geht runter!");
                      
              vLastAlert 2;
                      }

              return 
              v;

              Comment


              • #8
                ok i will test that ... i will write a report about hoe its working ...
                thanks

                regards eric

                Comment


                • #9


                  i dont know but the formula you posted isnt working
                  well the first formula was ok but the signals came to often ...
                  maybe you can change the 1st formula so that the email and add to list signals only come at the trendchanging
                  regards eric

                  Comment


                  • #10
                    Eric
                    Let us review the situation.

                    In your original formula you had the following conditions
                    if close >= 5ma then buy at the open (MARKET) of the bar that closes above (THISBAR)
                    if close < 5ma then sell at the open of the bar that closes below.

                    Now while that may work in real time (creating however another set of problems) it does not work in backtesting because you cannot buy/sell at the open of a the bar that gives a signal on the close.

                    Second issue is that in real time when prices are crossing a moving average they may cross it back and forth intrabar which means continuous buy/sell signals.

                    In light of the above the last formula I posted does the following.
                    When a new bar comes in and the prior bar closed above the ma then it buys at the open of the current bar (ie the new bar).
                    The opposite for a short ie when a new bar comes in and the prior close was below the ma then it sells at the open of the current bar.
                    This way the formula will work correctly (and realistically) in back testing but more importantly also in real time when it will provide only one signal.
                    My suggestion is that you run it on Tick Replay to see how it actually works
                    Alex

                    Comment


                    • #11
                      Eric
                      Following up on my prior message you may want to review how you copied the formula because this is how the $DAXI daily chart appears at my end. In looking closely to your chart it almost seems like you shortened the length of the MA
                      Alex

                      Last edited by ACM; 10-29-2003, 05:29 PM.

                      Comment

                      Working...
                      X