Announcement

Collapse
No announcement yet.

retaining a value in a custom formula

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

  • retaining a value in a custom formula

    Is there a way to retain values in a formula, so that (say) the value for the most recent swing high can be plotted as a horizontal line and then reset to the new level when a new swing high occurs? I've tried adapting code from existing studies like pivots, but no luck. Any ideas gratefully received!

  • #2
    Hi,

    If all you are trying to do is create a variable whos contents will survive through new iterations of main(), then define the variable outside main() and preMain().

    preMain(){
    .
    .
    .
    }

    // Variables defined here will keep their contents through
    // iterations of main

    var IwantToSaveThisInfo;

    main(){
    .
    .
    .
    }
    Garth

    Comment


    • #3
      Hi Garth

      Many thanks for the response. I'm not quite sure whether I made myself clear in the original post. I'm basically trying to write a study so that when a pattern occurs, the high of the last bar of the pattern is retained (and can be plotted as a horizontal line on a chart) until the next time the pattern occurs when the value/line will change to the high of the last bar of the new pattern.

      The problem I'm having is that I can write the code for the pattern OK, but obviously if I just ask it to return the high of the pattern bar, this will simply reiterate for each following bar and I end up with a study that plots the high of each bar.

      Is that what you thought I meant?

      Many thanks.

      Andy

      Comment


      • #4
        Hi,

        I guess I misunderstood. What exactly is the problem you are trying to solve. Remebering the vaule through each iteration of main() is what I was thinking the problem was, but is it drawing the lines? It's still not clear to me...

        G
        Garth

        Comment


        • #5
          If I understood correctly I think he wants to do something like in the attached image.
          Assume we have a formula that draws a line from Get Pivots (both highs and lows) then the line would remain at the last Pivot value until a new high/low Pivot is put in place.
          Alex
          Attached Files

          Comment


          • #6
            I'm trying to write a study so that every time a particular chart pattern occurs, the high of the last bar of the pattern is captured and displayed on the chart as a horizontal line. (It's the possible basis for a breakout trading system so the idea is to look for upward breaches by prices of this line.)

            Each time a new instance of the pattern occurs I want to discard the high of the previous instance of the pattern and start using the high of the new instance . And so on for each subsequent instance of the pattern.

            In terms of appearance, the study based on this code would look something like that of the x bar high study, though changes in the level would be rather less frequent as the pattern is obviously less common as it won't occur every x bars.

            Hope this makes a bit more sense - apologies for my lack of clarity in previous posts. Many thanks.

            A

            Comment


            • #7
              Hi Alexis

              That's exactly what I mean. I just can't get my head around how to retain the value until the next reset. Many thanks.

              A

              Comment


              • #8
                OK, well there are two parts to this problem. One is retaining the value in the EFS, which is done as I stated previosuly. The second, is how best to create the line.

                The easiest way to do this, is to just return the value you saved in the EFS. For example if it is the high():

                PHP Code:
                var LastHigh 0;

                main(){

                if (
                high() > LastHigh){
                   
                LastHigh high();
                }
                return(
                LastHigh);


                Would be a very simple way of always drawing a line at the highest high in a formula. You of course would do your pattern search instead of just looking for a high() > LastHigh, but the idea is the same. Does this help?


                Garth
                Garth

                Comment


                • #9
                  Many thanks Garth. I've put a sample of the code below, but for some reason I keep getting a syntax error message:

                  "missing ; before main(){ " which doesn't make a lot of sense. Am I doing something dumb?






                  function preMain() {
                  setPriceStudy(true);
                  setStudyTitle("Last High");
                  setCursorLabelName("LH");
                  setPlotType(PLOTTYPE_FLATLINES);
                  }

                  var LastHigh = 0;

                  main(){

                  if (high() > LastHigh){
                  LastHigh = high();
                  }
                  return(LastHigh);

                  }

                  Comment


                  • #10
                    Apologies

                    Apologies.

                    I'd omitted the rather crucial word "function" before "main". Duh!

                    Comment


                    • #11
                      Hi,

                      It always happens, you figure out the problem right after you ask someone for help. I think it is part of Murphy's Law.

                      I assume everything is working now?
                      Garth

                      Comment

                      Working...
                      X