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

Bar Brush not coloring

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

    Bar Brush not coloring

    Having a slight problem. I have a flag that is set and then it checks to see if that flag is set and a certain other attribute happens. If so, it colors that bar and adds text below the bar and then I'm setting the flag that got into that IF statement to false.

    For some reason if I remove that flag in IF statement the bar colors correctly but then it colors all the bars after that signal as well and adds the text BUT I just want to get the first instance of signal - color the bar and add the text and then remove the signal so it just does the first bar that got triggered and none of the other bars after that even if it follows the same signal.

    What is happening though is IF I set the signal to false after coloring the bars and adding the text it will only add the text but not color the bar. WHY?

    I even did a print statement after setting the barColor and setting the flag to false and shows the right color for the bar but what is printed is not the right color on the chart.

    Code:
    if (over)
                {
    
                    BarBrush = Brushes.White;
                    CandleOutlineBrush = Brushes.White;
    
                    if(EMA1.EmaClose[0] > VWAP[0])
                    {
                        secondary = false;
                    }
    
    
                    if(secondary && High[0] < VMA1[0] )
                    {
                        BarBrush = Brushes.Purple;
                        CandleOutlineBrush = Brushes.White;
                        Draw.Text(this, "secondary" + Close[0], false, "Arming", 0, Low[0] - 10, 0, Brushes.Cyan, new NinjaTrader.Gui.Tools.SimpleFont("Courier New", 20) {Bold = true }, TextAlignment.Center, null, Brushes.Black, 100);
    
                        secondary = false;
                        Print("Color: " + BarBrush);
                    }
    
                }​
    You can see the Text is there that says "Arming" but the bar color is not Purple like expected but the Print command is showing that it is when tested. Why would it not color the bar? Again if I remove that `secondary = false" in the IF statement above the Print statement it colors the bar(s) but I don't want all the bars to be colored just the first instance of it.

    Click image for larger version

Name:	image.png
Views:	183
Size:	21.9 KB
ID:	1224179

    #2
    Hello vegas3416,

    If you are not familiar with using prints to debug and understand behavior, please start by watching the videos on the forum post linked below.


    Next, above the condition in question, print the time of the bar and all values used in the condition along with labels for all values and all comparison operators.

    If the condition at the top is the only condition in question, then just print the bool with the time of the bar and a label.

    Provide the output from the output window saved to a text file.

    Let me know at what time you are expecting the bool to be false, when it is true.

    Then find the condition where this bool is being set and do the same, print the time of the bar and all values in that condition.

    I am happy to assist with analyzing the output.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      This is the text output: ​You can see right before it is set armed is True and then I have Before armed is being USED vs being set. Then after its used and AFTER I set it to false you see it set to false and the BarBrush color as well is the color expected but not showing up. All I can think of is the Time its being set is and the time it turns to false are the same but there is a race condition going on and its not actually looking at the specific time down to the milliseconds or something. I'm sure there is a minor tweak to get that past that I'm just not sure what exactly that is unless there needs to be a bar condition that this is all wrapped in so it only does one check on each bar or something like that.

      Two pictures attached and the chart picture has the times showing as well to follow that also.


      Click image for larger version

Name:	image.png
Views:	96
Size:	359.1 KB
ID:	1224224

      Click image for larger version

Name:	image.png
Views:	87
Size:	27.2 KB
ID:	1224225

      Comment


        #4
        Hello vegas3416,

        What is being printed here?

        Try printing the variable used in the condition above the condition that is setting the bar to white.

        Print(string.Format("{0} | over: {1}", Time[0], over));

        What outputs from this?

        On what line is over being assigned a value?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          So at 10:00 the variable “armed” is true and the color for barBrush prior is white.

          then at 10:00 also next line shows armed going to false and barBrush is setting to purple as it should and then the false afterwards is just showing that armed at this point is false

          I just have no idea why if I remove the setting of “armed” to false the bar color does change to Purple as expected but when I change it to false in the if statement so that the next time around it won’t enter that IF statement it’s like it’s overwritting the color as if it doesn’t care that I set it

          Comment


            #6
            Hello vegas3416,

            So your inquiring about the condition:
            Code:
            if(secondary && High[0] < VMA1[0] )
            Then make a print for this condition one line above the condition.

            Print(string.Format("{0} | secondary: {1} && High[0]: {2} < VMA1[0]: {3}", Time[0], secondary, High[0], VMA1[0]));

            There is no variable 'armed' in the code you have posted in post # 1.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Here is the output of what you suggested and I added in the barColor as well:

              11/17/2022 10:00:00 AM | armed: True && High[0]: 11615.75 < VMA1[0]: 11617.0782892338 -- BarColor: #FFFFFFFF - before call
              11/17/2022 10:00:00 AM | armed: False && High[0]: 11615.75 < VMA1[0]: 11617.0782892338 -- BarColor: #FF800080 - after call


              Code:
              if(armed && High[0] < VMA1[0])
                              {
              
                                  Print(string.Format("{0} | armed: {1} && High[0]: {2} < VMA1[0]: {3} -- BarColor: {4} - before call", Time[0], armed, High[0], VMA1[0], BarBrush));
              
                                  BarBrush = Brushes.Purple;
                                  CandleOutlineBrush = Brushes.White;
                                  Draw.Text(this, "armed" + Close[0], false, "Arming", 0, Low[0] - 10, 0, Brushes.Cyan, new NinjaTrader.Gui.Tools.SimpleFont("Courier New", 20) {Bold = true }, TextAlignment.Center, null, Brushes.Black, 100);
              
                                  armed = false;
                                  Print(string.Format("{0} | armed: {1} && High[0]: {2} < VMA1[0]: {3} -- BarColor: {4} - after call", Time[0], armed, High[0], VMA1[0], BarBrush));
                              }​

              Comment


                #8
                Even added another print statement outside of the IF statement to see if the candle color was still set and it was so not sure why its still getting overwritten:

                11/17/2022 10:00:00 AM | armed: True && High[0]: 11615.75 < VMA1[0]: 11617.0782892338 -- BarColor: #FFFFFFFF - before call
                11/17/2022 10:00:00 AM | armed: False && High[0]: 11615.75 < VMA1[0]: 11617.0782892338 -- BarColor: #FF800080 - after call
                11/17/2022 10:00:00 AM | armed: False && High[0]: 11615.75 < VMA1[0]: 11617.0782892338 -- BarColor: #FF800080 - outside of call​ ----THIS IS OUTSIDE OF IF STATEMENT

                Comment


                  #9
                  Hello vegas3416,

                  From post # 6:
                  Then make a print for this condition one line above the condition.
                  Please watch the video NinjaScript Editor 401 which discusses why you must add the print outside of the condition so that it prints when the condition is not evaluating as true.

                  Review this video a few times to fully understand the concept of how to use prints to understand behavior.

                  Only print what is in the condition in question.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Ok. So looking at this further. It seems it has to do with the current bar. Is there a line outside of each run that says after you get finished and it runs again it waits until the current bar its on is done before it runs again?

                    Code:
                    11/17/2022 10:00:00 AM | armed: True && High[0]: 11615.75 < VMA1[0]: 11617.0782892338 -- BarColor:  - outside of check  -- (This is outside of the overall check it does before it gets into the main IF statement to do this check)
                    11/17/2022 10:00:00 AM | armed: True && High[0]: 11615.75 < VMA1[0]: 11617.0782892338 -- BarColor: #FFFFFFFF - outside IF statement
                    11/17/2022 10:00:00 AM | armed: True && High[0]: 11615.75 < VMA1[0]: 11617.0782892338 -- BarColor: #FFFFFFFF - before call
                    11/17/2022 10:00:00 AM | armed: False && High[0]: 11615.75 < VMA1[0]: 11617.0782892338 -- BarColor: #FF800080 - after call
                    11/17/2022 10:00:00 AM | armed: False && High[0]: 11615.75 < VMA1[0]: 11617.0782892338 -- BarColor: #FF800080 - outside of call
                    11/17/2022 10:00:00 AM | armed: False && High[0]: 11615.75 < VMA1[0]: 11617.0782892338 -- BarColor: #FF800080 - outside of check
                    11/17/2022 10:00:00 AM | armed: False && High[0]: 11615.75 < VMA1[0]: 11617.0782892338 -- BarColor: #FFFFFFFF - outside IF statement
                    11/17/2022 10:00:00 AM | armed: False && High[0]: 11615.75 < VMA1[0]: 11617.0782892338 -- BarColor: #FFFFFFFF - outside of call​

                    Since its all the same time its running the code again over and over again it seems which is converting the bar back but maybe I'm missing a call I'm not aware of that tells it after you do your run you don't run again until the next bar occurs.

                    Comment


                      #11
                      Hello vegas3416,

                      Is there a line outside of each run that says after you get finished and it runs again it waits until the current bar its on is done before it runs again?
                      ​Unfortunately, I am not understanding what this means.. May I have you clarify what you are asking?

                      By "run" do you mean OnBarUpdate() being updated?
                      You want to trigger OnBarUpdate() to run again after it runs?

                      Since its all the same time its running the code again over and over again
                      Likely you have Calculate.OnEachTick or Calculate.OnPriceChange so OnBarUpdate is updating for every tick or price change. Print the Calculate property to find out.
                      If you want to trigger only when a bar closes, then do this when IsFirstTickOfBar is true.

                      it seems which is converting the bar back
                      What is being converted on the bar?

                      maybe I'm missing a call I'm not aware of that tells it after you do your run you don't run again until the next bar occurs.
                      Do you mean IsFirstTickOfBar?
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        This is the code: Its a pretty simple call. The 'over' param is just checking if a particular case happened. Nothing crazy there.

                        So what I mean by TIME is that at exactly 10:00 am the call is being called a couple times. I would have thought that after the call got into the `if(over)` section of the code it would run, do its checks and then not run again after it finished the code until the next bar showed up.

                        Right now though it runs the set of code and it seems like its changing the color as it needs to but then immediately runs again and changes the color back before it gets displayed to the user.

                        Why is the code running again on the same time and same tick? From the print statement you can see that it gets out of the if statement for the "if(over)" and the print statement is showing that its changing the color but then it immediately calls the "if(over)" statement again after it gets out of that statement.

                        Question is why is it going right back into that call on the same bar, time, tick - everything basically versus waiting for the next bar to occur or at the least a tick change. If it was a tick change I could do something like

                        barLocationOfAction = CurrentBar;

                        and then check that the re-call to that function doesn't equal that location before running again but I can't b/c the CurrentBar is the exact same.

                        Code:
                            Print(string.Format("{0} | armed: {1} && High[0]: {2} < VMA1[0]: {3} -- BarColor: {4} - outside of check", Time[0], armed, High[0], VMA1[0], BarBrush));
                        
                                    if (over)
                                    {
                        
                                        BarBrush = Brushes.White;
                                        CandleOutlineBrush = Brushes.White;
                        
                                        if(EMA1.EmaClose[0] > VWAP[0])
                                        {
                        
                                            armed = false;
                                        }
                        
                                        Print(string.Format("{0} | armed: {1} && High[0]: {2} < VMA1[0]: {3} -- BarColor: {4} - outside IF statement", Time[0], armed, High[0], VMA1[0], BarBrush));
                        
                                        if(armed && High[0] < VMA1[0])
                                        {
                        
                                            Print(string.Format("{0} | armed: {1} && High[0]: {2} < VMA1[0]: {3} -- BarColor: {4} - before call", Time[0], armed, High[0], VMA1[0], BarBrush));
                        
                                            BarBrush = Brushes.Purple;
                                            CandleOutlineBrush = Brushes.White;
                                            Draw.Text(this, "armed" + Close[0], false, "Arming", 0, Low[0] - 10, 0, Brushes.Cyan, new NinjaTrader.Gui.Tools.SimpleFont("Courier New", 20) {Bold = true }, TextAlignment.Center, null, Brushes.Black, 100);
                        
                                            armed = false;
                                            Print(string.Format("{0} | armed: {1} && High[0]: {2} < VMA1[0]: {3} -- BarColor: {4} - after call", Time[0], armed, High[0], VMA1[0], BarBrush));
                                        }
                        
                                        Print(string.Format("{0} | armed: {1} && High[0]: {2} < VMA1[0]: {3} -- BarColor: {4} - outside of call", Time[0], armed, High[0], VMA1[0], BarBrush));
                        
                                    }
                        
                                    else
                                    {
                                        BarBrush = Brushes.Crimson;
                                        CandleOutlineBrush = Brushes.White;
                                    }​

                        Comment


                          #13
                          Hello vegas3416,

                          Why is the code running again on the same time and same tick?
                          Likely you have Calculate.OnEachTick or Calculate.OnPriceChange so OnBarUpdate is updating for every tick or price change. Print the Calculate property to find out.
                          If you want to trigger only when a bar closes, then do this when IsFirstTickOfBar is true.​
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            I did the `IsFirstTickOfBar" is true check and it still runs on on every tick. I even changed the for the `Calculate" to be = Calculate.OnBarClose and doesn't change anything

                            Comment


                              #15
                              Just seriously don't understand how it will print the string `Arming` under the right candle but not change the candle color. Does not make any sense.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Human#102, Yesterday, 09:54 AM
                              2 responses
                              7 views
                              0 likes
                              Last Post Human#102  
                              Started by Patlpp, 08-16-2021, 03:10 PM
                              10 responses
                              498 views
                              0 likes
                              Last Post Joerg
                              by Joerg
                               
                              Started by AdamDJ8, 05-07-2024, 09:18 PM
                              2 responses
                              19 views
                              0 likes
                              Last Post -=Edge=-  
                              Started by StockTrader88, 03-06-2021, 08:58 AM
                              46 responses
                              4,103 views
                              3 likes
                              Last Post tradgrad  
                              Started by usasugardefender, Today, 01:42 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post usasugardefender  
                              Working...
                              X