ignoring all trades that are not making sense

This commit is contained in:
Taha Afzal committed 2020-03-02 09:46:10 -05:00
1 parent e9c9a3bad6
commit dec178f133
2 files changed
+166 -89

No files matched your search

@@ -363,42 +363,54 @@ namespace bts.history{
return string.Format("{0},{1},{2},{3},{4},{5},{6},{7}," +
"{8},{9},{10},{11},{12},{13},{14}," +
"{15},{16},{17},{18},{19},{20},{21}," +
"{22}",
"{22},{23},{24},{25},{26},{27},{28},{29}," +
"{30}",
// contract
"SyntheticD",
"SyntheticD", //0
"SyntheticS",
"QuotingLeg",
// stats
"IsSnipe",
"IsSnipe", //3
"HedgeBooksID",
"HedgeBooksActivePeriod",
"TimeLine",
"RequoteReactionTime",
"HedgeTimeWindow",
// trade
"TradeMsgID",
"TradeMsgID", //9
"TradeID",
"TradeSendTime",
"TradeTransactTime",
"TradePrice",
"TotQty",
"NOrders",
"NOrders",
"TradeQty",
"TradeAggressorSide",
"TradeAggressorIOD",
"TradeAggressorOID",
"TradeRestingID",
// requote order
"RequoteMsgID",
"RequoteOrderUpdateTime",
"RequoteLegMD"
"QOrdMsgID", //20
"QOrdPrice",
"QOrdQty",
"QOrdTransactTime",
"QOrdTimeDiff",
"QOrdMD",
// opp order
"OppOrdMsgID", //26
"OppOrdPrice",
"OppOrdQty",
"OppOrdTransactTime",
"OppOrdMD" //31
);
}
public static string quote_fill_to_csv(SpreaderFill f){
var r = f.quote_trade;
Func<Side,string> sign = (leg_side) => {
return leg_side == Side.Buy ? "+" : "-";
Func<Side,string> sign = (side) => {
return side == Side.Buy ? "+" : "-";
};
Func<OrderRow,string> ord_sign = (o) => o.Side == 0 ? "+" : "-";
Func<ProcessedBook,string> top_str = (b) =>
b == null ? String.Empty :
String.Format("{0} {1} {2} {3}",
MarketQueriesHelper.book_price(b,true,0),
MarketQueriesHelper.book_qty(b,true,0),
@@ -408,9 +420,10 @@ namespace bts.history{
var q_side = MarketQueriesHelper.get_quoting_side(f.spr_ord,
f.spr_ord_side);
return string.Format("{0},{1},{2},{3},{4},{5},{6},{7}," +
"{8},{9},{10},{11},{12},{13},{14}," +
"{8},{9},{10},{11},{12},{13},{14}," +
"{15},{16},{17},{18},{19},{20},{21}," +
"{22}",
"{22},{23},{24},{25},{26},{27},{28},{29}," +
"{30}",
// contract
_format_synthetic_detail(f.spr_ord,f.spr_ord_side),
String.Format("{0}/{1}",
@@ -442,8 +455,26 @@ namespace bts.history{
r.RestingOid,
// requote order
f.quote_order_update.MsgId,
_format_timestamp(f.quote_order_update.TransactTime),
top_str(f.quote_book_at_update_time)
f.quote_order_update.MdEntryPx,
String.Format("{0}{1}",
ord_sign(f.quote_order_update),
f.quote_order_update.MdEntryQty),
_format_timestamp(f.quote_order_update.TransactTime,false),
f.requote_reaction_time,
top_str(f.book_at_quote_update_time),
// opp order
f.opp_order_update == null ? 0 :
f.opp_order_update.MsgId,
f.opp_order_update == null ? Double.NaN :
f.opp_order_update.MdEntryPx,
f.opp_order_update == null ? String.Empty :
String.Format("{0}{1}",
ord_sign(f.opp_order_update),
f.opp_order_update.MdEntryQty),
f.opp_order_update == null ? String.Empty :
_format_timestamp(f.opp_order_update.TransactTime,false),
f.opp_order_update == null ? String.Empty :
top_str(f.book_at_opp_update_time)
);
}
public static string spreader_fill_to_csv(SpreaderFill f){
@@ -37,34 +37,39 @@ namespace bts.history{
public ProcessedBook at(int h){
return contract_books.ElementAt(h);
}
}
public class SpreaderFill{
// TODO, put both orders there and make
// quote_order_update a property based on
// the quote leg side
public readonly SpreaderOrderDefinition spr_ord;
public readonly Side spr_ord_side;
public readonly BooksSnapshot hedge_books;
public readonly TradeRow quote_trade;
public readonly OrderRow quote_order_update;
public readonly ProcessedBook quote_book_at_update_time;
public readonly OrderRow opp_order_update;
public readonly ProcessedBook book_at_quote_update_time;
public readonly ProcessedBook book_at_opp_update_time;
public readonly uint total_quote_leg_trades;
public SpreaderFill(SpreaderOrderDefinition spr_ord_a,
Side spr_ord_side_a,
BooksSnapshot hedge_books_a,
TradeRow quote_trade_a,
OrderRow quote_order_update_a,
ProcessedBook quote_book_at_update_time_a,
OrderRow opp_order_update_a,
ProcessedBook book_at_quote_update_time_a,
ProcessedBook book_at_opp_update_time_a,
uint total_quote_leg_trades_a){
spr_ord = spr_ord_a;
spr_ord_side = spr_ord_side_a;
hedge_books = hedge_books_a;
quote_trade = quote_trade_a;
quote_order_update = quote_order_update_a;
quote_book_at_update_time = quote_book_at_update_time_a;
opp_order_update = opp_order_update_a;
book_at_quote_update_time = book_at_quote_update_time_a;
book_at_opp_update_time = book_at_opp_update_time_a;
total_quote_leg_trades = total_quote_leg_trades_a;
Log.assert(quote_order_update != null);
Log.assert(opp_order_update == null ||
(quote_order_update.Side != opp_order_update.Side));
}
public Int64 requote_reaction_time{
get{
@@ -164,7 +169,7 @@ namespace bts.history{
// Alligning Books //////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
#region Alligning Books
#region Books
public static void align_bookseries(
IEnumerable<BookSeries> leg_book_serieses,
@@ -290,17 +295,19 @@ namespace bts.history{
return null;
}
// just a conveniant wrapper around the above
public static OrderRow find_last_priority_change (
IDictionary<UInt64,IEnumerable<OrderRow>> ord_map,
UInt64 oid,
Timestamp t0,
Timestamp t1){
IDictionary<UInt64,IEnumerable<OrderRow>> ord_map,
UInt64 oid,
Timestamp t0,
Timestamp t1){
IEnumerable<OrderRow> ord_updates;
if(ord_map.TryGetValue(oid, out ord_updates))
return find_last_priority_change(ord_updates,t0,t1);
return null;
}
//(assumes the order side is never changed)
public static Nullable<Side> find_order_side (
UInt64 oid,
IDictionary<UInt64,IEnumerable<OrderRow>> ord_map){
@@ -311,34 +318,99 @@ namespace bts.history{
return null;
}
// returns false if the order changes sides
public static bool unch_side (
ulong oid,
IDictionary<UInt64,IEnumerable<OrderRow>> ord_map){
IEnumerable<OrderRow> o_upds;
if(ord_map.TryGetValue(oid, out o_upds))
for(int i=1; i<o_upds.Count(); i++)
if(o_upds.ElementAt(i).Side != o_upds.ElementAt(i-1).Side)
return false;
return true;
}
// finds the last time the orders particpating in a trade
// changed their priority
public static Tuple<OrderRow,OrderRow> find_trade_orders (
public static bool unch_contract (
ulong oid,
IDictionary<UInt64,IEnumerable<OrderRow>> ord_map,
string contract){
IEnumerable<OrderRow> o_upds;
if(ord_map.TryGetValue(oid, out o_upds))
foreach (var u in o_upds)
if(u.Contract != contract)
return false;
return true;
}
public static bool trade_err_check (
TradeRow r,
IDictionary<UInt64,IEnumerable<OrderRow>> ord_map){
if (r.AggressorOid == r.RestingOid)
return false;
// side doesnt change on either order
// implied trades
if(r.AggressorSide == 0){
Console.WriteLine ("Ignoring, implied tid:{0}", r.MdTradeEntryId);
return false;
}
if(!unch_side(r.AggressorOid,ord_map)){
Console.WriteLine ("Ignoring, switched sides tid:{0} a:{1}",
r.MdTradeEntryId,r.AggressorOid);
return false;
}
if(!unch_side(r.RestingOid,ord_map)){
Console.WriteLine ("Ignoring, wwitched sides tid:{0} a:{1}",
r.MdTradeEntryId,r.RestingOid);
return false;
}
//crossing orders has same side
Nullable<Side> a_side = find_order_side(r.AggressorOid,ord_map),
p_side = find_order_side(r.RestingOid,ord_map);
if(a_side.HasValue && p_side.HasValue)
if(a_side.Value == p_side.Value){
Console.WriteLine ("Ignoring, same sides tid:{0} a:{1} p:{2}",
r.MdTradeEntryId,r.AggressorOid,r.RestingOid);
return false;
}
// same contract on either order as the trade
if(!unch_contract(r.AggressorOid,ord_map,r.Contract)){
Console.WriteLine ("Ignoring, incorrect contract tid:{0} a:{1}",
r.MdTradeEntryId,r.AggressorOid);
return false;
}
if(!unch_contract(r.AggressorOid,ord_map,r.Contract)){
Console.WriteLine ("Ignoring, incorrect contract tid:{0} a:{1}",
r.MdTradeEntryId,r.AggressorOid);
return false;
}
// header row
return true;
}
// finds the last updates (or create) that lead to a priority change
// for the order on either side of the trade
public static Tuple<OrderRow,OrderRow> find_crossing_orders (
TradeRow trade,
IDictionary<UInt64,IEnumerable<OrderRow>> ord_map){
UInt32 agg_side = trade.AggressorSide;
// gets side of the trade with side == q_side
if(agg_side != 0){
if(trade_err_check(trade,ord_map)){
Log.assert(trade.AggressorSide == 1 ||
trade.AggressorSide == 2);
if(trade.AggressorOid != trade.RestingOid){
OrderRow r_ord = find_last_priority_change(ord_map,
OrderRow r_ord = find_last_priority_change(ord_map,
trade.RestingOid,
MarketQueriesHelper.ref_min_dt,
trade.TransactTime);
OrderRow a_ord = find_last_priority_change(ord_map,
OrderRow a_ord = find_last_priority_change(ord_map,
trade.AggressorOid,
MarketQueriesHelper.ref_min_dt,
trade.TransactTime);
return Tuple.Create(r_ord,a_ord);
}
return Tuple.Create(r_ord,a_ord);
}
return null;
}
// returns
public static Tuple<OrderRow,OrderRow, bool> find_possible_requote_2 (
// tuple.Item3 is true is if the quoter order is the aggressor
// (ie snipe)
public static Tuple<OrderRow,OrderRow,bool> find_possible_requote_2 (
Timerange hedge_books_tr,
TradeRow q_leg_trade,
IDictionary<UInt64,IEnumerable<OrderRow>> ord_map,
@@ -349,11 +421,11 @@ namespace bts.history{
// 1 = Buy
// 2 = Sell
Func<OrderRow,Side> ord_side = o => {
var u = o.Side;
var u = o.Side;
Log.assert(u==0 || u==1);
return u == 0 ? Side.Buy : Side.Sell;
return u == 0 ? Side.Buy : Side.Sell;
};
var pa_tup = find_trade_orders(q_leg_trade,ord_map);
var pa_tup = find_crossing_orders(q_leg_trade,ord_map);
if(pa_tup != null){
var p_ord = pa_tup.Item1;
var a_ord = pa_tup.Item2;
@@ -370,43 +442,6 @@ namespace bts.history{
return null;
}
// for a trade that happened on the q leg,
// within the period the hedge_books were active,
// finds the order that was updated (priority changed)
// or entered within that period.
public static OrderRow find_possible_requote (
Timerange hedge_books_tr,
TradeRow q_leg_trade,
IDictionary<UInt64,IEnumerable<OrderRow>> ord_map,
Side q_side){
Log.assert(MarketQueriesHelper.within(hedge_books_tr,
q_leg_trade.TransactTime));
// 0 = No aggressor
// 1 = Buy
// 2 = Sell
UInt32 q_side_uint = Convert.ToUInt32((q_side == Side.Buy) ? 1 : 2);
UInt32 agg_side = q_leg_trade.AggressorSide;
// gets side of the trade with side == q_side
Func<Nullable<UInt64>> get_q_oid = ()=>{
Nullable<UInt64> rv = null;
if(agg_side != 0){
Log.assert(q_leg_trade.AggressorSide == 1 ||
q_leg_trade.AggressorSide == 2);
if(q_leg_trade.AggressorOid != q_leg_trade.RestingOid)
rv = (q_leg_trade.AggressorSide == q_side_uint) ?
q_leg_trade.AggressorOid : q_leg_trade.RestingOid;
}
return rv;
};
var q_oid = get_q_oid();
if(q_oid.HasValue)
return find_last_priority_change(ord_map,
q_oid.Value,
hedge_books_tr.T1,
q_leg_trade.TransactTime);
return null;
}
// finds all the qleg fills paired with hedge books snapshots
// (ie q leg trades that happened during the time the hedgebook
// snapshots were active) that would have given the desired
@@ -452,6 +487,14 @@ namespace bts.history{
IDictionary<UInt64,IEnumerable<OrderRow>> ord_map,
api.Side spr_ord_side,
double spr_ord_price){
Func<OrderRow,ProcessedBook> book_at_ord = (o) => {
if(o != null){
BookSeries bser;
if(md_map.TryGetValue(o.Contract, out bser))
return find_book_at(bser,o.TransactTime);
}
return null;
};
var rv = new List<SpreaderFill>();
var q_side = MarketQueriesHelper.get_quoting_side(spr_ord,spr_ord_side);
var hedge_books_q_leg_trades = find_qleg_trades_at_price(
@@ -464,20 +507,23 @@ namespace bts.history{
BooksSnapshot hedge_books = tup.Item1;
IEnumerable<TradeRow> q_leg_trades = tup.Item2;
foreach (var qlt in q_leg_trades){
OrderRow q_ord_row = find_possible_requote(
hedge_books.timerange,
qlt,
ord_map,
q_side);
var tr_ord = find_possible_requote_2(hedge_books.timerange,
qlt,
ord_map,
q_side);
var q_ord_row = tr_ord == null ? null :
tr_ord.Item3 ? tr_ord.Item2 : tr_ord.Item1;
if(q_ord_row!=null){
var q_book = find_book_at(md_map[q_ord_row.Contract],
q_ord_row.TransactTime);
var opp_ord_row = tr_ord.Item3 ? tr_ord.Item1 : tr_ord.Item2;
SpreaderFill spr_fill = new SpreaderFill(
spr_ord,
spr_ord_side,
hedge_books,qlt,
hedge_books,
qlt,
q_ord_row,
q_book,
opp_ord_row,
book_at_ord(q_ord_row),
book_at_ord(opp_ord_row),
(uint)hedge_books_q_leg_trades.Count());
rv.Add(spr_fill);
}