Skip to content

Commit

Permalink
.NET7 & RSIBands more accurate calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
pcortellezzi committed Jul 5, 2023
1 parent 514e41f commit 9b982d1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 55 deletions.
8 changes: 4 additions & 4 deletions Indicators/AverageTrueRangeNT8/AverageTrueRangeNT8.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<LangVersion>latest</LangVersion>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<Platforms>AnyCPU</Platforms>
Expand All @@ -10,14 +10,14 @@
<RootNamespace>AverageTrueRangeNT8</RootNamespace>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<OutputPath>C:\Users\phili\Apps\Quantower\TradingPlatform\v1.130.11\..\..\Settings\Scripts\Indicators\AverageTrueRangeNT8</OutputPath>
<OutputPath>C:\Users\phili\Apps\Quantower\TradingPlatform\v1.133.7\..\..\Settings\Scripts\Indicators\AverageTrueRangeNT8</OutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath>C:\Users\phili\Apps\Quantower\TradingPlatform\v1.130.11\..\..\Settings\Scripts\Indicators\AverageTrueRangeNT8</OutputPath>
<OutputPath>C:\Users\phili\Apps\Quantower\TradingPlatform\v1.133.7\..\..\Settings\Scripts\Indicators\AverageTrueRangeNT8</OutputPath>
</PropertyGroup>
<ItemGroup>
<Reference Include="TradingPlatform.BusinessLayer">
<HintPath>C:\Users\phili\Apps\Quantower\TradingPlatform\v1.130.11\bin\TradingPlatform.BusinessLayer.dll</HintPath>
<HintPath>C:\Users\phili\Apps\Quantower\TradingPlatform\v1.133.7\bin\TradingPlatform.BusinessLayer.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
Expand Down
63 changes: 17 additions & 46 deletions Indicators/RSIBands/RSIBands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@

namespace RSIBands {
public class RSIBands : Indicator {
private double PUp = 0;
private double NUp = 0;
private double PLow = 0;
private double NLow = 0;
private double PUpOld = 0;
private double NUpOld = 0;
private double PLowOld = 0;
private double NLowOld = 0;
private HistoricalDataCustom hdc = null;
private Indicator EMAu = null;
private Indicator EMAd = null;

private HistoricalData mtfData = null;
private Indicator mtfIndicator = null;
Expand Down Expand Up @@ -56,6 +51,12 @@ protected override void OnInit() {
this.mtfData = this.Symbol.GetHistory(MTFPeriod, this.HistoricalData.HistoryType, this.HistoricalData.FromTime);
this.mtfIndicator = new RSIBands(this.MTFPeriod, this.rsiPeriod, this.RSIUpperLevel, this.RSILowerLevel);
this.mtfData.AddIndicator(this.mtfIndicator);
} else {
this.EMAu = Core.Indicators.BuiltIn.EMA(2 * this.rsiPeriod - 1, PriceType.Open);
this.EMAd = Core.Indicators.BuiltIn.EMA(2 * this.rsiPeriod - 1, PriceType.Close);
this.hdc = new HistoricalDataCustom(this);
this.hdc.AddIndicator(this.EMAu);
this.hdc.AddIndicator(this.EMAd);
}
}

Expand All @@ -65,17 +66,14 @@ protected override void OnUpdate(UpdateArgs args) {
return;

if (this.mtfData == null) {
//indicator calculation
if (args.Reason != UpdateReason.NewTick) {
this.PUpOld = this.PUp;
this.NUpOld = this.NUp;
this.PLowOld = this.PLow;
this.NLowOld = this.NLow;
}

SetValue(BuiltInRSIequivalent(this.RSIUpperLevel, this.PUpOld, this.NUpOld, GetValue(1), this.Close(0), this.Close(1)));
SetValue(BuiltInRSIequivalent(this.RSILowerLevel, this.PLowOld, this.NLowOld, GetValue(1, 1), this.Close(0), this.Close(1)), 1);
} else {
this.hdc[PriceType.Open, 0] = Math.Max(Close() - Close(1), 0);
this.hdc[PriceType.Close, 0] = Math.Max(Close(1) - Close(), 0);
double xu = (this.rsiPeriod - 1) * (this.EMAd.GetValue() * this.RSIUpperLevel / (100 - this.RSIUpperLevel) - this.EMAu.GetValue());
double xl = (this.rsiPeriod - 1) * (this.EMAd.GetValue() * this.RSILowerLevel / (100 - this.RSILowerLevel) - this.EMAu.GetValue());
SetValue(xu >= 0 ? Close() + xu : Close() + xu * (100 - this.RSIUpperLevel) / this.RSIUpperLevel);
SetValue(xl >= 0 ? Close() + xl : Close() + xl * (100 - this.RSILowerLevel) / this.RSILowerLevel, 1);
}
else {
//generic MTF calculation
int mtfOffset = (int)this.mtfData.GetIndexByTime(this.Time().Ticks);
if ((this.mtfData[mtfOffset] as HistoryItemBar).TimeLeft == this.Time()) {
Expand All @@ -97,33 +95,6 @@ protected override void OnUpdate(UpdateArgs args) {

}

private double BuiltInRSIequivalent(int TargetRSILevel, double P, double N, double PrevRSIBand, double Close, double PrevClose) {
double W = 0;
double S = 0;
double diff = Close - PrevClose;
double HypotheticalCloseToMatchRSITarget = 0;

if (diff > 0)
W = diff;
else if (diff < 0)
S = -diff;

if (PrevRSIBand > PrevClose)
HypotheticalCloseToMatchRSITarget = PrevClose + P - P * this.rsiPeriod - ((N * this.rsiPeriod) - N) * TargetRSILevel / (TargetRSILevel - 100);
else
HypotheticalCloseToMatchRSITarget = PrevClose - N - P + N * this.rsiPeriod + P * this.rsiPeriod + (100 * P) / TargetRSILevel - (100 * P * this.rsiPeriod) / TargetRSILevel;

if (PrevRSIBand == GetValue(1)) {
this.PUp = ((this.rsiPeriod - 1) * P + W) / this.rsiPeriod;
this.NUp = ((this.rsiPeriod - 1) * N + S) / this.rsiPeriod;
} else if (PrevRSIBand == GetValue(1, 1)) {
this.PLow = ((this.rsiPeriod - 1) * P + W) / this.rsiPeriod;
this.NLow = ((this.rsiPeriod - 1) * N + S) / this.rsiPeriod;
}

return HypotheticalCloseToMatchRSITarget;
}

protected override void OnClear() {
this.mtfData?.Dispose();
this.mtfData = null;
Expand Down
9 changes: 5 additions & 4 deletions Indicators/RSIBands/RSIBands.csproj
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<LangVersion>latest</LangVersion>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<Platforms>AnyCPU</Platforms>
<AlgoType>Indicator</AlgoType>
<AssemblyName>RSIBands</AssemblyName>
<RootNamespace>RSIBands</RootNamespace>
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<OutputPath>C:\Users\phili\Apps\AMP Quantower\TradingPlatform\v1.130.6\..\..\Settings\Scripts\Indicators\RSIBands</OutputPath>
<OutputPath>C:\Users\phili\Apps\Quantower\TradingPlatform\v1.133.7\..\..\Settings\Scripts\Indicators\RSIBands</OutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath>C:\Users\phili\Apps\AMP Quantower\TradingPlatform\v1.130.6\..\..\Settings\Scripts\Indicators\RSIBands</OutputPath>
<OutputPath>C:\Users\phili\Apps\Quantower\TradingPlatform\v1.133.7\..\..\Settings\Scripts\Indicators\RSIBands</OutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
<Reference Include="TradingPlatform.BusinessLayer">
<HintPath>C:\Users\phili\Apps\AMP Quantower\TradingPlatform\v1.130.6\bin\TradingPlatform.BusinessLayer.dll</HintPath>
<HintPath>C:\Users\phili\Apps\Quantower\TradingPlatform\v1.133.7\bin\TradingPlatform.BusinessLayer.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
Expand Down
20 changes: 19 additions & 1 deletion Indicators/RSIBands/RSIBands.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33516.290
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RSIBands", "RSIBands.csproj", "{3830F46E-54B3-404D-B716-E983109CF7A7}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RSIBands", "RSIBands.csproj", "{3830F46E-54B3-404D-B716-E983109CF7A7}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AverageTrueRangeNT8", "..\AverageTrueRangeNT8\AverageTrueRangeNT8.csproj", "{AAB730F2-B408-45CF-94B3-1576C213F0A2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GFTColorBar", "..\GFTColorBar\GFTColorBar.csproj", "{FED22036-305B-45AC-907D-962E5CEFF0C9}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ScalpingGFT", "..\..\PlaceOrderStrategies\ScalpingGFT\ScalpingGFT.csproj", "{1906C269-F690-488A-926F-A4CA7076403A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -15,6 +21,18 @@ Global
{3830F46E-54B3-404D-B716-E983109CF7A7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3830F46E-54B3-404D-B716-E983109CF7A7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3830F46E-54B3-404D-B716-E983109CF7A7}.Release|Any CPU.Build.0 = Release|Any CPU
{AAB730F2-B408-45CF-94B3-1576C213F0A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AAB730F2-B408-45CF-94B3-1576C213F0A2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AAB730F2-B408-45CF-94B3-1576C213F0A2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AAB730F2-B408-45CF-94B3-1576C213F0A2}.Release|Any CPU.Build.0 = Release|Any CPU
{FED22036-305B-45AC-907D-962E5CEFF0C9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FED22036-305B-45AC-907D-962E5CEFF0C9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FED22036-305B-45AC-907D-962E5CEFF0C9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FED22036-305B-45AC-907D-962E5CEFF0C9}.Release|Any CPU.Build.0 = Release|Any CPU
{1906C269-F690-488A-926F-A4CA7076403A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1906C269-F690-488A-926F-A4CA7076403A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1906C269-F690-488A-926F-A4CA7076403A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1906C269-F690-488A-926F-A4CA7076403A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 9b982d1

Please sign in to comment.