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 CortexZenUSA, Today, 12:53 AM
        0 responses
        1 view
        0 likes
        Last Post CortexZenUSA  
        Started by CortexZenUSA, Today, 12:46 AM
        0 responses
        1 view
        0 likes
        Last Post CortexZenUSA  
        Started by usazencortex, Today, 12:43 AM
        0 responses
        5 views
        0 likes
        Last Post usazencortex  
        Started by sidlercom80, 10-28-2023, 08:49 AM
        168 responses
        2,265 views
        0 likes
        Last Post sidlercom80  
        Started by Barry Milan, Yesterday, 10:35 PM
        3 responses
        11 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Working...
        X