Skip to content

Commit 293d147

Browse files
committed
Cleanup
1 parent 565cc50 commit 293d147

File tree

11 files changed

+74
-59
lines changed

11 files changed

+74
-59
lines changed

Libs/Estimator.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<PropertyGroup>
88
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
9-
<Version>1.0.6</Version>
9+
<Version>1.0.8</Version>
1010
<Description>Statistics and performance metrics in trading, CAGR, Sharpe, MAE, MFE, and others.</Description>
1111
<Authors>artemiusgreat</Authors>
1212
<Copyright>indemos.com</Copyright>

Libs/Estimators/CAGR/CAGR.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ public class CAGR
2020
/// <summary>
2121
/// Calculate
2222
/// </summary>
23-
public virtual double Calculate() => Math.Pow(Items.Sum(o => o.Value), 1.0 / Count) - 1.0;
23+
public virtual double Calculate()
24+
{
25+
if (Count is 0)
26+
{
27+
return 0;
28+
}
29+
30+
return Math.Pow(Items.Sum(o => o.Value), 1.0 / Count) - 1.0;
31+
}
2432
}
2533
}

Libs/Estimators/Edge/EdgeRatio.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Estimator.Models;
2-
using System;
32
using System.Collections.Generic;
43

54
namespace Estimator.Estimators
@@ -19,6 +18,11 @@ public virtual double Calculate()
1918
var averageGain = new MFE { Items = Items }.Calculate();
2019
var averageLoss = new MAE { Items = Items }.Calculate();
2120

21+
if (averageLoss is 0)
22+
{
23+
return averageGain;
24+
}
25+
2226
return averageGain / averageLoss;
2327
}
2428
}

Libs/Estimators/Kestner/KestnerRatio.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public virtual double Calculate()
2020
var slope = regression.Covariance / regression.DevY;
2121
var error = Math.Sqrt(regression.DevY / (Items.Count - 1));
2222

23-
if (error == 0)
23+
if (error is 0)
2424
{
25-
error = 1.0;
25+
return slope;
2626
}
2727

2828
return slope / error;

Libs/Estimators/MAE/MAE.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Estimator.Models;
2-
using System;
32
using System.Collections.Generic;
43
using System.Linq;
54

Libs/Estimators/MFE/MFE.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Estimator.Models;
2-
using System;
32
using System.Collections.Generic;
43
using System.Linq;
54

Libs/Estimators/PF/PF.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public virtual double Calculate()
2020
var gains = Items.Where(o => o.Value > 0).Sum(o => o.Value);
2121
var losses = Items.Where(o => o.Value < 0).Sum(o => o.Value);
2222

23-
if (losses == 0)
23+
if (losses is 0)
2424
{
25-
gains = 1.0;
25+
return gains;
2626
}
2727

2828
return Math.Abs(gains / losses);

Libs/Estimators/RAR/RAR.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public class RAR
4040
/// </summary>
4141
public virtual double Calculate()
4242
{
43+
if (Items.Count is 0)
44+
{
45+
return 0;
46+
}
47+
4348
var mean = Items.Average(o => o.Value);
4449
var denominator = 0.0;
4550

@@ -64,7 +69,7 @@ public virtual double Calculate()
6469
case ModeEnum.Sterling: denominator = Math.Abs(denominator / Items.Count); break;
6570
}
6671

67-
if (denominator == 0)
72+
if (denominator is 0)
6873
{
6974
denominator = 1.0;
7075
}

Libs/Estimators/Regression/Regression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public virtual RegressionResponse Calculate()
6565

6666
var deviation = devX * devY;
6767

68-
if (deviation == 0)
68+
if (deviation is 0)
6969
{
7070
deviation = 1.0;
7171
}

Libs/Estimators/StandardScore/StandardScore.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,21 @@ public virtual double Calculate()
3737
}
3838

3939
var sum = gains + losses;
40-
var product = 2 * gains * losses;
40+
var product = 2.0 * gains * losses;
4141
var seriesCount = seriesGains + seriesLosses;
42-
var mean = product / sum + 1;
43-
var deviation = Math.Sqrt(product * (product - sum) / ((sum - 1.0) * sum * sum));
42+
var mean = product / sum + 1.0;
43+
var denominator = (sum - 1.0) * sum * sum;
4444

45-
if (deviation == 0)
45+
if (denominator is 0)
4646
{
47-
deviation = 1.0;
47+
return 0;
48+
}
49+
50+
var deviation = Math.Sqrt(product * (product - sum) / denominator);
51+
52+
if (deviation is 0)
53+
{
54+
return 0;
4855
}
4956

5057
return (seriesCount - mean - 0.5) / deviation;

0 commit comments

Comments
 (0)