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

PVSRA candles, last candle doesn't get correct color.

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

    PVSRA candles, last candle doesn't get correct color.

    I am trying to code PVSRA candles. It work well for all past candles but not for the last current candle. The last candle is always red or green. I need help.

    Code:
    protected override void OnBarUpdate()
    {
    pvsra_volume = Volume[0];
    pvsra_high = High[0];
    pvsra_low = Low[0];
    pvsra_close = Close[0];
    pvsra_open = Open[0];
    
    
    //label.new(overridesym ? 0 : na, low, text = "PVSRA Override: " + pvsra_sym, xloc = xloc.bar_index, yloc=yloc.belowbar,style=label.style_label_down, size=size.huge)
    
    // The below math matches MT4 PVSRA indicator source
    // average volume from last 10 candles
    //if (CurrentBars[0] == 0) return;
    
    sumVolume = SUM(Volume, 10)[0];
    av = sumVolume / 10;
    //climax volume on the previous candle
    value2 = Volume[0]*(High[0]-Low[0]);
    // highest climax volume of the last 10 candles
    
    // Add the current value to the list
    value2List.Add(value2);
    
    // Remove the oldest value if there are more than 10
    if (value2List.Count > 10)
    {
    value2List.RemoveAt(0);
    }
    
    // Compute the maximum value over the last 10 (or fewer) bars
    if (value2List.Count >= 10)
    {
    hivalue2 = value2List.Max();
    }
    
    // VA value determines the bar color. va = 0: normal. va = 1: climax. va = 2: rising
    va = (Volume[0] >= (av * 2) || value2 >= hivalue2) ? 1 : Volume[0] >= av * 1.5 ? 2 : 0;
    
    // Bullish or bearish coloring
    isBull = Close[0] > Open[0];
    
    // candleColor = iff(climax,iff(isBull,CUColor,CDColor),iff(aboveA, iff(isBull,AUColor,ADColor),iff(isBull,NUColor,NDC olor)))
    
    CUColor = new SolidColorBrush(Color.FromArgb(255, 0, 255, 0));
    CDColor = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));
    AUColor = new SolidColorBrush(Color.FromArgb(255, 0, 0, 255));
    ADColor = new SolidColorBrush(Color.FromArgb(255, 255, 0, 255));
    NUColor = new SolidColorBrush(Color.FromArgb(255, 153, 153, 153));
    NDColor = new SolidColorBrush(Color.FromArgb(255, 77, 77, 77));
    
    candleColor = isBull ? NUColor : NDColor;
    
    if (va == 1)
    {
    candleColor = isBull ? CUColor : CDColor;
    }
    else if (va == 2)
    {
    candleColor = isBull ? AUColor : ADColor;
    }
    
    BarBrushes[0] = candleColor;
    CandleOutlineBrushes[0] = candleColor;
    
    
    }

    #2
    Hello supremeMarshall,

    Are you running the script OnBarClose? If so the last bar is not included in OnBarUpdate calls.

    If that is the situation you can use OnPriceChange or OnEachTick instead which includes the building bar. If you need your logic to run OnBarClose still you can surround that logic with a condition checking for the first tick of the bar. https://ninjatrader.com/support/help...ghtsub=isfirst
    JesseNinjaTrader Customer Service

    Comment


      #3
      I have tried using on each tick and all calculations aren't working correctly. I tried with `if (IsFirstTickOfBar)` and still doesn't work. All candles are gray for a couple of times and then all candles are always red or green. All past candles does have the correct color but not on every bar created.
      Last edited by supremeMarshall; 10-03-2023, 11:21 AM.

      Comment


        #4
        Hello supremeMarshall,

        To clarify you see that when the script is applied all the historical bars up to the current bar are correct and then in Realtime the building bar is being colored sometimes and other times not?

        I am not sure based on the description what the problem is, you mentioned that all bars are gray sometimes and then other times they are red or green, did you mean the right most building bar? The next statement leads me to believe there is not a problem with historical because you mentioned all past candles have correct color.

        JesseNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Jesse View Post
          Hello supremeMarshall,

          To clarify you see that when the script is applied all the historical bars up to the current bar are correct and then in Realtime the building bar is being colored sometimes and other times not?

          I am not sure based on the description what the problem is, you mentioned that all bars are gray sometimes and then other times they are red or green, did you mean the right most building bar? The next statement leads me to believe there is not a problem with historical because you mentioned all past candles have correct color.
          All historical bars are fine. All new bars aren't being calculated properly. All new bars are all the bars since I applied the indicator.

          Comment


            #6
            Click image for larger version

Name:	image.png
Views:	232
Size:	156.5 KB
ID:	1271338​This is running the script on each tick.

            Comment


              #7
              Hello supremeMarshall,

              You may want to try the logic without surrounding it with a first tick of bar condition, the condition in realtime is going to be executed for the first tick of the bar so only the opening values of the bar would be known. You would have to update the bar for each tick if you wanted to color the building bar continuously until it closes.
              JesseNinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_Jesse View Post
                Hello supremeMarshall,

                You may want to try the logic without surrounding it with a first tick of bar condition, the condition in realtime is going to be executed for the first tick of the bar so only the opening values of the bar would be known. You would have to update the bar for each tick if you wanted to color the building bar continuously until it closes.
                In my last image I am not surrounding my code with "first tick of bar condition", all of the codes run on each tick and the color aren't the same as historical bars.

                Comment


                  #9
                  Hello supremeMarshall,

                  When you use OnBarClose calculation mode do you see that all new bars in realtime match historical?
                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by NinjaTrader_Jesse View Post
                    Hello supremeMarshall,

                    When you use OnBarClose calculation mode do you see that all new bars in realtime match historical?
                    When I use OnBarClose, all new bars aren't matching historical.

                    Comment


                      #11
                      Hello supremeMarshall,

                      That means the logic is working differently in those two use cases. To test what is different you would need to add prints into your logic to check that it is working identically in both use cases while using OnBarClose calculation mode. Historical processing always uses OnBarClose so if you use that in realtime the general expectation is that your logic is going to be executed in the same way.

                      JesseNinjaTrader Customer Service

                      Comment


                        #12
                        supremeMarshall that's looks very promising!

                        Could you fix the problem with last bar finally?

                        I was trying to find vector candles or PVSRA indicators for NT. Are you planning to share the indicator with the community? That would be awesome!

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by burtoninlondon, Today, 12:38 AM
                        0 responses
                        5 views
                        0 likes
                        Last Post burtoninlondon  
                        Started by AaronKoRn, Yesterday, 09:49 PM
                        0 responses
                        14 views
                        0 likes
                        Last Post AaronKoRn  
                        Started by carnitron, Yesterday, 08:42 PM
                        0 responses
                        11 views
                        0 likes
                        Last Post carnitron  
                        Started by strategist007, Yesterday, 07:51 PM
                        0 responses
                        13 views
                        0 likes
                        Last Post strategist007  
                        Started by StockTrader88, 03-06-2021, 08:58 AM
                        44 responses
                        3,982 views
                        3 likes
                        Last Post jhudas88  
                        Working...
                        X