• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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().
        https://ninjatrader.com/support/help...tatechange.htm

        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 Erwin Beckers, Today, 01:23 AM
        0 responses
        2 views
        0 likes
        Last Post Erwin Beckers  
        Started by Mark Taylor, Yesterday, 11:36 PM
        0 responses
        4 views
        0 likes
        Last Post Mark Taylor  
        Started by Bluebeep, Yesterday, 08:38 PM
        0 responses
        10 views
        0 likes
        Last Post Bluebeep  
        Started by stokhastic, Yesterday, 07:08 PM
        0 responses
        4 views
        0 likes
        Last Post stokhastic  
        Started by jlkramer16, Yesterday, 04:29 PM
        1 response
        6 views
        0 likes
        Last Post jlkramer16  
        Working...
        X