From b16ff50120ad42fc4e17eedfbcced4e10e2126f3 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Thu, 13 Jun 2024 15:16:39 +0000 Subject: [PATCH] cleanup: Normalize indentation in TradeBarsStream --- .../MarketData/Charting/TradeBarsStream.cs | 299 +++++++++--------- 1 file changed, 146 insertions(+), 153 deletions(-) diff --git a/src/DataStreamSuite/SharedStreams/Business/MarketData/Charting/TradeBarsStream.cs b/src/DataStreamSuite/SharedStreams/Business/MarketData/Charting/TradeBarsStream.cs index 5fa0cf634..929795693 100644 --- a/src/DataStreamSuite/SharedStreams/Business/MarketData/Charting/TradeBarsStream.cs +++ b/src/DataStreamSuite/SharedStreams/Business/MarketData/Charting/TradeBarsStream.cs @@ -1,161 +1,154 @@ using BCG; using BCG.Utilities.General; -using System; +namespace bts; -namespace bts { - [DataStreamUserRespresentation("tradebars")] - public class TradeBarsStream : ViewStream, IWorker { +[DataStreamUserRespresentation("tradebars")] +public class TradeBarsStream : ViewStream, IWorker { - public class TradeBar { - static double[] calculate_log_returns(double[] prices) { - // Calculate logarithmic returns - double[] returns = new double[prices.Length - 1]; - for (int i = 1; i < prices.Length; i++) { - returns[i - 1] = Math.Log(prices[i] / prices[i - 1]); - } - return returns; + public class TradeBar { + static double[] calculate_log_returns(double[] prices) { + // Calculate logarithmic returns + double[] returns = new double[prices.Length - 1]; + for (int i = 1; i < prices.Length; i++) { + returns[i - 1] = Math.Log(prices[i] / prices[i - 1]); + } + return returns; + } + + static double calculate_volatility(double[] returns) { + // Calculate standard deviation of logarithmic returns + double mean = returns.Average(); + double sum_of_squares = returns.Sum(x => Math.Pow(x - mean, 2)); + double variance = sum_of_squares / (returns.Length - 1); + double volatility = Math.Sqrt(variance); + + return volatility; + } + public void add_trade(IBlueRow r) { + double price = r.get("price"); + //prices.Add(price); + double qty = r.get(col_name: "qty"); + if (n_entries == 0) { + open = price; + low = price < low ? price : low; + high = price > high ? price : high; + } + n_entries++; + volume += qty; + vwap_sum += price * qty; + close = price; + if ("Buy".Equals(r.get("aggressor"))) { + n_buys_aggs++; + } else { + n_sell_aggs++; + } + } + private List prices = new List(); + public readonly string security; + public double low = Double.MaxValue, high, open, close, median, std_dev, vwap_sum, volume; + public uint n_buys_aggs, n_sell_aggs, n_entries; + public DateTime bar_start = DateTime.Now; + public TradeBar(string security_) { + security = security_; + } + public double volatility { + get { + //if (prices.Count == 1) + return 0.0; + // var log_returns = calculate_log_returns(prices.ToArray()); + // return calculate_volatility(log_returns); + } + } + } + + private readonly int _bar_period_seconds; + private readonly ViewStreamDatabase _vsd; + private ViewStream _timesales_stream; + private ViewStream _bbo_stream; + private IDictionary _sec_bar_map = new Dictionary(); + private bool _dirty = false; + private TimeSpan _fire_period; + private DateTime _last_fire_time = DateTime.Now; + private BlueTable _table = new BlueTable(BlueDataUtility.form_schema("TradeBars", + Tuple.Create("id", TypeCode.Int32), + Tuple.Create("bar_start", TypeCode.DateTime), + Tuple.Create("bar_end", TypeCode.DateTime), + Tuple.Create("day_of_week", TypeCode.String), + Tuple.Create("n_trades", TypeCode.UInt32), + Tuple.Create("security", TypeCode.String), + Tuple.Create("sec_key", TypeCode.UInt32), + Tuple.Create("low", TypeCode.Double), + Tuple.Create("high", TypeCode.Double), + Tuple.Create("open", TypeCode.Double), + Tuple.Create("close", TypeCode.Double), + Tuple.Create("vwap_sum", TypeCode.Double), + Tuple.Create("volume", TypeCode.Double), + Tuple.Create("n_buy_aggs", TypeCode.UInt32), + Tuple.Create("n_sell_aggs", TypeCode.UInt32), + Tuple.Create("volatility", TypeCode.Double))); + + + public TradeBarsStream(int bar_period_seconds) + : this(bar_period_seconds, ViewStreamDatabase.instance) { } + public TradeBarsStream(int bar_period_seconds, ViewStreamDatabase vsd) { + _vsd = vsd; + _bar_period_seconds = bar_period_seconds; + _timesales_stream = new TimeSalesStream(10000, vsd); + _fire_period = TimeSpan.FromSeconds(bar_period_seconds); + _bbo_stream = new BBOStream(_vsd); + } + + protected override void myOnFirstSubscriber() { + _timesales_stream.onChange += _on_timesales; + _bbo_stream.onChange += _on_bbo; + _vsd.addWorker(this); + } + + protected override void myOnLastSubscriber() { + _vsd.removeWorker(this); + _timesales_stream.onChange -= _on_timesales; + _bbo_stream.onChange -= _on_bbo; + } + + public void doWork() { + if (DateTime.Now - _last_fire_time > _fire_period) { + if (_dirty) { + _dirty = false; + _last_fire_time = DateTime.Now; + BlueTable added_rows = new BlueTable(_table.schema); + foreach (var kv in _sec_bar_map) { + var sec_key = kv.Key; + var tbar = kv.Value; + IBlueRow new_row = new BlueRow(_table.schema, _table.Count, tbar.bar_start, DateTime.Now, + tbar.bar_start.DayOfWeek.ToString(), + tbar.n_entries, tbar.security, sec_key, tbar.low, tbar.high, tbar.open, + tbar.close, tbar.vwap_sum, tbar.volume, tbar.n_sell_aggs, tbar.n_sell_aggs, + tbar.volatility); + added_rows.addRow(new_row); + _table.addRow(new_row); } + _sec_bar_map.Clear(); + DataPacket dp_out = new DataPacket(_table, added_rows, new BlueTable(_table.schema)); + myFireEvent(dp_out); + } + } + } - static double calculate_volatility(double[] returns) { - // Calculate standard deviation of logarithmic returns - double mean = returns.Average(); - double sum_of_squares = returns.Sum(x => Math.Pow(x - mean, 2)); - double variance = sum_of_squares / (returns.Length - 1); - double volatility = Math.Sqrt(variance); + private void _on_timesales(object obj) { + DataPacket dp = obj as DataPacket; + foreach (IBlueRow r in dp.addedRows) { + uint sec_key = r.get("sec_key"); + TradeBar tbar = null; + if (!_sec_bar_map.TryGetValue(sec_key, out tbar)) { + tbar = new TradeBar(r.get("sec")); + _sec_bar_map.Add(sec_key, tbar); + } + _dirty = true; + tbar.add_trade(r); + } + } - return volatility; - } - public void add_trade(IBlueRow r) { - double price = r.get("price"); - //prices.Add(price); - double qty = r.get(col_name: "qty"); - if (n_entries == 0) { - open = price; - low = price < low ? price : low; - high = price > high ? price : high; - } - n_entries++; - volume += qty; - vwap_sum += price * qty; - close = price; - if ("Buy".Equals(r.get("aggressor"))) { - n_buys_aggs++; - } else { - n_sell_aggs++; - } - } - private List prices = new List(); - public readonly string security; - public double low = Double.MaxValue, high, open, close, median, std_dev, vwap_sum, volume; - public uint n_buys_aggs, n_sell_aggs, n_entries; - public DateTime bar_start = DateTime.Now; - public TradeBar(string security_) { - security = security_; - } - public double volatility { - get { - //if (prices.Count == 1) - return 0.0; - var log_returns = calculate_log_returns(prices.ToArray()); - return calculate_volatility(log_returns); - } - } - } - - private readonly int _bar_period_seconds; - private readonly ViewStreamDatabase _vsd; - private ViewStream _timesales_stream; - private ViewStream _bbo_stream; - private IDictionary _sec_bar_map = new Dictionary(); - private bool _dirty = false; - private TimeSpan _fire_period; - private DateTime _last_fire_time = DateTime.Now; - private BlueTable _table = new BlueTable(BlueDataUtility.form_schema("TradeBars", - Tuple.Create("id", TypeCode.Int32), - Tuple.Create("bar_start", TypeCode.DateTime), - Tuple.Create("bar_end", TypeCode.DateTime), - Tuple.Create("day_of_week", TypeCode.String), - Tuple.Create("n_trades", TypeCode.UInt32), - Tuple.Create("security", TypeCode.String), - Tuple.Create("sec_key", TypeCode.UInt32), - Tuple.Create("low", TypeCode.Double), - Tuple.Create("high", TypeCode.Double), - Tuple.Create("open", TypeCode.Double), - Tuple.Create("close", TypeCode.Double), - Tuple.Create("vwap_sum", TypeCode.Double), - Tuple.Create("volume", TypeCode.Double), - Tuple.Create("n_buy_aggs", TypeCode.UInt32), - Tuple.Create("n_sell_aggs", TypeCode.UInt32), - Tuple.Create("volatility", TypeCode.Double))); - - - public TradeBarsStream(int bar_period_seconds) - : this(bar_period_seconds, ViewStreamDatabase.instance) { } - public TradeBarsStream(int bar_period_seconds, ViewStreamDatabase vsd) { - _vsd = vsd; - _bar_period_seconds = bar_period_seconds; - _timesales_stream = new TimeSalesStream(10000, vsd); - _fire_period = TimeSpan.FromSeconds(bar_period_seconds); - _bbo_stream = new BBOStream(_vsd); - } - - protected override void myOnFirstSubscriber() { - _timesales_stream.onChange += _on_timesales; - _bbo_stream.onChange += _on_bbo; - _vsd.addWorker(this); - } - - protected override void myOnLastSubscriber() { - _vsd.removeWorker(this); - _timesales_stream.onChange -= _on_timesales; - _bbo_stream.onChange -= _on_bbo; - } - - public void doWork() { - if (DateTime.Now - _last_fire_time > _fire_period) { - if (_dirty) { - _dirty = false; - _last_fire_time = DateTime.Now; - BlueTable added_rows = new BlueTable(_table.schema); - foreach (var kv in _sec_bar_map) { - var sec_key = kv.Key; - var tbar = kv.Value; - IBlueRow new_row = new BlueRow(_table.schema, _table.Count, tbar.bar_start, DateTime.Now, - tbar.bar_start.DayOfWeek.ToString(), - tbar.n_entries, tbar.security, sec_key, tbar.low, tbar.high, tbar.open, - tbar.close, tbar.vwap_sum, tbar.volume, tbar.n_sell_aggs, tbar.n_sell_aggs, - tbar.volatility); - added_rows.addRow(new_row); - _table.addRow(new_row); - } - _sec_bar_map.Clear(); - DataPacket dp_out = new DataPacket(_table, added_rows, new BlueTable(_table.schema)); - myFireEvent(dp_out); - } - } - } - - private void _on_timesales(object obj) { - DataPacket dp = obj as DataPacket; - foreach (IBlueRow r in dp.addedRows) { - uint sec_key = r.get("sec_key"); - TradeBar tbar = null; - if (!_sec_bar_map.TryGetValue(sec_key, out tbar)) { - tbar = new TradeBar(r.get("sec")); - _sec_bar_map.Add(sec_key, tbar); - } - _dirty = true; - tbar.add_trade(r); - } - - - } - - private void _on_bbo(object obj) { - - } - } - - -} \ No newline at end of file + private void _on_bbo(object obj) { + } +}