Skip to content

Commit

Permalink
Basic activity pnl in attribution for MC
Browse files Browse the repository at this point in the history
  • Loading branch information
gavbrennan committed Jul 5, 2024
1 parent 9ea83f4 commit c38923b
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 4 deletions.
101 changes: 101 additions & 0 deletions src/Qwack.Models/Models/AttributionSteps/ActivityStep.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System.Collections.Generic;
using System.Linq;
using Qwack.Core.Basic;
using Qwack.Core.Cubes;
using Qwack.Core.Instruments;
using Qwack.Core.Models;
using static Qwack.Core.Basic.Consts.Cubes;

namespace Qwack.Models.Models.AttributionSteps;

public class ActivityStep(Portfolio startPortfolio, Portfolio endPortfolio) : IPnLAttributionStep
{
public (ICube endOfStepPvCube, IPvModel model) Attribute(IPvModel model, IPvModel endModel, ResultCube resultsCube, ICube lastPvCube,
ICube riskCube, Currency reportingCcy)
{
var (newTrades, removedTrades, ammendedTradesStart, ammendedTradesEnd) = startPortfolio.ActivityBooks(endPortfolio, endModel.VanillaModel.BuildDate);

var pfEndDict = endPortfolio.Instruments.ToDictionary(x => x.TradeId, x => x.PortfolioName);
var pfStartDict = startPortfolio.Instruments.ToDictionary(x => x.TradeId, x => x.PortfolioName);

if (newTrades.Instruments.Count > 0)
{
model = model.Rebuild(model.VanillaModel, newTrades);
var newTradesPnL = model.PV(reportingCcy);
var tidIx = newTradesPnL.GetColumnIndex(TradeId);
var tTypeIx = newTradesPnL.GetColumnIndex(TradeType);
foreach (var t in newTradesPnL.GetAllRows())
{
var tid = (string)t.MetaData[tidIx];
var row = new Dictionary<string, object>
{
{ TradeId, tid},
{ TradeType, t.MetaData[tTypeIx] },
{ Step, "Activity" },
{ SubStep, "New" },
{ SubSubStep, string.Empty },
{ PointLabel, string.Empty },
{ "Portfolio", pfEndDict[tid]}
};
resultsCube.AddRow(row, t.Value);
}
}

if (removedTrades.Instruments.Count > 0)
{
model = model.Rebuild(model.VanillaModel, removedTrades);
var removedTradesPnL = model.PV(reportingCcy);

var tidIx = removedTradesPnL.GetColumnIndex(TradeId);
var tTypeIx = removedTradesPnL.GetColumnIndex(TradeType);

foreach (var t in removedTradesPnL.GetAllRows())
{
var tid = (string)t.MetaData[tidIx];
var row = new Dictionary<string, object>
{
{ TradeId, tid },
{ TradeType, t.MetaData[tTypeIx] },
{ Step, "Activity" },
{ SubStep, "Removed" },
{ SubSubStep, string.Empty },
{ PointLabel, string.Empty },
{ "Portfolio", pfStartDict[tid]}
};
resultsCube.AddRow(row, -t.Value);
}
}

if (ammendedTradesStart.Instruments.Count > 0)
{
model = model.Rebuild(model.VanillaModel, ammendedTradesStart);
var amendedTradesPnLStart = model.PV(reportingCcy);
model = model.Rebuild(model.VanillaModel, ammendedTradesEnd);
var amendedTradesPnLEnd = model.PV(reportingCcy);
var amendedPnL = amendedTradesPnLEnd.QuickDifference(amendedTradesPnLStart);

var tidIx = amendedTradesPnLStart.GetColumnIndex(TradeId);
var tTypeIx = amendedTradesPnLStart.GetColumnIndex(TradeType);

foreach (var t in amendedPnL.GetAllRows())
{
var tid = (string)t.MetaData[tidIx];
var row = new Dictionary<string, object>
{
{ TradeId, tid },
{ TradeType, t.MetaData[tTypeIx] },
{ Step, "Activity" },
{ SubStep, "Ammended" },
{ SubSubStep, string.Empty },
{ PointLabel, string.Empty },
{ "Portfolio", pfStartDict[tid]}
};
resultsCube.AddRow(row, t.Value);
}
}

model = model.Rebuild(model.VanillaModel, endPortfolio);

return (lastPvCube, model);
}
}
8 changes: 6 additions & 2 deletions src/Qwack.Models/Models/PnLAttribution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1500,7 +1500,7 @@ private static ICube EnrichWithPortfolio(this ICube results, Portfolio pfolio)
return o;
}

public static ICube ExplainAttribution(this Portfolio portfolio, AssetFxMCModel startModel, AssetFxMCModel endModel, Currency reportingCcy, ICube startingGreeks, ICurrencyProvider currencyProvider, IFutureSettingsProvider futureSettings, ICalendarProvider calendarProvider, bool useSpreadDelta = false)
public static ICube ExplainAttribution(this Portfolio startPortfolio, Portfolio endPortfolio, AssetFxMCModel startModel, AssetFxMCModel endModel, Currency reportingCcy, ICube startingGreeks, ICurrencyProvider currencyProvider, IFutureSettingsProvider futureSettings, ICalendarProvider calendarProvider, bool useSpreadDelta = false)
{
var cube = new ResultCube();
var dataTypes = new Dictionary<string, Type>
Expand Down Expand Up @@ -1543,10 +1543,14 @@ public static ICube ExplainAttribution(this Portfolio portfolio, AssetFxMCModel
(lastPvCube, model) =
new FxVolsStep().Attribute(model, endModel, cube, lastPvCube, startingGreeks, reportingCcy);

//finally unexplained step
//unexplained step
(lastPvCube, model) =
new FinalStep().Attribute(model, endModel, cube, lastPvCube, startingGreeks, reportingCcy);

//activity step
(lastPvCube, model) =
new ActivityStep(startPortfolio, endPortfolio).Attribute(model, endModel, cube, lastPvCube, startingGreeks, reportingCcy);

return cube;
}

Expand Down
2 changes: 1 addition & 1 deletion version.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<VersionPrefix>0.8.51</VersionPrefix>
<VersionPrefix>0.8.52</VersionPrefix>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.8.51
0.8.52

0 comments on commit c38923b

Please sign in to comment.