NexusFi: Find Your Edge


Home Menu

 





Need help understanding connections and relationships; simple conditional accumulator


Discussion in NinjaTrader

Updated
    1. trending_up 1,344 views
    2. thumb_up 0 thanks given
    3. group 1 followers
    1. forum 4 posts
    2. attach_file 1 attachments




 
Search this Thread

Need help understanding connections and relationships; simple conditional accumulator

  #1 (permalink)
 
aventeren's Avatar
 aventeren 
Bellingham, WA USA
 
Experience: Beginner
Platform: NT
Broker: Mirus (Broker), Continuum (Data), Dorman (Clearing)
Trading: Futures
Posts: 202 since Mar 2013
Thanks Given: 428
Thanks Received: 202

Hello, I was curious if someone would be able to help me sort out a simple bit of code that I'm trying to code.

Goal: Calculate two moving averages, then calculate the slopes of those moving averages; if the slopes are less than a threshold assign a 0 value else 1; add the 1s and 0s; plot the result in a bar histogram to get an indication of whether your conditions were satisfied.

I'm getting wrapped around the axel how everything talks and communicates with everything (ie, doubles, arrays, DataSeries, Value.Set, Values[0], plots, etc). My head is spinning and I need help.

I guess that I'll have to start off step-wise by trying to confirm how all of the various inputs will need to talk and communicate with one another. Here are my assumptions:

1. In the Initialize() section, I need to add a new plot of the Accumulator, and I am assuming that I will add this plot via:

Add(new Plot(Color.Blue, PlotStyle.Bar, "Accumulator"));

and I am assuming that this bit of code now defines the plot for Values[0]...but I don't understand where to add values to Values[0].

2. In the OnBarUpdate() section, I need to have the:

if(CurrentBar < 1)
{
return;
}

but I am not sure why. I think this is because if there are not enough bars to do the math, that we just need to return nothing...as we don't have enough information yet. Is that correct?

3. Within the OnBarUpdate() section, I then have determined that I need to have a BarsInProgress sub-section where my magic is going to happen, and I am assuming that I will add this within the overall OnBarUpdate() section by adding in the code:

if(BarsInProgress == 1)
{
//magic
}

On to magic.

4. I have then determined, that within the BarsInProgress == 1 sub-section, that I need to have an additional section that looks at FirstTickOfBar. I have assumed that this is where I need to populate my EMA8 and EMA20 arrays. However, before populating my arrays, I needed to define them, so I defined them as:

private double[] = EMA8Array = new double[8];
private double[] = EMA20Array = new double[20];

Is that right? Do these arrays need to be defined with 8 and 20 spots--or will just 2 or another number suffice? I probably need a little help on why 8/20 versus something else.

I think I would then need to populate the EMA8Array via:

EMA8Array[8] = EMA8Array[7];
EMA8Array[7] = EMA8Array[6];
EMA8Array[6] = EMA8Array[5];
EMA8Array[5] = EMA8Array[4];
EMA8Array[4] = EMA8Array[3];
EMA8Array[3] = EMA8Array[2];
EMA8Array[2] = EMA8Array[1];
EMA8Array[1] = EMA8Array[0];

and I would populate the EMA20Array in a similar fashion:

EMA20Array[20] = EMA20Array[19];
EMA20Array[19] = EMA20Array[18];
EMA20Array[18] = EMA20Array[17];
EMA20Array[17] = EMA20Array[16];
EMA20Array[16] = EMA20Array[15];
EMA20Array[15] = EMA20Array[14];
EMA20Array[14] = EMA20Array[13];
EMA20Array[13] = EMA20Array[12];
EMA20Array[12] = EMA20Array[11];
EMA20Array[11] = EMA20Array[10];
EMA20Array[10] = EMA20Array[9];
EMA20Array[9] = EMA20Array[8];
EMA20Array[8] = EMA20Array[7];
EMA20Array[7] = EMA20Array[6];
EMA20Array[6] = EMA20Array[5];
EMA20Array[5] = EMA20Array[4];
EMA20Array[4] = EMA20Array[3];
EMA20Array[3] = EMA20Array[2];
EMA20Array[2] = EMA20Array[1];
EMA20Array[1] = EMA20Array[0];

Is the above necessary? Do I need to do this? Why or why not?

5. The next step would be to define variables for the moving averages. Let's assume that I am trying to calculate an EMA with a period of 8 based on closes and an EMA with a period of 20 based on the closes. Given that the moving averages will not be integers, I'm assuming that they will need to be type double (as opposed to type int) and that I will define them as

private double EMA8;
private double EMA20;

Is this correct?

6. The next step would be actually calculating the moving averages, but this is where I take a second wrap around the axel, as I am not sure if these should be defined as

EMA8 = EMA(Closes[1], 8);
EMA20 = EMA(Closes[1], 20);

or

EMA8 = EMA(Closes[1], 8).Value[0];
EMA20 = EMA(Closes[1], 20).Value[0];

If they should be defined using the .Value[0] code, I don't understand why. Maybe someone can help me?

7. The next step would then be to pass the EMA8 and EMA20 values into the arrays, and I think I would do this by:

EMA8Array[0] = EMA8;
EMA20Array[0] = EMA20;

Is this right?

8. Now for another turn around the axel: I have read that if you create a new DataSeries and that your chart has 200 bars, that the DataSeries will also have 200 values. So I created new DataSeries by:

private DataSeries EMA8Series;
private DataSeries EMA20Series;

I then think that I need to populate these DataSeries by:

EMA8Series[0] = EMA8Array[0];
EMA20Series[0] = EMA20Array[0];

But this is what is confusing to me, because I think of the arrays as holding values on a tick by tick basis (ie, what was the EMA value one tick ago) and the DataSeries as holding the EMA values that were in place when the previous bar closed. Is that right? How should I be thinking about how doubles talk to arrays talk to DataSeries? And how are tick movements intracandle different from differences between bars (ie, intercandle)? How should I be thinking about how these values are being stored? I'm really confused about this part, ie, intracandle vs. intercandle. I need help.

9. I then came across a bit of code that I need to set the DataSeries by:

EMA8Series.Set(1, EMA8Array[1]);
EMA8Series.Set(2, EMA8Array[2]);
EMA20Series.Set(1, EMA20Array[1]);
EMA20Series.Set(2, EMA20Array[2]);

But I flat don't understand this. I'm hoping that someone can explain to me what the above code would do.

10. Now I would calculate my slopes. But first I created two double variables to hold the slope values:

private double EMA8SlopeValue;
private double EMA20SlopeValue;

and then I would assign values to the slope values via:

EMA8SlopeValue = radToDegrees * (Math.Atan((EMA8Series[0] - EMA8Series[1]) * 2.5 / ATR(Closes[1], 3)[0]));
EMA20SlopeValue = radToDegrees * (Math.Atan((EMA20Series[0] - EMA20Series[1]) * 2.5 / ATR(Closes[1], 3)[0]));

NOTE: I set up a Constant of:

private const double radToDegrees = 180/Math.PI;

So now, I should have slope values in the EMA8SlopeValue and EMA20SlopeValue double variables, which in theory I should be able to now test off of.

11. The next step would be to see if the slope values meet or exceed my slope thresholds, and if they meet my condition I assign a 1 or a 0 to a slope accumulator value. However, I first had to create two more int variables to hold my slope accumulator values:

private int EMA8SlopeAccumulatorValue;
private int EMA20SlopeAccumulatorValue;

I then ran my if statements and assigned a 1 or 0:

if(EMA8SlopeValue < 25 && EMA8SlopeValue > -25)
EMA8AccumulatorValue = 0;
else
EMA8AccumulatorValue = 1;

if(EMA20SlopeValue < 20 && EMA20SlopeValue > -20)
EMA20AccumulatorValue = 0;
else
EMA20AccumulatorValue = 1;

So now, in theory I should have either a 1 or a 0 in my slope accumulator values.

12. Now I need to add the slope accumulator values up, and give that result to the Accumulator plot DataSeries, but that just ain't workin':

Accumulator.Set(EMA8AccumulatorValue + EMA20AccumulatorValue);

So I am able to compile the code, and I get no compile errors. But then I reload the indicator on my chart, and I get zip: it's blank. So now I am here, trying to get some help. I think this is easy, but I just think I am missing some very fundamental understandings of how all of these things relate to one another. I did such a thorough post with the hope that others will be able to learn something from the 10,000' view on how all of these items interrelate.

I really appreciate any help anyone can offer.

I am standing by to answer questions.

Thanks,

Aventeren

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
ZombieSqueeze
Platforms and Indicators
Trade idea based off three indicators.
Traders Hideout
Better Renko Gaps
The Elite Circle
Exit Strategy
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Just another trading journal: PA, Wyckoff & Trends
26 thanks
Diary of a simple price action trader
26 thanks
Tao te Trade: way of the WLD
23 thanks
My NQ Trading Journal
16 thanks
HumbleTraders next chapter
9 thanks
  #3 (permalink)
 
aventeren's Avatar
 aventeren 
Bellingham, WA USA
 
Experience: Beginner
Platform: NT
Broker: Mirus (Broker), Continuum (Data), Dorman (Clearing)
Trading: Futures
Posts: 202 since Mar 2013
Thanks Given: 428
Thanks Received: 202


All--

I just opened the Log under the Control Center > Log. Evidently I am getting the following error:

"Error calling "OnBarUpdate' method for indicator 'Name' on bar 21: Index was outside the bounds of the array.

So this makes me think this has something to do with how I have defined and populated my arrays.

Hopefully this bit of info will help...

Thanks,

Aventeren

Started this thread Reply With Quote
  #4 (permalink)
 
aventeren's Avatar
 aventeren 
Bellingham, WA USA
 
Experience: Beginner
Platform: NT
Broker: Mirus (Broker), Continuum (Data), Dorman (Clearing)
Trading: Futures
Posts: 202 since Mar 2013
Thanks Given: 428
Thanks Received: 202

...I fixed it, but all of my questions still remain.

I changed the array sizes from [1], [3], [8] and [20] to [50] for each, which did the trick. My plot now works (ie, 0, 1 or 2).

I know that I should be pleased, but I'm not. I'm still trying to understand the broader context so I don't run into these problems in the future.

Thanks for any help you can offer.

Aventeren

Started this thread Reply With Quote
  #5 (permalink)
 
aventeren's Avatar
 aventeren 
Bellingham, WA USA
 
Experience: Beginner
Platform: NT
Broker: Mirus (Broker), Continuum (Data), Dorman (Clearing)
Trading: Futures
Posts: 202 since Mar 2013
Thanks Given: 428
Thanks Received: 202

Howdy--

Does anyone know how to "in fill" the data gaps caused by plotting higher time frames on a lower time frame charts, which occur, I suspect, because of how OnBarUpdate works differently for each time frame.

I have attached a screenshot of what the accumulator looks like for 4 moving averages, with 4 different slope thresholds for 3 different time frames.



Is there a way to somehow fill in the higher time frame array values?

Ideally, I would see all three time frames have consecutive bars like the 4R Accumulator in Panel 2 (orange).

Thanks,

Aventeren

Started this thread Reply With Quote




Last Updated on May 24, 2013


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts