|
| 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 | +} |
0 commit comments