Announcement

Collapse
No announcement yet.

Previous value from the current function

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

  • Previous value from the current function

    Hi there.
    I'm trying to make a function that calculates the number of bars that appears after a condition happens.
    The idea is simple. If a binary indicates 1 on current bar and 0 on the previous bar the no of bars counted should be zero. And if the condition mentioned didn't happen, we should get the previous bar count and sum one.
    The problem is that I need the previous value calculated by the own function being calculated.
    Is this possible?
    I made a research in the forum and the materials in the support area but could find nothing. Every case I checked the problem with the previous value is with a global or local variable.

    The result is:
    Error: efsExternal: Cyclic dependency has been detected


    Thanks in advance.
    The code is below.

    function preMain() {

    setPriceStudy(false);
    setStudyTitle("BS");
    setCursorLabelName("BS", 0 );
    setPlotType(PLOTTYPE_HISTOGRAM,0);
    }

    var bs = null;

    function main() {

    if(getBarState() == BARSTATE_NEWBAR ) {
    var cond = efsExternal("B.F. - 2.2 - Highlight DM BIN.efs");
    var cond0 = cond.getValue(0);
    var cond_1 = cond.getValue(-1);
    //The efsExternal above is a simple binary indicator that defines the condition wich starts the bar count

    var bs_10 = efsExternal("BS.efs");
    var bs_1 = bs_10.getValue(-1);
    //Here is my try to get the previous value calculated in main()

    if( cond0 == -1 && cond_1 != -1) {
    bs = 0
    } else { bs = bs_1 + 1
    }
    } else { bs = null
    }

    return bs;
    }
    Last edited by bdfranca; 10-03-2011, 08:49 PM.

  • #2
    Your issue may have to do with how many elements are returned by the efsExternal function. Returning one element is called differently than if it returns an array of elements.

    This useful post by Alexis Montenegro might help:



    Wayne

    Comment


    • #3
      bdfranca, I responded to you before about the "bInit pattern" in your coding. You're not applying it in the example you posted.

      Again, I have no idea what you're trying to do since I don't see the code in your other EFS files, but I do know the coding rules:

      1. You initiate a series object only ONCE in your code and then you extract values from it for the rest of its lifetime.

      2. Given #1, that means that your series objects will be GLOBAL variables, not local.

      PHP Code:
      var bInit false;
      var 
      bs 0;
      var 
      cond null;
      var 
      bs_10 null;

      function 
      preMain() {
        
      setPriceStudy(false);
        
      setStudyTitle("BS");
        
      setCursorLabelName("BS"0);
        
      setPlotType(PLOTTYPE_HISTOGRAM0);
      }

      function 
      main() {
        if (
      bInit == false) {
          
      cond efsExternal("B.F. - 2.2 - Highlight DM BIN.efs");
          
      bs_10 efsExternal("BS.efs");
          
      bInit true;
        }
        if (
      getBarState() == BARSTATE_NEWBAR) {
          var 
      cond0 cond.getValue(0);
          var 
      cond_1 cond.getValue(-1);
          
      //The efsExternal above is a simple binary indicator that defines the condition wich starts the bar count
          
      var bs_1 bs_10.getValue(-1);
          
      //Here is my try to get the previous value calculated in main() 
          
      if (cond0 == -&& cond_1 != -1) {
            
      bs 0
          
      } else {
            
      bs bs_1 1;
          }
        } else {
          
      bs 0;
        }
        return 
      bs;

      Comment


      • #4
        waynecd
        Thanks for the post but my problem is kind different. I need to call the previous value of the indicator that is being calculated by the same indicator.
        For example, to calculate BS(0) I need BS(-1). So how can I call BS(-1) inside BS(0)? The function file is BS.efs, how can I use the line:
        bsx0 = efsExternal(BS.efs); bsx = bsx0.getValue(-1);
        inside the own BS.efs file without getting the cyclic dependency error?

        SteveH
        Sorry disturbing you but EFS language is being really dificult for me. I couldn`t even understand the first answer you gave me. Now I think I get it. The variables I want to call the values trought the formula must be inside bInit condition. That being true the code you posted is flawless. But the cyclic dependency error still remains. I hired a java professional but the sintax of esignal has it own characteristics and that`s what is freezing me a lot.
        Thanks a lot for your help, I apply yours answers in many different ways in other problems I have here.
        And sorry if is a simple question.

        Now I`m working based on SteveH`s post but I`m stuck again.
        I`ll try to be more clear about I want. I coded it in other software so I can make you a picture.
        There're 3 indicators there. Ema(30), a binary (1 mean close cross ema(30) upwards) and the bar count.
        So, when close cross ema(30) upwards (and upwards only) the count of bars is set to zero. Each following bar that appears and doesn`t present the condition (close cross ema(30) upwards) will sum 1 on the previous value of bar count untill the condition happens again and the bar count is set to zero again.


        Again, thanks a lot for helping me.

        Rergards
        Attached Files

        Comment


        • #5
          Is this what you want? [Tested only with v10.6]

          Just add a 30 ema from the basic studies onto your price chart so you can compare the histogram with that.
          Attached Files
          Last edited by SteveH; 10-04-2011, 02:36 PM.

          Comment


          • #6
            Same study but with options added to change ema, bar thickness and colors so you can see the kinds of things you need to do if you want to get fancy with options for "Edit Studies" in your future scripts.

            Notice in both scripts that I used setComputeOnClose() in the preMain(). There's no need for this script to look at each price transaction. If you want to include it in another script that does, then sure, you remove the setComputeOnClose() and check for BARSTATE_NEWBAR like you were doing with your script initially.

            bdfranca.efs took 5 mins to write...just the amount of time it took to type it. In the future, if you describe a problem you want simply and thoroughly, others here may know exactly what to do in no time flat and volunteer their code for your cause.
            Attached Files
            Last edited by SteveH; 10-04-2011, 02:47 PM.

            Comment


            • #7
              That's it! But there's a problem. Well... I'll explain everything cause it's easier.
              My objective here is to do a function wich I give it two external binary values that determines 2 different conditions that are excludent.

              Why external binary values? Cause this is easier to me to test various ideias mutually exclusive, dispite my terrible EFS language knowledge I`m capable to make binary functions. I used the binary close x ema(30) to explain the idea.

              So... 2 conditions may happen 1 or -1. If 1 happened, the function should plot 1 until and only if -1 happens. 1 may even happen again but it shouldn`t change nothing, it will change only and if -1 happen. And when -1 happens, it will repeat -1 until and only if 1 happens.
              Why am I concerned about bar count so? Cause thats the only way that I know to do a function with 2 conditions mutually exclusive.
              How it works?
              Set condition 1 as a binary that returns me 1. (FUNCTION 1)
              Set condition 2 as a binary that returns me -1. (FUNCTION 1)
              Set the bar count for condition 1. (FUNCTION 2)
              Set the bar count for condition 2. (FUNCTION 2)
              Make a comparison and highlight the bars.(FUNCTION 2)
              So if BarCount1 < BarCount2 it means that condition 1 has happened more recently and the bars should be painted in green (for example). The contrary is true, when condition 2 happens more recently BarCount1 > BarCount2 and the bars should be painted in red (for example).
              See figure attached. We are talking about lines on window number 3 in figure. There we have two counts of numbers of bars. One for each condition that can happen (close cross ema(30) up (GREEN LINE) against close cross ema(30) down (RED LINE)). This condition is for example purposes, I know that the conditions used here are already mutually exclusive, think in binary terms.
              What I need is in window number 4. It`s exactly that.

              I tried many different ways and I even got to a feasible solution this way:
              Set condition 1 as a binary that returns me 1. (FUNCTION 1)
              Set condition 2 as a binary that returns me -1. (FUNCTION 1)
              Set the bar count for condition 1. (FUNCTION 2)
              Set the bar count for condition 2. (FUNCTION 3)
              Make a comparison and highlight the bars.(FUNCTION 4)

              Function 1, it's a simple binary that determines the conditions, it's in file > B.F. - 2.1 - Highlight DM BIN
              Function 2, bar count for positive condition (1), it's in file > B.F. - 2.2 - Highlight DM BS P
              Function 3, bar count for negative condition (-1), it's in file > B.F. - 2.2 - Highlight DM BS N
              Function 4, comparison of count of number of bars and highlight > B.F. - 2.3 - Highlight DM BS Final

              Ok, I know I don`t need 4 functions to get the result I need but it was the only way that I could solve it.
              Even though I got to this solution it still has a problem.

              If I use functions like setComputeOnClose or anyone of those that gets the bars state, I need to refresh the chart every time a new bar closes cause it doesn`t paint the bars automatically. If I don`t refresh every new bar is painted in grey.
              And if I don`t use an kind of functions mentioned above (setComputeOnClose or bar state functions), each time price changes the function is calculated again and the sum used in the bar count skyrocket the bar count value instead of differ of its previous value by only one. Them the comparison of the numbers of bars counted after a condition happened just don`t make sense anymore cause it`s biased.

              And yes, I am paying someone to help me. But the guy has never seen Esignal or worked with financial market. And I have never seen EFS codes, well to be true that`s the third week working with EFS language and the only knowledge I have about programming language I learned digging excel and metastock.

              SteveH, send me bank name and acc number that I`ll ask the programmer I hired to send you the money I paid him. Lol!

              Thanks a lot for all attention!
              Attached Files

              Comment


              • #8
                FUNCTION 1
                Attached Files

                Comment


                • #9
                  FUNCTION 2
                  Attached Files

                  Comment


                  • #10
                    FUNCTION 3
                    Attached Files

                    Comment


                    • #11
                      FUNCTION 4

                      I didn't know that the files names were modified when attached.
                      The function names are:

                      1 - B.F. - 2.1 - Highlight DM BIN.efs
                      2 - B.F. - 2.2 - Highlight DM BS P.efs
                      3 - B.F. - 2.2 - Highlight DM BS N.efs
                      4 - B.F. - 2.3 - Highlight DM BS Final.efs
                      Attached Files

                      Comment


                      • #12
                        SteveH, I rewrote all formulas based on yours but the problem in realtime persists.
                        If I use setComputeOnClose() it simply doesn't paint the bars automatically. To do it I must refresh the chart.

                        If I use getBarState() == BARSTATE_NEWBAR sometimes it doesn't respect the condition given until I refresh the chart.

                        An if I do the inverse, using (getBarState() == BARSTATE_CURENTBAR) {bs=0} else {FORMULA}. bs keep equal 0 until I refresh the chart.
                        Last edited by bdfranca; 10-05-2011, 09:17 AM.

                        Comment


                        • #13
                          POST SUMMARIZED

                          As I said I rewrote all formulas based on SteveH code. Thanks SteveH.
                          But it still doesn't work in realtime.
                          In real time if use setComputeonClose or any of the bar state function I must keep refreshing the chart to get the right result. On market closed I have no problem but when it's open the results are biased till I refresh the chart.
                          I don'n think the codes are wrong but I don't know what else to do...
                          I'll leave the modified formulas here if anyone may help me.

                          The idea is simple.
                          Formula 1 calculates a simple binary (1 and -1).
                          Formula 2 count the no of bars after 1 an -1 happened (one formula for each condition).
                          Formula 3 compare the results and paint the bars following the comparison.

                          What I want?
                          I would like to have a base formula that has as an input a simple binary formula and that counts the number of bars that happened after the given binary condition happened.
                          Let's call this formula BarsSince.
                          So, to determine a mutually exclusive condition I would just need to calculate two simple binary condition, one for each case, and make a new script where I would use BarsSince (with efsExternal) to count the numbers of bars that happened after each binary condition happened.
                          In this same formula I would compare the results (the number of bars counted for each condition that happened) and paint the bars following the comparison.

                          If there's any questions just pm me. I'll be here all day long, this formula is really important for me.

                          Thanks in advance for those that may help and for those thats already helping me.

                          Bruno Franca
                          Attached Files
                          Last edited by bdfranca; 10-05-2011, 09:56 AM.

                          Comment


                          • #14
                            Problem example...
                            If use setComputeOnClose a similar problem happens.
                            And if I refresh the chart then I get the right result.

                            Thanks again.

                            Bruno Franca
                            Attached Files
                            Last edited by bdfranca; 10-05-2011, 10:07 AM.

                            Comment


                            • #15
                              Right (after refreshing the chart)
                              Attached Files
                              Last edited by bdfranca; 10-05-2011, 11:23 AM.

                              Comment

                              Working...
                              X