Skip to content

Commit 96946bb

Browse files
committed
AverageTrueRange NT8 & RSIBands
1 parent fe187da commit 96946bb

File tree

6 files changed

+305
-0
lines changed

6 files changed

+305
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright QUANTOWER LLC. © 2017-2022. All rights reserved.
2+
3+
using System;
4+
using System.Drawing;
5+
using TradingPlatform.BusinessLayer;
6+
7+
namespace AverageTrueRangeNT8
8+
{
9+
/// <summary>
10+
/// An example of blank indicator. Add your code, compile it and use on the charts in the assigned trading terminal.
11+
/// Information about API you can find here: http://api.quantower.com
12+
/// Code samples: https://github.com/Quantower/Examples
13+
/// </summary>
14+
public class AverageTrueRangeNT8 : Indicator, IWatchlistIndicator
15+
{
16+
[InputParameter("Period", 10, 1, 99999, 1, 0)]
17+
public int Period = 14;
18+
19+
public int MinHistoryDepths => 1000;
20+
21+
private double Value;
22+
private double prevValue;
23+
24+
/// <summary>
25+
/// Indicator's constructor. Contains general information: name, description, LineSeries etc.
26+
/// </summary>
27+
public AverageTrueRangeNT8()
28+
: base()
29+
{
30+
// Defines indicator's name and description.
31+
this.Name = "AverageTrueRangeNT8";
32+
this.Description = "Average True Range with NT8 Formula";
33+
34+
// Defines line on demand with particular parameters.
35+
this.AddLineSeries("ATR", Color.CadetBlue, 1, LineStyle.Solid);
36+
37+
// By default indicator will be applied on main window of the chart
38+
this.SeparateWindow = true;
39+
this.UpdateType = IndicatorUpdateType.OnBarClose;
40+
this.IsUpdateTypesSupported = false;
41+
}
42+
43+
/// <summary>
44+
/// This function will be called after creating an indicator as well as after its input params reset or chart (symbol or timeframe) updates.
45+
/// </summary>
46+
protected override void OnInit()
47+
{
48+
// Add your initialization code here
49+
}
50+
51+
/// <summary>
52+
/// Calculation entry point. This function is called when a price data updates.
53+
/// Will be runing under the HistoricalBar mode during history loading.
54+
/// Under NewTick during realtime.
55+
/// Under NewBar if start of the new bar is required.
56+
/// </summary>
57+
/// <param name="args">Provides data of updating reason and incoming price.</param>
58+
protected override void OnUpdate(UpdateArgs args)
59+
{
60+
61+
double high0 = this.High();
62+
double low0 = this.Low();
63+
64+
if (args.Reason == UpdateReason.HistoricalBar || args.Reason == UpdateReason.NewBar)
65+
this.prevValue = this.Value;
66+
67+
if (this.Count < 1)
68+
this.Value = high0 - low0;
69+
else
70+
{
71+
double close1 = this.Close(1);
72+
double trueRange = Math.Max(Math.Abs(low0 - close1), Math.Max(high0 - low0, Math.Abs(high0 - close1)));
73+
this.Value = ((Math.Min(this.Count + 1, this.Period) - 1) * this.prevValue + trueRange) / Math.Min(this.Count + 1, this.Period);
74+
}
75+
if (this.Value < 1)
76+
this.SetValue(Math.Round(this.Value, 3));
77+
else
78+
this.SetValue(Math.Round(this.Value, 2));
79+
}
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project Sdk="Microsoft.NET.Sdk">
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<LangVersion>latest</LangVersion>
6+
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
7+
<Platforms>AnyCPU</Platforms>
8+
<AlgoType>Indicator</AlgoType>
9+
<AssemblyName>AverageTrueRangeNT8</AssemblyName>
10+
<RootNamespace>AverageTrueRangeNT8</RootNamespace>
11+
</PropertyGroup>
12+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
13+
<OutputPath>C:\Users\phili\Apps\Quantower\TradingPlatform\v1.130.11\..\..\Settings\Scripts\Indicators\AverageTrueRangeNT8</OutputPath>
14+
</PropertyGroup>
15+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
16+
<OutputPath>C:\Users\phili\Apps\Quantower\TradingPlatform\v1.130.11\..\..\Settings\Scripts\Indicators\AverageTrueRangeNT8</OutputPath>
17+
</PropertyGroup>
18+
<ItemGroup>
19+
<Reference Include="TradingPlatform.BusinessLayer">
20+
<HintPath>C:\Users\phili\Apps\Quantower\TradingPlatform\v1.130.11\bin\TradingPlatform.BusinessLayer.dll</HintPath>
21+
<Private>False</Private>
22+
</Reference>
23+
</ItemGroup>
24+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.6.33712.159
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AverageTrueRangeNT8", "AverageTrueRangeNT8.csproj", "{035E6691-7D18-4122-B462-F23D4F6B78F6}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{035E6691-7D18-4122-B462-F23D4F6B78F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{035E6691-7D18-4122-B462-F23D4F6B78F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{035E6691-7D18-4122-B462-F23D4F6B78F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{035E6691-7D18-4122-B462-F23D4F6B78F6}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {E83EFCFF-1C1A-4D20-B1BB-17B46128BC40}
24+
EndGlobalSection
25+
EndGlobal

Indicators/RSIBands/RSIBands.cs

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// Copyright QUANTOWER LLC. © 2017-2022. All rights reserved.
2+
3+
using System;
4+
using System.Drawing;
5+
using TradingPlatform.BusinessLayer;
6+
7+
namespace RSIBands
8+
{
9+
/// <summary>
10+
/// An example of blank indicator. Add your code, compile it and use on the charts in the assigned trading terminal.
11+
/// Information about API you can find here: http://api.quantower.com
12+
/// Code samples: https://github.com/Quantower/Examples
13+
/// </summary>
14+
public class RSIBands : Indicator
15+
{
16+
private double PUp = 0;
17+
private double NUp = 0;
18+
private double PLow = 0;
19+
private double NLow = 0;
20+
private double PUpOld = 0;
21+
private double NUpOld = 0;
22+
private double PLowOld = 0;
23+
private double NLowOld = 0;
24+
25+
private double diff = 0;
26+
private double W = 0;
27+
private double S = 0;
28+
private double HypotheticalCloseToMatchRSITarget = 0;
29+
30+
[InputParameter("Period", 10, 1, 99999, 1, 0)]
31+
public int Period = 14;
32+
33+
[InputParameter("RSI Upper Level", 20, 1, 99999, 1, 1)]
34+
public int RSIUpperLevel = 70;
35+
36+
[InputParameter("RSI Lower Level", 30, 1, 99999, 1, 1)]
37+
public int RSILowerLevel = 30;
38+
39+
public override string ShortName => $"{this.Name} ({this.Period}: {this.RSIUpperLevel}/{this.RSILowerLevel})";
40+
41+
/// <summary>
42+
/// Indicator's constructor. Contains general information: name, description, LineSeries etc.
43+
/// </summary>
44+
public RSIBands()
45+
: base()
46+
{
47+
// Defines indicator's name and description.
48+
Name = "RSIBands";
49+
Description = @"Francois Bertrand's RSI Bands as outlined in Stocks & Commodities April 2008 issue.";
50+
51+
// Defines line on demand with particular parameters.
52+
AddLineSeries("RSIBandUpper", Color.CadetBlue, 1, LineStyle.Solid);
53+
AddLineSeries("RSIBandLower", Color.CadetBlue, 1, LineStyle.Solid);
54+
55+
// By default indicator will be applied on main window of the chart
56+
SeparateWindow = false;
57+
UpdateType = IndicatorUpdateType.OnBarClose;
58+
IsUpdateTypesSupported = false;
59+
}
60+
61+
/// <summary>
62+
/// This function will be called after creating an indicator as well as after its input params reset or chart (symbol or timeframe) updates.
63+
/// </summary>
64+
protected override void OnInit()
65+
{
66+
// Add your initialization code here
67+
}
68+
69+
/// <summary>
70+
/// Calculation entry point. This function is called when a price data updates.
71+
/// Will be runing under the HistoricalBar mode during history loading.
72+
/// Under NewTick during realtime.
73+
/// Under NewBar if start of the new bar is required.
74+
/// </summary>
75+
/// <param name="args">Provides data of updating reason and incoming price.</param>
76+
protected override void OnUpdate(UpdateArgs args)
77+
{
78+
79+
if (this.Count < 1)
80+
return;
81+
82+
if (args.Reason != UpdateReason.NewTick)
83+
{
84+
this.PUpOld = this.PUp;
85+
this.NUpOld = this.NUp;
86+
this.PLowOld = this.PLow;
87+
this.NLowOld = this.NLow;
88+
}
89+
90+
91+
SetValue(BuiltInRSIequivalent(this.RSIUpperLevel, this.PUpOld, this.NUpOld, GetValue(1)));
92+
SetValue(BuiltInRSIequivalent(this.RSILowerLevel, this.PLowOld, this.NLowOld, GetValue(1, 1)), 1);
93+
}
94+
95+
private double BuiltInRSIequivalent(int TargetRSILevel, double P, double N, double PrevRSIBand)
96+
{
97+
this.W = 0;
98+
this.S = 0;
99+
100+
this.diff = this.Close(0) - this.Close(1);
101+
102+
if (this.diff > 0)
103+
this.W = this.diff;
104+
else if (this.diff < 0)
105+
this.S = -this.diff;
106+
107+
if (PrevRSIBand > this.Close(1))
108+
this.HypotheticalCloseToMatchRSITarget = this.Close(1) + P - P * this.Period - ((N * this.Period) - N) * TargetRSILevel / (TargetRSILevel - 100);
109+
else
110+
this.HypotheticalCloseToMatchRSITarget = this.Close(1) - N - P + N * this.Period + P * this.Period + (100 * P) / TargetRSILevel - (100 * P * this.Period) / TargetRSILevel;
111+
112+
if (PrevRSIBand == GetValue(1))
113+
{
114+
this.PUp = ((this.Period - 1) * P + this.W) / this.Period;
115+
this.NUp = ((this.Period - 1) * N + this.S) / this.Period;
116+
}
117+
else if (PrevRSIBand == GetValue(1, 1))
118+
{
119+
this.PLow = ((this.Period - 1) * P + this.W) / this.Period;
120+
this.NLow = ((this.Period - 1) * N + this.S) / this.Period;
121+
}
122+
123+
return HypotheticalCloseToMatchRSITarget;
124+
}
125+
}
126+
}

Indicators/RSIBands/RSIBands.csproj

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project Sdk="Microsoft.NET.Sdk">
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<LangVersion>latest</LangVersion>
6+
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
7+
<Platforms>AnyCPU</Platforms>
8+
<AlgoType>Indicator</AlgoType>
9+
<AssemblyName>RSIBands</AssemblyName>
10+
<RootNamespace>RSIBands</RootNamespace>
11+
</PropertyGroup>
12+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
13+
<OutputPath>C:\Users\phili\Apps\Quantower\TradingPlatform\v1.130.11\..\..\Settings\Scripts\Indicators\RSIBands</OutputPath>
14+
</PropertyGroup>
15+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
16+
<OutputPath>C:\Users\phili\Apps\Quantower\TradingPlatform\v1.130.11\..\..\Settings\Scripts\Indicators\RSIBands</OutputPath>
17+
</PropertyGroup>
18+
<ItemGroup>
19+
<Reference Include="TradingPlatform.BusinessLayer">
20+
<HintPath>C:\Users\phili\Apps\Quantower\TradingPlatform\v1.130.11\bin\TradingPlatform.BusinessLayer.dll</HintPath>
21+
<Private>False</Private>
22+
</Reference>
23+
</ItemGroup>
24+
</Project>

Indicators/RSIBands/RSIBands.sln

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.5.33516.290
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RSIBands", "RSIBands.csproj", "{3830F46E-54B3-404D-B716-E983109CF7A7}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{3830F46E-54B3-404D-B716-E983109CF7A7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{3830F46E-54B3-404D-B716-E983109CF7A7}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{3830F46E-54B3-404D-B716-E983109CF7A7}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{3830F46E-54B3-404D-B716-E983109CF7A7}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {72A281B4-F213-481D-9DCD-EDE61206B430}
24+
EndGlobalSection
25+
EndGlobal

0 commit comments

Comments
 (0)