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 12-27-2017, 01:42 PM   #1
gordongekko
Senior Member
 
Join Date: Nov 2017
Location: Las Vegas, NV
Posts: 120
Thanks: 74
Thanked 3 times in 3 posts
Default BarsInProgress Clarification

Can somebody clarify what exactly BarsInProgress refers to or more specifically when this example if statement would be executed, Assuming I'm using 2 data series. The main data series being 1m and the higher time frame being 5m. Since the [1] data series in this case is the 5m does that mean this if statement will only run at the conclusion of every 5m bar? In other words, it only runs this method at the start of each new 5m bar for the previous bar which is now [1][1]? If my understanding is not correct please explain exactly what happens when BarsInProgress == 1. I need to know what happens here so I can make multi-time frame indicators that actually work as intended. Thanks in advance.

if((BarsInProgress == 1)
{
do something.
}
gordongekko is offline  
Reply With Quote
Old 12-27-2017, 02:12 PM   #2
NinjaTrader_ChrisL
NinjaTrader Customer Service
 
NinjaTrader_ChrisL's Avatar
 
Join Date: Apr 2017
Location: Denver, CO
Posts: 1,063
Thanks: 33
Thanked 209 times in 204 posts
Default

Hello,

Thank you for the post.

Quote:
Since the [1] data series, in this case, is the 5m does that mean this if statement will only run at the conclusion of every 5m bar?
As long as your Strategy/Indicator's calculation mode is set to Calculate.OnBarClose, that is correct.

Quote:
... it only runs this method at the start of each new 5m bar for the previous bar which is now [1][1]
It will run the IF statement at the close of the most current bar (Bars[1][0]) then after the call to OnBarUpdate is finished, it will become Bars[1][1] in the next call to OnBarUpdate.

The condition if(BarsInProgress == 1) is to filter context. So you know that the OnBarUpdate function is being called for that series specifically. While you know that the OnBarUpdate function is being called for the secondary series, you can use the singular price series arrays such as Close[] Open[] etc. Even if the OnBarUpdate is being called for the primary series (if(BarsInProgress == 0)), you can still access the secondary series with the plural price data arrays: Closes[1][0] would be the most current bar of the 5M series.

Please see this help guide link on multi-timeframe and multi-instrument strategies, see the sections titled "Working With Multi-Time Frame Objects" and "How Bars Data is Referenced":
https://ninjatrader.com/support/help...nstruments.htm

Please let us know if we may be of any further assistance.
NinjaTrader_ChrisL is offline  
Reply With Quote
The following user says thank you to NinjaTrader_ChrisL for this post:
Old 12-27-2017, 02:25 PM   #3
gordongekko
Senior Member
 
Join Date: Nov 2017
Location: Las Vegas, NV
Posts: 120
Thanks: 74
Thanked 3 times in 3 posts
Default

Hi Chris,

Thanks for the reply. I'm still confused about 1 thing.
Assuming again we are talking about the main data series[0][0] being 1m and the secondary data series 5m [1][0] every 5th bar both of these data series are going to be in progress at the exact same time unless one data series always takes priority over the other. Does the on bar update call each data series in sequential order? The reason this is important is that in the below example. It's saying that if i lock the method behind a BarsInProgress == 1 if statement then i can assign values without even referencing the data series number in the array. So I could assign the value of Close[0] to a double called 5mclose and it would never confuse the 5m bar close with the 1m bar close that would update at the same time every 5th bar? Ultimately what im saying is that it appears that it's not possible for more than one data series to be in-progress simultaneously. Is this correct?


This is the example code provided from the reference samples:

protected override void OnBarUpdate()
{
// Executed on primary bar updates only
if (BarsInProgress == 0)
{
// Set DataSeries object to store the trading range of the primary bar
primarySeries[0] = Close[0] - Open[0];
}
else if (BarsInProgress == 1) // Executed on secondary bar updates only
{
// Set the DataSeries object to store the trading range of the secondary bar
secondarySeries[0] = Close[0] - Open[0];
}

// When both trading ranges of the current bars on both time frames are positive and there is not currently a position, enter long
if (primarySeries[0] > 0 && secondarySeries[0] > 0 && Position.MarketPosition == MarketPosition.Flat)
EnterLong();
}
Last edited by gordongekko; 12-27-2017 at 02:27 PM.
gordongekko is offline  
Reply With Quote
Old 12-27-2017, 03:24 PM   #4
NinjaTrader_ChrisL
NinjaTrader Customer Service
 
NinjaTrader_ChrisL's Avatar
 
Join Date: Apr 2017
Location: Denver, CO
Posts: 1,063
Thanks: 33
Thanked 209 times in 204 posts
Default

Hello,

Thank you for the follow-up.

Code:
...it's not possible for more than one data series to be in-progress simultaneously. Is this correct?
That is correct. OnBarUpdate will be called for one series at a time.

The reason we use if((BarsInProgress == 1) is for the context of which series this OnBarUpdate belongs to. If BarsInProgress == 1, Close[0] will refer to the secondary series. If BarsInProgress == 0, Close[0] will refer to the primary series.

Quote:
Does the on bar update call each data series in sequential order?
If the strategy/indicator has two data series of the same instrument and CalculationMode == Calculate.OnEachTick, the primary series OnBarUpdate will be called, then the secondary series. If the instruments are different, OnBarUpdate will be called in the sequence the ticks are received.

Please let us know if you have any questions.
NinjaTrader_ChrisL is offline  
Reply With Quote
The following user says thank you to NinjaTrader_ChrisL for this post:
Old 12-27-2017, 03:33 PM   #5
gordongekko
Senior Member
 
Join Date: Nov 2017
Location: Las Vegas, NV
Posts: 120
Thanks: 74
Thanked 3 times in 3 posts
Default

So if i'm using 3 different time frames (1m[0][0] 3m[1][0] and 10m[2][0]) for the same instrument will on bar update be called for each series in sequential order (as they appear in the array as 0,1, and 2) if more then one close at the same time which will happen every 3rd and 10th bar. Another question is this. Why gate a method behind an if statement requiring barsinprogress == (data series) when you can just say this : If( close[0] > close[1] && close [1][0] > close[1][1] && close[2][0] > close[2][1])
{
run method
}

How is this different from making 3 different statements and assigning the result of a method to a seperate double and comparing those in a 4th if statement? Are they both viable and do they produce the same result?
gordongekko is offline  
Reply With Quote
Old 12-28-2017, 03:19 PM   #6
NinjaTrader_ChrisL
NinjaTrader Customer Service
 
NinjaTrader_ChrisL's Avatar
 
Join Date: Apr 2017
Location: Denver, CO
Posts: 1,063
Thanks: 33
Thanked 209 times in 204 posts
Default

Hello,

Thank you for the reply.

It will depend on the time frame you have chosen for the three time series. If your primary series was a 1 minute, 2nd was a 3 minute, and the 3rd was a 10 minute and your calculation mode is set to OnBarClose, then yes, they will process in order.

Alternatively, if your primary series is a 1-minute series and your secondary series is a 1 tick series and your calculation mode is set to OnBarClose, then they will not process in order.

The reason you would check if a specific bar is closing with (BarsInProgress == X) is that there might be times where you want to know when the bar of a specific series closes. Or maybe you want to perform logic every 10 minutes instead of every minute.

Using Closes[x][x] is ok to use if you just want the price of your other series at an arbitrary time.

In this reference sample, they are entering a position on a 5-minute series and exiting that position on a 15-minute series:
https://ninjatrader.com/support/foru...ead.php?t=5787

Please let us know if you have any questions.
NinjaTrader_ChrisL is offline  
Reply With Quote
The following user says thank you to NinjaTrader_ChrisL for this post:
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
Clarification on trade muto77 Platform Technical Support 1 09-07-2017 12:03 AM
SetTrailStop() Clarification scjohn Strategy Development 1 11-06-2014 03:57 PM
BarsSinceEntry value clarification tblock Strategy Development 1 05-18-2009 02:53 AM
BarsInProgress[1] causing expiration of BarsInProgress[0] orders nolantx Platform Technical Support 14 02-08-2009 11:05 AM
Clarification Mike_32 Platform Technical Support 14 05-11-2008 01:49 PM


All times are GMT -6. The time now is 12:37 PM.

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.