Sniper analysis works on multiple files now

This commit is contained in:
Taha Afzal committed 2020-01-14 09:32:08 -05:00
1 parent 542b8e946d
commit a659fbc7cd
3 files changed
+66 -31

No files matched your search

@@ -18,7 +18,7 @@ namespace bts.history{
public readonly BookEntry synthetic_book;
public SyntheticPrice(IEnumerable<BookEntry> leg_book_a,
BookEntry synthetic_book_a){
leg_book = leg_book_a;
leg_book = leg_book_a.ToArray();
synthetic_book = synthetic_book_a;
}
}
@@ -15,6 +15,26 @@ using mbts.co2;
using Google.Protobuf.WellKnownTypes;
namespace bts.history{
// in local time (eastern)
class TradingTime{
public readonly uint start_m, stop_m;
public TradingTime(uint start_hrs, uint start_mins,
uint stop_hrs, uint stop_mins){
start_m = start_hrs*60 + start_mins;
stop_m = stop_hrs*60 + stop_mins;
}
public bool within (Timestamp t){
DateTime t_utc = MarketQueriesHelper.to_utc_dt(t);
DateTime t_local = TimeZoneInfo.ConvertTime(t_utc,TimeZoneInfo.Local);
return true;
}
public static bool within (Timestamp t, IEnumerable<TradingTime> t_list){
foreach (var tt in t_list)
if(!tt.within(t))
return false;
return true;
}
}
// secondary level helper functions (parsing, reading files etc)
class MarketQueriesHelper{
// time range helpers ///////////////////////////////////////////////////
@@ -32,7 +52,7 @@ namespace bts.history{
public static Int64 period_ns(Timerange r){
return diff_ns(r.T2,r.T1);
}
public static DateTime epoch_to_utc_dt(Timestamp ts){
public static DateTime to_utc_dt(Timestamp ts){
DateTime rv = _epoch_dt.AddSeconds(ts.Seconds);
rv = rv.AddMilliseconds(ts.Nanos/1000000);
return rv;
@@ -112,7 +132,8 @@ namespace bts.history{
{"CL",-2},
{"HE",-2},
{"ZC",-2},
{"NG",-3},
{"NG",-2},
{"GC",-2}
};
// these are in central time
private static Dictionary<string, Tuple<DateTime,DateTime>[]> _close_times = new
@@ -17,7 +17,6 @@ using System.Collections.Concurrent;
// date-range, product, path (or file), optional spread
namespace bts.history{
class SpreaderTickSniperAnalyzer{
public SpreaderTickSniperAnalyzer(HistoricalDataOptions hd_options){
Console.WriteLine("SpreaderTickSniperAnalyzer:Hit key to continue:"); Console.ReadKey();
// product, input_path, output_path
@@ -26,24 +25,41 @@ namespace bts.history{
var symbol = args[0];
var path = DataIOHelper.get_data_path(args[1]);
var output_path = DataIOHelper.get_data_path(args[2]);
foreach (string file in Directory.GetFiles(path)){
string output_file = String.Format("{0}/{1}_sniper.csv",
output_path,
Path.GetFileNameWithoutExtension(file));
_execute_single(file,symbol,output_file);
List<MarketQueries.SyntheticPrice> opps = new List<MarketQueries.SyntheticPrice>();
foreach (string file in Directory.GetFiles(path)){
Console.WriteLine("Processing {0} ...", file);
IDictionary<string,BookSeries> spr_book_map =
DataIOHelper.load_books(file,symbol,true);
Console.WriteLine("Downloaded {0} books",spr_book_map.Count());
_execute_single(spr_book_map,symbol,ref opps);
}
opps.Sort((a,b) => a.synthetic_book.TimeRange.T1.compare(
b.synthetic_book.TimeRange.T1));
// write to file
string output_file = String.Format("{0}/sniper.csv",
output_path);
(new FileInfo(output_file)).Directory.Create();
using (StreamWriter tw = new StreamWriter(output_file)){
_write_header(tw);
foreach (var o in opps)
tw.WriteLine(DataIOHelper.synthetic_price_to_csv(o));
}
}
public void analyze(){
}
private void _execute_single (string file_path,
private static void _write_header(StreamWriter tw){
string header = String.Empty;
for (int i=0; i<3; i++)
header += String.Format("{0},", DataIOHelper.leg_book_header());
header += DataIOHelper.synthetic_book_header();
tw.WriteLine(header);
}
private void _execute_single (IDictionary<string,BookSeries> spr_book_map,
string symbol,
string outputfile_path){
IDictionary<string,BookSeries> spr_book_map =
DataIOHelper.load_books(file_path,symbol,true);
Console.WriteLine("Downloaded {0} books",spr_book_map.Count());
// 3. generating circular spreads
ref List<MarketQueries.SyntheticPrice> opps){
Console.WriteLine("Generating circular spreads");
var circ_spreads = MarketQueriesHelper.generate_3_legged_circulars(
symbol,
@@ -52,23 +68,18 @@ namespace bts.history{
var spread_map = circ_spreads.ToDictionary(
k=>k,
k=>MarketQueriesHelper.parse_synthetic(k));
(new FileInfo(outputfile_path)).Directory.Create();
StreamWriter tw = new StreamWriter(outputfile_path);
// 4. compute the synthetic prices now
string header = String.Empty;
for (int i=0; i < 3; i++)
header += String.Format("{0},", DataIOHelper.leg_book_header());
header += DataIOHelper.synthetic_book_header();
tw.WriteLine(header);
foreach (var kv in spread_map)
_evaluate_synthetic(spr_book_map,kv.Value,tw);
tw.Close();
_evaluate_synthetic(spr_book_map,kv.Value,ref opps);
}
private static void _evaluate_synthetic(
IDictionary<string, BookSeries> leg_book_map,
SpreaderOrderDefinition s,
StreamWriter tw){
ref List<MarketQueries.SyntheticPrice> opps){
var n = s.Security.Synthetic.Name;
List<BookSeries> leg_books = new List<BookSeries>();
foreach (var l in s.Security.Synthetic.Legs){
@@ -81,23 +92,25 @@ namespace bts.history{
System.Diagnostics.Stopwatch w = new System.Diagnostics.Stopwatch();
w.Restart();
int spr_count = 0;
List<MarketQueries.SyntheticPrice> l_opps = new List<MarketQueries.SyntheticPrice>();
MarketQueries.get_synthetic_prices(s,leg_books,spr=> {
_detect_opp(n,spr,tw);
if(_detect_opp(n,spr))
l_opps.Add(spr);
spr_count++;
});
w.Stop();
opps.AddRange(l_opps);
Console.WriteLine("{0}:{1} {2} msecs", n,spr_count,w.ElapsedMilliseconds);
}
// if it is an sniper opportunity, it writes it
// to the list
private static void _detect_opp (
private static bool _detect_opp (
string n,
MarketQueries.SyntheticPrice spr,
StreamWriter tw){
foreach(BookEntry be in spr.leg_book)
if(!MarketQueriesHelper.reactable(be))
return;
MarketQueries.SyntheticPrice spr){
// foreach(BookEntry be in spr.leg_book)
// if(!MarketQueriesHelper.reactable(be))
// return;
BookEntry p = spr.synthetic_book;
double b = MarketQueriesHelper.book_price(p,true,0),
a = MarketQueriesHelper.book_price(p,false,0);
@@ -107,14 +120,15 @@ namespace bts.history{
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(DataIOHelper.synthetic_price_to_csv(spr));
return true;
}
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(DataIOHelper.synthetic_price_to_csv(spr));
return true;
}
return false;
}
}
}