Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Drawaes committed Sep 17, 2021
1 parent f34bdbf commit e7414d4
Show file tree
Hide file tree
Showing 58 changed files with 195 additions and 280 deletions.
8 changes: 4 additions & 4 deletions src/Qwack.Core/Basic/Correlation/CorrelationTimeVector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public class CorrelationTimeVector : ICorrelationMatrix
public string[] LabelsY { get; set; }
public double[][] Correlations { get; set; }

private IInterpolator1D[] _interps;
private double[] _times;
private Interpolator1DType _interpType;
private readonly IInterpolator1D[] _interps;
private readonly double[] _times;
private readonly Interpolator1DType _interpType;

public CorrelationTimeVector() { }

Expand Down Expand Up @@ -95,7 +95,7 @@ public ICorrelationMatrix Bump(double epsilon)
}

public TO_CorrelationMatrix GetTransportObject() =>
new TO_CorrelationMatrix
new()
{
Correlations = Correlations,
LabelsX = LabelsX,
Expand Down
2 changes: 1 addition & 1 deletion src/Qwack.Core/Basic/Currency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Qwack.Core.Basic
/// </summary>
public class Currency
{
private ICalendarProvider _calendarProvider;
private readonly ICalendarProvider _calendarProvider;

public Currency(ICalendarProvider calendarProvider) => _calendarProvider = calendarProvider;

Expand Down
7 changes: 2 additions & 5 deletions src/Qwack.Core/Basic/FxMatrix.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Qwack.Core.Basic;
using Qwack.Core.Models;
using Qwack.Dates;
using Qwack.Transport.TransportObjects.MarketData.Models;
Expand All @@ -12,7 +9,7 @@ namespace Qwack.Core.Basic
{
public class FxMatrix : IFxMatrix
{
private ICurrencyProvider _currencyProvider;
private readonly ICurrencyProvider _currencyProvider;

public FxMatrix(TO_FxMatrix transportObject, ICurrencyProvider currencyProvider, ICalendarProvider calendarProvider):this(currencyProvider)
{
Expand Down Expand Up @@ -99,7 +96,7 @@ public IFxMatrix Rebase(DateTime newBuildDate, Dictionary<Currency,double> newSp
public string GetDiscountCurve(string currency) => DiscountCurveMap.TryGetValue(_currencyProvider.GetCurrency(currency), out var curve) ? curve : null;

public TO_FxMatrix GetTransportObject() =>
new TO_FxMatrix
new()
{
BaseCurrency = BaseCurrency.Ccy,
BuildDate = BuildDate,
Expand Down
6 changes: 3 additions & 3 deletions src/Qwack.Core/Basic/FxPair.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public FxPair(TO_FxPair transportObject, ICurrencyProvider currencyProvider, ICa

public override bool Equals(object x)
{
if (!(x is FxPair x1))
if (x is not FxPair x1)
{
return false;
}
Expand All @@ -53,7 +53,7 @@ public override int GetHashCode()
public override string ToString() => $"{Domestic}/{Foreign}";

public TO_FxPair GetTransportObject() =>
new TO_FxPair
new()
{
Domestic = Domestic.Ccy,
Foreign = Foreign.Ccy,
Expand Down Expand Up @@ -81,7 +81,7 @@ public static DateTime SpotDate(this FxPair fxPair, DateTime valDate)
return d;
}

public static FxPair FxPairFromString(this string pair, ICurrencyProvider currencyProvider, ICalendarProvider calendarProvider) => new FxPair
public static FxPair FxPairFromString(this string pair, ICurrencyProvider currencyProvider, ICalendarProvider calendarProvider) => new()
{
Domestic = currencyProvider.GetCurrency(pair.Substring(0, 3)),
Foreign = currencyProvider.GetCurrency(pair.Substring(pair.Length - 3, 3)),
Expand Down
6 changes: 3 additions & 3 deletions src/Qwack.Core/Cubes/ResultCube.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public ResultCubeRow(TO_ResultCubeRow transportObject, Type[] types)
}

public TO_ResultCubeRow ToTransportObject() =>
new TO_ResultCubeRow
new()
{
MetaData = MetaData.Select(x=>(string)Convert.ChangeType(x,typeof(string))).ToArray(),
Value = Value
Expand All @@ -46,7 +46,7 @@ public ResultCubeRow(TO_ResultCubeRow transportObject, Type[] types)

public class ResultCube : ICube
{
private object _locker = new object();
private readonly object _locker = new();

private readonly Type[] _numericalTypes = { typeof(double) };
private List<ResultCubeRow> _rows;
Expand Down Expand Up @@ -128,7 +128,7 @@ public ResultCubeRow[] GetAllRows()
}

public TO_ResultCube ToTransportObject() =>
new TO_ResultCube
new()
{
FieldNames = _fieldNames,
Rows = _rows.Select(x=>x.ToTransportObject()).ToList(),
Expand Down
4 changes: 2 additions & 2 deletions src/Qwack.Core/Curves/BasicPriceCurve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class BasicPriceCurve : IPriceCurve
private readonly double[] _prices;
private readonly PriceCurveType _curveType;
private IInterpolator1D _interp;
private ICurrencyProvider _currencyProvider;
private readonly ICurrencyProvider _currencyProvider;

public bool UnderlyingsAreForwards => _curveType == PriceCurveType.LME;

Expand Down Expand Up @@ -191,7 +191,7 @@ public DateTime PillarDatesForLabel(string label)
}

public TO_BasicPriceCurve ToTransportObject() =>
new TO_BasicPriceCurve
new()
{
AssetId = AssetId,
BuildDate = BuildDate,
Expand Down
11 changes: 5 additions & 6 deletions src/Qwack.Core/Curves/CompositePriceCurve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ namespace Qwack.Core.Curves
{
public class CompositePriceCurve : IPriceCurve
{
private readonly Func<IFundingModel> fModelFunc;
private readonly Func<IPriceCurve> pCurveFunc;
private readonly IFundingModel _fModel;
private readonly IPriceCurve _pCurve;

public DateTime BuildDate { get; private set; }
public Currency CompoCurrency { get; }
public Currency CurveCurrency => pCurveFunc.Invoke().Currency;
Expand All @@ -28,10 +33,6 @@ public class CompositePriceCurve : IPriceCurve
public int NumberOfPillars => pCurveFunc.Invoke().NumberOfPillars;

public Currency Currency { get => CompoCurrency; set => throw new Exception("Cant set currency"); }

private Func<IFundingModel> fModelFunc;
private Func<IPriceCurve> pCurveFunc;

public PriceCurveType CurveType => PriceCurveType.Linear;

public CompositePriceCurve(DateTime buildDate, Func<IPriceCurve> priceCurve, Func<IFundingModel> fundingModel, Currency compoCurrency)
Expand All @@ -42,8 +43,6 @@ public CompositePriceCurve(DateTime buildDate, Func<IPriceCurve> priceCurve, Fun
CompoCurrency = compoCurrency;
}

IFundingModel _fModel;
IPriceCurve _pCurve;
public CompositePriceCurve(DateTime buildDate, IPriceCurve priceCurve, IFundingModel fundingModel, Currency compoCurrency)
{
BuildDate = buildDate;
Expand Down
2 changes: 1 addition & 1 deletion src/Qwack.Core/Curves/ContangoPriceCurve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Qwack.Core.Curves
public class ContangoPriceCurve : IPriceCurve
{
private IInterpolator1D _interp;
private ICurrencyProvider _currencyProvider;
private readonly ICurrencyProvider _currencyProvider;

public PriceCurveType CurveType => PriceCurveType.Linear;
public CommodityUnits Units { get; set; } = CommodityUnits.troyOunce;
Expand Down
3 changes: 1 addition & 2 deletions src/Qwack.Core/Curves/EquityPriceCurve.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Qwack.Math;
using Qwack.Math.Interpolation;
Expand All @@ -15,7 +14,7 @@ namespace Qwack.Core.Curves
public class EquityPriceCurve : IPriceCurve
{
private IInterpolator1D _interpDivYield;
private ICurrencyProvider _currencyProvider;
private readonly ICurrencyProvider _currencyProvider;

public PriceCurveType CurveType => PriceCurveType.Linear;
public CommodityUnits Units { get; set; } = CommodityUnits.Unspecified;
Expand Down
16 changes: 5 additions & 11 deletions src/Qwack.Core/Curves/FxForwardCurve.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Qwack.Math.Interpolation;
using Qwack.Dates;
Expand All @@ -12,25 +11,20 @@ namespace Qwack.Core.Curves
{
public class FxForwardCurve : IPriceCurve
{
private readonly IFundingModel _fModel;
private readonly Func<IFundingModel> fModelFunc;

public DateTime BuildDate { get; private set; }
public Currency DomesticCurrency { get; }
public Currency ForeignCurrency { get; }
public string Name { get; set; }
public CommodityUnits Units { get; set; }
public int NumberOfPillars => 0;

public PriceCurveType CurveType => PriceCurveType.Linear;

public Currency Currency { get => ForeignCurrency; set => throw new Exception(); }

public bool UnderlyingsAreForwards => true;

private Func<IFundingModel> fModelFunc;

public DateTime[] PillarDates => null;

public string AssetId => ForeignCurrency.Ccy;

public Frequency SpotLag { get; set; } = new Frequency("0b");
public Calendar SpotCalendar { get; set; }

Expand All @@ -42,7 +36,7 @@ public FxForwardCurve(DateTime buildDate, Func<IFundingModel> fundingModel, Curr
ForeignCurrency = foreignCurrency;
}

IFundingModel _fModel;

public FxForwardCurve(DateTime buildDate, IFundingModel fundingModel, Currency domesticCurrency, Currency foreignCurrency)
{
BuildDate = buildDate;
Expand All @@ -55,7 +49,7 @@ public FxForwardCurve(DateTime buildDate, IFundingModel fundingModel, Currency d
public double GetAveragePriceForDates(DateTime[] dates)
{
var model = fModelFunc.Invoke();
return dates.Select(date=>model.GetFxRate(date, DomesticCurrency, ForeignCurrency)).Average();
return dates.Select(date => model.GetFxRate(date, DomesticCurrency, ForeignCurrency)).Average();
}

public double GetPriceForDate(DateTime date)
Expand Down
6 changes: 3 additions & 3 deletions src/Qwack.Core/Curves/HazzardCurve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ namespace Qwack.Core.Curves
{
public class HazzardCurve
{
private readonly IInterpolator1D _hazzardCurve;
private double? _constPD = null;

public HazzardCurve(DateTime originDate, DayCountBasis basis, IInterpolator1D hazzardRateInterpolator)
{
OriginDate = originDate;
Expand All @@ -24,7 +27,6 @@ public HazzardCurve(TO_HazzardCurve transportObject):this(transportObject.Origin
ConstantPD = transportObject.ConstantPD.Value;
}

private double? _constPD = null;
public double ConstantPD
{
get
Expand Down Expand Up @@ -67,8 +69,6 @@ public double GetSurvivalProbabilitySlope(DateTime endDate)

public double GetDefaultProbability(DateTime startDate, DateTime endDate) => 1.0 - GetSurvivalProbability(startDate, endDate);

private IInterpolator1D _hazzardCurve;

public double RiskyDiscountFactor(DateTime startDate, DateTime endDate, IIrCurve discountCurve, double LGD)
{
var dp = GetDefaultProbability(startDate, endDate);
Expand Down
13 changes: 5 additions & 8 deletions src/Qwack.Core/Curves/IrCurve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Qwack.Core.Curves
{
public class IrCurve : IIrCurve
{
private DateTime _buildDate;
private readonly DateTime _buildDate;
private readonly DateTime[] _pillars;
private readonly double[] _rates;
private readonly DayCountBasis _basis = DayCountBasis.Act_365F;
Expand Down Expand Up @@ -49,11 +49,8 @@ public IrCurve(DateTime[] pillars, double[] rates, DateTime buildDate, string na
}

public IrCurve(TO_IrCurve transportObject, ICurrencyProvider currencyProvider)
:this(transportObject.Pillars, transportObject.Rates, transportObject.BuildDate, transportObject.Name, transportObject.InterpKind,
currencyProvider.GetCurrency(transportObject.Ccy), transportObject.CollateralSpec, transportObject.RateStorageType)
{
_basis = transportObject.Basis;
}
: this(transportObject.Pillars, transportObject.Rates, transportObject.BuildDate, transportObject.Name, transportObject.InterpKind,
currencyProvider.GetCurrency(transportObject.Ccy), transportObject.CollateralSpec, transportObject.RateStorageType) => _basis = transportObject.Basis;

public DateTime BuildDate => _buildDate;
public string Name => _name;
Expand Down Expand Up @@ -277,13 +274,13 @@ public IrCurve BumpRateFlat(double delta, bool mutate)
return o;
}

public IrCurve Clone() => new IrCurve((DateTime[])PillarDates.Clone(), (double[])_rates.Clone(), BuildDate, Name, _interpKind, Currency, CollateralSpec, RateStorageType)
public IrCurve Clone() => new((DateTime[])PillarDates.Clone(), (double[])_rates.Clone(), BuildDate, Name, _interpKind, Currency, CollateralSpec, RateStorageType)
{
SolveStage = SolveStage
};

public TO_IrCurve GetTransportObject() =>
new TO_IrCurve
new()
{
Basis = Basis,
BuildDate = BuildDate,
Expand Down
2 changes: 1 addition & 1 deletion src/Qwack.Core/Curves/SparsePriceCurve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class SparsePriceCurve : IPriceCurve
private readonly SparsePriceCurveType _curveType;
private IInterpolator1D _interpA;
private IInterpolator1D _interpB;
private ICurrencyProvider _currencyProvider;
private readonly ICurrencyProvider _currencyProvider;

public bool UnderlyingsAreForwards => _curveType == SparsePriceCurveType.Coal;

Expand Down
2 changes: 1 addition & 1 deletion src/Qwack.Core/Instruments/Asset/Future.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public IAssetInstrument SetStrike(double strike)
public FxConversionType FxType(IAssetFxModel model) => model.GetPriceCurve(AssetId).Currency == Currency ? FxConversionType.None : FxConversionType.ConvertThenAverage;
public string FxPair(IAssetFxModel model) => model.GetPriceCurve(AssetId).Currency == Currency ? string.Empty : $"{model.GetPriceCurve(AssetId).Currency}/{Currency}";

public Dictionary<string, List<DateTime>> PastFixingDates(DateTime valDate) => new Dictionary<string, List<DateTime>>();
public Dictionary<string, List<DateTime>> PastFixingDates(DateTime valDate) => new();

public DateTime LastSensitivityDate => ExpiryDate;

Expand Down
2 changes: 1 addition & 1 deletion src/Qwack.Core/Instruments/Cashflow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public CashFlow()
public FloatRateIndex RateIndex { get; set; }


public CashFlow Clone() => new CashFlow
public CashFlow Clone() => new()
{
AccrualPeriodStart = AccrualPeriodStart,
AccrualPeriodEnd = AccrualPeriodEnd,
Expand Down
35 changes: 17 additions & 18 deletions src/Qwack.Core/Instruments/CashflowSchedule.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using Qwack.Core.Basic;
using Qwack.Core.Basic;
using Qwack.Core.Curves;
using Qwack.Core.Models;
using Qwack.Dates;
using Qwack.Transport.BasicTypes;

namespace Qwack.Core.Instruments
{
public class CashFlowSchedule
{
public List<CashFlow> Flows { get; set; }
public DayCountBasis DayCountBasis { get; set; }
public ResetType ResetType { get; set; }
public AverageType AverageType { get; set; }

public CashFlowSchedule Clone() => new CashFlowSchedule
namespace Qwack.Core.Instruments
{
public class CashFlowSchedule
{
public List<CashFlow> Flows { get; set; }
public DayCountBasis DayCountBasis { get; set; }
public ResetType ResetType { get; set; }
public AverageType AverageType { get; set; }

public CashFlowSchedule Clone() => new()
{
Flows = new List<CashFlow>(Flows.Select(x => x.Clone())),
DayCountBasis = DayCountBasis,
ResetType = ResetType,
AverageType = AverageType
};
}

};
}

public static class CashFlowScheduleEx
{
public static double PV(this CashFlowSchedule schedule, IrCurve discountCurve, IrCurve forecastCurve, bool updateState, bool updateDf, bool updateEstimate, DayCountBasis basisFloat, DateTime? filterDate)
Expand Down Expand Up @@ -186,5 +185,5 @@ public static double PV(this CashFlowSchedule schedule, Currency reportingCCy, I

return totalPv;
}
}
}
}
}

0 comments on commit e7414d4

Please sign in to comment.