Need help understanding connections and relationships; simple conditional accumulator 
May 24th, 2013, 02:07 PM  #1 (permalink) 
Patient Discipline
Bellingham, WA USA
Futures Experience: Beginner
Platform: NT
Broker/Data: Mirus (Broker), Continuum (Data), Dorman (Clearing)
Favorite Futures: Futures
Posts: 202 since Mar 2013
Thanks: 426 given,
190
received

Need help understanding connections and relationships; simple conditional accumulator
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 stepwise 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 subsection 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 subsection, 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 spotsor 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 