Skip to content

Commit 3c9d20c

Browse files
committed
Fix calculation of some functions
1 parent e8bebeb commit 3c9d20c

6 files changed

Lines changed: 29 additions & 57 deletions

File tree

src/TALib.NETCore/Function.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,20 @@ public int Lookback(params int[] options)
4747
var optInParameters = method.GetParameters().Where(pi => pi.Name.StartsWith(OptInPrefix)).ToList();
4848
var paramsArray = new object[optInParameters.Count];
4949
Array.Fill(paramsArray, Type.Missing);
50-
if (options.Length == optInParameters.Count)
50+
51+
var defOptInParameters = Options.Select(NormalizeOptionalParameter).ToList();
52+
for (int i = 0, paramsArrayIndex = 0; i < defOptInParameters.Count; i++)
5153
{
52-
var defOptInParameters = Options.Select(NormalizeOptionalParameter).ToList();
53-
for (int i = 0, paramsArrayIndex = 0; i < defOptInParameters.Count; i++)
54+
var optInParameter = optInParameters.SingleOrDefault(p => p.Name == defOptInParameters[i]);
55+
if (optInParameter != null)
5456
{
55-
var optInParameter = optInParameters.SingleOrDefault(p => p.Name == defOptInParameters[i]);
56-
if (optInParameter != null)
57+
if (optInParameter.ParameterType.IsEnum && optInParameter.ParameterType.IsEnumDefined(options[i]))
5758
{
58-
if (optInParameter.ParameterType.IsEnum && optInParameter.ParameterType.IsEnumDefined(options[i]))
59-
{
60-
paramsArray[paramsArrayIndex++] = Enum.ToObject(optInParameter.ParameterType, options[i]);
61-
}
62-
else
63-
{
64-
paramsArray[paramsArrayIndex++] = options[i];
65-
}
59+
paramsArray[paramsArrayIndex++] = Enum.ToObject(optInParameter.ParameterType, options[i]);
60+
}
61+
else
62+
{
63+
paramsArray[paramsArrayIndex++] = options[i];
6664
}
6765
}
6866
}

src/TALib.NETCore/TACore.cs

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,9 @@ private static RetCode TA_INT_MACD(double[] inReal, int startIdx, int endIdx, do
228228
{
229229
outBegIdx = outNbElement = 0;
230230

231-
int tempInteger;
232231
if (optInSlowPeriod < optInFastPeriod)
233232
{
234-
tempInteger = optInSlowPeriod;
235-
optInSlowPeriod = optInFastPeriod;
236-
optInFastPeriod = tempInteger;
233+
(optInSlowPeriod, optInFastPeriod) = (optInFastPeriod, optInSlowPeriod);
237234
}
238235

239236
double k1;
@@ -270,7 +267,7 @@ private static RetCode TA_INT_MACD(double[] inReal, int startIdx, int endIdx, do
270267
return RetCode.Success;
271268
}
272269

273-
tempInteger = endIdx - startIdx + 1 + lookbackSignal;
270+
var tempInteger = endIdx - startIdx + 1 + lookbackSignal;
274271
var fastEMABuffer = new double[tempInteger];
275272
var slowEMABuffer = new double[tempInteger];
276273

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

327-
int tempInteger;
328324
if (optInSlowPeriod < optInFastPeriod)
329325
{
330-
tempInteger = optInSlowPeriod;
331-
optInSlowPeriod = optInFastPeriod;
332-
optInFastPeriod = tempInteger;
326+
(optInSlowPeriod, optInFastPeriod) = (optInFastPeriod, optInSlowPeriod);
333327
}
334328

335329
decimal k1;
@@ -366,7 +360,7 @@ private static RetCode TA_INT_MACD(decimal[] inReal, int startIdx, int endIdx, d
366360
return RetCode.Success;
367361
}
368362

369-
tempInteger = endIdx - startIdx + 1 + lookbackSignal;
363+
var tempInteger = endIdx - startIdx + 1 + lookbackSignal;
370364
var fastEMABuffer = new decimal[tempInteger];
371365
var slowEMABuffer = new decimal[tempInteger];
372366

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

424-
int tempInteger;
425418
if (optInSlowPeriod < optInFastPeriod)
426419
{
427-
tempInteger = optInSlowPeriod;
428-
optInSlowPeriod = optInFastPeriod;
429-
optInFastPeriod = tempInteger;
420+
(optInSlowPeriod, optInFastPeriod) = (optInFastPeriod, optInSlowPeriod);
430421
}
431422

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

444-
tempInteger = outBegIdx1 - outBegIdx2;
445-
for (int i = 0, j = tempInteger; i < outNbElement1; i++, j++)
435+
for (int i = 0, j = outBegIdx1 - outBegIdx2; i < outNbElement1; i++, j++)
446436
{
447437
if (doPercentageOutput)
448438
{
@@ -467,12 +457,9 @@ private static RetCode TA_INT_PO(decimal[] inReal, int startIdx, int endIdx, dec
467457
{
468458
outBegIdx = outNbElement = 0;
469459

470-
int tempInteger;
471460
if (optInSlowPeriod < optInFastPeriod)
472461
{
473-
tempInteger = optInSlowPeriod;
474-
optInSlowPeriod = optInFastPeriod;
475-
optInFastPeriod = tempInteger;
462+
(optInSlowPeriod, optInFastPeriod) = (optInFastPeriod, optInSlowPeriod);
476463
}
477464

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

490-
tempInteger = outBegIdx1 - outBegIdx2;
491-
for (int i = 0, j = tempInteger; i < outNbElement1; i++, j++)
477+
for (int i = 0, j = outBegIdx1 - outBegIdx2; i < outNbElement1; i++, j++)
492478
{
493479
if (doPercentageOutput)
494480
{

src/TALib.NETCore/TAFunc/TA_MacdExt.cs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,10 @@ public static RetCode MacdExt(double[] inReal, int startIdx, int endIdx, double[
2323
return RetCode.BadParam;
2424
}
2525

26-
int tempInteger;
2726
if (optInSlowPeriod < optInFastPeriod)
2827
{
29-
tempInteger = optInSlowPeriod;
30-
optInSlowPeriod = optInFastPeriod;
31-
optInFastPeriod = tempInteger;
32-
33-
MAType tempMAType = optInSlowMAType;
34-
optInSlowMAType = optInFastMAType;
35-
optInFastMAType = tempMAType;
28+
(optInSlowPeriod, optInFastPeriod) = (optInFastPeriod, optInSlowPeriod);
29+
(optInSlowMAType, optInFastMAType) = (optInFastMAType, optInSlowMAType);
3630
}
3731

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

51-
tempInteger = endIdx - startIdx + 1 + lookbackSignal;
45+
var tempInteger = endIdx - startIdx + 1 + lookbackSignal;
5246
var fastMABuffer = new double[tempInteger];
5347
var slowMABuffer = new double[tempInteger];
5448

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

118-
int tempInteger;
119112
if (optInSlowPeriod < optInFastPeriod)
120113
{
121-
tempInteger = optInSlowPeriod;
122-
optInSlowPeriod = optInFastPeriod;
123-
optInFastPeriod = tempInteger;
124-
125-
MAType tempMAType = optInSlowMAType;
126-
optInSlowMAType = optInFastMAType;
127-
optInFastMAType = tempMAType;
114+
(optInSlowPeriod, optInFastPeriod) = (optInFastPeriod, optInSlowPeriod);
115+
(optInSlowMAType, optInFastMAType) = (optInFastMAType, optInSlowMAType);
128116
}
129117

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

143-
tempInteger = endIdx - startIdx + 1 + lookbackSignal;
131+
var tempInteger = endIdx - startIdx + 1 + lookbackSignal;
144132
var fastMABuffer = new decimal[tempInteger];
145133
var slowMABuffer = new decimal[tempInteger];
146134

src/TALib.NETCore/TAFunc/TA_StochRsi.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static RetCode StochRsi(double[] inReal, int startIdx, int endIdx, double
1919
return RetCode.BadParam;
2020
}
2121

22-
int lookbackSTOCHF = StochFLookback(optInFastDMAType);
22+
int lookbackSTOCHF = StochFLookback(optInFastDMAType, optInFastKPeriod, optInFastDPeriod);
2323
int lookbackTotal = StochRsiLookback(optInFastDMAType, optInTimePeriod, optInFastKPeriod, optInFastDPeriod);
2424
if (startIdx < lookbackTotal)
2525
{
@@ -67,7 +67,7 @@ public static RetCode StochRsi(decimal[] inReal, int startIdx, int endIdx, decim
6767
return RetCode.BadParam;
6868
}
6969

70-
int lookbackSTOCHF = StochFLookback(optInFastDMAType);
70+
int lookbackSTOCHF = StochFLookback(optInFastDMAType, optInFastKPeriod, optInFastDPeriod);
7171
int lookbackTotal = StochRsiLookback(optInFastDMAType, optInTimePeriod, optInFastKPeriod, optInFastDPeriod);
7272
if (startIdx < lookbackTotal)
7373
{

src/TALib.NETCore/TAFunc/TA_Sub.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static RetCode Sub(decimal[] inReal0, decimal[] inReal1, int startIdx, in
4747
int outIdx = default;
4848
for (int i = startIdx; i <= endIdx; i++)
4949
{
50-
outReal[outIdx++] = inReal0[i] * inReal1[i];
50+
outReal[outIdx++] = inReal0[i] - inReal1[i];
5151
}
5252

5353
outBegIdx = startIdx;

src/TALib.NETCore/TALib.NETCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<Copyright>TicTacTec LLC</Copyright>
1111
<PackageLicenseFile>LICENSE</PackageLicenseFile>
1212
<PackageProjectUrl>https://ta-lib.org</PackageProjectUrl>
13-
<Version>0.4.3</Version>
13+
<Version>0.4.4</Version>
1414
<RepositoryUrl>https://github.com/hmG3/TA-Lib.NETCore</RepositoryUrl>
1515
<RepositoryType>git</RepositoryType>
1616
<PackageIcon>talib.png</PackageIcon>

0 commit comments

Comments
 (0)