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 07-05-2018, 09:53 PM   #1
imalil
Senior Member
 
Join Date: Jun 2013
Posts: 311
Thanks: 20
Thanked 24 times in 24 posts
Default firstbarofsession bars back

Hello-

I'm looking for a chosen time of day--doesn't have to be firstbarofsession--to get this on the chosen bar:
var = value[0] - value[1];
then the rest of the bars that day I want this:
var = var + (value[0] - value[1]);

This does not work:

if (firstbarofsession)
var = value[0] - value[1];
else
var = var + (value[0] - value[1]);

I always get var = value[0];

How can this be done?

I'm using intraday tick bars.

Thank you.
imalil is offline  
Reply With Quote
Old 07-06-2018, 07:12 AM   #2
NinjaTrader_Paul
NinjaTrader Customer Service
 
NinjaTrader_Paul's Avatar
 
Join Date: May 2014
Posts: 4,045
Thanks: 424
Thanked 1,054 times in 1,011 posts
Default

Hello imalil,

Thanks for your post.

"var" is a C# keyword that means to allow the compiler to determine the type of variable that follows the word "var".

I would suggest something like: var X = value[0] - value[1]; In this case X would contain the value and var would define X as a double variable.

However, this statement:

if (firstbarofsession)
var = value[0] - value[1];
else
var = var + (value[0] - value[1]);


even if changed to:

if (firstbarofsession)
var X = value[0] - value[1];
else
X = X+(value[0] - value[1]);


would not work because you would be defining the variable conditionally.

If you know the variable will be used as a double it would be better to declare the variable as a double and you may want to declare it at the class level, for example:

public class imalilTest : Indicator
{
private double myAccumulator = 0.0; // declare as double and set to zero value.
protected override void OnStateChange()
{


then in the OnBarUpdate() section:

if (Bars.IsFirstBarOfSession)
myAccumulator = value[0] - value[1];
else
myAccumulator = myAccumulator + (value[0] - value[1]);
NinjaTrader_Paul is offline  
Reply With Quote
Old 07-06-2018, 09:25 PM   #3
imalil
Senior Member
 
Join Date: Jun 2013
Posts: 311
Thanks: 20
Thanked 24 times in 24 posts
Default

Thanks, I'll try it.

Incidentally, I typed 'var' as an abbreviation for variable. I'm not actually using 'var' as my variable. But thanks for the info.
imalil is offline  
Reply With Quote
Old 07-08-2018, 09:46 PM   #4
imalil
Senior Member
 
Join Date: Jun 2013
Posts: 311
Thanks: 20
Thanked 24 times in 24 posts
Default

Hello-

I did this:

Quote:
public class imalilTest : Indicator
{
private double myAccumulator = 0.0; // declare as double and set to zero value.
protected override void OnStateChange()
{

then in the OnBarUpdate() section:

if (Bars.IsFirstBarOfSession)
myAccumulator = value[0] - value[1];
else
myAccumulator = myAccumulator + (value[0] - value[1]);
I'm printing to output in both conditions so I know value[0] & value[1] have values, and I know it stays in the 'else' statement, myAccumulator = myAccumulator + (value[0] - value[1]);, on every bar, but the only value I get is myAccumulator = value[0];.

I'm using simulated data feed on a test tickbar workspace.

Is there a function that says something like 'if (current bar is (user-defined time))' instead of if (Bars.IsFirstBarOfSession)?

I'm looking for anything that will let my variable accumulate the way I want it to.

Thank you.
imalil is offline  
Reply With Quote
Old 07-09-2018, 08:19 AM   #5
NinjaTrader_Paul
NinjaTrader Customer Service
 
NinjaTrader_Paul's Avatar
 
Join Date: May 2014
Posts: 4,045
Thanks: 424
Thanked 1,054 times in 1,011 posts
Default

Hello imalil,

Thanks for your post.

Please post or provide your exact code used.
NinjaTrader_Paul is offline  
Reply With Quote
Old 07-09-2018, 06:36 PM   #6
imalil
Senior Member
 
Join Date: Jun 2013
Posts: 311
Thanks: 20
Thanked 24 times in 24 posts
Default

Quote:
public class Imbaltest : Indicator
{
private double VolAccum = 0.0;
private double ImbalAccum = 0.0;
private double ATSAccum = 0.0;



Quote:
if (Bars.IsFirstBarOfSession)
{
VolAccum = DeltaModSeries[0] - DeltaModSeries[1];
ImbalAccum = AISaskbidFactorDiffSeries[0] - AISaskbidFactorDiffSeries[1];
ATSAccum = SMAdiffSeries[0] - SMAdiffSeries[1];
}
else
{
Print("VolAccum1 " + VolAccum.ToString("0"));
Print("ImbalAccum1 " + ImbalAccum.ToString("0"));
Print("ATSAccum1 " + ATSAccum.ToString("0"));
VolAccum = VolAccum + (DeltaModSeries[0] - DeltaModSeries[1]);
ImbalAccum = ImbalAccum + (AISaskbidFactorDiffSeries[0] - AISaskbidFactorDiffSeries[1]);
ATSAccum = ATSAccum + (SMAdiffSeries[0] - SMAdiffSeries[1]);
Print("VolAccum2 " + VolAccum.ToString("0"));
Print("ImbalAccum2 " + ImbalAccum.ToString("0"));
Print("ATSAccum2 " + ATSAccum.ToString("0"));
}
Regarding: if (Bars.IsFirstBarOfSession): does this need to occur at the beginning of OnBarUpdate? Can it occur towards the end after certain variables have been calculated?

Sorry for the dumb questions, I just want to get this to work.

Thank you.
imalil is offline  
Reply With Quote
Old 07-10-2018, 07:19 AM   #7
NinjaTrader_Paul
NinjaTrader Customer Service
 
NinjaTrader_Paul's Avatar
 
Join Date: May 2014
Posts: 4,045
Thanks: 424
Thanked 1,054 times in 1,011 posts
Default

Hello imalil,

Thanks for your reply and posting some of your code.

I do not see anything that would prevent the accumulators from accumulating. You may want to create a simple test indicator that only has one accumulator in which to test.

I've attached an example of adding the code to an SMA indicator code and its output which shows it accumulates as expected and is clearly not the value of Value[0].

You can place Bars.IsFirstBarOfSession anywhere that you need it, it does not need to be at the top of OnbarUpdate unless you have a need for it to be there.
Attached Images
File Type: png imalil-1.PNG (134.8 KB, 2 views)
NinjaTrader_Paul is offline  
Reply With Quote
Old 07-10-2018, 04:44 PM   #8
imalil
Senior Member
 
Join Date: Jun 2013
Posts: 311
Thanks: 20
Thanked 24 times in 24 posts
Default

Thanks for the code, I understand how it works. Mine is still giving me accum = value[0] and never visits the firstbarofsession curly braces. I think where mine differs from your SMA example is the values I want to accumulate are level 2, non historical. They vanish when I refresh my charts. I'm using OnMarketData, calc on bar close. I don't want to calculate my data this way, but as of right now I can't get my results any other way. Is this what's keeping it from accumulating?

My [0] and [1] values are correct.

I read the firstbarofsession help guide and saw nothing about level 2 issues.

I'll gladly use something other than firstbarofsession, I just don't know what that is.

Thank you.
imalil is offline  
Reply With Quote
Old 07-11-2018, 06:57 AM   #9
NinjaTrader_Paul
NinjaTrader Customer Service
 
NinjaTrader_Paul's Avatar
 
Join Date: May 2014
Posts: 4,045
Thanks: 424
Thanked 1,054 times in 1,011 posts
Default

Hello imalil,

Thanks for your reply.

You would need to use FirstBarOfSession in the OnBarUpdate() method.

You may want to look at the NinjaTrader indicator "BuySellVolume" as an example of accumulators collecting data in OnMarketData() and then plotting that data in OnBarUpdate() (and is where the accumulators are reset).
NinjaTrader_Paul 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
Bars.FirstBarOfSession GKonheiser Strategy Development 2 09-24-2015 03:07 AM
Bars.FirstBarOfSession newworldguy Strategy Development 5 07-16-2014 07:36 AM


All times are GMT -6. The time now is 03:16 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.