- Neue pure Klasse TradeMath (Logic/): ExitValue, RealizedPnl, PnlPercent (Null-Guard), WeightedAverageEntryPrice (Div-durch-0-Schutz). - CopyTradingEngine ruft sie jetzt im Demo-Close-PnL und bei der Positions- Aufstockung (Demo + Live, gewichteter Einstiegspreis) – verhaltensneutral. - TradeMathTests (6 Faelle: Gewinn/Verlust, %-Basis, 0-Kapital/0-Size-Guards). Gesamt 160 Tests gruen. Build/Smoke gruen. (Phase-0.1-SELL-Wiring weiterhin bewusst zurueckgestellt bis zum Server-Test; pure Leiter liegt getestet bereit.) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
52 lines
1.6 KiB
C#
52 lines
1.6 KiB
C#
using PolyTrader.Modules.CopyTrading.Logic;
|
|
using Xunit;
|
|
using static PolyTrader.Modules.CopyTrading.Logic.TradeMath;
|
|
|
|
namespace PolyTrader.Tests
|
|
{
|
|
/// <summary>
|
|
/// Sicherheitsnetz für die geldkritische Trade-Mathematik (PnL, PnL-%, gewichteter Entry).
|
|
/// </summary>
|
|
public class TradeMathTests
|
|
{
|
|
[Fact]
|
|
public void ExitValue_is_shares_times_price()
|
|
{
|
|
Assert.Equal(99m, ExitValue(100m, 0.99m));
|
|
}
|
|
|
|
[Fact]
|
|
public void RealizedPnl_profit_and_loss()
|
|
{
|
|
Assert.Equal(49m, RealizedPnl(sizeShares: 100m, exitPrice: 0.99m, entryAmountUsd: 50m));
|
|
Assert.Equal(-20m, RealizedPnl(sizeShares: 100m, exitPrice: 0.40m, entryAmountUsd: 60m));
|
|
}
|
|
|
|
[Fact]
|
|
public void PnlPercent_relative_to_entry_capital()
|
|
{
|
|
Assert.Equal(98m, PnlPercent(realizedPnl: 49m, entryAmountUsd: 50m));
|
|
Assert.Equal(-20m, PnlPercent(realizedPnl: -20m, entryAmountUsd: 100m));
|
|
}
|
|
|
|
[Fact]
|
|
public void PnlPercent_zero_entry_returns_zero_not_divide_by_zero()
|
|
{
|
|
Assert.Equal(0m, PnlPercent(49m, 0m));
|
|
}
|
|
|
|
[Fact]
|
|
public void WeightedAverageEntryPrice_is_amount_over_size()
|
|
{
|
|
Assert.Equal(0.5m, WeightedAverageEntryPrice(totalAmountUsd: 100m, totalSize: 200m));
|
|
Assert.Equal(0.51m, WeightedAverageEntryPrice(51m, 100m));
|
|
}
|
|
|
|
[Fact]
|
|
public void WeightedAverageEntryPrice_zero_size_returns_zero()
|
|
{
|
|
Assert.Equal(0m, WeightedAverageEntryPrice(100m, 0m));
|
|
}
|
|
}
|
|
}
|