Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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?
      BertrandNinjaTrader Customer Service

      Comment


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

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by DJ888, 04-16-2024, 06:09 PM
        6 responses
        18 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by Jon17, Today, 04:33 PM
        0 responses
        1 view
        0 likes
        Last Post Jon17
        by Jon17
         
        Started by Javierw.ok, Today, 04:12 PM
        0 responses
        6 views
        0 likes
        Last Post Javierw.ok  
        Started by timmbbo, Today, 08:59 AM
        2 responses
        10 views
        0 likes
        Last Post bltdavid  
        Started by alifarahani, Today, 09:40 AM
        6 responses
        41 views
        0 likes
        Last Post alifarahani  
        Working...
        X