Skip to content

Commit

Permalink
massive refactoring, rem old projs
Browse files Browse the repository at this point in the history
  • Loading branch information
dv-lebedev committed Jul 30, 2023
1 parent 536e37e commit b8005fd
Show file tree
Hide file tree
Showing 61 changed files with 941 additions and 142 deletions.
Binary file not shown.
Binary file modified .vs/PairTradingView/v17/.suo
Binary file not shown.
Binary file modified .vs/pair-trading-view/v17/.wsuo
Binary file not shown.
Binary file not shown.
10 changes: 5 additions & 5 deletions PairTradingView.Infrastructure/FinancialPair.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ You may obtain a copy of the License at
limitations under the License.
*/

using Statistics;
using Statistics.Models;
using PairTradingView.Shared.Statistics;
using PairTradingView.Shared.Statistics.Models;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -53,7 +53,7 @@ public FinancialPair(Stock x, Stock y)
protected void SetRegression(double[] x, double[] y)
{
Regression = new LinearRegression();
Regression.Compute(y.ToDecimal(), x.ToDecimal());
Regression.Compute(y, x);
}

protected void SetValues()
Expand All @@ -63,11 +63,11 @@ protected void SetValues()

if (Regression.RValue >= 0)
{
DeltaValues = x.Zip(y, (i, j) => j - i * Regression.Beta.ToDouble()).ToArray();
DeltaValues = x.Zip(y, (i, j) => j - i * Regression.Beta).ToArray();
}
else
{
DeltaValues = x.Zip(y, (i, j) => j + i * Regression.Beta.ToDouble()).ToArray();
DeltaValues = x.Zip(y, (i, j) => j + i * Regression.Beta).ToArray();
}
}

Expand Down
17 changes: 12 additions & 5 deletions PairTradingView.Infrastructure/PairTradingView.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Statistics">
<HintPath>..\..\statistics\Statistics\bin\Debug\Statistics.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand All @@ -51,8 +48,18 @@
<Compile Include="FinancialPair.cs" />
<Compile Include="ILogger.cs" />
<Compile Include="Logger.cs" />
<Compile Include="MathUtils.cs" />
<Compile Include="MovingAverages.cs" />
<Compile Include="Statistics\DifferentLengthException.cs" />
<Compile Include="Statistics\IRegression.cs" />
<Compile Include="Statistics\IRegressionMethod.cs" />
<Compile Include="Statistics\MathUtils.cs" />
<Compile Include="Statistics\Matrix.cs" />
<Compile Include="Statistics\Methods\BasicRegression.cs" />
<Compile Include="Statistics\Methods\OrdinaryLeastSquares.cs" />
<Compile Include="Statistics\Models\AutoRegression.cs" />
<Compile Include="Statistics\Models\LinearRegression.cs" />
<Compile Include="Statistics\Models\LogitRegression.cs" />
<Compile Include="Statistics\Models\MultipleLinearRegression.cs" />
<Compile Include="Statistics\MovingAverages.cs" />
<Compile Include="RiskManager.cs" />
<Compile Include="Stock.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
9 changes: 4 additions & 5 deletions PairTradingView.Infrastructure/RiskManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ You may obtain a copy of the License at
limitations under the License.
*/

using Statistics;
using Statistics.Models;
using PairTradingView.Shared.Statistics.Models;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -76,9 +75,9 @@ public void Calculate()
foreach (var pair in pairs)
{
var regression = new LinearRegression();
regression.Compute(pair.DeltaValues.ToDecimal(), synthIndex.ToDecimal());
regression.Compute(pair.DeltaValues, synthIndex);

pair.Weight = 1 / (1 + Math.Abs(regression.Beta.ToDouble()));
pair.Weight = 1 / (1 + Math.Abs(regression.Beta));

summary += pair.Weight;
}
Expand All @@ -91,7 +90,7 @@ public void Calculate()

foreach (var pair in pairs)
{
double beta = pair.Regression.Beta.ToDouble();
double beta = pair.Regression.Beta;
double weight = 1.0 / (1.0 + Math.Abs(beta));

pair.X.TradeVolume = pair.TradeVolume * (weight * Math.Abs(beta));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Copyright(c) 2023 Denis Lebedev
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

using System;

namespace PairTradingView.Shared.Statistics
{
[Serializable]
public class DifferentLengthException : Exception
{
}
}
23 changes: 23 additions & 0 deletions PairTradingView.Infrastructure/Statistics/IRegression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright(c) 2023 Denis Lebedev
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

namespace PairTradingView.Shared.Statistics
{
public interface IRegression
{
IRegressionMethod RegressionMethod { get; }
}
}
27 changes: 27 additions & 0 deletions PairTradingView.Infrastructure/Statistics/IRegressionMethod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright(c) 2023 Denis Lebedev
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

namespace PairTradingView.Shared.Statistics
{
public interface IRegressionMethod
{
double[] Coefs { get; }
double[] RSquaredValues { get; }
double[] RValues { get; }

void Compute(double[] y, params double[][] xn);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,63 @@ You may obtain a copy of the License at
limitations under the License.
*/


using System;
using System.Linq;

namespace PairTradingView.Shared
namespace PairTradingView.Shared.Statistics
{
public static class MathUtils
{
public static double[] GetError(double[] values)
{
double[] result = new double[values.Length - 1];

for (int i = 1; i < values.Length; i++)
{
result[i - 1] = values[i] - values[i - 1];
}

return result;
}

public static double MultiplyArrays(double[] x, double[] y)
{
if (x == null) throw new ArgumentNullException("x");
if (y == null) throw new ArgumentNullException("y");

if (x.Length != y.Length)
throw new DifferentLengthException();

double result = 0;

for (int i = 0; i < x.Length; i++)
{
result += x[i] * y[i];
}

return result;
}

public static double Pow(double value, double power)
{
return (double)Math.Pow((double)value, power);
}

public static double Pow(double[] values, double power)
{
if (values == null) throw new ArgumentNullException("values");

double result = 0;

for (int i = 0; i < values.Length; i++)
{
result += Math.Pow(values[i], power);
}

return result;
}

public static double GetStandardDeviation(double[] values)
{
if (values == null) throw new ArgumentNullException("values");
Expand Down
Loading

0 comments on commit b8005fd

Please sign in to comment.