Go Back   NinjaTrader Support Forum > NinjaTrader 8 > Indicator Development

Indicator Development Support for the development of custom indicators using NinjaScript.

NinjaTrader
Reply
 
Thread Tools Display Modes
Old 06-13-2018, 10:31 AM   #1
Matheusfx
Junior Member
 
Join Date: Aug 2015
Posts: 11
Thanks: 3
Thanked 0 times in 0 posts
Default BarBrushes has 0 items at all times

Hi, I am trying to create an indicator that paints candles differently when some conditions are met. Previous candle's colors can change on each bar close, so I can't really use BarBrush, so I'm trying to use BarBrushes instead.


The problem is: The BarBrushes array is empty anywhere I try to use it (OnStateChange when State == State.DataLoaded, and OnBarUpdate with Calculate.OnBarClose). Am I doing something wrong or is it a bug?
Matheusfx is offline  
Reply With Quote
Old 06-13-2018, 10:42 AM   #2
NinjaTrader_JoshG
NinjaTrader Customer Service
 
NinjaTrader_JoshG's Avatar
 
Join Date: Jun 2017
Location: Denver, CO
Posts: 477
Thanks: 13
Thanked 104 times in 100 posts
Default

Hello Matheusfx,
Thanks for your post.

BarBrushes is going to return null unless an explicit color overwrite is used first. Are you you setting a color first before you check the array?
NinjaTrader_JoshG is offline  
Reply With Quote
Old 06-13-2018, 11:03 AM   #3
Matheusfx
Junior Member
 
Join Date: Aug 2015
Posts: 11
Thanks: 3
Thanked 0 times in 0 posts
Default

I'm getting out of bounds exceptions when trying to set the colors, not null returns :/
I'm using the array directly to set the colors, I expected for it to have an item count equal to the amount of loaded bars, with null values where the candle's color wasn't overriden.
How should I go about setting these colors, if this is not how it works?
Matheusfx is offline  
Reply With Quote
Old 06-13-2018, 12:22 PM   #4
NinjaTrader_JoshG
NinjaTrader Customer Service
 
NinjaTrader_JoshG's Avatar
 
Join Date: Jun 2017
Location: Denver, CO
Posts: 477
Thanks: 13
Thanked 104 times in 100 posts
Default

Can you provide a short sample that produces this error? I think it may help to see how you are setting BarBrushes.
NinjaTrader_JoshG is offline  
Reply With Quote
Old 06-13-2018, 12:49 PM   #5
Matheusfx
Junior Member
 
Join Date: Aug 2015
Posts: 11
Thanks: 3
Thanked 0 times in 0 posts
Default

Quote:
Originally Posted by NinjaTrader_JoshG View Post
Can you provide a short sample that produces this error? I think it may help to see how you are setting BarBrushes.
I don't have access to the editor at the moment, but I'll try to give some directions. I'm not setting BarBrushes at all, I think an accurate representation of the problem would be:
1. Create an empty indicator.
2.Inside of the OnStateChange() method:
Code:
if(State == State.DataLoaded) {
    BarBrushes[0] = Brushes.Blue
}
This produces an Out of Bounds exception, instead of returning null.
Printing BarBrushes.Count shows that it's item count is 0.
Matheusfx is offline  
Reply With Quote
Old 06-13-2018, 01:03 PM   #6
NinjaTrader_JoshG
NinjaTrader Customer Service
 
NinjaTrader_JoshG's Avatar
 
Join Date: Jun 2017
Location: Denver, CO
Posts: 477
Thanks: 13
Thanked 104 times in 100 posts
Default

I would not expect for you to be able to access that index from State.DataLoaded

How are you setting BarBrushes inside OnBarUpdate() to produce the bounds error? The following snippet would set the current bar to the color orange when my condition is met.

Code:
protected override void OnBarUpdate()
{
	if (Close[0] > Open[0])
		BarBrushes[0] = Brushes.Orange;
}
NinjaTrader_JoshG is offline  
Reply With Quote
Old 06-13-2018, 01:43 PM   #7
Matheusfx
Junior Member
 
Join Date: Aug 2015
Posts: 11
Thanks: 3
Thanked 0 times in 0 posts
Default

Quote:
Originally Posted by NinjaTrader_JoshG View Post
I would not expect for you to be able to access that index from State.DataLoaded

How are you setting BarBrushes inside OnBarUpdate() to produce the bounds error? The following snippet would set the current bar to the color orange when my condition is met.

Code:
protected override void OnBarUpdate()
{
	if (Close[0] > Open[0])
		BarBrushes[0] = Brushes.Orange;
}
BarBrushes is also empty on the OnBarUpdate() method. If I recall correctly the error happens before the script even runs OnBarUpdate(), I also set the colors in order from oldest to the most recent candle, which means it begins trying to set something like
Code:
BarBrushes[99] = Brushes.Orange
and then continues setting the colors of the candles that match the criteria, until it reaches the current one, which is 0.

If the explanation is too messy I'll try to post a proper code sample when I get home.
Matheusfx is offline  
Reply With Quote
Old 06-13-2018, 01:53 PM   #8
NinjaTrader_JoshG
NinjaTrader Customer Service
 
NinjaTrader_JoshG's Avatar
 
Join Date: Jun 2017
Location: Denver, CO
Posts: 477
Thanks: 13
Thanked 104 times in 100 posts
Default

Your sample is not too messy. The reason an out of bounds error is occurring is because a value is not being assigned to BarBrushes on each bar. I am just not certain why that is, so I will need to look into it.

If you describe what it is you are trying to accomplish I may be able to assist you more on how to achieve your goal.
NinjaTrader_JoshG is offline  
Reply With Quote
Old 06-13-2018, 05:52 PM   #9
Matheusfx
Junior Member
 
Join Date: Aug 2015
Posts: 11
Thanks: 3
Thanked 0 times in 0 posts
Default

I managed to fix the out of bounds error in the OnBarUpdate() method by creating another indicator from scratch and moving it to the new one, I have absolutely no idea why that worked, as I made sure I was copying every piece of code I wrote to the other file.


The BarBrushes array really wasn't ready in the State.DataLoaded, and I had to populate the BarBrushes array with null values inside of OnBarUpdate() by doing:
Code:
BarBrushes[0] = null;
I was able to do the stuff that I needed to do inside the OnStateChange() method by checking if the State == State.RealTime, as the array was already pre-populated with null values at this point.
Matheusfx is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
SetZOrder doesn twork with BarBrushes TazoTodua Indicator Development 5 03-12-2018 12:08 PM
Using BarBrushes[] hides plots on top of bar KhaosTrader Version 8 Beta 10 08-27-2016 03:26 PM
Session Times vs PC Times daven Platform Technical Support 3 01-21-2014 01:35 PM
Testing multiple contracts w/ diff. open times simultaneously - 15min bar start times ChiTrader2000 Strategy Development 12 06-08-2009 08:34 AM
Session times vs Open & Close Times Laserdan Platform Technical Support 1 10-15-2008 05:46 PM


All times are GMT -6. The time now is 09:38 AM.

Futures, foreign currency and options trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing ones financial security or lifestyle. Only risk capital should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results. View Full Risk Disclosure.

CFTC Rules 4.41 - Hypothetical or Simulated performance results have certain limitations, unlike an actual performance record, simulated results do not represent actual trading. Also, since the trades have not been executed, the results may have under-or-over compensated for the impact, if any, of certain market factors, such as lack of liquidity. Simulated trading programs in general are also subject to the fact that they are designed with the benefit of hindsight. No representation is being made that any account will or is likely to achieve profit or losses similar to those shown.

This website is hosted and operated by NinjaTrader, LLC (“NT”), a software development company which owns and supports all proprietary technology relating to and including the NinjaTrader trading platform. NT is an affiliated company to NinjaTrader Brokerage (“NTB”), which is a NFA registered introducing broker (NFA #0339976) providing brokerage services to traders of futures and foreign exchange products. This website is intended for educational and informational purposes only and should not be viewed as a solicitation or recommendation of any product, service or trading strategy. No offer or solicitation to buy or sell securities, securities derivative or futures products of any kind, or any type of trading or investment advice, recommendation or strategy, is made, given, or in any manner endorsed by any NT affiliate and the information made available on this Web site is not an offer or solicitation of any kind. Specific questions related to a brokerage account should be sent to your broker directly. The content and opinions expressed on this website are those of the authors and do not necessarily reflect the official policy or position of NT or any of its affiliates. 

Vendors along with their websites, products and services, collectively referred to as (“Vendor Content”), are independent persons or companies that are in no manner affiliated with NT or any if its affiliates. NT or any of its affiliates are not responsible for, do not approve, recommend or endorse any Vendor Content referenced on this website and it’s your sole responsibility to evaluate Vendor Content. Please be aware that any performance information provided by a vendor should be considered hypothetical and must contain the disclosures required by NFA Rule 2-29(c). If you are interested in learning more about, or investigating the quality of, any such Vendor Content you must contact the vendor, provider or seller of such Vendor Content. No person employed by, or associated with, NT or any of its affiliates is authorized to provide any information about any such Vendor Content.