Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Plotting the VMA difference

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

    Plotting the VMA difference

    I have a simple indicator by which I am trying to plot the difference between two VMAs. It compiles perfectly well, but I do not see anything on the chart. I have checked and re-checked the code, but have no clue what is wrong here. Can someone help please?

    All I want to do is to backcolor the chart when the differences between two VMAs is greater than 2.

    protected override void OnBarUpdate()
    {VmaDifference.Set(VMA(34,34)[0]-VMA(500,14)[0]);
    if (VmaDifference[1]<2
    && VmaDifference[0]>=2)
    {
    BackColorAll = Color.Green;
    }
    else if
    (VmaDifference[1]>-2
    && VmaDifference[0]<=-2)
    {
    BackColorAll = Color.Green;
    }
    else
    {
    BackColorAll = Color.Blue;
    }

    #2
    To identify the problem, you would need to post the entire code.

    One possible explanation for the bug is that you try to access VMADifference[1] for the first bar, which is CurrentBar == 0. For this bar VMADifference[1] is not yet defined. You would need to change the code to

    Code:
    protected override void OnBarUpdate()
            {VmaDifference.Set(VMA(34,34)[0]-VMA(500,14)[0]);
    if (CurrentBar < 1)  // line added
       return;   // line added
    if        (VmaDifference[1]<2
                    && VmaDifference[0]>=2)
                {
                    BackColorAll = Color.Green;
                 }
                else if                 
                    (VmaDifference[1]>-2
                    && VmaDifference[0]<=-2)
                {        
                    BackColorAll = Color.Green;
                }
                else 
                {
                   BackColorAll = Color.Blue;
                }
    }
    Also you should not compare the VMADifference to an absolute value such as 2. Such a value maybe correct for a specific instrument and a specific timeframe. But you would need to recode your indicator for use with every different instrument or timeframe. Best compare the VMADifference with the multiple of the average true range.

    Comment


      #3
      pandyav, were you able to resolve with Harry's kind pointer?

      Comment


        #4
        Yes, I was. Thank you for checking in.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        558 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        324 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        101 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        545 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        547 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X