Skip to content

Commit

Permalink
Fix calculation of some functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hmG3 committed Dec 19, 2020
1 parent e8bebeb commit 3c9d20c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 57 deletions.
24 changes: 11 additions & 13 deletions src/TALib.NETCore/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,20 @@ public int Lookback(params int[] options)
var optInParameters = method.GetParameters().Where(pi => pi.Name.StartsWith(OptInPrefix)).ToList();
var paramsArray = new object[optInParameters.Count];
Array.Fill(paramsArray, Type.Missing);
if (options.Length == optInParameters.Count)

var defOptInParameters = Options.Select(NormalizeOptionalParameter).ToList();
for (int i = 0, paramsArrayIndex = 0; i < defOptInParameters.Count; i++)
{
var defOptInParameters = Options.Select(NormalizeOptionalParameter).ToList();
for (int i = 0, paramsArrayIndex = 0; i < defOptInParameters.Count; i++)
var optInParameter = optInParameters.SingleOrDefault(p => p.Name == defOptInParameters[i]);
if (optInParameter != null)
{
var optInParameter = optInParameters.SingleOrDefault(p => p.Name == defOptInParameters[i]);
if (optInParameter != null)
if (optInParameter.ParameterType.IsEnum && optInParameter.ParameterType.IsEnumDefined(options[i]))
{
if (optInParameter.ParameterType.IsEnum && optInParameter.ParameterType.IsEnumDefined(options[i]))
{
paramsArray[paramsArrayIndex++] = Enum.ToObject(optInParameter.ParameterType, options[i]);
}
else
{
paramsArray[paramsArrayIndex++] = options[i];
}
paramsArray[paramsArrayIndex++] = Enum.ToObject(optInParameter.ParameterType, options[i]);
}
else
{
paramsArray[paramsArrayIndex++] = options[i];
}
}
}
Expand Down
30 changes: 8 additions & 22 deletions src/TALib.NETCore/TACore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,9 @@ private static RetCode TA_INT_MACD(double[] inReal, int startIdx, int endIdx, do
{
outBegIdx = outNbElement = 0;

int tempInteger;
if (optInSlowPeriod < optInFastPeriod)
{
tempInteger = optInSlowPeriod;
optInSlowPeriod = optInFastPeriod;
optInFastPeriod = tempInteger;
(optInSlowPeriod, optInFastPeriod) = (optInFastPeriod, optInSlowPeriod);
}

double k1;
Expand Down Expand Up @@ -270,7 +267,7 @@ private static RetCode TA_INT_MACD(double[] inReal, int startIdx, int endIdx, do
return RetCode.Success;
}

tempInteger = endIdx - startIdx + 1 + lookbackSignal;
var tempInteger = endIdx - startIdx + 1 + lookbackSignal;
var fastEMABuffer = new double[tempInteger];
var slowEMABuffer = new double[tempInteger];

Expand Down Expand Up @@ -324,12 +321,9 @@ private static RetCode TA_INT_MACD(decimal[] inReal, int startIdx, int endIdx, d
{
outBegIdx = outNbElement = 0;

int tempInteger;
if (optInSlowPeriod < optInFastPeriod)
{
tempInteger = optInSlowPeriod;
optInSlowPeriod = optInFastPeriod;
optInFastPeriod = tempInteger;
(optInSlowPeriod, optInFastPeriod) = (optInFastPeriod, optInSlowPeriod);
}

decimal k1;
Expand Down Expand Up @@ -366,7 +360,7 @@ private static RetCode TA_INT_MACD(decimal[] inReal, int startIdx, int endIdx, d
return RetCode.Success;
}

tempInteger = endIdx - startIdx + 1 + lookbackSignal;
var tempInteger = endIdx - startIdx + 1 + lookbackSignal;
var fastEMABuffer = new decimal[tempInteger];
var slowEMABuffer = new decimal[tempInteger];

Expand Down Expand Up @@ -421,12 +415,9 @@ private static RetCode TA_INT_PO(double[] inReal, int startIdx, int endIdx, doub
{
outBegIdx = outNbElement = 0;

int tempInteger;
if (optInSlowPeriod < optInFastPeriod)
{
tempInteger = optInSlowPeriod;
optInSlowPeriod = optInFastPeriod;
optInFastPeriod = tempInteger;
(optInSlowPeriod, optInFastPeriod) = (optInFastPeriod, optInSlowPeriod);
}

RetCode retCode = Ma(inReal, startIdx, endIdx, tempBuffer, out var outBegIdx2, out _, optInMethod, optInFastPeriod);
Expand All @@ -441,8 +432,7 @@ private static RetCode TA_INT_PO(double[] inReal, int startIdx, int endIdx, doub
return retCode;
}

tempInteger = outBegIdx1 - outBegIdx2;
for (int i = 0, j = tempInteger; i < outNbElement1; i++, j++)
for (int i = 0, j = outBegIdx1 - outBegIdx2; i < outNbElement1; i++, j++)
{
if (doPercentageOutput)
{
Expand All @@ -467,12 +457,9 @@ private static RetCode TA_INT_PO(decimal[] inReal, int startIdx, int endIdx, dec
{
outBegIdx = outNbElement = 0;

int tempInteger;
if (optInSlowPeriod < optInFastPeriod)
{
tempInteger = optInSlowPeriod;
optInSlowPeriod = optInFastPeriod;
optInFastPeriod = tempInteger;
(optInSlowPeriod, optInFastPeriod) = (optInFastPeriod, optInSlowPeriod);
}

RetCode retCode = Ma(inReal, startIdx, endIdx, tempBuffer, out var outBegIdx2, out _, optInMethod, optInFastPeriod);
Expand All @@ -487,8 +474,7 @@ private static RetCode TA_INT_PO(decimal[] inReal, int startIdx, int endIdx, dec
return retCode;
}

tempInteger = outBegIdx1 - outBegIdx2;
for (int i = 0, j = tempInteger; i < outNbElement1; i++, j++)
for (int i = 0, j = outBegIdx1 - outBegIdx2; i < outNbElement1; i++, j++)
{
if (doPercentageOutput)
{
Expand Down
24 changes: 6 additions & 18 deletions src/TALib.NETCore/TAFunc/TA_MacdExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,10 @@ public static RetCode MacdExt(double[] inReal, int startIdx, int endIdx, double[
return RetCode.BadParam;
}

int tempInteger;
if (optInSlowPeriod < optInFastPeriod)
{
tempInteger = optInSlowPeriod;
optInSlowPeriod = optInFastPeriod;
optInFastPeriod = tempInteger;

MAType tempMAType = optInSlowMAType;
optInSlowMAType = optInFastMAType;
optInFastMAType = tempMAType;
(optInSlowPeriod, optInFastPeriod) = (optInFastPeriod, optInSlowPeriod);
(optInSlowMAType, optInFastMAType) = (optInFastMAType, optInSlowMAType);
}

int lookbackSignal = MaLookback(optInSignalMAType, optInSignalPeriod);
Expand All @@ -48,7 +42,7 @@ public static RetCode MacdExt(double[] inReal, int startIdx, int endIdx, double[
return RetCode.Success;
}

tempInteger = endIdx - startIdx + 1 + lookbackSignal;
var tempInteger = endIdx - startIdx + 1 + lookbackSignal;
var fastMABuffer = new double[tempInteger];
var slowMABuffer = new double[tempInteger];

Expand Down Expand Up @@ -115,16 +109,10 @@ public static RetCode MacdExt(decimal[] inReal, int startIdx, int endIdx, decima
return RetCode.BadParam;
}

int tempInteger;
if (optInSlowPeriod < optInFastPeriod)
{
tempInteger = optInSlowPeriod;
optInSlowPeriod = optInFastPeriod;
optInFastPeriod = tempInteger;

MAType tempMAType = optInSlowMAType;
optInSlowMAType = optInFastMAType;
optInFastMAType = tempMAType;
(optInSlowPeriod, optInFastPeriod) = (optInFastPeriod, optInSlowPeriod);
(optInSlowMAType, optInFastMAType) = (optInFastMAType, optInSlowMAType);
}

int lookbackSignal = MaLookback(optInSignalMAType, optInSignalPeriod);
Expand All @@ -140,7 +128,7 @@ public static RetCode MacdExt(decimal[] inReal, int startIdx, int endIdx, decima
return RetCode.Success;
}

tempInteger = endIdx - startIdx + 1 + lookbackSignal;
var tempInteger = endIdx - startIdx + 1 + lookbackSignal;
var fastMABuffer = new decimal[tempInteger];
var slowMABuffer = new decimal[tempInteger];

Expand Down
4 changes: 2 additions & 2 deletions src/TALib.NETCore/TAFunc/TA_StochRsi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static RetCode StochRsi(double[] inReal, int startIdx, int endIdx, double
return RetCode.BadParam;
}

int lookbackSTOCHF = StochFLookback(optInFastDMAType);
int lookbackSTOCHF = StochFLookback(optInFastDMAType, optInFastKPeriod, optInFastDPeriod);
int lookbackTotal = StochRsiLookback(optInFastDMAType, optInTimePeriod, optInFastKPeriod, optInFastDPeriod);
if (startIdx < lookbackTotal)
{
Expand Down Expand Up @@ -67,7 +67,7 @@ public static RetCode StochRsi(decimal[] inReal, int startIdx, int endIdx, decim
return RetCode.BadParam;
}

int lookbackSTOCHF = StochFLookback(optInFastDMAType);
int lookbackSTOCHF = StochFLookback(optInFastDMAType, optInFastKPeriod, optInFastDPeriod);
int lookbackTotal = StochRsiLookback(optInFastDMAType, optInTimePeriod, optInFastKPeriod, optInFastDPeriod);
if (startIdx < lookbackTotal)
{
Expand Down
2 changes: 1 addition & 1 deletion src/TALib.NETCore/TAFunc/TA_Sub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static RetCode Sub(decimal[] inReal0, decimal[] inReal1, int startIdx, in
int outIdx = default;
for (int i = startIdx; i <= endIdx; i++)
{
outReal[outIdx++] = inReal0[i] * inReal1[i];
outReal[outIdx++] = inReal0[i] - inReal1[i];
}

outBegIdx = startIdx;
Expand Down
2 changes: 1 addition & 1 deletion src/TALib.NETCore/TALib.NETCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Copyright>TicTacTec LLC</Copyright>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageProjectUrl>https://ta-lib.org</PackageProjectUrl>
<Version>0.4.3</Version>
<Version>0.4.4</Version>
<RepositoryUrl>https://github.com/hmG3/TA-Lib.NETCore</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageIcon>talib.png</PackageIcon>
Expand Down

0 comments on commit 3c9d20c

Please sign in to comment.