1

I am coding an algorithm and I am using arrays of custom data and if it looks a bit strange is that I am coding the algorithm for a futures trading platform and so they may have functions that don't look standard C#.

I'm trying to Resize my arrays because I need them resized every time a new value is found to be added and then I use the SetValue sometimes to replace the last value found when a better one is found again within the next 5 values after the last value was set.

Trouble is, when I debug it in Visual Studio, it stops at line after ArrayResize and when I hover over the LastLSwDMIpriceBar[k], it shows the k = 0, just as I expected it to be, as it would be the first element in the array of one, so what Index is then outside the bounds of the array?

The way I understand and hoped the code to work is this: when the conditions met are by setting LSwDMIbool to true, the array is resized from 0 to 1 element and the element with index [0] is then set as LastLSwDMIpriceBar[k]. Am I wrong about this?

Any help would be greatly appreciated.

private int k, l;    
private int[] LastLSwDMIpriceBar;
LastLSwDMIpriceBar = new int [1];
.....
if (LSwDMIbool)
{
  lastLSwDMIbar = CurrentBar - 2 - LowestBar(LSwDMI, 5);    
  LastLSwDMI[0] = DMI(Closes[2], Convert.ToInt32(DmiPeriod)).Values[0].GetValueAt(lastLSwDMIbar);
  Array.Resize(ref LastLSwDMIpriceBar, l++);
  LastLSwDMIpriceBar[k] = CurrentBar - LowestBar(Lows[2], 5);   
  k++;
  LSwDMIprice[0] = Lows[2].GetValueAt(LastLSwDMIpriceBar[k]);
}
  ......
if(!LSwDMIbool)
{                           
    for (int  LastBar = CurrentBar - 1; IsFirstTickOfBar && LastBar <= lastLSwDMIbar + 5; LastBar++)
        LastLSwDMIpriceBar.SetValue((CurrentBar - LowestBar(Lows[2], 5)), k);
}
Hadi Samadzad
  • 1,480
  • 2
  • 13
  • 22

2 Answers2

1

You may find this helpful: https://stackoverflow.com/a/24858/12343726

I believe you want ++l instead of l++ so that the new value of l is used when resizing the array. I suspect that l is zero the first time Array.Resize is called so the array is being resized to zero.

Daniel
  • 61
  • 5
  • 1
    To clarify, why this works: Using `l++` as a parameter in a method will first use the value of `l` to call the method which in your case is `Array.Resize(..)` and then the value of `l` will be incremented by `1`. So in the first iteration `Array.Resize(..)` is called with the value of `0`. `++l` works slightly differently. It first increments `l` by `1` and then passes the new value of `l` to the method. So in the first iteration `Array.Resize(..)` will be called with the value of `1`. – FlyingFoX Dec 28 '19 at 21:15
  • That was exactly it, thank you! The first upper bound was indeed printed as -1, so ++l was what I needed and helped me discover also my k++ needed to be further down. – Sorin Pascu Dec 28 '19 at 23:28
1

I'm trying to Resize my arrays because I need them resized every time a new value is found to be added

Don't use an array for this!

Instead, use a List<T>.

private int k, l;    
private List<int> LastLSwDMIpriceBar = new List<int>();
.....
if (LSwDMIbool)
{
  lastLSwDMIbar = CurrentBar - 2 - LowestBar(LSwDMI, 5);    
  LastLSwDMI[0] = DMI(Closes[2], Convert.ToInt32(DmiPeriod)).Values[0][lastLSwDMIbar];

  var newValue = CurrentBar - LowestBar(Lows[2], 5);
  LastLSwDMIpriceBar.Add(newValue);
  k++;
  LSwDMIprice[0] = Lows[2][newValue];

}
  ......
if(!LSwDMIbool)
{                           
    for (int  LastBar = CurrentBar - 1; IsFirstTickOfBar && LastBar <= lastLSwDMIbar + 5; LastBar++)
        LastLSwDMIpriceBar[k] = CurrentBar - LowestBar(Lows[2], 5));
}
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Hi Joel. Thank you for your answer, but could you please explain why would you rather use list than array in this situation? It seems to be working so far. I still have to see if I am getting the data that I'm expecting, but the code runs as it is and the arrays change with every new data, so what would be the issue? Thank you – Sorin Pascu Dec 28 '19 at 23:41
  • Array.Resize() allocates a whole new block of memory behind the scenes and copies each element one by one. `List` uses a buffer to minimize how often this needs to happen. It's also built with adding items as part of the design intent, where the design intent for arrays is they are fixed in size. – Joel Coehoorn Dec 28 '19 at 23:44
  • So basically, it would be a performance issue, then? In case I am using List, I would have thought I would then use List.Insert(index, T) to modify the last value, instead of SetValue with array, but I see you've done differently. Is that another way of changing List values? – Sorin Pascu Dec 28 '19 at 23:54
  • Performance is just one part of it. This is the exact job lists where designed for, and I suspect if we could see more of code we could use them to simplify it considerably. Lists can use the same `[]` notation as arrays, but also have the `Add()` method to append new items. Almost no one ever uses `GetValue()` or `SetValue()` with arrays. – Joel Coehoorn Dec 29 '19 at 00:05