Announcement

Collapse
No announcement yet.

Previous Value of a Custom variable

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Previous Value of a Custom variable

    Is there an easy way fo referencing the previous value of a variable which you have calculated by a formula. It is easy to get the previous high for instance by high(-1) but if you have a value that you have calculated say KeltnerUpperValue you cant get the previous value by typing KeltnerUpperValue(-1), or getValue("KeltnerUpperValue,-1) can you?

    I know you can calculate th previous value by entering the calcualtion referencing the previous bars data or built in functions but can't see how you do the same thing with your own calculated values except the long way round. Help please this has got to be easy.

    slipperx

    slipperx
    Honesty and Valour - where did they go?

  • #2
    Slipperx
    If the custom variable is not a series and you are returning it from main then you could use the ref() function.
    For more information on this function see this article in the EFS KnowledgeBase
    Alex

    Comment


    • #3
      Thanks Alex

      Now you've told me I remember seeing it - spent ages trying to find it - I think something needs doing about the knowledgebase and the help files could be a bit more expansive. Your help is greatly appreciated.
      Regards
      slipperx
      Honesty and Valour - where did they go?

      Comment


      • #4
        Alex

        the only problem is the value is not the value being returned but rather a condition within the code to colour bars. Ref() only returns the value of the return output.

        For instance say you were plotting the Momentum in the lower pane and wanted to colour the bars if the Upper Keltner Band (calculated in main()) broke above the Upper Bollinger Band. So you have a value for the Upper Keltner Band from your calculation for the current bar but just want to reference the value of the Upper Keltner band for the previous bar knowing that the function main() returns the momentum value and not the upper band value.

        Is there a straightforward way of soing that. Not so easyas I thought.

        slipperx
        Honesty and Valour - where did they go?

        Comment


        • #5
          Slipperx
          If the values you want to retrieve are those of 2 or 3 bars ago at the most then you could use the method shown below.
          If instead you need historical values that go further back then you may want to use an array. For more information on arrays and the required syntax run a search in the EFS KnowledgeBase for array. You may also want to search the Bulletin Board as this is a topic that has been covered extensively
          Alex

          PHP Code:
          var vValueCurrent null;//global variable
          var vValue1BarAgo null;//as above
          var vValue2BarAgo null;//as above

          function main(){

              if(
          getBarState()==BARSTATE_NEWBAR){
                  
          vValue2BarAgo=vValue1BarAgo;
                  
          vValue1BarAgo=vValueCurrent;
              }
              
              
          vValueCurrent //your calculations for vValueCurrent
              
              
          if(vValue2BarAgo<close(-2)&&vValue1BarAgo>close(-1){
                  
          //commands to execute
              
          }
              
              return 
          vValueCurrent;

          Comment


          • #6
            Thanks Alex thas great.
            slipperx
            Honesty and Valour - where did they go?

            Comment

            Working...
            X