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 CarlTrading, 03-31-2026, 09:41 PM
        1 response
        157 views
        1 like
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 04-01-2026, 02:41 AM
        0 responses
        91 views
        1 like
        Last Post CarlTrading  
        Started by CaptainJack, 03-31-2026, 11:44 PM
        0 responses
        143 views
        2 likes
        Last Post CaptainJack  
        Started by CarlTrading, 03-30-2026, 11:51 AM
        0 responses
        130 views
        1 like
        Last Post CarlTrading  
        Started by CarlTrading, 03-30-2026, 11:48 AM
        0 responses
        107 views
        0 likes
        Last Post CarlTrading  
        Working...
        X