Advertisement
Guest User

Candlestick compressor

a guest
May 21st, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.40 KB | None | 0 0
  1.         //
  2.         // Converts candlestick data to another interval
  3.         List<double[]> instr(InsuranceContext context, int mult, out int countOut, bool fullCandlesOnly = false)
  4.         {
  5.             // Get raw input data
  6.             DateTime[] timestamp = context.PriceInstrument.Timestamps;
  7.             double[] high = context.PriceInstrument.High;
  8.             double[] low = context.PriceInstrument.Low;
  9.             double[] open = context.PriceInstrument.Open;
  10.             double[] close = context.PriceInstrument.Close;
  11.             double[] volume = context.PriceInstrument.Volume;
  12.             int interval = 1;
  13.             int compInterval = interval * mult;
  14.  
  15.             // Output data
  16.             var len = timestamp.Length;
  17.             var ret = new List<double[]>();
  18.             var timestampOut = new List<DateTime>();
  19.             var highOut = new List<double>();
  20.             var lowOut = new List<double>();
  21.             var openOut = new List<double>();
  22.             var closeOut = new List<double>();
  23.             var volumeOut = new List<double>();
  24.             countOut = 0;
  25.  
  26.             var curTime = DateTime.MinValue;
  27.             var ts = TimeSpan.FromMinutes(compInterval);
  28.             DateTime time;
  29.  
  30.             for (int i = 0; i < len; i++)
  31.             {
  32.                 // Round down the time to match new interval
  33.                 try
  34.                 {
  35.                     time = RoundDown(timestamp[i], ts);
  36.                 }
  37.                 catch
  38.                 {
  39.                     throw new Exception("DateTime RoundDown failed!");
  40.                 }
  41.  
  42.  
  43.                 if (time > curTime) // Create new "candle"
  44.                 {
  45.                     try
  46.                     {
  47.                         timestampOut.Add(time);
  48.                         highOut.Add(high[i]);
  49.                         lowOut.Add(low[i]);
  50.                         openOut.Add(open[i]);
  51.                         closeOut.Add(close[i]);
  52.                         volumeOut.Add(volume[i]);
  53.                         countOut = 1;
  54.  
  55.                         curTime = time;
  56.                     }
  57.                     catch
  58.                     {
  59.                         throw new Exception("Creating candle failed!");
  60.                     }
  61.                 }
  62.                 else // ...or edit data for current candle
  63.                 {
  64.                     try
  65.                     {
  66.                         var pos = timestampOut.Count - 1;
  67.  
  68.                         highOut[pos] = Math.Max(highOut[pos], high[i]);
  69.                         lowOut[pos] = Math.Min(lowOut[pos], low[i]);
  70.                         closeOut[pos] = close[i];
  71.                         volumeOut[pos] += volume[i];
  72.                         countOut++;
  73.                     }
  74.                     catch
  75.                     {
  76.                         throw new Exception("Editing candle failed!");
  77.                     }
  78.                 }
  79.             }
  80.  
  81.             if (fullCandlesOnly && countOut < compInterval)
  82.             {
  83.                 highOut = highOut.Take(highOut.Count - 1).ToList();
  84.                 lowOut = lowOut.Take(lowOut.Count - 1).ToList();
  85.                 openOut = openOut.Take(openOut.Count - 1).ToList();
  86.                 closeOut = closeOut.Take(closeOut.Count - 1).ToList();
  87.                 volumeOut = volumeOut.Take(volumeOut.Count - 1).ToList();
  88.             }
  89.  
  90.             // Save data and gtfo
  91.             ret.Add(highOut.ToArray());
  92.             ret.Add(lowOut.ToArray());
  93.             ret.Add(openOut.ToArray());
  94.             ret.Add(closeOut.ToArray());
  95.             ret.Add(volumeOut.ToArray());
  96.  
  97.             return ret;
  98.         }
  99.  
  100.         // Rounding functions for DateTime
  101.         public static DateTime RoundUp(DateTime dt, TimeSpan ts)
  102.         {
  103.             return Round(dt, ts, true);
  104.         }
  105.  
  106.         public static DateTime RoundDown(DateTime dt, TimeSpan ts)
  107.         {
  108.             return Round(dt, ts, false);
  109.         }
  110.  
  111.         private static DateTime Round(DateTime dt, TimeSpan ts, bool up)
  112.         {
  113.             var remainder = dt.Ticks % ts.Ticks;
  114.             if (remainder == 0)
  115.             {
  116.                 return dt;
  117.             }
  118.  
  119.             long delta;
  120.             if (up)
  121.             {
  122.                 delta = ts.Ticks - remainder;
  123.             }
  124.             else
  125.             {
  126.                 delta = -remainder;
  127.             }
  128.  
  129.             return dt.AddTicks(delta);
  130.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement