360 lines
13 KiB
C#
360 lines
13 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reactive.Linq;
|
|
using System.Reactive.Concurrency;
|
|
using System.Text;
|
|
using System.Collections.Concurrent;
|
|
using System.Runtime.CompilerServices;
|
|
|
|
using QuickFix;
|
|
using QuickFix.Fields;
|
|
|
|
using mbts;
|
|
using bts.api;
|
|
using bts.api.core;
|
|
using bts.api.orderman;
|
|
using bts.api.user;
|
|
using bts.api.admin;
|
|
|
|
|
|
namespace bts.fix
|
|
{
|
|
abstract class SessionBase : QuickFix.MessageCracker, QuickFix.IApplication
|
|
{
|
|
protected static AuthService _authservice = bts.api.Locator.locate<AuthService>();
|
|
protected static Lazy<AdminService> _adminservice = new Lazy<AdminService>(() => bts.api.Locator.locate<AdminService>(), true);
|
|
protected static Lazy<bts.metrics.ShellLatencyService> _latencyservice =
|
|
new Lazy<bts.metrics.ShellLatencyService>(() => bts.api.Locator.locate<bts.metrics.ShellLatencyService>(), true);
|
|
|
|
protected string _user_name;
|
|
protected AuthToken _token;
|
|
|
|
protected IDisposable _admindisp;
|
|
protected IObservable<AdminModelWrapper> _adminobservable;
|
|
protected AdminDataImmutable? _admin_data;
|
|
|
|
protected readonly IFixRouter _router;
|
|
protected readonly SessionID _sess;
|
|
|
|
public SessionBase(IFixRouter router, SessionID sess)
|
|
{
|
|
_router = router;
|
|
_sess = sess;
|
|
|
|
_clockdisp = _latencyservice.Value.subscribe_clock().Subscribe(x =>
|
|
{
|
|
var t = x.all.FirstOrDefault();
|
|
if(t != null)
|
|
_clock = t.Timestamp;
|
|
});
|
|
|
|
_adminobservable = _adminservice.Value.subscribei_admin_data().Select(x =>
|
|
{
|
|
return new AdminModelWrapper(x.all.TryFirst(), _user_name);
|
|
});
|
|
_admindisp = _adminobservable.Subscribe(x => _admin_data = x.admin_data);
|
|
}
|
|
|
|
abstract public void FromApp(Message message, SessionID sessionID);
|
|
abstract public void OnLogon(SessionID sessionID); //subscribe to your stuff
|
|
abstract public void OnLogout(SessionID sessionID); //unsub
|
|
|
|
virtual public void ToApp(Message message, SessionID sessionID)
|
|
{
|
|
Log.assert_d(sessionID.Equals(_sess));
|
|
log_info("OUT: " + message);
|
|
}
|
|
|
|
readonly IDisposable _clockdisp; ulong _clock = 0;
|
|
virtual public void FromAdmin(Message message, SessionID sessionID)
|
|
{
|
|
Log.assert_d(sessionID.Equals(_sess));
|
|
|
|
MsgType type = Message.IdentifyType(message.ToString());
|
|
switch (type.getValue())
|
|
{
|
|
case MsgType.LOGON:
|
|
{
|
|
try
|
|
{
|
|
QuickFix.FIX44.Logon msg = (QuickFix.FIX44.Logon)message;
|
|
_user_name = msg.Username.getValue();
|
|
string pass = string.Empty;
|
|
if(msg.IsSetPassword())
|
|
pass = msg.Password.getValue();
|
|
var resp = _authservice.authenticate_login(new AuthenticateLoginRequest {
|
|
Login = msg.Username.getValue(), Password = pass});
|
|
if (resp != null && resp.IsValid)
|
|
_token = resp.Token;
|
|
else
|
|
throw new RejectLogon("Logon rejected " + resp?.Reason ?? string.Empty);
|
|
}
|
|
catch (RejectLogon)
|
|
{
|
|
throw;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new RejectLogon("Logon rejected " + ex.ToString());
|
|
}
|
|
}
|
|
break;
|
|
case MsgType.HEARTBEAT:
|
|
{
|
|
QuickFix.FIX44.Heartbeat hb = (QuickFix.FIX44.Heartbeat)message;
|
|
handle_heartbeat(hb, sessionID);
|
|
}
|
|
break;
|
|
default:
|
|
log_info("ADMININ: " + message);
|
|
break;
|
|
}
|
|
}
|
|
|
|
async protected void handle_heartbeat(QuickFix.FIX44.Heartbeat hb, SessionID sessionID)
|
|
{
|
|
var resp = await _latencyservice.Value.report_async(new bts.metrics.Report { HostName = "BTSFIX_" + sessionID.ToString(),
|
|
Timestamp = _clock, User = _user_name });
|
|
if (resp.success)
|
|
send(hb, sessionID);
|
|
}
|
|
|
|
virtual public void ToAdmin(Message message, SessionID sessionID)
|
|
{
|
|
Log.assert_d(sessionID.Equals(_sess));
|
|
log_info("ADMINOUT: " + message);
|
|
}
|
|
|
|
virtual public void OnCreate(SessionID sessionID)
|
|
{
|
|
log_info("CREATE");
|
|
}
|
|
|
|
virtual public void OnMessage(QuickFix.FIX44.BusinessMessageReject n, SessionID s)
|
|
{
|
|
//Log.assert(false, "biz reject wtf " + n.EncodedText.getValue());
|
|
Log.FIX.warn(_sess.ToString() + " Business Reject " + n.BusinessRejectReason.getValue() + " " + n.Text.getValue());
|
|
}
|
|
virtual public void OnMessage(QuickFix.FIX44.News n, SessionID s)
|
|
{
|
|
log_info("news " + n.EncodedHeadline.getValue());
|
|
}
|
|
|
|
|
|
protected void log_info(string message, [CallerFilePath] string file = "", [CallerLineNumber] int line = 0)
|
|
{
|
|
Log.FIX.info(_sess.ToString() + " " + message, file, line);
|
|
}
|
|
|
|
protected void log_error(string message, [CallerFilePath] string file = "", [CallerLineNumber] int line = 0)
|
|
{
|
|
Log.FIX.error(_sess.ToString() + " " + message, file, line);
|
|
}
|
|
|
|
protected void send(Message mess, SessionID sess)
|
|
{
|
|
try
|
|
{
|
|
Session.SendToTarget(mess, sess);
|
|
}
|
|
catch (SessionNotFound ex)
|
|
{
|
|
log_error("==session not found exception!==");
|
|
log_error(ex.ToString());
|
|
Log.assert(false, "session not found " + sess);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
log_error("==exception sending to " + sess + " " + ex.ToString());
|
|
}
|
|
}
|
|
|
|
|
|
protected void send_reject(SessionID sess, string text, Message refmessage = null)
|
|
{
|
|
var seq = new QuickFix.Fields.MsgSeqNum();
|
|
QuickFix.FIX44.Reject rej = null;
|
|
if (refmessage.Header.IsSetField(seq))
|
|
rej = new QuickFix.FIX44.Reject(new QuickFix.Fields.RefSeqNum(seq.getValue()));
|
|
else
|
|
rej = new QuickFix.FIX44.Reject();
|
|
rej.Text = new Text(text);
|
|
send(rej, sess);
|
|
}
|
|
|
|
protected bool security_types_equal(bts.api.SecurityType btstype, QuickFix.Fields.SecurityType fixtype)
|
|
{
|
|
return btstype == convert_security_type(fixtype);
|
|
}
|
|
|
|
protected QuickFix.Fields.SecurityType convert_security_type(bts.api.SecurityType btstype)
|
|
{
|
|
switch (btstype)
|
|
{
|
|
case bts.api.SecurityType.Future:
|
|
return new QuickFix.Fields.SecurityType("FUT");
|
|
case bts.api.SecurityType.Option:
|
|
return new QuickFix.Fields.SecurityType("OPT");
|
|
case bts.api.SecurityType.Spread:
|
|
return new QuickFix.Fields.SecurityType("MLEG");
|
|
case bts.api.SecurityType.Stock:
|
|
return new QuickFix.Fields.SecurityType("CS");
|
|
case bts.api.SecurityType.Synthetic:
|
|
return new QuickFix.Fields.SecurityType("SYNTH");
|
|
default:
|
|
return new QuickFix.Fields.SecurityType();
|
|
}
|
|
}
|
|
|
|
protected bts.api.SecurityType convert_security_type(QuickFix.Fields.SecurityType fixtype)
|
|
{
|
|
switch (fixtype.getValue())
|
|
{
|
|
case "FUT":
|
|
return bts.api.SecurityType.Future;
|
|
case "OPT":
|
|
return bts.api.SecurityType.Option;
|
|
case "MLEG":
|
|
return bts.api.SecurityType.Spread;
|
|
case "CS":
|
|
return bts.api.SecurityType.Stock;
|
|
case "SYNTH":
|
|
return bts.api.SecurityType.Synthetic;
|
|
default:
|
|
return bts.api.SecurityType.Unset;
|
|
}
|
|
}
|
|
|
|
protected bool exchanges_equal(bts.api.Venue btstype, QuickFix.Fields.SecurityExchange fixtype)
|
|
{
|
|
return btstype == convert_exchange(fixtype);
|
|
}
|
|
|
|
protected QuickFix.Fields.SecurityExchange convert_exchange(bts.api.Venue btstype)
|
|
{
|
|
switch (btstype)
|
|
{
|
|
case bts.api.Venue.Bts:
|
|
return new QuickFix.Fields.SecurityExchange("BTS");
|
|
case bts.api.Venue.Cboe:
|
|
return new QuickFix.Fields.SecurityExchange("XCBO");
|
|
case bts.api.Venue.Cboecfe:
|
|
return new QuickFix.Fields.SecurityExchange("XCBF");
|
|
case bts.api.Venue.Glbx:
|
|
return new QuickFix.Fields.SecurityExchange("XCME");
|
|
case bts.api.Venue.Ice:
|
|
return new QuickFix.Fields.SecurityExchange("IEPA");
|
|
case bts.api.Venue.Sim:
|
|
return new QuickFix.Fields.SecurityExchange("SIM");
|
|
default:
|
|
return new QuickFix.Fields.SecurityExchange();
|
|
}
|
|
}
|
|
|
|
protected bts.api.Venue convert_exchange(QuickFix.Fields.SecurityExchange fixtype)
|
|
{
|
|
switch (fixtype.getValue())
|
|
{
|
|
case "BTS":
|
|
return bts.api.Venue.Bts;
|
|
case "XCBO":
|
|
return bts.api.Venue.Cboe;
|
|
case "XCBF":
|
|
return bts.api.Venue.Cboecfe;
|
|
case "XCME":
|
|
case "XCBT":
|
|
case "XCEC":
|
|
case "XNYM":
|
|
return bts.api.Venue.Glbx;
|
|
case "IEPA":
|
|
case "IFUS":
|
|
case "IFEU":
|
|
case "IFLL":
|
|
return bts.api.Venue.Ice;
|
|
case "SIM":
|
|
return bts.api.Venue.Sim;
|
|
default:
|
|
return bts.api.Venue.Unset;
|
|
}
|
|
}
|
|
|
|
protected QuickFix.Fields.OrdStatus translate_status(in bts.api.orderman.OrderStateImmutable state,
|
|
ref bts.api.orderman.NativeOrderStateImmutable? native)
|
|
{
|
|
native = null;
|
|
if (state.HasMarket)
|
|
{
|
|
native = state.Market.NativeState;
|
|
return new OrdStatus((char)native?.NativeStatus);
|
|
}
|
|
else if (state.HasLimit)
|
|
{
|
|
native = state.Limit.NativeState;
|
|
return new OrdStatus((char)native?.NativeStatus);
|
|
}
|
|
else if (state.HasStop)
|
|
{
|
|
native = state.Stop.NativeState;
|
|
return new OrdStatus((char)native?.NativeStatus);
|
|
}
|
|
else if (state.HasStopLimit)
|
|
{
|
|
native = state.StopLimit.NativeState;
|
|
return new OrdStatus((char)native?.NativeStatus);
|
|
}
|
|
else
|
|
{
|
|
switch (state.Status)
|
|
{
|
|
case OrderStatus.Canceled:
|
|
return new OrdStatus(OrdStatus.CANCELED);
|
|
case OrderStatus.Completed:
|
|
return new OrdStatus(OrdStatus.FILLED);
|
|
case OrderStatus.Disconnected:
|
|
return new OrdStatus(OrdStatus.STOPPED);
|
|
case OrderStatus.Error:
|
|
return new OrdStatus(OrdStatus.REJECTED);
|
|
case OrderStatus.Inactive:
|
|
return new OrdStatus(OrdStatus.PENDING_NEW);
|
|
case OrderStatus.PendingWorking:
|
|
return new OrdStatus(OrdStatus.PENDING_NEW);
|
|
case OrderStatus.RejectedBySpark:
|
|
return new OrdStatus(OrdStatus.REJECTED);
|
|
case OrderStatus.Working:
|
|
{
|
|
if (state.FilledQuantity < state.Quantity)
|
|
return new OrdStatus(OrdStatus.PARTIALLY_FILLED);
|
|
else
|
|
return new OrdStatus(OrdStatus.NEW);
|
|
}
|
|
default:
|
|
return new OrdStatus();
|
|
}
|
|
}
|
|
}
|
|
|
|
protected uint lookup_account(string acct)
|
|
{
|
|
if (_admin_data.HasValue)
|
|
{
|
|
var ac = _admin_data.Value.Accounts.Where(x => x.Name == acct).TryFirst();
|
|
if(ac.HasValue)
|
|
return ac.Value.Id;
|
|
}
|
|
throw new IncorrectTagValue(new Account().Tag);
|
|
}
|
|
|
|
protected uint lookup_user(string user)
|
|
{
|
|
if (_admin_data.HasValue)
|
|
{
|
|
var ac = _admin_data.Value.Users.Where(x => x.Name == user).TryFirst();
|
|
if (ac.HasValue)
|
|
return ac.Value.Id;
|
|
}
|
|
throw new IncorrectTagValue(new Username().Tag);
|
|
}
|
|
|
|
}
|
|
} |