Announcement

Collapse
No announcement yet.

What have I done wrong?

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

  • What have I done wrong?

    Heeeelp!!! All I'm trying to do is have a green arrow belowbar when macd turns up and red arrow abovebar for when it turns down. I tip my hat to those of you who have a grasp of this coding thingy. I've got long way to go

    Thanks
    David
    Attached Files
    I've never had a losing trade, I just ran out of time.

  • #2
    David,

    There are several things [problems] with your code:
    a/ you address myRef as array, when is not declared as such [maybe EFS takes it as array, but not documented],
    b/ you did not declare your macd as global var and it should,
    c/ you refer to "macd" as a regular var, when it is a series and should use "getValue"

    Correct those and will work.
    Something like:

    var stMACD=macd(x,y,z);

    Then, in main

    if (stMACD.getValue(0)>stMACD.getValue(-1)) {...}
    else if (....) {...}

    Good Luck!
    Last edited by mbuta; 10-19-2008, 09:04 AM.
    Mihai Buta

    Comment


    • #3
      Mihai

      a/ you address myRef as array, when is not declared as such [maybe EFS takes it as array, but not documented],
      FWIW myRef is an array in this case because the script is returning multiple values.
      The functionality of ref() as an array is documented in this article in the EFS KnowledgeBase and a complete example of its use as an array is provided in this article also in the EFS KnowledgeBase
      Alex



      Originally posted by mbuta
      David,

      There are several things [problems] with your code:
      a/ you address myRef as array, when is not declared as such [maybe EFS takes it as array, but not documented],
      b/ you did not declare your macd as global var and it should,
      c/ you refer to "macd" as a regular var, when it is a series and should use "getValue"

      Correct those and will work.
      Something like:

      var stMACD=macd(x,y,z);

      Then, in main

      if (stMACD.getValue(0)>stMACD.getValue(-1)) {...}
      else if (....) {...}

      Good Luck!

      Comment


      • #4
        Re: What have I done wrong?

        David
        While the use of ref() is technically correct it is not required in this case. This is because unlike the script you were working on in this thread where Filt was a single value and not a series (which means it did not store historical values of itself) the macd() function instead creates a series which means that you can access its prior values using the getValue() method. If for example you declare the MACD study as follows
        PHP Code:
        var myMACD macd(12,26,9); 
        you can then retrieve any of its prior values in the following way
        PHP Code:
        myMACD.getValue(0)//value of myMACD at the current bar
        myMACD.getValue(-1)//value of myMACD at the prior bar
        myMACD.getValue(-2)//value of myMACD two bars ago
        //etc 
        Mihai's suggestion will allow you to identify when the the MACD is rising or falling however it is not quite complete as is because the arrows will be drawn on every bar in which the MACD is rising or falling rather than just at the turning points.
        So what you need to do in a case like this is to create a condition that is unique eg
        PHP Code:
        if(myMACD.getValue(-1) < myMACD.getValue(-2) && myMACD.getValue(0) > myMACD.getValue(-1){
        //if the MACD of one bar ago was less than the MACD of two bars ago and the current MACD is greater than 
        //the MACD at the prior bar
            
        drawShape(....);

        which will allow you to identify the specific point when the MACD is turning (upwards in the example). You would then write an opposite condition for when the MACD turns downwards.
        This way the arrow will be drawn only on the bars in which the MACD has turned and not on every bar.
        Having said that even this solution is not quite complete because if you are running the script in real time the MACD could turn upwards or downwards intrabar - which would draw the arrow - but then at the close of the bar it could continue in its prior direction. Since in the above code there is no provision to remove the arrow this would remain on the chart even if the MACD has not reversed its course. If you then reloaded the efs you would see those shapes disappear because the conditions would no longer be true on historical bars.
        So the logic would need to be as follows
        PHP Code:
        if(myMACD.getValue(-1) < myMACD.getValue(-2) && myMACD.getValue(0) > myMACD.getValue(-1)){
        //if the MACD of one bar ago was less than the MACD of two bars ago and the current MACD is greater than 
        //the MACD at the prior bar
            
        drawShape(Shape.UPARROWmyMACD.getValue(0), Color.bluerawtime(0));
        }else{
            
        removeShape(rawtime(0))

        This way if the condition is true the arrow will be drawn and if at any point during that bar the condition is no longer true the arrow will be removed
        Note that in the above example I replaced the AboveBar1 location parameter with the value of myMACD at the current bar. This is because AboveBar1, BelowBar1, etc are referred to the price bars and would draw the arrows completely outside of the range of the MACD indicator pane.
        Alex



        Originally posted by David Vels
        Heeeelp!!! All I'm trying to do is have a green arrow belowbar when macd turns up and red arrow abovebar for when it turns down. I tip my hat to those of you who have a grasp of this coding thingy. I've got long way to go

        Thanks
        David

        Comment


        • #5
          Thankyou Mihai and Alex for your prompt replies. Both of your answers have been very educational. For whatever reason I was unable to get both uparrows and downarrows working in the same study. What I've done is save them as two separate studies (MACD-UP and MACD-DOWN) and then merge them on the chart and this works fine.

          Many thanks again
          David
          I've never had a losing trade, I just ran out of time.

          Comment


          • #6
            David
            You are most welcome
            Post the script as you have modified and I or someone else can take a look at it and provide you with some guidance
            Alex


            Originally posted by David Vels
            Thankyou Mihai and Alex for your prompt replies. Both of your answers have been very educational. For whatever reason I was unable to get both uparrows and downarrows working in the same study. What I've done is save them as two separate studies (MACD-UP and MACD-DOWN) and then merge them on the chart and this works fine.

            Many thanks again
            David

            Comment

            Working...
            X