refactoring

This commit is contained in:
Taha Afzal committed 2019-12-11 13:33:07 -05:00
1 parent 6fd829df49
commit 4e48bcf6b5
5 files changed
+92 -74

No files matched your search

@@ -20,47 +20,47 @@ namespace bts.history{
synthetic_book = synthetic_book_a;
}
}
// public static void get_synthetic_prices(
// SpreaderOrderDefinition synth,
// DateTime date,
// Action<IEnumerable<BookEntry>, BookEntry> on_price){
// var sspr_name = synth.Security.Synthetic.Name;
// List<BookSeries> leg_book_series = new List<BookSeries>();
// foreach (ContractLeg l in synth.Security.Synthetic.Legs){
// var bser = MarketQueriesHelper.get_book_state(
// l.Security.Symbol,
// l.Security.Glbx.SecurityDesc,
// date);
// leg_book_series.Add(bser);
// }
// align_bookseries_fast(
// (abooks) => {
// var sbe = _compute_synthetic_top_book(synth, abooks.ToArray());
// on_price(abooks,sbe);
// },
// leg_book_series.ToArray());
// }
public static IEnumerable<SyntheticPrice> get_synthetic_prices(
SpreaderOrderDefinition synth,
DateTime date){
var sspr_name = synth.Security.Synthetic.Name;
public static void get_synthetic_prices(
SpreaderOrderDefinition synth,
DateTime date,
Action<SyntheticPrice> on_price){
var sspr_name = synth.Security.Synthetic.Name;
List<BookSeries> leg_book_series = new List<BookSeries>();
foreach (ContractLeg l in synth.Security.Synthetic.Legs){
BookSeries bser =
MarketQueriesHelper.get_book_state(l.Security.Symbol,
var bser = MarketQueriesHelper.get_book_series_fast(
l.Security.Symbol,
l.Security.Glbx.SecurityDesc,
date);
leg_book_series.Add(bser);
}
var alligned_books = allign_bookseries(leg_book_series.ToArray());
var rv = new List<SyntheticPrice>();
foreach (var ab in alligned_books){
var sp = _compute_synthetic_top_book(synth,ab.ToArray());
rv.Add(new SyntheticPrice(ab,sp));
}
return rv;
}
align_bookseries(
(abooks) => {
var sbe = _compute_synthetic_top_book(synth, abooks.ToArray());
on_price(new SyntheticPrice(abooks,sbe));
},
leg_book_series.ToArray());
}
// public static IEnumerable<SyntheticPrice> get_synthetic_prices(
// SpreaderOrderDefinition synth,
// DateTime date){
// var sspr_name = synth.Security.Synthetic.Name;
// List<BookSeries> leg_book_series = new List<BookSeries>();
// foreach (ContractLeg l in synth.Security.Synthetic.Legs){
// BookSeries bser =
// MarketQueriesHelper.get_book_series(l.Security.Symbol,
// l.Security.Glbx.SecurityDesc,
// date);
// leg_book_series.Add(bser);
// }
// var alligned_books = allign_bookseries(leg_book_series.ToArray());
// var rv = new List<SyntheticPrice>();
// foreach (var ab in alligned_books){
// var sp = _compute_synthetic_top_book(synth,ab.ToArray());
// rv.Add(new SyntheticPrice(ab,sp));
// }
// return rv;
// }
// public static BookEntry compute_synthetic_book(
// SpreaderOrderDefinition order_def,
// params BookEntry[] leg_books){
@@ -142,10 +142,10 @@ namespace bts.history{
public static IEnumerable<IEnumerable<BookEntry>> allign_bookseries(
params BookSeries[] leg_book_serieses){
var rv = new List<IEnumerable<BookEntry>>();
align_bookseries_fast(s => rv.Add(s), leg_book_serieses);
align_bookseries(s => rv.Add(new List<BookEntry>(s)), leg_book_serieses);
return rv;
}
public static void align_bookseries_fast(
public static void align_bookseries(
Action<IEnumerable<BookEntry>> on_book,
params BookSeries[] leg_book_serieses){
// book series with all leg books
@@ -170,7 +170,7 @@ namespace bts.history{
foreach (var t in all){
curr_vals[t.Item1]=t.Item2;
if(is_ready())
on_book(new List<BookEntry>(curr_vals));
on_book(curr_vals);
}
}
@@ -264,9 +264,9 @@ namespace bts.history{
}
return rv;
}
public BookSeries get_book_series (string symbol,
string sec_txt,
DateTime date){
public BookSeries get_book_series(string symbol,
string sec_txt,
DateTime date){
var key = String.Format("{0}_{1}_{2}",symbol,sec_txt,
date.ToShortDateString());
Tuple<DateTime,BookSeries> rv;
@@ -301,12 +301,19 @@ namespace bts.history{
return i.Size;
}
public static BookSeries get_book_state (string symbol,
public static BookSeries get_book_series (string symbol,
string sec_txt,
DateTime date){
string fp = get_book_file(symbol,sec_txt,date);
return new BookSeries(symbol, fp);
}
private static BookSeriesCacher _bser_cacher = new BookSeriesCacher();
public static BookSeries get_book_series_fast (string symbol,
string sec_txt,
DateTime date){
return _bser_cacher.get_book_series(symbol,sec_txt,date);
}
public static void verify_book_series_order (IEnumerable<ProcessedBook> books){
for(int i=1;i< books.Count();i++){
@@ -35,7 +35,7 @@ namespace bts.history{
string contract = args[3];
UInt32 m1 = UInt32.Parse(args[4]),
m2 = UInt32.Parse(args[5]);
BookSeries bser = MarketQueriesHelper.get_book_state(symbol,
BookSeries bser = MarketQueriesHelper.get_book_series(symbol,
contract,
dt);
var books = MarketQueries.find_in_range(bser,m1,m2);
@@ -97,7 +97,7 @@ namespace bts.history{
_compare_seq_no (coex.ElementAt(6),3,4); //1035
_compare_seq_no (coex.ElementAt(7),4,4); //1050
MarketQueries.align_bookseries_fast(
MarketQueries.align_bookseries(
l => {
var b1 = l.ElementAt(0);
var b2 = l.ElementAt(1);
@@ -126,7 +126,7 @@ namespace bts.history{
bser_2 = cons_series(200,2,15,10),
bser_3 = cons_series(400,2,15,5);
var res = new List<Tuple<BookEntry,BookEntry,BookEntry>>();
MarketQueries.align_bookseries_fast(
MarketQueries.align_bookseries(
l => res.Add(Tuple.Create(l.ElementAt(0),
l.ElementAt(1),
l.ElementAt(2))),
@@ -141,7 +141,7 @@ namespace bts.history{
bser_2 = cons_series(100,3,23,null);
bser_3 = cons_series(100,5,31,null);
res = new List<Tuple<BookEntry,BookEntry,BookEntry>>();
MarketQueries.align_bookseries_fast(
MarketQueries.align_bookseries(
l => res.Add(Tuple.Create(l.ElementAt(0),
l.ElementAt(1),
l.ElementAt(2))),
@@ -37,38 +37,49 @@ namespace bts.history{
String.Format("{0}/{1}_{2}.csv",
path,
symbol,
dt.ToShortDateString().Replace("/","_")));
System.Diagnostics.Stopwatch w = new System.Diagnostics.Stopwatch();
foreach (var kv in spread_map){
var s = kv.Value;
var n = s.Security.Synthetic.Name;
w.Restart();var spr_prices = MarketQueries.get_synthetic_prices(s,dt);w.Stop();
Console.WriteLine("{0}:{1} {2} msecs", n,spr_prices.Count(),w.ElapsedMilliseconds);
//spr_prices = MarketQueriesHelper.filter_end_entries(spr_prices);
foreach (var spr in spr_prices){
BookEntry p = spr.synthetic_book;
double b = MarketQueriesHelper.book_price(p,true,0),
a = MarketQueriesHelper.book_price(p,false,0);
UInt32 bq = MarketQueriesHelper.book_qty(p,true,0),
aq = MarketQueriesHelper.book_qty(p,false,0);
if(bq>0 && b>0){
Console.WriteLine("SELL:{0} t:{1} active_for:{2} ns {3}:{4}",
n, p.TimeRange.T1,
MarketQueriesHelper.period_ns(p.TimeRange),b,bq);
tw.WriteLine(MarketQueriesHelper.synthetic_price_to_csv(spr));
}
if(aq>0 && a<0){
Console.WriteLine("BUY:{0} t:{1} active_for:{2} ns {3}:{4}",
n, p.TimeRange.T1, MarketQueriesHelper.period_ns(p.TimeRange),
a,aq);
tw.WriteLine(MarketQueriesHelper.synthetic_price_to_csv(spr));
}
}
}
dt.ToShortDateString().Replace("/","_")));
foreach (var kv in spread_map)
_evaluate_synthetic(kv.Value,dt,tw);
tw.Close();
}
public void analyze(){
}
private void _evaluate_synthetic(SpreaderOrderDefinition s,
DateTime dt,
StreamWriter tw){
var n = s.Security.Synthetic.Name;
System.Diagnostics.Stopwatch w = new System.Diagnostics.Stopwatch();
w.Restart();
int spr_count = 0;
MarketQueries.get_synthetic_prices(s,dt,spr=>
{_detect_opp(n,spr,tw);spr_count++;});
w.Stop();
Console.WriteLine("{0}:{1} {2} msecs", n,spr_count,w.ElapsedMilliseconds);
}
private static void _detect_opp (
string n,
MarketQueries.SyntheticPrice spr,
StreamWriter tw){
BookEntry p = spr.synthetic_book;
double b = MarketQueriesHelper.book_price(p,true,0),
a = MarketQueriesHelper.book_price(p,false,0);
UInt32 bq = MarketQueriesHelper.book_qty(p,true,0),
aq = MarketQueriesHelper.book_qty(p,false,0);
if(bq>0 && b>0){
Console.WriteLine("SELL:{0} t:{1} active_for:{2} ns {3}:{4}",
n, p.TimeRange.T1,
MarketQueriesHelper.period_ns(p.TimeRange),b,bq);
tw.WriteLine(MarketQueriesHelper.synthetic_price_to_csv(spr));
}
if(aq>0 && a<0){
Console.WriteLine("BUY:{0} t:{1} active_for:{2} ns {3}:{4}",
n, p.TimeRange.T1, MarketQueriesHelper.period_ns(p.TimeRange),
a,aq);
tw.WriteLine(MarketQueriesHelper.synthetic_price_to_csv(spr));
}
}
}
}