Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
artemiusgreat committed Apr 1, 2024
1 parent 9d21663 commit 1c4095d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 32 deletions.
11 changes: 8 additions & 3 deletions Client/Components/BaseComponent.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Client.Components
{
public class BaseComponent : ComponentBase
{
private static object protection = new();

/// <summary>
/// Updater
/// </summary>
Expand All @@ -18,9 +20,12 @@ public class BaseComponent : ComponentBase
/// </summary>
protected virtual Task Render(Action action) => Updater.Send(() =>
{
action();
InvokeAsync(StateHasChanged);
Thread.Sleep(1);
lock (protection)
{
action();
InvokeAsync(StateHasChanged);
Thread.Sleep(1);
}
}).Task;
}
}
8 changes: 4 additions & 4 deletions Core/Validators/OrderPriceValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public OrderPriceValidator()
Include(new OrderValidator());

When(o => _orderTypes.ContainsKey(o.Type ?? OrderTypeEnum.None) is false, () => RuleFor(o => o.Transaction.Price).NotEmpty());
When(o => Equals(o.Side, OrderSideEnum.Buy) && Equals(o.Type, OrderTypeEnum.Stop), () => RuleFor(o => o.Transaction.Price).GreaterThanOrEqualTo(o => o.Transaction.Instrument.Points.Last().Ask));
When(o => Equals(o.Side, OrderSideEnum.Sell) && Equals(o.Type, OrderTypeEnum.Stop), () => RuleFor(o => o.Transaction.Price).LessThanOrEqualTo(o => o.Transaction.Instrument.Points.Last().Bid));
When(o => Equals(o.Side, OrderSideEnum.Buy) && Equals(o.Type, OrderTypeEnum.Limit), () => RuleFor(o => o.Transaction.Price).LessThanOrEqualTo(o => o.Transaction.Instrument.Points.Last().Ask));
When(o => Equals(o.Side, OrderSideEnum.Sell) && Equals(o.Type, OrderTypeEnum.Limit), () => RuleFor(o => o.Transaction.Price).GreaterThanOrEqualTo(o => o.Transaction.Instrument.Points.Last().Bid));
When(o => o.Side is OrderSideEnum.Buy && o.Type is OrderTypeEnum.Stop, () => RuleFor(o => o.Transaction.Price).GreaterThanOrEqualTo(o => o.Transaction.Instrument.Points.Last().Ask));
When(o => o.Side is OrderSideEnum.Sell && o.Type is OrderTypeEnum.Stop, () => RuleFor(o => o.Transaction.Price).LessThanOrEqualTo(o => o.Transaction.Instrument.Points.Last().Bid));
When(o => o.Side is OrderSideEnum.Buy && o.Type is OrderTypeEnum.Limit, () => RuleFor(o => o.Transaction.Price).LessThanOrEqualTo(o => o.Transaction.Instrument.Points.Last().Ask));
When(o => o.Side is OrderSideEnum.Sell && o.Type is OrderTypeEnum.Limit, () => RuleFor(o => o.Transaction.Price).GreaterThanOrEqualTo(o => o.Transaction.Instrument.Points.Last().Bid));

When(o => Equals(o.Side, OrderSideEnum.Buy) && Equals(o.Type, OrderTypeEnum.StopLimit), () =>
{
Expand Down
26 changes: 14 additions & 12 deletions Gateways/Simulation/Libs/Adapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ protected virtual PositionModel CreatePosition(OrderModel order)
/// <returns></returns>
protected virtual PositionModel UpdatePosition(OrderModel nextOrder, PositionModel previousPosition)
{
var isSameBuy = Equals(previousPosition.Order.Side, OrderSideEnum.Buy) && Equals(nextOrder.Side, OrderSideEnum.Buy);
var isSameSell = Equals(previousPosition.Order.Side, OrderSideEnum.Sell) && Equals(nextOrder.Side, OrderSideEnum.Sell);
var isSameBuy = previousPosition.Order.Side is OrderSideEnum.Buy && nextOrder.Side is OrderSideEnum.Buy;
var isSameSell = previousPosition.Order.Side is OrderSideEnum.Sell && nextOrder.Side is OrderSideEnum.Sell;

nextOrder.Transaction.Status = OrderStatusEnum.Filled;

Expand All @@ -346,10 +346,11 @@ protected virtual PositionModel UpdatePosition(OrderModel nextOrder, PositionMod
/// <param name="order"></param>
/// <param name="previousPosition"></param>
/// <returns></returns>
protected virtual PositionModel IncreasePosition(OrderModel order, PositionModel previousPosition)
protected virtual PositionModel IncreasePosition(OrderModel order, PositionModel previousPos)
{
var nextOrder = order.Clone() as OrderModel;
var nextPosition = previousPosition.Clone() as PositionModel;
var nextPosition = previousPos.Clone() as PositionModel;
var previousPosition = previousPos.Clone() as PositionModel;

nextPosition.Orders = previousPosition.Orders.Concat(new[] { nextOrder }).ToList();
nextPosition.Order.Transaction.Id = nextOrder.Transaction.Id;
Expand Down Expand Up @@ -383,10 +384,11 @@ protected virtual PositionModel IncreasePosition(OrderModel order, PositionModel
/// <param name="order"></param>
/// <param name="previousPosition"></param>
/// <returns></returns>
protected virtual PositionModel DecreasePosition(OrderModel order, PositionModel previousPosition)
protected virtual PositionModel DecreasePosition(OrderModel order, PositionModel previousPos)
{
var nextOrder = order.Clone() as OrderModel;
var nextPosition = previousPosition.Clone() as PositionModel;
var nextPosition = previousPos.Clone() as PositionModel;
var previousPosition = previousPos.Clone() as PositionModel;

nextPosition.Orders = previousPosition.Orders.Concat(new[] { nextOrder }).ToList();
nextPosition.Order.Transaction.Id = nextOrder.Transaction.Id;
Expand Down Expand Up @@ -439,12 +441,12 @@ protected virtual void OnPointUpdate(object sender, NotifyCollectionChangedEvent
}

var isExecutable = false;
var isBuyStop = Equals(order.Side, OrderSideEnum.Buy) && Equals(order.Type, OrderTypeEnum.Stop);
var isSellStop = Equals(order.Side, OrderSideEnum.Sell) && Equals(order.Type, OrderTypeEnum.Stop);
var isBuyLimit = Equals(order.Side, OrderSideEnum.Buy) && Equals(order.Type, OrderTypeEnum.Limit);
var isSellLimit = Equals(order.Side, OrderSideEnum.Sell) && Equals(order.Type, OrderTypeEnum.Limit);
var isBuyStopLimit = Equals(order.Side, OrderSideEnum.Buy) && Equals(order.Type, OrderTypeEnum.StopLimit) && pointModel.Ask >= order.ActivationPrice;
var isSellStopLimit = Equals(order.Side, OrderSideEnum.Sell) && Equals(order.Type, OrderTypeEnum.StopLimit) && pointModel.Bid <= order.ActivationPrice;
var isBuyStop = order.Side is OrderSideEnum.Buy && order.Type is OrderTypeEnum.Stop;
var isSellStop = order.Side is OrderSideEnum.Sell && order.Type is OrderTypeEnum.Stop;
var isBuyLimit = order.Side is OrderSideEnum.Buy && order.Type is OrderTypeEnum.Limit;
var isSellLimit =order.Side is OrderSideEnum.Sell && order.Type is OrderTypeEnum.Limit;
var isBuyStopLimit = order.Side is OrderSideEnum.Buy && order.Type is OrderTypeEnum.StopLimit && pointModel.Ask >= order.ActivationPrice;
var isSellStopLimit = order.Side is OrderSideEnum.Sell && order.Type is OrderTypeEnum.StopLimit && pointModel.Bid <= order.ActivationPrice;

if (isBuyStopLimit || isSellStopLimit)
{
Expand Down
15 changes: 9 additions & 6 deletions Gateways/Simulation/Tests/DecreasePosition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public void Decrease()

}).First();

var previousPosition = Account.ActivePositions[orderY.Transaction.Id];
var nextPosition = base.DecreasePosition(order, previousPosition);
var nextPosition = base.DecreasePosition(order, Account.ActivePositions[orderY.Transaction.Id]);

Assert.Single(Account.Positions);
Assert.Empty(Account.ActiveOrders);
Expand Down Expand Up @@ -67,6 +66,8 @@ public void Decrease()

// Gain

var previousPosition = Account.Positions.Last();

Assert.Equal(previousPosition.GainLossEstimate, previousPosition.GainLoss);
Assert.Equal(previousPosition.GainLossPointsEstimate, previousPosition.GainLossPoints);
}
Expand All @@ -83,8 +84,7 @@ public void Close()

}).First();

var previousPosition = Account.ActivePositions[orderY.Transaction.Id];
var nextPosition = base.DecreasePosition(order, previousPosition);
var nextPosition = base.DecreasePosition(order, Account.ActivePositions[orderY.Transaction.Id]);

Assert.Single(Account.Positions);
Assert.Single(Account.ActivePositions);
Expand Down Expand Up @@ -119,6 +119,8 @@ public void Close()

// Gain

var previousPosition = Account.Positions.Last();

Assert.Equal(previousPosition.GainLossEstimate, previousPosition.GainLoss);
Assert.Equal(previousPosition.GainLossPointsEstimate, previousPosition.GainLossPoints);
}
Expand All @@ -135,8 +137,7 @@ public void Inverse()

}).First();

var previousPosition = Account.ActivePositions[orderY.Transaction.Id];
var nextPosition = base.DecreasePosition(order, previousPosition);
var nextPosition = base.DecreasePosition(order, Account.ActivePositions[orderY.Transaction.Id]);

Assert.Single(Account.Positions);
Assert.Empty(Account.ActiveOrders);
Expand Down Expand Up @@ -171,6 +172,8 @@ public void Inverse()

// Gain

var previousPosition = Account.Positions.Last();

Assert.Equal(previousPosition.GainLossEstimate, previousPosition.GainLoss);
Assert.Equal(previousPosition.GainLossPointsEstimate, previousPosition.GainLossPoints);
}
Expand Down
8 changes: 1 addition & 7 deletions Gateways/Simulation/Tests/IncreasePosition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public void Increase()

// Increase

var previousPosition = Account.ActivePositions[orderY.Transaction.Id];
var nextPosition = base.IncreasePosition(order, previousPosition);
var nextPosition = base.IncreasePosition(order, Account.ActivePositions[orderY.Transaction.Id]);
var averageTradePrice =
nextPosition.Orders.Sum(o => o.Transaction.Volume * o.Transaction.Price) /
nextPosition.Orders.Sum(o => o.Transaction.Volume);
Expand Down Expand Up @@ -72,11 +71,6 @@ public void Increase()
Assert.Equal(nextPosition.Order.Transaction.Price, averageTradePrice);
Assert.Equal(nextPosition.Order.Transaction.Volume, order.Transaction.Volume + orderY.Transaction.Volume);

// Gain

Assert.Equal(previousPosition.GainLossEstimate, previousPosition.GainLoss);
Assert.Equal(previousPosition.GainLossPointsEstimate, previousPosition.GainLossPoints);

// Estimate

var step = instrumentY.StepValue / instrumentY.StepSize;
Expand Down

0 comments on commit 1c4095d

Please sign in to comment.