72 lines
2.5 KiB
C#
72 lines
2.5 KiB
C#
using bts;
|
|
using bts.api;
|
|
|
|
using mbts.spark;
|
|
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
|
|
|
|
namespace mbts {
|
|
public static class SecurityExpirationHelper {
|
|
private static ConcurrentDictionary<bts.api.SecurityTuple, TimeSpan> _exp_time_map = null;
|
|
|
|
static SecurityExpirationHelper() {
|
|
Action<SecurityType, Venue, string, TimeSpan> add_st =
|
|
(t, v, s, ts) => {
|
|
bts.api.SecurityTuple sec_tup = new bts.api.SecurityTuple() {
|
|
Type = t,
|
|
Venue = v,
|
|
Symbol = s
|
|
};
|
|
_exp_time_map.TryAdd(sec_tup, ts);
|
|
};
|
|
// TODO (get it from Sec MetaData)
|
|
_exp_time_map = new ConcurrentDictionary<bts.api.SecurityTuple, TimeSpan>();
|
|
add_st(SecurityType.Future, Venue.Deribit, "BTC", TimeSpan.FromHours(8));
|
|
add_st(SecurityType.Option, Venue.Deribit, "BTC", TimeSpan.FromHours(8));
|
|
add_st(SecurityType.Future, Venue.Deribit, "ETH", TimeSpan.FromHours(8));
|
|
add_st(SecurityType.Option, Venue.Deribit, "ETH", TimeSpan.FromHours(8));
|
|
}
|
|
|
|
static TimeSpan get_exp_time(bts.api.SecurityTuple st) {
|
|
TimeSpan rv = TimeSpan.FromHours(8);
|
|
_exp_time_map.TryGetValue(st, out rv);
|
|
return rv;
|
|
}
|
|
|
|
public static bool is_unexpired(bts.api.SecurityTuple st, DateTime exp_date) {
|
|
TimeSpan exp_time = get_exp_time(st);
|
|
DateTime full_exp_utc = exp_date.ToUniversalTime().Date.Add(exp_time);
|
|
DateTime now_utc = DateTime.UtcNow;
|
|
return now_utc < full_exp_utc;
|
|
}
|
|
|
|
public static bool is_unexpired(bts.api.SecurityImmutable sec) {
|
|
var sec_type = sec.security_type();
|
|
switch (sec_type) {
|
|
case bts.api.SecurityType.Future:
|
|
case bts.api.SecurityType.Option:
|
|
var st = sec.security_tuple();
|
|
var exp = sec.to_sec_exp().expiration;
|
|
return is_unexpired(st, exp);
|
|
case bts.api.SecurityType.Spread:
|
|
foreach (var l in sec.Spread.Legs)
|
|
if (!is_unexpired(l.Security))
|
|
return false;
|
|
return true;
|
|
case bts.api.SecurityType.Spot:
|
|
return true;
|
|
case bts.api.SecurityType.Synthetic:
|
|
foreach (var l in sec.Synthetic.Legs)
|
|
if (!is_unexpired(l.Security))
|
|
return false;
|
|
return true;
|
|
case bts.api.SecurityType.Unset:
|
|
return true;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
}
|