Skip to content

Commit

Permalink
Added default rounding to 4 decimal places
Browse files Browse the repository at this point in the history
Replaced ElementAtOrDefault with direct indexing
Fixed bugs in 7 calculations
  • Loading branch information
ooples committed Feb 23, 2022
1 parent dbaba4d commit 9bf0f54
Show file tree
Hide file tree
Showing 22 changed files with 6,721 additions and 6,722 deletions.
166 changes: 83 additions & 83 deletions Calculations/BollingerBands.cs

Large diffs are not rendered by default.

182 changes: 91 additions & 91 deletions Calculations/Chande.cs

Large diffs are not rendered by default.

106 changes: 53 additions & 53 deletions Calculations/Demark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ public static StockData CalculateDemarkRangeExpansionIndex(this StockData stockD

for (int i = 0; i < stockData.Count; i++)
{
decimal high = highList.ElementAtOrDefault(i);
decimal prevHigh2 = i >= 2 ? highList.ElementAtOrDefault(i - 2) : 0;
decimal prevHigh5 = i >= 5 ? highList.ElementAtOrDefault(i - 5) : 0;
decimal prevHigh6 = i >= 6 ? highList.ElementAtOrDefault(i - 6) : 0;
decimal low = lowList.ElementAtOrDefault(i);
decimal prevLow2 = i >= 2 ? lowList.ElementAtOrDefault(i - 2) : 0;
decimal prevLow5 = i >= 5 ? lowList.ElementAtOrDefault(i - 5) : 0;
decimal prevLow6 = i >= 6 ? lowList.ElementAtOrDefault(i - 6) : 0;
decimal prevClose7 = i >= 7 ? inputList.ElementAtOrDefault(i - 7) : 0;
decimal prevClose8 = i >= 8 ? inputList.ElementAtOrDefault(i - 8) : 0;
decimal prevRei1 = i >= 1 ? reiList.ElementAtOrDefault(i - 1) : 0;
decimal prevRei2 = i >= 2 ? reiList.ElementAtOrDefault(i - 2) : 0;
decimal high = highList[i];
decimal prevHigh2 = i >= 2 ? highList[i - 2] : 0;
decimal prevHigh5 = i >= 5 ? highList[i - 5] : 0;
decimal prevHigh6 = i >= 6 ? highList[i - 6] : 0;
decimal low = lowList[i];
decimal prevLow2 = i >= 2 ? lowList[i - 2] : 0;
decimal prevLow5 = i >= 5 ? lowList[i - 5] : 0;
decimal prevLow6 = i >= 6 ? lowList[i - 6] : 0;
decimal prevClose7 = i >= 7 ? inputList[i - 7] : 0;
decimal prevClose8 = i >= 8 ? inputList[i - 8] : 0;
decimal prevRei1 = i >= 1 ? reiList[i - 1] : 0;
decimal prevRei2 = i >= 2 ? reiList[i - 2] : 0;
decimal n = (high >= prevLow5 || high >= prevLow6) && (low <= prevHigh5 || low <= prevHigh6) ? 0 : 1;
decimal m = prevHigh2 >= prevClose8 && (prevLow2 <= prevClose7 || prevLow2 <= prevClose8) ? 0 : 1;
decimal s = high - prevHigh2 + (low - prevLow2);
Expand All @@ -44,7 +44,7 @@ public static StockData CalculateDemarkRangeExpansionIndex(this StockData stockD
decimal s2Sum = s2List.TakeLastExt(length).Sum();

decimal rei = s2Sum != 0 ? s1Sum / s2Sum * 100 : 0;
reiList.Add(rei);
reiList.AddRounded(rei);

var signal = GetRsiSignal(rei - prevRei1, prevRei1 - prevRei2, rei, prevRei1, 100, -100);
signalsList.Add(signal);
Expand Down Expand Up @@ -77,30 +77,30 @@ public static StockData CalculateDemarkPressureRatioV1(this StockData stockData,

for (int i = 0; i < stockData.Count; i++)
{
decimal currentHigh = highList.ElementAtOrDefault(i);
decimal currentLow = lowList.ElementAtOrDefault(i);
decimal currentOpen = openList.ElementAtOrDefault(i);
decimal currentClose = inputList.ElementAtOrDefault(i);
decimal currentVolume = volumeList.ElementAtOrDefault(i);
decimal prevClose = i >= 1 ? inputList.ElementAtOrDefault(i - 1) : 0;
decimal prevPr1 = i >= 1 ? pressureRatioList.ElementAtOrDefault(i - 1) : 0;
decimal prevPr2 = i >= 2 ? pressureRatioList.ElementAtOrDefault(i - 2) : 0;
decimal currentHigh = highList[i];
decimal currentLow = lowList[i];
decimal currentOpen = openList[i];
decimal currentClose = inputList[i];
decimal currentVolume = volumeList[i];
decimal prevClose = i >= 1 ? inputList[i - 1] : 0;
decimal prevPr1 = i >= 1 ? pressureRatioList[i - 1] : 0;
decimal prevPr2 = i >= 2 ? pressureRatioList[i - 2] : 0;
decimal gapup = prevClose != 0 ? (currentOpen - prevClose) / prevClose : 0;
decimal gapdown = currentOpen != 0 ? (prevClose - currentOpen) / currentOpen : 0;

decimal bp = gapup > 0.15m ? (currentHigh - prevClose + currentClose - currentLow) * currentVolume :
currentClose > currentOpen ? (currentClose - currentOpen) * currentVolume : 0;
bpList.Add(bp);
bpList.AddRounded(bp);

decimal sp = gapdown > 0.15m ? (prevClose - currentLow + currentHigh - currentClose) * currentVolume :
currentClose < currentOpen ? (currentClose - currentOpen) * currentVolume : 0;
spList.Add(sp);
spList.AddRounded(sp);

decimal bpSum = bpList.TakeLastExt(length).Sum();
decimal spSum = spList.TakeLastExt(length).Sum();

decimal pressureRatio = bpSum - spSum != 0 ? MinOrMax(100 * bpSum / (bpSum - spSum), 100, 0) : 0;
pressureRatioList.Add(pressureRatio);
pressureRatioList.AddRounded(pressureRatio);

var signal = GetRsiSignal(pressureRatio - prevPr1, prevPr1 - prevPr2, pressureRatio, prevPr1, 75, 25);
signalsList.Add(signal);
Expand Down Expand Up @@ -133,29 +133,29 @@ public static StockData CalculateDemarkPressureRatioV2(this StockData stockData,

for (int i = 0; i < stockData.Count; i++)
{
decimal currentHigh = highList.ElementAtOrDefault(i);
decimal currentLow = lowList.ElementAtOrDefault(i);
decimal currentOpen = openList.ElementAtOrDefault(i);
decimal currentClose = inputList.ElementAtOrDefault(i);
decimal currentVolume = volumeList.ElementAtOrDefault(i);
decimal currentHigh = highList[i];
decimal currentLow = lowList[i];
decimal currentOpen = openList[i];
decimal currentClose = inputList[i];
decimal currentVolume = volumeList[i];
decimal delta = currentClose - currentOpen;
decimal trueRange = currentHigh - currentLow;
decimal ratio = trueRange != 0 ? delta / trueRange : 0;
decimal prevPr1 = i >= 1 ? pressureRatioList.ElementAtOrDefault(i - 1) : 0;
decimal prevPr2 = i >= 2 ? pressureRatioList.ElementAtOrDefault(i - 2) : 0;
decimal prevPr1 = i >= 1 ? pressureRatioList[i - 1] : 0;
decimal prevPr2 = i >= 2 ? pressureRatioList[i - 2] : 0;

decimal buyingPressure = delta > 0 ? ratio * currentVolume : 0;
bpList.Add(buyingPressure);
bpList.AddRounded(buyingPressure);

decimal sellingPressure = delta < 0 ? ratio * currentVolume : 0;
spList.Add(sellingPressure);
spList.AddRounded(sellingPressure);

decimal bpSum = bpList.TakeLastExt(length).Sum();
decimal spSum = spList.TakeLastExt(length).Sum();
decimal denom = bpSum + Math.Abs(spSum);

decimal pressureRatio = denom != 0 ? MinOrMax(100 * bpSum / denom, 100, 0) : 50;
pressureRatioList.Add(pressureRatio);
pressureRatioList.AddRounded(pressureRatio);

var signal = GetRsiSignal(pressureRatio - prevPr1, prevPr1 - prevPr2, pressureRatio, prevPr1, 75, 25);
signalsList.Add(signal);
Expand Down Expand Up @@ -187,21 +187,21 @@ public static StockData CalculateDemarkReversalPoints(this StockData stockData,

for (int i = 0; i < stockData.Count; i++)
{
decimal currentValue = inputList.ElementAtOrDefault(i);
decimal currentValue = inputList[i];

decimal uCount = 0, dCount = 0;
for (int j = 0; j < length1; j++)
{
decimal value = i >= j ? inputList.ElementAtOrDefault(i - j) : 0;
decimal prevValue = i >= j + length2 ? inputList.ElementAtOrDefault(i - (j + length2)) : 0;
decimal value = i >= j ? inputList[i - j] : 0;
decimal prevValue = i >= j + length2 ? inputList[i - (j + length2)] : 0;

uCount += value > prevValue ? 1 : 0;
dCount += value < prevValue ? 1 : 0;
}

decimal drp = dCount == length1 ? 1 : uCount == length1 ? -1 : 0;
decimal drpPrice = drp != 0 ? currentValue : 0;
drpPriceList.Add(drpPrice);
drpPriceList.AddRounded(drpPrice);

var signal = GetConditionSignal(drp > 0 || uCount > dCount, drp < 0 || dCount > uCount);
signalsList.Add(signal);
Expand Down Expand Up @@ -232,21 +232,21 @@ public static StockData CalculateDemarkSetupIndicator(this StockData stockData,

for (int i = 0; i < stockData.Count; i++)
{
decimal currentValue = inputList.ElementAtOrDefault(i);
decimal currentValue = inputList[i];

decimal uCount = 0, dCount = 0;
for (int j = 0; j < length; j++)
{
decimal value = i >= j ? inputList.ElementAtOrDefault(i - j) : 0;
decimal prevValue = i >= j + length ? inputList.ElementAtOrDefault(i - (j + length)) : 0;
decimal value = i >= j ? inputList[i - j] : 0;
decimal prevValue = i >= j + length ? inputList[i - (j + length)] : 0;

uCount += value > prevValue ? 1 : 0;
dCount += value < prevValue ? 1 : 0;
}

decimal drp = dCount == length ? 1 : uCount == length ? -1 : 0;
decimal drpPrice = drp != 0 ? currentValue : 0;
drpPriceList.Add(drpPrice);
drpPriceList.AddRounded(drpPrice);

var signal = GetConditionSignal(drp > 0 || uCount > dCount, drp < 0 || dCount > uCount);
signalsList.Add(signal);
Expand Down Expand Up @@ -280,29 +280,29 @@ public static StockData CalculateDemarker(this StockData stockData, MovingAvgTyp

for (int i = 0; i < stockData.Count; i++)
{
decimal currentLow = lowList.ElementAtOrDefault(i);
decimal currentHigh = highList.ElementAtOrDefault(i);
decimal prevHigh = i >= 1 ? highList.ElementAtOrDefault(i - 1) : 0;
decimal prevLow = i >= 1 ? lowList.ElementAtOrDefault(i - 1) : 0;
decimal currentLow = lowList[i];
decimal currentHigh = highList[i];
decimal prevHigh = i >= 1 ? highList[i - 1] : 0;
decimal prevLow = i >= 1 ? lowList[i - 1] : 0;

decimal dMax = currentHigh > prevHigh ? currentHigh - prevHigh : 0;
dMaxList.Add(dMax);
dMaxList.AddRounded(dMax);

decimal dMin = currentLow < prevLow ? prevLow - currentLow : 0;
dMinList.Add(dMin);
dMinList.AddRounded(dMin);
}

var maxMaList = GetMovingAverageList(stockData, maType, length, dMaxList);
var minMaList = GetMovingAverageList(stockData, maType, length, dMinList);
for (int i = 0; i < stockData.Count; i++)
{
decimal maxMa = maxMaList.ElementAtOrDefault(i);
decimal minMa = minMaList.ElementAtOrDefault(i);
decimal prevDemarker1 = i >= 1 ? demarkerList.ElementAtOrDefault(i - 1) : 0;
decimal prevDemarker2 = i >= 2 ? demarkerList.ElementAtOrDefault(i - 2) : 0;
decimal maxMa = maxMaList[i];
decimal minMa = minMaList[i];
decimal prevDemarker1 = i >= 1 ? demarkerList[i - 1] : 0;
decimal prevDemarker2 = i >= 2 ? demarkerList[i - 2] : 0;

decimal demarker = maxMa + minMa != 0 ? MinOrMax(maxMa / (maxMa + minMa) * 100, 100, 0) : 0;
demarkerList.Add(demarker);
demarkerList.AddRounded(demarker);

var signal = GetRsiSignal(demarker - prevDemarker1, prevDemarker1 - prevDemarker2, demarker, prevDemarker1, 70, 30);
signalsList.Add(signal);
Expand Down
Loading

0 comments on commit 9bf0f54

Please sign in to comment.