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

opacity - good practice?

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

    opacity - good practice?

    This code works but is it good practice to have it in on bar update?

    protected override void OnBarUpdate()
    ...
    double opacity = .25

    if(trendUP)
    {
    BackBrush = BgColorUp;
    Brush newBrush = BackBrush.Clone();
    newBrush.Opacity = opacity;
    newBrush.Freeze();
    BackBrush = newBrush;
    }

    else if(trendDOWN)
    {
    BackBrush = BgColorDown;
    Brush newBrush = BackBrush.Clone();
    newBrush.Opacity = opacity;
    newBrush.Freeze();
    BackBrush = newBrush;
    }

    #2
    Hello nkhoi,

    This would be inefficient.

    Instead of creating a new brush on every new bar, it would be more efficient to declare the variable of the brush within the scope of the class and initialize the brush once as the script starts in State.DataLoaded.

    If you have two brushes, create two variables that are initialized in State.DataLoaded, and then switch between the variables in the logic of OnBarUpdate().
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello nkhoi,

      This would be inefficient.

      Instead of creating a new brush on every new bar, it would be more efficient to declare the variable of the brush within the scope of the class and initialize the brush once as the script starts in State.DataLoaded.

      If you have two brushes, create two variables that are initialized in State.DataLoaded, and then switch between the variables in the logic of OnBarUpdate().

      I am not C programmer but I understand the point of not to call newBrush every bar. This is what I came up with. Will this be the same as what you mention?
      -------------------------
      private System.Windows.Media.Brush bgColorUp = Brushes.RoyalBlue;
      private System.Windows.Media.Brush newBrushA, newBrushB, newBrushC, newBrushD ;
      ....
      .....
      protected override void OnBarUpdate()
      {
      //Add your custom indicator logic here.

      if (newBrushA == null)
      {
      BackBrushAll = BgColorUp;
      newBrushA = BackBrushAll.Clone();
      newBrushA.Opacity = opacity;
      newBrushA.Freeze();
      }
      .....
      //
      if (trendUP)
      BackBrushAll = newBrushA;

      Comment


        #4
        Hello nkhoi,

        No, I am suggesting that you do the initialize or clone in OnStateChange when State is State.DataLoaded.

        Below is a publicly available link to the help guide on OnStateChange().


        private Brush newBrushA;

        else if (State == State.DataLoaded)
        {
        newBrushA = BackBrush.Clone();
        newBrushA.Opacity = opacity;
        newBrushA.Freeze();
        }

        protected override void OnBarUpdate()
        {
        if (trendUP)
        BackBrushAll = newBrushA;
        }
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Irukandji, Yesterday, 02:53 AM
        2 responses
        17 views
        0 likes
        Last Post Irukandji  
        Started by adeelshahzad, Today, 03:54 AM
        0 responses
        3 views
        0 likes
        Last Post adeelshahzad  
        Started by CortexZenUSA, Today, 12:53 AM
        0 responses
        3 views
        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  
        Working...
        X