Skip to content

Commit

Permalink
Add files via upload (#126)
Browse files Browse the repository at this point in the history
* Add files via upload

* Update STIRFacts.cs

* Update BenchmarkRiskFacts.cs
  • Loading branch information
gavbrennan committed Mar 13, 2020
1 parent ba19955 commit dbc08c6
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 50 deletions.
2 changes: 1 addition & 1 deletion clients/Qwack.Excel/Instruments/InstrumentFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ public class InstrumentFunctions
Direction = TradeDirection.Long,
ExpiryDate = ExpiryDate,
PaymentCurrency = currency,
FxConversionType = currency.Ccy != "USD" ? FxConversionType.ConvertThenAverage : FxConversionType.None,
FxConversionType = currency.Ccy != "USD" & !AssetId.EndsWith(currency.Ccy) ? FxConversionType.ConvertThenAverage : FxConversionType.None,
PaymentDate = PaymentOffsetOrDate is double pdd ? DateTime.FromOADate(pdd) : ExpiryDate.AddPeriod(RollType.F, pCal, new Frequency(paymentOffset)),
TradeId = ObjectName,
DiscountCurve = DiscountCurve,
Expand Down
83 changes: 42 additions & 41 deletions src/Qwack.Core/Cubes/CubeEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public static ICube Difference(this ICube baseCube, ICube cubeToSubtract)
o.Initialize(baseCube.DataTypes);
var baseRows = baseCube.GetAllRows().ToList();
var subRows = cubeToSubtract.GetAllRows().ToList();
foreach (var br in baseRows)
foreach(var br in baseRows)
{
var rowFound = false;
foreach (var sr in subRows)
foreach(var sr in subRows)
{
if (Enumerable.SequenceEqual(br.MetaData, sr.MetaData))
if(Enumerable.SequenceEqual(br.MetaData,sr.MetaData))
{
o.AddRow(br.MetaData, br.Value - sr.Value);
subRows.Remove(sr);
Expand All @@ -33,7 +33,7 @@ public static ICube Difference(this ICube baseCube, ICube cubeToSubtract)
}
}

if (!rowFound) //zero to subtract
if(!rowFound) //zero to subtract
{
o.AddRow(br.MetaData, br.Value);
}
Expand Down Expand Up @@ -65,10 +65,10 @@ public static ICube QuickDifference(this ICube baseCube, ICube cubeToSubtract)
var baseRows = baseCube.GetAllRows().ToArray();
var subRows = cubeToSubtract.GetAllRows().ToArray();

if (baseRows.Length != subRows.Length)
if(baseRows.Length!=subRows.Length)
throw new Exception("Cubes must have same number of rows to quick-diff them");

for (var i = 0; i < baseRows.Length; i++)
for(var i=0;i<baseRows.Length;i++)
{
var br = baseRows[i];
var sr = subRows[i];
Expand All @@ -95,7 +95,7 @@ public static ICube Pivot(this ICube cube, string[] fieldsToAggregateBy, Aggrega
var ixs = fieldsToAggregateBy.Select(f => types.IndexOf(f)).ToArray();

var rows = cube.GetAllRows();
var distinctValues = rows.Select(x => string.Join("~", ixs.Select(ix => x.MetaData[ix].ToString()))).Distinct();
var distinctValues = rows.Select(x => string.Join("~", ixs.Select(ix => x.MetaData[ix]?.ToString()??string.Empty))).Distinct();

var outCube = new ResultCube();
var oT = new Dictionary<string, Type>();
Expand All @@ -107,7 +107,7 @@ public static ICube Pivot(this ICube cube, string[] fieldsToAggregateBy, Aggrega
var metaDict = new Dictionary<string, object[]>();
foreach (var row in rows)
{
var rowKey = string.Join("~", ixs.Select(i => row.MetaData[i].ToString()));
var rowKey = string.Join("~", ixs.Select(i => row.MetaData[i]?.ToString()??string.Empty));
if (!aggData.ContainsKey(rowKey))
{
if (aggregationAction == AggregationAction.Min)
Expand Down Expand Up @@ -155,7 +155,7 @@ public static ICube Pivot(this ICube cube, string[] fieldsToAggregateBy, Aggrega
return outCube;
}

public static ICube Filter(this ICube cube, Dictionary<string, object> fieldsToFilterOn, bool filterOut = false)
public static ICube Filter(this ICube cube, Dictionary<string, object> fieldsToFilterOn, bool filterOut=false)
{
foreach (var fieldToFilterOn in fieldsToFilterOn.Keys)
if (!cube.DataTypes.ContainsKey(fieldToFilterOn))
Expand Down Expand Up @@ -188,13 +188,14 @@ public static ICube Filter(this ICube cube, Dictionary<string, object> fieldsToF
return outCube;
}

public static ICube BucketTimeAxis(this ICube cube, string timeFieldName, string bucketedFieldName, Dictionary<DateTime, string> bucketBoundaries)
public static ICube BucketTimeAxis(this ICube cube, string timeFieldName, string bucketedFieldName, Dictionary<DateTime,string> bucketBoundaries)
{
if (!cube.DataTypes.ContainsKey(timeFieldName))
throw new Exception($"Cannot filter on field {timeFieldName} as it is not present");

var outCube = new ResultCube();
var newTypes = new Dictionary<string, Type>(cube.DataTypes) { { bucketedFieldName, typeof(string) } };
var newTypes = new Dictionary<string, Type>(cube.DataTypes);
newTypes.Add(bucketedFieldName, typeof(string));
outCube.Initialize(newTypes);

var buckets = bucketBoundaries.Keys.OrderBy(x => x).ToList();
Expand All @@ -204,7 +205,7 @@ public static ICube BucketTimeAxis(this ICube cube, string timeFieldName, string
{
var date = (DateTime)row.MetaData[bucketFieldIx];
var bucket = buckets.BinarySearch(date);
if (bucket < 0) bucket = ~bucket;
if(bucket<0) bucket = ~bucket;
var bucketLabel = bucketBoundaries[buckets[bucket]];

var metaList = new List<object>(row.MetaData)
Expand All @@ -217,10 +218,10 @@ public static ICube BucketTimeAxis(this ICube cube, string timeFieldName, string
return outCube;
}

public static Dictionary<object, List<ResultCubeRow>> ToDictionary(this ICube cube, string keyField)
public static Dictionary<object,List<ResultCubeRow>> ToDictionary(this ICube cube, string keyField)
{
if (!cube.DataTypes.ContainsKey(keyField))
throw new Exception($"Cannot filter on field {keyField} as it is not present");
if (!cube.DataTypes.ContainsKey(keyField))
throw new Exception($"Cannot filter on field {keyField} as it is not present");

var output = new Dictionary<object, List<ResultCubeRow>>();

Expand All @@ -238,9 +239,9 @@ public static ICube BucketTimeAxis(this ICube cube, string timeFieldName, string
return output;
}

public static ICube Filter(this ICube cube, List<KeyValuePair<string, object>> fieldsToFilterOn, bool filterOut = false)
public static ICube Filter(this ICube cube, List<KeyValuePair<string, object>> fieldsToFilterOn, bool filterOut=false)
{
foreach (var fieldToFilterOn in fieldsToFilterOn.Select(x => x.Key))
foreach (var fieldToFilterOn in fieldsToFilterOn.Select(x=>x.Key))
if (!cube.DataTypes.ContainsKey(fieldToFilterOn))
throw new Exception($"Cannot filter on field {fieldToFilterOn} as it is not present");

Expand All @@ -253,7 +254,7 @@ public static ICube Filter(this ICube cube, List<KeyValuePair<string, object>> f
.Distinct()
.ToDictionary(x => x, x => fieldNames.IndexOf(x));
var values = new Dictionary<string, List<object>>();
foreach (var kv in fieldsToFilterOn)
foreach(var kv in fieldsToFilterOn)
{
if (!values.ContainsKey(kv.Key))
values[kv.Key] = new List<object> { kv.Value };
Expand All @@ -266,7 +267,7 @@ public static ICube Filter(this ICube cube, List<KeyValuePair<string, object>> f
var rowIsRelevant = true;
foreach (var kv in values)
{
if (!kv.Value.Any(v => IsEqual(row.MetaData[indexes[kv.Key]], v)))
if (!kv.Value.Any(v=>IsEqual(row.MetaData[indexes[kv.Key]], v)))
{
rowIsRelevant = false;
break;
Expand Down Expand Up @@ -316,7 +317,7 @@ public static ICube Sort(this ICube cube)
var fieldNames = cube.DataTypes.Keys.ToList();
var indexes = Enumerable.Range(0, fieldNames.Count).Reverse().ToArray();
var rows = new List<ResultCubeRow>(cube.GetAllRows());
foreach (var ix in indexes)
foreach(var ix in indexes)
{
rows = rows.OrderBy(x => x.MetaData[ix]).ToList();
}
Expand Down Expand Up @@ -404,7 +405,7 @@ public static ICube Merge(this ICube baseCube, ICube otherCube)
var row = new object[cleanRow.Length];
Array.Copy(cleanRow, row, row.Length);
var br = baseRows[i];
for (var j = 0; j < baseIx.Length; j++)
for(var j=0;j<baseIx.Length;j++)
{
row[baseIx[j]] = br.MetaData[j];
}
Expand All @@ -429,7 +430,7 @@ public static ICube Merge(this ICube baseCube, ICube otherCube)

public static T[] KeysForField<T>(this ICube cube, string fieldName)
{
if (!cube.DataTypes.ContainsKey(fieldName))
if(!cube.DataTypes.ContainsKey(fieldName))
throw new Exception($"Cubes does contain field {fieldName}");
if (cube.DataTypes[fieldName] != typeof(T))
throw new Exception($"Field type does not match");
Expand Down Expand Up @@ -472,10 +473,10 @@ public static T[] KeysForField<T>(this ICube cube, string fieldName)
distinctV = distinctV.OrderBy(x => x).ToArray();
}

var o = new object[distinctV.Length + 1, distinctH.Length + 1];
for (var r = 0; r < o.GetLength(0); r++)
var o = new object[distinctV.Length+1, distinctH.Length+1];
for(var r=0;r<o.GetLength(0);r++)
{
for (var c = 0; c < o.GetLength(1); c++)
for(var c=0;c<o.GetLength(1);c++)
{
if (r == 0 && c == 0)
continue;
Expand All @@ -493,7 +494,7 @@ public static T[] KeysForField<T>(this ICube cube, string fieldName)
{ verticalField, distinctV[r - 1] },
{ horizontalField, distinctH[c - 1] }
}).GetAllRows();
if (data.Any())
if(data.Any())
{
o[r, c] = data.First().Value;
}
Expand All @@ -503,7 +504,7 @@ public static T[] KeysForField<T>(this ICube cube, string fieldName)
return o;
}

public static ICube Merge(this ICube baseCube, ICube otherCube, Dictionary<string, object> fieldsToAdd, Dictionary<string, object> fieldsToOverride = null, bool mergeTypes = false)
public static ICube Merge(this ICube baseCube, ICube otherCube, Dictionary<string,object> fieldsToAdd, Dictionary<string, object> fieldsToOverride = null, bool mergeTypes = false)
{
var o = new ResultCube();

Expand Down Expand Up @@ -538,15 +539,15 @@ public static ICube Merge(this ICube baseCube, ICube otherCube, Dictionary<strin
{
var br = otherRows[i];
var rowDict = new Dictionary<string, object>();
for (var j = 0; j < br.MetaData.Length; j++)
for(var j=0;j<br.MetaData.Length;j++)
{
rowDict.Add(otherFieldNames[j], br.MetaData[j]);
}
foreach (var fa in fieldsToAdd)
foreach(var fa in fieldsToAdd)
{
rowDict[fa.Key] = fa.Value;
}
if (fieldsToOverride != null)
if(fieldsToOverride!=null)
{
foreach (var fa in fieldsToOverride)
{
Expand All @@ -564,19 +565,19 @@ public static bool IsEqual(object v1, object v2)
switch (v1)
{
case string vs:
return v2 is string && vs == (string)v2;
return v2 is string && (string)v1 == (string)v2;
case double vd:
return v2 is double && vd == (double)v2;
return v2 is double && (double)v1 == (double)v2;
case bool vb:
return v2 is bool && vb == (bool)v2;
return v2 is bool && (bool)v1 == (bool)v2;
case decimal vq:
return v2 is decimal && vq == (decimal)v2;
return v2 is decimal && (decimal)v1 == (decimal)v2;
case DateTime vt:
return v2 is DateTime && vt == (DateTime)v2;
return v2 is DateTime && (DateTime)v1 == (DateTime)v2;
case char vc:
return v2 is char && vc == (char)v2;
return v2 is char && (char)v1 == (char)v2;
case int vi:
return v2 is int && vi == (int)v2;
return v2 is int && (int)v1 == (int)v2;
}
return false;
}
Expand All @@ -586,14 +587,14 @@ public static object[] KeysForField(this ICube cube, string fieldName)
if (!cube.DataTypes.ContainsKey(fieldName))
throw new Exception($"Cubes does contain field {fieldName}");

switch (Type.GetTypeCode(cube.DataTypes[fieldName]))
switch(Type.GetTypeCode(cube.DataTypes[fieldName]))
{
case TypeCode.String:
return cube.KeysForField<string>(fieldName);
case TypeCode.Double:
return cube.KeysForField<double>(fieldName).Select(x => (object)x).ToArray();
case TypeCode.DateTime:
return cube.KeysForField<DateTime>(fieldName).Select(x => (object)x).ToArray();
return cube.KeysForField<DateTime>(fieldName).Select(x=>(object)x).ToArray();
case TypeCode.Int32:
return cube.KeysForField<int>(fieldName).Select(x => (object)x).ToArray();
case TypeCode.Boolean:
Expand All @@ -615,7 +616,7 @@ public static void ToCSVFile(this ICube cube, string fileName)

foreach (var row in cube.GetAllRows())
{
output.Add(string.Join(",",
output.Add(string.Join(",",
row.MetaData.Select(x => Convert.ToString(x))
.Concat(new[] { Convert.ToString(row.Value) })));
}
Expand Down Expand Up @@ -644,7 +645,7 @@ public static ICube FromCSVFile(string fileName)
{
var rawRow = rawData[i].Split(',');
var rowMeta = new object[rawRow.Length - 1];
for (var j = 0; j < rowMeta.Length; j++)
for(var j=0;j<rowMeta.Length;j++)
{
rowMeta[j] = Convert.ChangeType(rawRow[j], fieldTypes[j]);
}
Expand Down
19 changes: 17 additions & 2 deletions src/Qwack.Core/Instruments/Funding/STIRFuture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Qwack.Core.Instruments.Funding
{
public class STIRFuture : IFundingInstrument
public class STIRFuture : IFundingInstrument, IAssetInstrument
{
public double Price { get; set; } = 100.0;
public double ContractSize { get; set; } = 1.0;
Expand All @@ -29,10 +29,14 @@ public class STIRFuture : IFundingInstrument

public double UnitPV01 => ContractSize * DCF * 0.0001;

public string[] AssetIds => Array.Empty<string>();

public Currency PaymentCurrency => Currency;

public double Pv(IFundingModel Model, bool updateState)
{
var fairPrice = CalculateParRate(Model);
var PV = (Price - fairPrice) / 100.0 * Position * ContractSize * DCF;
var PV = (fairPrice - Price) / 100.0 * Position * ContractSize * DCF;
return PV;
}

Expand Down Expand Up @@ -100,5 +104,16 @@ public IFundingInstrument SetParRate(double parRate)
}

public List<CashFlow> ExpectedCashFlows(IAssetFxModel model) => new List<CashFlow>();

public string[] IrCurves(IAssetFxModel model) => new[] { ForecastCurve };
public Dictionary<string, List<DateTime>> PastFixingDates(DateTime valDate) => new Dictionary<string, List<DateTime>>();
public FxConversionType FxType(IAssetFxModel model) => FxConversionType.None;
public string FxPair(IAssetFxModel model) => null;
IAssetInstrument IAssetInstrument.Clone() => (STIRFuture)Clone();

public IAssetInstrument SetStrike(double strike)
{
throw new NotImplementedException();
}
}
}
2 changes: 1 addition & 1 deletion src/Qwack.Core/Instruments/Portfolio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ public static double Notional(this IInstrument ins)

public static double SaCcrAddon(this Portfolio pf, IAssetFxModel model, Currency reportingCcy, Dictionary<string, string> AssetIdToHedgingSetMap)
{
if (!pf.Instruments.All(x => x is ISaCcrEnabled))
if (!pf.Instruments.All(x => x is ISaCcrEnabled || (x is CashWrapper cw && cw.UnderlyingInstrument is ISaCcrEnabled)))
throw new Exception("Portfolio contains non-SACCR enabled instruments");

var assetIds = pf.AssetIds();
Expand Down
Loading

0 comments on commit dbc08c6

Please sign in to comment.