Added SpreadStatsStream
This commit is contained in:
1 parent
f2169c6bd4
commit
0a6733180d
8 files changed
+409
-46
No files matched your search
@@ -50,7 +50,7 @@ namespace BCG {
|
||||
int data_col_no = 1;
|
||||
for (int jp = 0; jp < _join_specs.Length; jp++) {
|
||||
NonIncJoinBookkeeper s = new NonIncJoinBookkeeper(_join_specs[jp], jp, data_col_no, _row_hasher);
|
||||
Log.Gen.info($"NonIncJoinStream.myOnFirstSubscriber jp: {jp} spec: {_join_specs[jp]}");
|
||||
//Log.Gen.info($"NonIncJoinStream.myOnFirstSubscriber jp: {jp} spec: {_join_specs[jp]}");
|
||||
data_col_no += _join_specs[jp].data_col_names.Length;
|
||||
s.on_new_data += _stream_callback;
|
||||
_book_keeping_data.Add(s);
|
||||
@@ -71,7 +71,7 @@ namespace BCG {
|
||||
}
|
||||
|
||||
private void _stream_callback() {
|
||||
Log.Gen.info("NonIncJoinStream._stream_callback received callback");
|
||||
//Log.Gen.info("NonIncJoinStream._stream_callback received callback");
|
||||
_is_dirty = true;
|
||||
doWork();
|
||||
}
|
||||
@@ -99,7 +99,7 @@ namespace BCG {
|
||||
|
||||
private int _n_total_fires = 0, _n_expensive_fires = 0;
|
||||
private void _form_and_fire() {
|
||||
Log.Gen.info($"NonIncJoinStream._form_and_fire _is_ready(): {_is_ready()}");
|
||||
//Log.Gen.info($"NonIncJoinStream._form_and_fire _is_ready(): {_is_ready()}");
|
||||
if (_is_ready()) {
|
||||
_construct_schema();
|
||||
var timer = Stopwatch.StartNew();
|
||||
@@ -126,9 +126,9 @@ namespace BCG {
|
||||
_n_total_fires++;
|
||||
//if (timer.ElapsedMilliseconds >= 250) {
|
||||
_n_expensive_fires++;
|
||||
string log_msg = $"Join and map time for {_book_keeping_data.Count} "
|
||||
+ $"tables {timer.Elapsed.TotalMilliseconds} msecs, ratio {_n_expensive_fires}/{_n_total_fires}";
|
||||
Log.Gen.info(log_msg);
|
||||
//string log_msg = $"Join and map time for {_book_keeping_data.Count} "
|
||||
// + $"tables {timer.Elapsed.TotalMilliseconds} msecs, ratio {_n_expensive_fires}/{_n_total_fires}";
|
||||
//Log.Gen.info(log_msg);
|
||||
//}
|
||||
var dp_out = BlueDataUtility.build_dp_inc(dp_output_rows, myLastValue as DataPacket, true);
|
||||
myFireEvent(dp_out);
|
||||
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
namespace bts.SilverPine {
|
||||
using BCG;
|
||||
|
||||
using bts.api;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
[DataStreamUserRespresentation("SpatialFuturePairsStream")]
|
||||
public class SpatialFuturePairsStream : ViewStream {
|
||||
private ViewStream _main_stream;
|
||||
private IBlueTableSchema _schema;
|
||||
private HashSet<string> _key_map = new HashSet<string>();
|
||||
public SpatialFuturePairsStream()
|
||||
: this(ViewStreamDatabase.instance) { }
|
||||
|
||||
public SpatialFuturePairsStream(ViewStreamDatabase vsd) {
|
||||
_main_stream = new InstrumentsStream(vsd);
|
||||
_main_stream = new FilterStream(_main_stream, "sec_type='Future'");
|
||||
_schema = new BlueTableSchema("SpatialFutures", new ColumnInfo("id", 0, TypeCode.Int32));
|
||||
_schema = _schema.add_column(TypeCode.String, "s1");
|
||||
_schema = _schema.add_column(TypeCode.String, "venue_1");
|
||||
_schema = _schema.add_column(TypeCode.String, "s2");
|
||||
_schema = _schema.add_column(TypeCode.String, "venue_2");
|
||||
|
||||
}
|
||||
|
||||
protected override void myOnFirstSubscriber() {
|
||||
_main_stream.onChange += _main_stream_onchange;
|
||||
}
|
||||
|
||||
protected override void myOnLastSubscriber() {
|
||||
_main_stream.onChange -= _main_stream_onchange;
|
||||
}
|
||||
|
||||
private static string _get_key(IBlueRow r1, IBlueRow r2) {
|
||||
string s1 = r1.get<string>("sec");
|
||||
string s2 = r2.get<string>("sec");
|
||||
string key = string.Compare(s1, s2, StringComparison.Ordinal) <= 0 ? s1 + s2 : s2 + s1;
|
||||
return key;
|
||||
}
|
||||
private bool _combine(IBlueRow r1, IBlueRow r2) {
|
||||
string key = _get_key(r1, r2);
|
||||
if (_key_map.Contains(key))
|
||||
return false;
|
||||
if (r1.get<string>("symbol") != r2.get<string>("symbol"))
|
||||
return false;
|
||||
if (r1.get_enum<Venue>("venue") == r2.get_enum<Venue>("venue"))
|
||||
return false;
|
||||
if (r1.get<int>("sec_key") == r2.get<int>("sec_key")) {
|
||||
return false;
|
||||
}
|
||||
DateTime e1 = r1.get_datetime("expiration");
|
||||
DateTime e2 = r2.get_datetime("expiration");
|
||||
bool exps_eq = e1 == e2 || (e1.Year >= 2100 && e2.Year >= 2100);
|
||||
if (!exps_eq)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private void _main_stream_onchange(object dp_obj) {
|
||||
_key_map.Clear();
|
||||
DataPacket dp = dp_obj as DataPacket;
|
||||
BlueTable table = new BlueTable(_schema);
|
||||
var rows = dp.newRows.to_list();
|
||||
foreach (var r1 in rows) {
|
||||
foreach (var r2 in rows) {
|
||||
if (_combine(r1, r2)) {
|
||||
IBlueRow nr = new BlueRow(_schema, table.Count, r1.get<string>("sec"), r1.get<string>("venue"),
|
||||
r2.get<string>("sec"), r2.get<string>("venue"));
|
||||
table.addRow(nr);
|
||||
string key = _get_key(r1, r2);
|
||||
_key_map.Add(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
var dp_out = BlueDataUtility.build_dp(table, myLastValue as DataPacket);
|
||||
myFireEvent(dp_out);
|
||||
}
|
||||
}
|
||||
}
|
||||
+165
-33
@@ -7,6 +7,24 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MathNet.Numerics.Statistics;
|
||||
using MathNet.Numerics.LinearAlgebra;
|
||||
using Amazon.Runtime;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MathNet.Numerics.Statistics;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MathNet.Numerics.Statistics;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MathNet.Numerics.Statistics;
|
||||
// stats calculator
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MathNet.Numerics.Statistics;
|
||||
|
||||
/// <summary>
|
||||
/// Provides evaluation metrics to assess the quality of a synthetic spread for mean-reversion trading.
|
||||
@@ -14,10 +32,114 @@ using MathNet.Numerics.LinearAlgebra;
|
||||
/// </summary>
|
||||
public static class SpreadPerformanceEvaluator
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a single trade action (entry or exit) taken during the simulation.
|
||||
/// </summary>
|
||||
public struct TradeAction
|
||||
|
||||
public static class SpreadStatistics {
|
||||
public class SpreadStatsResult {
|
||||
public Dictionary<string, double> bid_zscore_distribution { get; set; }
|
||||
public Dictionary<string, double> ask_zscore_distribution { get; set; }
|
||||
public Dictionary<string, double> mid_zscore_distribution { get; set; }
|
||||
|
||||
public DescriptiveStatistics bid_stats { get; set; }
|
||||
public DescriptiveStatistics ask_stats { get; set; }
|
||||
public DescriptiveStatistics mid_stats { get; set; }
|
||||
}
|
||||
|
||||
private static readonly string[] _zscore_labels = new[]
|
||||
{
|
||||
"Z < -2σ",
|
||||
"-2σ ≤ Z < -1σ",
|
||||
"-1σ ≤ Z < 0σ",
|
||||
"0σ ≤ Z < 1σ",
|
||||
"1σ ≤ Z < 2σ",
|
||||
"Z ≥ 2σ"
|
||||
};
|
||||
|
||||
public static SpreadStatsResult analyze_spread_quotes(List<(DateTime timestamp, double bid, double bid_qty, double ask, double ask_qty)> quotes) {
|
||||
if (quotes == null || quotes.Count == 0)
|
||||
throw new ArgumentException("Spread quote list cannot be null or empty.");
|
||||
|
||||
var bid_prices = quotes.Select(q => q.bid).ToList();
|
||||
var ask_prices = quotes.Select(q => q.ask).ToList();
|
||||
var mid_prices = quotes.Select(q => (q.bid + q.ask) / 2.0).ToList();
|
||||
|
||||
var bid_stats = new DescriptiveStatistics(bid_prices);
|
||||
var ask_stats = new DescriptiveStatistics(ask_prices);
|
||||
var mid_stats = new DescriptiveStatistics(mid_prices);
|
||||
|
||||
var bid_zscores = bid_prices.Select(x => zscore(x, bid_stats.Mean, bid_stats.StandardDeviation)).ToList();
|
||||
var ask_zscores = ask_prices.Select(x => zscore(x, ask_stats.Mean, ask_stats.StandardDeviation)).ToList();
|
||||
var mid_zscores = mid_prices.Select(x => zscore(x, mid_stats.Mean, mid_stats.StandardDeviation)).ToList();
|
||||
|
||||
var bid_distribution = bin_zscores_to_percentages(bid_zscores);
|
||||
var ask_distribution = bin_zscores_to_percentages(ask_zscores);
|
||||
var mid_distribution = bin_zscores_to_percentages(mid_zscores);
|
||||
|
||||
return new SpreadStatsResult {
|
||||
bid_zscore_distribution = bid_distribution,
|
||||
ask_zscore_distribution = ask_distribution,
|
||||
mid_zscore_distribution = mid_distribution,
|
||||
bid_stats = bid_stats,
|
||||
ask_stats = ask_stats,
|
||||
mid_stats = mid_stats
|
||||
};
|
||||
}
|
||||
|
||||
private static double zscore(double x, double mean, double std_dev) {
|
||||
return std_dev == 0 ? 0 : (x - mean) / std_dev;
|
||||
}
|
||||
|
||||
private static Dictionary<string, double> bin_zscores_to_percentages(List<double> zscores) {
|
||||
var bins = _zscore_labels.ToDictionary(label => label, _ => 0);
|
||||
|
||||
foreach (var z in zscores) {
|
||||
if (z < -2) bins["Z < -2σ"]++;
|
||||
else if (z < -1) bins["-2σ ≤ Z < -1σ"]++;
|
||||
else if (z < 0) bins["-1σ ≤ Z < 0σ"]++;
|
||||
else if (z < 1) bins["0σ ≤ Z < 1σ"]++;
|
||||
else if (z < 2) bins["1σ ≤ Z < 2σ"]++;
|
||||
else bins["Z ≥ 2σ"]++;
|
||||
}
|
||||
|
||||
double total = zscores.Count;
|
||||
return bins.ToDictionary(kvp => kvp.Key, kvp => total == 0 ? 0 : (kvp.Value / total) * 100.0);
|
||||
}
|
||||
|
||||
public static double score_spread(SpreadStatsResult result) {
|
||||
var ask_bins = result.ask_zscore_distribution;
|
||||
var bid_bins = result.bid_zscore_distribution;
|
||||
var mid_bins = result.mid_zscore_distribution;
|
||||
|
||||
double buy_opportunity = ask_bins["Z < -2σ"] + ask_bins["-2σ ≤ Z < -1σ"];
|
||||
double sell_opportunity = bid_bins["Z ≥ 2σ"] + bid_bins["1σ ≤ Z < 2σ"];
|
||||
double opportunity_score = buy_opportunity + sell_opportunity;
|
||||
|
||||
double below_mean = mid_bins["Z < -2σ"] + mid_bins["-2σ ≤ Z < -1σ"] + mid_bins["-1σ ≤ Z < 0σ"];
|
||||
double above_mean = mid_bins["Z ≥ 2σ"] + mid_bins["1σ ≤ Z < 2σ"] + mid_bins["0σ ≤ Z < 1σ"];
|
||||
double symmetry_score = 1.0 - Math.Abs(below_mean - above_mean) / (below_mean + above_mean + 1e-6);
|
||||
|
||||
double amplitude_score = result.mid_stats.StandardDeviation;
|
||||
double skew_penalty = 1.0 / (1.0 + Math.Abs(result.mid_stats.Skewness));
|
||||
double kurtosis_bonus = Math.Min(result.mid_stats.Kurtosis, 6.0) / 6.0;
|
||||
|
||||
double score =
|
||||
opportunity_score *
|
||||
symmetry_score *
|
||||
amplitude_score *
|
||||
skew_penalty *
|
||||
(1.0 + kurtosis_bonus * 0.1);
|
||||
|
||||
return score;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Represents a single trade action (entry or exit) taken during the simulation.
|
||||
/// </summary>
|
||||
public struct TradeAction
|
||||
{
|
||||
public bool IsEntry { get; set; } // True if this action opened a position
|
||||
public bool IsLong { get; set; } // True = Long, False = Short
|
||||
@@ -178,44 +300,54 @@ public static (double Mean, double StdDev) EvaluateMeanAndStdDev(List<Tuple<doub
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Counts how many times the spread ask dropped below (mean - threshold) [buy signal]
|
||||
/// and how many times the spread bid rose above (mean + threshold) [sell signal].
|
||||
/// Assumes spreadData is a trailing, causal window.
|
||||
/// </summary>
|
||||
/// <returns>(BuyExcursions, SellExcursions)</returns>
|
||||
public static (uint BuyExcursions, double cumm_buy_price, uint SellExcursions, double cumm_sell_price) EvaluateExcursionCount(
|
||||
List<Tuple<double, double, double, double>> spreadData,
|
||||
double entryThresholdMultiplier = 0.5)
|
||||
{
|
||||
if (spreadData == null || spreadData.Count < 2)
|
||||
return (0, Double.NaN, 0, Double.NaN);
|
||||
/// <summary>
|
||||
/// Counts how many times the spread ask dropped below (mean - threshold) [buy signal]
|
||||
/// and how many times the spread bid rose above (mean + threshold) [sell signal].
|
||||
/// Assumes spreadData is a trailing, causal window.
|
||||
/// </summary>
|
||||
/// <returns>(BuyExcursions, SellExcursions)</returns>
|
||||
public static (uint BuyExcursions, double cumm_buy_price, uint SellExcursions, double cumm_sell_price) EvaluateExcursionCount(
|
||||
List<Tuple<double, double, double, double>> spreadData,
|
||||
double entryThresholdMultiplier = 0.5) {
|
||||
if (spreadData == null || spreadData.Count < 2)
|
||||
return (0, Double.NaN, 0, Double.NaN);
|
||||
|
||||
var midPrices = spreadData.Select(x => (x.Item1 + x.Item3) / 2.0).ToList();
|
||||
double mean = midPrices.Average();
|
||||
double stddev = midPrices.StandardDeviation();
|
||||
double lowerThreshold = mean - entryThresholdMultiplier * stddev;
|
||||
double upperThreshold = mean + entryThresholdMultiplier * stddev;
|
||||
var midPrices = spreadData.Select(x => (x.Item1 + x.Item3) / 2.0).ToList();
|
||||
double mean = midPrices.Average();
|
||||
double stddev = midPrices.StandardDeviation();
|
||||
double buy_threshold = mean - entryThresholdMultiplier * stddev;
|
||||
double sell_threshold = mean + entryThresholdMultiplier * stddev;
|
||||
|
||||
int buyCount = 0;
|
||||
int sellCount = 0;
|
||||
int buyCount = 0;
|
||||
int sellCount = 0;
|
||||
//bool in_buy_zone = false;
|
||||
//bool in_sell_zone = false;
|
||||
|
||||
double cumm_buy_price = 0.0, cumm_sell_price = 0.0;
|
||||
foreach (var (bid, _, ask, _) in spreadData)
|
||||
{
|
||||
if (ask < lowerThreshold){
|
||||
double cumm_buy_price = 0.0, cumm_sell_price = 0.0;
|
||||
foreach (var (bid, _, ask, _) in spreadData) {
|
||||
if (ask <= buy_threshold) {
|
||||
//if (!in_buy_zone) {
|
||||
cumm_buy_price += ask;
|
||||
buyCount++;
|
||||
}
|
||||
//in_buy_zone = true;
|
||||
}
|
||||
//} else {
|
||||
// in_buy_zone = false;
|
||||
//}
|
||||
|
||||
if (bid > upperThreshold){
|
||||
if (bid >= sell_threshold) {
|
||||
//if (!in_sell_zone) {
|
||||
cumm_sell_price += bid;
|
||||
sellCount++;
|
||||
}
|
||||
}
|
||||
//in_sell_zone = true;
|
||||
}
|
||||
//} else {
|
||||
// in_sell_zone = false;
|
||||
//}
|
||||
}
|
||||
|
||||
return ((uint)buyCount, cumm_buy_price/buyCount, (uint)sellCount, cumm_sell_price/sellCount);
|
||||
}
|
||||
return ((uint)buyCount, cumm_buy_price / buyCount, (uint)sellCount, cumm_sell_price / sellCount);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Counts how many times the spread mid-price crosses its mean level.
|
||||
|
||||
+10
-2
@@ -1,5 +1,8 @@
|
||||
namespace bts.SilverPine {
|
||||
using BCG;
|
||||
|
||||
using System.Diagnostics;
|
||||
|
||||
public class SpreadPerformanceStream : ViewStream {
|
||||
ViewStream _spread_price_stream = null;
|
||||
private readonly IBlueTableSchema _schema;
|
||||
@@ -30,8 +33,8 @@ namespace bts.SilverPine {
|
||||
Tuple.Create("average_zscore_decay", TypeCode.Double),
|
||||
Tuple.Create("variance_ratio", TypeCode.Double),
|
||||
Tuple.Create("n_zero_crossings", TypeCode.UInt32),
|
||||
// distributions
|
||||
Tuple.Create("≤ 0.5σ", TypeCode.Double),
|
||||
// distributions
|
||||
Tuple.Create("Z ≤ 0.5σ", TypeCode.Double),
|
||||
Tuple.Create("0.5σ – 1.0σ", TypeCode.Double),
|
||||
Tuple.Create("1.0σ – 2.0σ", TypeCode.Double),
|
||||
Tuple.Create("> 2.0σ", TypeCode.Double),
|
||||
@@ -68,6 +71,8 @@ namespace bts.SilverPine {
|
||||
DataPacket dp = dp_obj as DataPacket;
|
||||
List<Tuple<DateTime, double, double, double, double>> spread_ts_prices = new List<Tuple<DateTime, double, double, double, double>>();
|
||||
DateTime min_time = DateTime.MaxValue; DateTime max_time = DateTime.MinValue;
|
||||
var sw = Stopwatch.StartNew();
|
||||
sw.Start();
|
||||
foreach (IBlueRow r in dp.newRows) {
|
||||
DateTime ts = r.get_datetime("timestamp");
|
||||
TimeSpan elapsed_time = DateTime.Now - ts;
|
||||
@@ -120,6 +125,9 @@ namespace bts.SilverPine {
|
||||
BlueTable t = new BlueTable(_schema);
|
||||
t.addRow(nr);
|
||||
var dp_out = BlueDataUtility.build_dp_inc(t, myLastValue as DataPacket, true);
|
||||
var perf_msecs = sw.ElapsedMilliseconds;
|
||||
sw.Stop();
|
||||
Log.Gen.info($"SpreadPerformanceStream._handle_spread_prices comp_time: {perf_msecs} msecs");
|
||||
myFireEvent(dp_out);
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
namespace bts.SilverPine {
|
||||
using BCG;
|
||||
|
||||
using MathNet.Numerics.LinearAlgebra.Factorization;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
[DataStreamUserRespresentation("SpreadStatsStream")]
|
||||
public class SpreadStatsStream : ViewStream {
|
||||
private ViewStream _spread_price_stream;
|
||||
private readonly IBlueTableSchema _schema;
|
||||
private readonly TimeSpan _window_sz;
|
||||
private readonly string _spread;
|
||||
public SpreadStatsStream(string s1, string s2, double w1, double w2, int rolling_window_sz)
|
||||
: this(s1, s2, w1, w2, rolling_window_sz, ViewStreamDatabase.instance) { }
|
||||
public SpreadStatsStream(string s1, string s2, double w1, double w2, int rolling_window_sz, ViewStreamDatabase vsd) {
|
||||
_window_sz = TimeSpan.FromSeconds(rolling_window_sz);
|
||||
_spread = $"{w1} X {s1} - {w2} X {s2}";
|
||||
_schema = new BlueTableSchema("SpreaddStats", new ColumnInfo ("id", 0, TypeCode.Int32));
|
||||
_schema = _schema.add_column(TypeCode.String, "spread");
|
||||
_schema = _schema.add_column(TypeCode.String, "window");
|
||||
// basic stats
|
||||
_schema = _schema.add_column(TypeCode.Double, "bid_mean");
|
||||
_schema = _schema.add_column(TypeCode.Double, "ask_mean");
|
||||
_schema = _schema.add_column(TypeCode.Double, "bid_stddev");
|
||||
_schema = _schema.add_column(TypeCode.Double, "ask_stddev");
|
||||
_schema = _schema.add_column(TypeCode.Double, "bid_skewness");
|
||||
_schema = _schema.add_column(TypeCode.Double, "ask_skewness");
|
||||
_schema = _schema.add_column(TypeCode.Double, "bid_kurtosis");
|
||||
_schema = _schema.add_column(TypeCode.Double, "ask_kurtosis");
|
||||
_schema = _schema.add_column(TypeCode.Double, "bid_max");
|
||||
_schema = _schema.add_column(TypeCode.Double, "ask_max");
|
||||
_schema = _schema.add_column(TypeCode.Double, "bid_min");
|
||||
_schema = _schema.add_column(TypeCode.Double, "ask_min");
|
||||
// bid bins
|
||||
_schema = _schema.add_column(TypeCode.Double, "bid Z < -2σ");
|
||||
_schema = _schema.add_column(TypeCode.Double, "bid -2σ ≤ Z < -1σ");
|
||||
_schema = _schema.add_column(TypeCode.Double, "bid -1σ ≤ Z < 0σ");
|
||||
// positive z's
|
||||
_schema = _schema.add_column(TypeCode.Double, "bid 0σ ≤ Z < 1σ");
|
||||
_schema = _schema.add_column(TypeCode.Double, "bid 1σ ≤ Z < 2σ");
|
||||
_schema = _schema.add_column(TypeCode.Double, "bid Z ≥ 2σ");
|
||||
// ask bins
|
||||
_schema = _schema.add_column(TypeCode.Double, "ask Z < -2σ");
|
||||
_schema = _schema.add_column(TypeCode.Double, "ask -2σ ≤ Z < -1σ");
|
||||
_schema = _schema.add_column(TypeCode.Double, "ask -1σ ≤ Z < 0σ");
|
||||
// positive z's
|
||||
_schema = _schema.add_column(TypeCode.Double, "ask 0σ ≤ Z < 1σ");
|
||||
_schema = _schema.add_column(TypeCode.Double, "ask 1σ ≤ Z < 2σ");
|
||||
_schema = _schema.add_column(TypeCode.Double, "ask Z ≥ 2σ");
|
||||
// opps
|
||||
_schema = _schema.add_column(TypeCode.Int32, "n_buy_ticks");
|
||||
_schema = _schema.add_column(TypeCode.Double, "cumm_buy_price");
|
||||
_schema = _schema.add_column(TypeCode.Int32, "n_sell_ticks");
|
||||
_schema = _schema.add_column(TypeCode.Double, "cumm_sell_price");
|
||||
_schema = _schema.add_column(TypeCode.Int32, "n_trade_ticks");
|
||||
_spread_price_stream = new SyntheticSpreadStream(s1, s2, w1, w2, vsd);
|
||||
_spread_price_stream = new TimeSeriesStream(_spread_price_stream);
|
||||
_spread_price_stream = new ThrottleStream(_spread_price_stream, TimeSpan.FromSeconds(5.0), vsd);
|
||||
}
|
||||
|
||||
protected override void myOnFirstSubscriber() {
|
||||
_spread_price_stream.onChange += _handle_spread_prices;
|
||||
}
|
||||
|
||||
protected override void myOnLastSubscriber() {
|
||||
_spread_price_stream.onChange -= _handle_spread_prices;
|
||||
}
|
||||
|
||||
private void _handle_spread_prices(object dp_obj) {
|
||||
List<IBlueRow> rows = (dp_obj as DataPacket).newRows.to_list();
|
||||
Func<IBlueRow, bool> row_filter = (r) => {
|
||||
DateTime ts = r.get_datetime("timestamp");
|
||||
DateTime start_time = DateTime.Now.Subtract(_window_sz);
|
||||
return ts >= start_time;
|
||||
};
|
||||
var rolling_win_rows = rows.Where(r => row_filter(r));
|
||||
var spread_prices = rolling_win_rows.Select(r => (
|
||||
r.get_datetime("timestamp"),
|
||||
r.get<double>("spread_bid_price"),
|
||||
r.get<double>("spread_bid_sz"),
|
||||
r.get<double>("spread_ask_price"),
|
||||
r.get<double>("spread_ask_sz"))).ToList();
|
||||
spread_prices.Sort((a,b) => a.Item1.CompareTo(b.Item1));
|
||||
DateTime t0 = spread_prices.First().Item1, t1 = spread_prices.Last().Item1;
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
Log.Gen.info($"SpreadStatsStream.analyze_spread_quotes executed in {stopwatch.ElapsedMilliseconds} ms");
|
||||
var result = SpreadPerformanceEvaluator.SpreadStatistics.analyze_spread_quotes(spread_prices);
|
||||
(uint n_buy_signals, double cumm_buy_price, uint n_sell_signals, double cumm_sell_price) =
|
||||
SpreadPerformanceEvaluator.EvaluateExcursionCount(spread_prices.Select(t => Tuple.Create(t.Item2, t.Item3, t.Item4, t.Item5)).ToList(), 2.0);
|
||||
IBlueRow nr = new BlueRow(_schema,
|
||||
0,
|
||||
_spread,
|
||||
$"n_ticks: {spread_prices.Count} t0: {t0.ToLongTimeString()} -> t1: {t1.ToLongTimeString()}",
|
||||
result.bid_stats.Mean,
|
||||
result.ask_stats.Mean,
|
||||
result.bid_stats.StandardDeviation,
|
||||
result.ask_stats.StandardDeviation,
|
||||
result.bid_stats.Skewness,
|
||||
result.ask_stats.Skewness,
|
||||
result.bid_stats.Kurtosis,
|
||||
result.ask_stats.Kurtosis,
|
||||
result.bid_stats.Maximum,
|
||||
result.ask_stats.Maximum,
|
||||
result.bid_stats.Minimum,
|
||||
result.ask_stats.Minimum,
|
||||
// bid distribution
|
||||
result.bid_zscore_distribution["Z < -2σ"],
|
||||
result.bid_zscore_distribution["-2σ ≤ Z < -1σ"],
|
||||
result.bid_zscore_distribution["-1σ ≤ Z < 0σ"],
|
||||
result.bid_zscore_distribution["0σ ≤ Z < 1σ"],
|
||||
result.bid_zscore_distribution["1σ ≤ Z < 2σ"],
|
||||
result.bid_zscore_distribution["Z ≥ 2σ"],
|
||||
// ask distributions
|
||||
result.ask_zscore_distribution["Z < -2σ"],
|
||||
result.ask_zscore_distribution["-2σ ≤ Z < -1σ"],
|
||||
result.ask_zscore_distribution["-1σ ≤ Z < 0σ"],
|
||||
result.ask_zscore_distribution["0σ ≤ Z < 1σ"],
|
||||
result.ask_zscore_distribution["1σ ≤ Z < 2σ"],
|
||||
result.ask_zscore_distribution["Z ≥ 2σ"],
|
||||
n_buy_signals,
|
||||
cumm_buy_price,
|
||||
n_sell_signals,
|
||||
cumm_sell_price,
|
||||
n_buy_signals + n_sell_signals
|
||||
);
|
||||
stopwatch.Stop();
|
||||
BlueTable t = new BlueTable(_schema);
|
||||
t.addRow(nr);
|
||||
var dp_out = BlueDataUtility.build_dp(t, myLastValue as DataPacket);
|
||||
myFireEvent(dp_out);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -161,7 +161,7 @@ namespace bts.SilverPine {
|
||||
|
||||
private void _scan_bbo() {
|
||||
// this shouldn be made incremental using the TableUpdate
|
||||
bts.Log.Gen.info("BBOStream._scan_bbo");
|
||||
//bts.Log.Gen.info("BBOStream._scan_bbo");
|
||||
if (_cached_state == null)
|
||||
return;
|
||||
BlueTable new_state = new BlueTable(_bbo_schema);
|
||||
|
||||
@@ -32,6 +32,7 @@ namespace bts.SilverPine {
|
||||
Tuple.Create("sec_type", TypeCode.String),
|
||||
Tuple.Create("venue", TypeCode.String),
|
||||
Tuple.Create("symbol", TypeCode.String),
|
||||
Tuple.Create("currency", TypeCode.String),
|
||||
Tuple.Create("expiration", TypeCode.DateTime),
|
||||
Tuple.Create("strike", TypeCode.Double),
|
||||
Tuple.Create("callput", TypeCode.String)
|
||||
@@ -82,7 +83,7 @@ namespace bts.SilverPine {
|
||||
foreach (var kv in _cached_state) {
|
||||
SecurityKey skey = kv.Key;
|
||||
int id = Convert.ToInt32(skey);
|
||||
SecurityImmutable sec = kv.Value;
|
||||
SecurityImmutable sec = kv.Value;
|
||||
bts.api.SecurityType sec_type = sec.security_type();
|
||||
object exp = DBNull.Value;
|
||||
if (sec_type == bts.api.SecurityType.Future || sec_type == bts.api.SecurityType.Option)
|
||||
@@ -93,7 +94,7 @@ namespace bts.SilverPine {
|
||||
object cp = DBNull.Value;
|
||||
if (sec_type == bts.api.SecurityType.Option)
|
||||
cp = sec.Option.CallPut.ToString();
|
||||
IBlueRow r = new BlueRow(_instrument_schema, id, skey, sec.security_desc(), sec.security_type().ToString(), sec.display_exchange().ToString(), sec.Symbol, exp, strike, cp);
|
||||
IBlueRow r = new BlueRow(_instrument_schema, id, skey, sec.security_desc(), sec.security_type().ToString(), sec.display_exchange().ToString(), sec.Symbol, sec.Currency, exp, strike, cp);
|
||||
new_state.addRow(r);
|
||||
}
|
||||
DataPacket dp = null;
|
||||
@@ -118,4 +119,4 @@ namespace bts.SilverPine {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -96,7 +96,7 @@ namespace bts.SilverPine
|
||||
IBlueTableSchema out_schema = get_schema(r.schema);
|
||||
MutableBlueRow rv = new MutableBlueRow(out_schema, r);
|
||||
rv.setValue("spread_bid_price", spread_bid_price);
|
||||
rv.setValue("spread_ask_price", spread_bid_price);
|
||||
rv.setValue("spread_ask_price", spread_ask_price);
|
||||
rv.setValue("spread_bid_sz", spread_bid_sz);
|
||||
rv.setValue("spread_ask_sz", spread_ask_sz);
|
||||
return rv;
|
||||
|
||||
Reference in new issue
Block a user