Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Converts candlestick data to another interval
- List<double[]> instr(InsuranceContext context, int mult, out int countOut, bool fullCandlesOnly = false)
- {
- // Get raw input data
- DateTime[] timestamp = context.PriceInstrument.Timestamps;
- double[] high = context.PriceInstrument.High;
- double[] low = context.PriceInstrument.Low;
- double[] open = context.PriceInstrument.Open;
- double[] close = context.PriceInstrument.Close;
- double[] volume = context.PriceInstrument.Volume;
- int interval = 1;
- int compInterval = interval * mult;
- // Output data
- var len = timestamp.Length;
- var ret = new List<double[]>();
- var timestampOut = new List<DateTime>();
- var highOut = new List<double>();
- var lowOut = new List<double>();
- var openOut = new List<double>();
- var closeOut = new List<double>();
- var volumeOut = new List<double>();
- countOut = 0;
- var curTime = DateTime.MinValue;
- var ts = TimeSpan.FromMinutes(compInterval);
- DateTime time;
- for (int i = 0; i < len; i++)
- {
- // Round down the time to match new interval
- try
- {
- time = RoundDown(timestamp[i], ts);
- }
- catch
- {
- throw new Exception("DateTime RoundDown failed!");
- }
- if (time > curTime) // Create new "candle"
- {
- try
- {
- timestampOut.Add(time);
- highOut.Add(high[i]);
- lowOut.Add(low[i]);
- openOut.Add(open[i]);
- closeOut.Add(close[i]);
- volumeOut.Add(volume[i]);
- countOut = 1;
- curTime = time;
- }
- catch
- {
- throw new Exception("Creating candle failed!");
- }
- }
- else // ...or edit data for current candle
- {
- try
- {
- var pos = timestampOut.Count - 1;
- highOut[pos] = Math.Max(highOut[pos], high[i]);
- lowOut[pos] = Math.Min(lowOut[pos], low[i]);
- closeOut[pos] = close[i];
- volumeOut[pos] += volume[i];
- countOut++;
- }
- catch
- {
- throw new Exception("Editing candle failed!");
- }
- }
- }
- if (fullCandlesOnly && countOut < compInterval)
- {
- highOut = highOut.Take(highOut.Count - 1).ToList();
- lowOut = lowOut.Take(lowOut.Count - 1).ToList();
- openOut = openOut.Take(openOut.Count - 1).ToList();
- closeOut = closeOut.Take(closeOut.Count - 1).ToList();
- volumeOut = volumeOut.Take(volumeOut.Count - 1).ToList();
- }
- // Save data and gtfo
- ret.Add(highOut.ToArray());
- ret.Add(lowOut.ToArray());
- ret.Add(openOut.ToArray());
- ret.Add(closeOut.ToArray());
- ret.Add(volumeOut.ToArray());
- return ret;
- }
- // Rounding functions for DateTime
- public static DateTime RoundUp(DateTime dt, TimeSpan ts)
- {
- return Round(dt, ts, true);
- }
- public static DateTime RoundDown(DateTime dt, TimeSpan ts)
- {
- return Round(dt, ts, false);
- }
- private static DateTime Round(DateTime dt, TimeSpan ts, bool up)
- {
- var remainder = dt.Ticks % ts.Ticks;
- if (remainder == 0)
- {
- return dt;
- }
- long delta;
- if (up)
- {
- delta = ts.Ticks - remainder;
- }
- else
- {
- delta = -remainder;
- }
- return dt.AddTicks(delta);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement