Skip to content

Commit

Permalink
bug fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Ariel Silahian committed Jan 25, 2024
1 parent 7922350 commit bffcdea
Show file tree
Hide file tree
Showing 23 changed files with 407 additions and 261 deletions.
18 changes: 16 additions & 2 deletions App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,29 @@ public partial class App : Application
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);

//Initialize logging
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo("log4net.config"));

//Launch the GC cleanup thread
Task.Run(async () => { await GCCleanupAsync(); });

//Load Plugins
Task.Run(async () => { await LoadPlugins(); });
Task.Run(async () =>
{
try
{
await LoadPlugins();
}
catch (Exception ex)
{
// Handle the exception
Application.Current.Dispatcher.Invoke(() =>
{
MessageBox.Show("ERROR LOADING Plugins: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
});
}
});

}

Expand Down
30 changes: 13 additions & 17 deletions DataRetriever/DataProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ public class DataProcessor
public DataProcessor(IDataRetriever dataRetriever)
{
_dataRetriever = dataRetriever;
_dataRetriever.OnDataReceived += async (sender, e) => await EnqueueDataAsync(sender, e);
_dataRetriever.OnDataReceived += (sender, e) => EnqueueData(sender, e);
StartProcessing();
}

private async Task EnqueueDataAsync(object sender, DataEventArgs e)
private void EnqueueData(object sender, DataEventArgs e)
{
if (_dataQueue.Count < MAX_QUEUE_SIZE)
{
_dataQueue.Add(e);
}
else
{
await Task.Delay(BACK_PRESSURE_DELAY);
Task.Delay(BACK_PRESSURE_DELAY);
}
}

Expand Down Expand Up @@ -83,15 +83,15 @@ private void HandleData(DataEventArgs e)
ParseOrderBook(orderBook);
}
break;
case "ActiveOrders":
ParseActiveOrders(e.ParsedModel as List<VisualHFT.Model.Order>);
case "ExecutedOrder":
ParseExecutedOrder(e.ParsedModel as VisualHFT.Model.Order);
break;
//case "Execution":
// ParseExecution(e.ParsedModel as VisualHFT.Model.Execution);
// break;
case "Strategies":
ParseActiveStrategies(e.ParsedModel as List<StrategyVM>);
break;
case "Exposures":
ParseExposures(e.ParsedModel as List<Exposure>);
break;
case "HeartBeats":
ParseHeartBeat(e.ParsedModel as List<VisualHFT.Model.Provider>);
break;
Expand All @@ -102,6 +102,10 @@ private void HandleData(DataEventArgs e)
break;
}
}
private void ParseExecutedOrder(Order? order)
{
HelperCommon.EXECUTEDORDERS.AddOrder(order);
}
#region Parsing Methods
private void ParseSymbols(IEnumerable<string> symbols)
{
Expand All @@ -110,15 +114,7 @@ private void ParseSymbols(IEnumerable<string> symbols)
private void ParseOrderBook(IEnumerable<OrderBook> orderBooks)
{
HelperOrderBook.Instance.UpdateData(orderBooks);

}
private void ParseExposures(IEnumerable<Exposure> exposures)
{
HelperCommon.EXPOSURES.UpdateData(exposures);
}
private void ParseActiveOrders(IEnumerable<VisualHFT.Model.Order> activeOrders)
{
HelperCommon.ACTIVEORDERS.UpdateData(activeOrders.ToList());

}
private void ParseActiveStrategies(IEnumerable<StrategyVM> activeStrategies)
{
Expand Down
41 changes: 27 additions & 14 deletions DataTradeRetriever/FIXTradesRetriever.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class FIXTradesRetriever : IDataTradeRetriever, IDisposable

public event EventHandler<IEnumerable<VisualHFT.Model.Order>> OnInitialLoad;
public event EventHandler<IEnumerable<VisualHFT.Model.Order>> OnDataReceived;
public event EventHandler<Order> OnDataUpdated;

protected virtual void RaiseOnInitialLoad(IEnumerable<VisualHFT.Model.Order> ord) => OnInitialLoad?.Invoke(this, ord);
protected virtual void RaiseOnDataReceived(IEnumerable<VisualHFT.Model.Order> ord) => OnDataReceived?.Invoke(this, ord);
public FIXTradesRetriever(string logFilePath, int providerId, string providerName)
Expand Down Expand Up @@ -66,7 +68,7 @@ private async void _timer_Elapsed(object sender, ElapsedEventArgs e)
var symbols = parsedTrades.Select(x => x.Symbol).Distinct().ToList();
if (symbols.Any())
{
foreach( var symbol in symbols)
foreach (var symbol in symbols)
{
HelperSymbol.Instance.UpdateData(symbol);
}
Expand Down Expand Up @@ -241,7 +243,7 @@ public ReadOnlyCollection<VisualHFT.Model.Position> Positions
order.LastUpdated = HelperTimeProvider.Now;

if (order.Status == eORDERSTATUS.REPLACED)
{
{
order.ClOrdId = _clordId;
parsedOrders.Add(_clordId, order);
parsedOrders.Remove(_orig_clordId);
Expand Down Expand Up @@ -283,7 +285,7 @@ private async Task<List<string>> ReadNewLinesFromFileAsync(string filePath)
const int bufferSize = 8192; // 8 KB
char[] buffer = new char[bufferSize];
StringBuilder sb = new StringBuilder();
List<string> newLines = new List<string>();
List<string> newLines = new List<string>();

using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var sr = new StreamReader(fs, Encoding.Default))
Expand Down Expand Up @@ -322,25 +324,26 @@ private async Task<List<string>> ReadNewLinesFromFileAsync(string filePath)

private eORDERSTATUS ParseOrderStatus(string fixStatus)
{

// Convert the FIX status to your application's order status
switch (fixStatus[0])
{
case OrdStatus.NEW: return eORDERSTATUS.NEW;
case OrdStatus.PARTIALLY_FILLED: return eORDERSTATUS.PARTIALFILLED;
case OrdStatus.FILLED: return eORDERSTATUS.FILLED;
case OrdStatus.CANCELED: return eORDERSTATUS.CANCELED;
case OrdStatus.REJECTED: return eORDERSTATUS.REJECTED;
case OrdStatus.REPLACED: return eORDERSTATUS.REPLACED;
case OrdStatus.EXPIRED: return eORDERSTATUS.REJECTED;
case OrdStatus.PENDING_REPLACE: return eORDERSTATUS.REPLACESENT;
case OrdStatus.PENDING_CANCEL: return eORDERSTATUS.CANCELEDSENT;
case OrdStatus.NEW: return eORDERSTATUS.NEW;
case OrdStatus.PARTIALLY_FILLED: return eORDERSTATUS.PARTIALFILLED;
case OrdStatus.FILLED: return eORDERSTATUS.FILLED;
case OrdStatus.CANCELED: return eORDERSTATUS.CANCELED;
case OrdStatus.REJECTED: return eORDERSTATUS.REJECTED;
case OrdStatus.REPLACED: return eORDERSTATUS.REPLACED;
case OrdStatus.EXPIRED: return eORDERSTATUS.REJECTED;
case OrdStatus.PENDING_REPLACE: return eORDERSTATUS.REPLACESENT;
case OrdStatus.PENDING_CANCEL: return eORDERSTATUS.CANCELEDSENT;
default:
return eORDERSTATUS.NONE;
}
}
private eORDERTYPE ParseOrderType(string fixOrdType)
{
switch(fixOrdType[0])
switch (fixOrdType[0])
{
case OrdType.MARKET: return eORDERTYPE.MARKET;
case OrdType.LIMIT: return eORDERTYPE.LIMIT;
Expand All @@ -349,7 +352,7 @@ private eORDERTYPE ParseOrderType(string fixOrdType)
}
private eORDERTIMEINFORCE ParseOrderTIF(string fixTIF)
{
switch(fixTIF[0])
switch (fixTIF[0])
{
case QuickFix.Fields.TimeInForce.IMMEDIATE_OR_CANCEL: return eORDERTIMEINFORCE.IOC;
case QuickFix.Fields.TimeInForce.FILL_OR_KILL: return eORDERTIMEINFORCE.FOK;
Expand Down Expand Up @@ -398,5 +401,15 @@ public void Dispose()
Dispose(true);
GC.SuppressFinalize(this);
}

public void AddExecution(Execution? execution)
{
throw new NotImplementedException();
}

public void AddOrder(Order? order)
{
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using VisualHFT.Helpers;
using VisualHFT.Model;
using QuickFix.Fields;
using QuickFix.DataDictionary;
using System.Windows.Shapes;


namespace VisualHFT.DataTradeRetriever
{
public class EmptyTradesRetriever : IDataTradeRetriever, IDisposable
public class GenericTradesRetriever : IDataTradeRetriever, IDisposable
{
private List<VisualHFT.Model.Position> _positions;
private List<VisualHFT.Model.Order> _orders;
private object _locker = new object();

int _providerId;
string _providerName;
DateTime? _sessionDate = null;
Expand All @@ -28,17 +21,26 @@ public class EmptyTradesRetriever : IDataTradeRetriever, IDisposable

public event EventHandler<IEnumerable<VisualHFT.Model.Order>> OnInitialLoad;
public event EventHandler<IEnumerable<VisualHFT.Model.Order>> OnDataReceived;
protected virtual void RaiseOnInitialLoad(IEnumerable<VisualHFT.Model.Order> ord) => OnInitialLoad?.Invoke(this, ord);
protected virtual void RaiseOnDataReceived(IEnumerable<VisualHFT.Model.Order> ord) => OnDataReceived?.Invoke(this, ord);
public EmptyTradesRetriever()
public event EventHandler<VisualHFT.Model.Execution> OnExecutionReceived;
public event EventHandler<Order> OnDataUpdated;

public GenericTradesRetriever()
{
_positions = new List<VisualHFT.Model.Position>();
_orders = new List<VisualHFT.Model.Order>();

HelperTimeProvider.OnSetFixedTime += HelperTimeProvider_OnSetFixedTime;
}
~EmptyTradesRetriever()
~GenericTradesRetriever()
{
Dispose(false);
}
private void HelperTimeProvider_OnSetFixedTime(object? sender, EventArgs e)
{
if (_sessionDate != HelperTimeProvider.Now.Date)
SessionDate = HelperTimeProvider.Now.Date;
}

public DateTime? SessionDate
{
get { return _sessionDate; }
Expand All @@ -48,7 +50,8 @@ public DateTime? SessionDate
{
_sessionDate = value;
_orders.Clear();
RaiseOnInitialLoad(this.Orders);
_positions.Clear();
OnInitialLoad?.Invoke(this, this.Orders);
}
}
}
Expand All @@ -66,7 +69,10 @@ protected virtual void Dispose(bool disposing)
if (!_disposed)
{
if (disposing)
{}
{


}
_disposed = true;
}
}
Expand All @@ -75,5 +81,25 @@ public void Dispose()
Dispose(true);
GC.SuppressFinalize(this);
}
public void AddOrder(Order? order)
{
if (order == null)
return;
lock (_locker)
{
var existingOrder = _orders.FirstOrDefault(x => x.OrderID == order.OrderID);
if (existingOrder == null)
{
_orders.Add(order);
OnDataReceived?.Invoke(this, new List<Order> { order });
}
else
{
existingOrder = order;
OnDataUpdated?.Invoke(this, order);
}
}

}
}
}
16 changes: 14 additions & 2 deletions DataTradeRetriever/MSSQLServerTradesRetriever.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ public class MSSQLServerTradesRetriever : IDataTradeRetriever, IDisposable

public event EventHandler<IEnumerable<VisualHFT.Model.Order>> OnInitialLoad;
public event EventHandler<IEnumerable<VisualHFT.Model.Order>> OnDataReceived;
public event EventHandler<Order> OnDataUpdated;

protected virtual void RaiseOnInitialLoad(IEnumerable<VisualHFT.Model.Order> ord) => OnInitialLoad?.Invoke(this, ord);
protected virtual void RaiseOnDataReceived(IEnumerable<VisualHFT.Model.Order> ord) => OnDataReceived?.Invoke(this, ord);


public MSSQLServerTradesRetriever()
{
_positions = new List<VisualHFT.Model.Position>();
_orders = new List<VisualHFT.Model.Order>();
_orders = new List<VisualHFT.Model.Order>();
_timer = new System.Timers.Timer(POLLING_INTERVAL);
_timer.Elapsed += _timer_Elapsed;
_timer.Start();
Expand Down Expand Up @@ -145,7 +147,7 @@ public ReadOnlyCollection<VisualHFT.Model.Position> Positions
_LAST_POSITION_ID = result.Max(x => x.ID);

var ret = result.Select(x => new VisualHFT.Model.Position(x)).ToList(); //convert to our model
//find provider's name
//find provider's name
ret.ForEach(x =>
{
x.CloseProviderName = allProviders.Where(p => p.ProviderCode == x.CloseProviderId).DefaultIfEmpty(new Provider()).FirstOrDefault().ProviderName;
Expand Down Expand Up @@ -192,5 +194,15 @@ public void Dispose()
Dispose(true);
GC.SuppressFinalize(this);
}

public void AddExecution(Execution? execution)
{
throw new NotImplementedException();
}

public void AddOrder(Order? order)
{
throw new NotImplementedException();
}
}
}
6 changes: 3 additions & 3 deletions Helpers/HelperCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public static DateTime GetCurrentSessionEnd(DateTime? currentSessionDate)
}
public static int TimerMillisecondsToGetVariables = 1000 * 10; //10 seconds

public static IDataTradeRetriever EXECUTEDORDERS = new EmptyTradesRetriever();
public static IDataTradeRetriever EXECUTEDORDERS = new GenericTradesRetriever();
//public static IDataTradeRetriever EXECUTEDORDERS = new MSSQLServerTradesRetriever();
//public static IDataTradeRetriever EXECUTEDORDERS = new FIXTradesRetriever([path_to_fix_log_file], 1, "CME");

public static HelperExposure EXPOSURES = new HelperExposure();
public static HelperActiveOrder ACTIVEORDERS = new HelperActiveOrder();
//public static HelperExposure EXPOSURES = new HelperExposure();
//public static HelperActiveOrder ACTIVEORDERS = new HelperActiveOrder();
public static HelperStrategy ACTIVESTRATEGIES = new HelperStrategy();
public static HelperStrategyParams STRATEGYPARAMS = new HelperStrategyParams();

Expand Down
Loading

0 comments on commit bffcdea

Please sign in to comment.