Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
artemiusgreat committed Aug 28, 2024
1 parent 296e91f commit f4ca6e3
Show file tree
Hide file tree
Showing 12 changed files with 488 additions and 750 deletions.
41 changes: 38 additions & 3 deletions Core/Models/Transactions/OrderModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Terminal.Core.Enums;
using Terminal.Core.Extensions;

Expand Down Expand Up @@ -88,11 +89,45 @@ public OrderModel()
}

/// <summary>
/// Estimate current or close price for one of the instruments in the order
/// Position direction
/// </summary>
/// <param name="order"></param>
/// <returns></returns>
public double? GetVolume()
{
var volume = Transaction?.CurrentVolume ?? 0;
var sideVolume = Orders.Sum(o => o.Transaction?.CurrentVolume ?? 0);

return volume + sideVolume;
}

/// <summary>
/// Estimate open price for one of the instruments in the order
/// </summary>
/// <param name="order"></param>
/// <returns></returns>
public virtual double? GetOpenEstimate()
{
var point = Transaction.Instrument.Point;

if (point is not null)
{
switch (Side)
{
case OrderSideEnum.Buy: return point.Ask;
case OrderSideEnum.Sell: return point.Bid;
}
}

return null;
}

/// <summary>
/// Estimate close price for one of the instruments in the order
/// </summary>
/// <param name="order"></param>
/// <returns></returns>
public virtual double? GetPriceEstimate()
public virtual double? GetCloseEstimate()
{
var point = Transaction.Instrument.Point;

Expand All @@ -117,7 +152,7 @@ public OrderModel()
{
if (Transaction is not null)
{
return (((price ?? GetPriceEstimate()) - Price) * GetDirection()) ?? 0;
return (((price ?? GetCloseEstimate()) - Price) * GetDirection()) ?? 0;
}

return 0;
Expand Down
31 changes: 13 additions & 18 deletions Gateway/Simulation/Libs/Adapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,7 @@ protected virtual PositionModel SendOrder(OrderModel nextOrder)
.Where(subOrder => Equals(subOrder.Instruction, InstructionEnum.Side))
.ForEach(subOrder => UpdateSide(subOrder, nextPosition.Order)));

var sum =
position.Order.Transaction.CurrentVolume +
position.Order.Orders.Sum(o => o.Transaction.CurrentVolume);
var sum = position.Order.GetVolume();

switch (true)
{
Expand All @@ -292,9 +290,7 @@ protected virtual PositionModel SendOrder(OrderModel nextOrder)
}
}

var nextSum =
nextPosition.Order.Transaction.CurrentVolume +
nextPosition.Order.Orders.Sum(o => o.Transaction.CurrentVolume);
var nextSum = nextPosition.Order.GetVolume();

if (nextSum.Is(0) is false)
{
Expand All @@ -313,11 +309,11 @@ protected virtual PositionModel SendOrder(OrderModel nextOrder)
/// <returns></returns>
protected virtual PositionModel CreatePosition(OrderModel order)
{
void updateOrder(OrderModel o)
void updateOrder(OrderModel o, OrderModel group = null)
{
o.Type ??= OrderTypeEnum.Market;
o.TimeSpan ??= OrderTimeSpanEnum.Gtc;
o.Price = o.GetPriceEstimate();
o.Price = o.GetOpenEstimate();
o.Type ??= group?.Type ?? OrderTypeEnum.Market;
o.TimeSpan ??= group?.TimeSpan ?? OrderTimeSpanEnum.Gtc;
o.Transaction ??= new TransactionModel();
o.Transaction.Time ??= DateTime.Now;
o.Transaction.Status = OrderStatusEnum.Filled;
Expand All @@ -326,19 +322,18 @@ void updateOrder(OrderModel o)
}

var position = new PositionModel();
var nextOrder = order.Clone() as OrderModel;

if (nextOrder.Transaction is not null)
if (order.Transaction is not null)
{
updateOrder(nextOrder);
updateOrder(order);
}

nextOrder
order
.Orders
.Where(o => Equals(o.Instruction, InstructionEnum.Side))
.ForEach(o => updateOrder(o));
.ForEach(o => updateOrder(o, order));

position.Order = nextOrder;
position.Order = order;

return position;
}
Expand All @@ -365,8 +360,8 @@ void updateOrder(OrderModel o)
protected virtual ResponseModel<OrderModel> UpdateSide(OrderModel order, OrderModel update)
{
var response = new ResponseModel<OrderModel>();
var orderName = order.Transaction.Instrument.Name;
var updateName = update.Transaction.Instrument.Name;
var orderName = order.Transaction?.Instrument?.Name;
var updateName = update.Transaction?.Instrument?.Name;

if (Equals(updateName, orderName) is false)
{
Expand Down
21 changes: 0 additions & 21 deletions Gateway/Simulation/Tests/Connectors.cs

This file was deleted.

82 changes: 0 additions & 82 deletions Gateway/Simulation/Tests/CreateOrders.cs

This file was deleted.

Loading

0 comments on commit f4ca6e3

Please sign in to comment.