Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Can not access property in base class of indicator

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

    Can not access property in base class of indicator

    Hi
    I try to develop a indicator that is as same as system indicator VOL. the only different i try to do is show different color of volume bar dependence on a bar is long bar or short bar. I create a new indicator and copy relevant code from system indicator VOL to my source code file. and the key code is below:

    protected override void OnBarUpdate()
    {
    Value[0] = Instrument.MasterInstrument.InstrumentType == InstrumentType.CryptoCurrency ? Core.Globals.ToCryptocurrencyVolume((long)Volume[0]) : Volume[0];
    // here a exception is thrown
    BarBrushes[0] = (Open[0] >= Close[0]) ? this.ShortBarBrush : this.LongBarBrush;
    }

    the exception message is :
    System.InvalidOperationException: 'The calling thread cannot access this object because a different thread owns it.'

    I also try to use PlotBrushes, but it still does not work. any suggestion?
    Thanks

    #2
    What are ShortBarBrush and LongBarBrush?

    Have they been Frozen?

    Good reading here.

    Comment


      #3
      Originally posted by bltdavid View Post
      What are ShortBarBrush and LongBarBrush?

      Have they been Frozen?

      Good reading here.
      Both are my own properties, below is how i define them:
      Code:
          public class VOLPro : Indicator
          {
              public SolidColorBrush LongColorBrush { get; set; }
              public SolidColorBrush ShortColorBrush { get; set; }
              protected override void OnStateChange()
              {
                   ...
              }
              ...
          }
      Below is how I initialize them:
      Code:
              protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                      this.LongColorBrush = new SolidColorBrush(Color.FromRgb(255, 112, 95));
                      this.ShortColorBrush = new SolidColorBrush(Color.FromRgb(37, 173, 175));
                      ...
                  }
                  ...
               }​
      Below is how i use them:
      Code:
              protected override void OnBarUpdate()
              {
      //            if (this.LongColorBrush == null)
      //                this.LongColorBrush = new SolidColorBrush(Color.FromRgb(255, 112, 95));
      //            if (this.ShortColorBrush == null)
      //                this.ShortColorBrush = new SolidColorBrush(Color.FromRgb(37, 173, 175));
      
                  Value[0] = Instrument.MasterInstrument.InstrumentType == InstrumentType.CryptoCurrency ? Core.Globals.ToCryptocurrencyVolume((long)Volume[0]) : Volume[0];
                  // this line code cause problem 
                  PlotBrushes[0][0] = (Open[0] >= Close[0]) ? this.ShortColorBrush : this.LongColorBrush;
                  //BarBrushes[0] = (Open[0] >= Close[0]) ? this.ShortColorBrush : this.LongColorBrush;
              }​

      Comment


        #4
        Hello, thanks for writing in. The brushes you are using must be frozen. The page the BLTDavid linked shows how you can do that:

        https://ninjatrader.com/support/help...ingcustombrush es

        Kind regards,

        -ChrisL

        Comment


          #5
          Thanks for letting us see your code.

          Yep, you'd be much better off using 'Brush' rather than 'SolidColorBrush'.

          Why?
          So that you can use the Brushes static class, and do things like,

          Code:
          private Brush LongColorBrush = Brushes.Green;
          private Brush ShortColorBrush = Brushes.Red;
          But, that is not really the best approach.
          That is, don't do it that way -- that way is poor design.
          Let's discuss.

          -=o=-

          An even better way, study the section that shows you how to use public
          Brush properties, so that you can select the color in the property grid.
          (Public Brush properties have to be serialized, just like public Color
          properties back in NT7.)

          That is, skip defining Brush variables, you don't need them. Define
          a Brush property, like this,

          Code:
          [XmlIgnore]
          public Brush LongColorBrush { get; set; }
          
          [Browsable(false)]
          public string LongColorBrushSerialize
          {
              get { return Serialize.BrushToString(LongColorBrush); }
              set { LongColorBrush = Serialize.StringToBrush(value); }
          }
          Inside SetDefaults, assign your default color,

          Code:
              LongColorBrush = Brushes.Green;
          ​
          That's it!
          Now use LongColorBrush as you normally would.

          -=o=-

          Why do all this?
          The primary benefit, that is, the real coup de grace, is,
          if you can avoid using SolidColorBrush, you also avoid having to worry
          about freezing your brushes.

          Which means, drum-roll, you will avoid that crazy error you found and
          wrote into this forum about. Yep, goodness all around.

          -=o=-

          Carefully study the links provided.
          The answers are in there.

          Also, study @VolumeProfile.cs -- great examples in that indicator.

          Just my 2˘.

          Last edited by bltdavid; 12-28-2022, 10:37 AM.

          Comment


            #6
            thanks, it works

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            633 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            364 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            105 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            567 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            568 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X