Lean  $LEAN_TAG$
IchimokuKinkoHyo.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
17 using System;
18 
20 {
21  /// <summary>
22  /// This indicator computes the Ichimoku Kinko Hyo indicator. It consists of the following main indicators:
23  /// Tenkan-sen: (Highest High + Lowest Low) / 2 for the specific period (normally 9)
24  /// Kijun-sen: (Highest High + Lowest Low) / 2 for the specific period (normally 26)
25  /// Senkou A Span: (Tenkan-sen + Kijun-sen )/ 2 from a specific number of periods ago (normally 26)
26  /// Senkou B Span: (Highest High + Lowest Low) / 2 for the specific period (normally 52), from a specific number of periods ago (normally 26)
27  /// </summary>
29  {
30  /// <summary>
31  /// The Tenkan-sen component of the Ichimoku indicator
32  /// </summary>
34 
35  /// <summary>
36  /// The Kijun-sen component of the Ichimoku indicator
37  /// </summary>
39 
40  /// <summary>
41  /// The Senkou A Span component of the Ichimoku indicator
42  /// </summary>
44 
45  /// <summary>
46  /// The Senkou B Span component of the Ichimoku indicator
47  /// </summary>
49 
50  /// <summary>
51  /// The Chikou Span component of the Ichimoku indicator
52  /// </summary>
54 
55  /// <summary>
56  /// The Tenkan-sen Maximum component of the Ichimoku indicator
57  /// </summary>
59 
60  /// <summary>
61  /// The Tenkan-sen Minimum component of the Ichimoku indicator
62  /// </summary>
64 
65  /// <summary>
66  /// The Kijun-sen Maximum component of the Ichimoku indicator
67  /// </summary>
69 
70  /// <summary>
71  /// The Kijun-sen Minimum component of the Ichimoku indicator
72  /// </summary>
74 
75  /// <summary>
76  /// The Senkou B Maximum component of the Ichimoku indicator
77  /// </summary>
79 
80  /// <summary>
81  /// The Senkou B Minimum component of the Ichimoku indicator
82  /// </summary>
84 
85  /// <summary>
86  /// The Delayed Tenkan Senkou A component of the Ichimoku indicator
87  /// </summary>
89 
90  /// <summary>
91  /// The Delayed Kijun Senkou A component of the Ichimoku indicator
92  /// </summary>
94 
95  /// <summary>
96  /// The Delayed Maximum Senkou B component of the Ichimoku indicator
97  /// </summary>
99 
100  /// <summary>
101  /// The Delayed Minimum Senkou B component of the Ichimoku indicator
102  /// </summary>
104 
105  /// <summary>
106  /// Creates a new IchimokuKinkoHyo indicator from the specific periods
107  /// </summary>
108  /// <param name="tenkanPeriod">The Tenkan-sen period</param>
109  /// <param name="kijunPeriod">The Kijun-sen period</param>
110  /// <param name="senkouAPeriod">The Senkou A Span period</param>
111  /// <param name="senkouBPeriod">The Senkou B Span period</param>
112  /// <param name="senkouADelayPeriod">The Senkou A Span delay</param>
113  /// <param name="senkouBDelayPeriod">The Senkou B Span delay</param>
114  public IchimokuKinkoHyo(int tenkanPeriod = 9, int kijunPeriod = 26, int senkouAPeriod = 26,
115  int senkouBPeriod = 52, int senkouADelayPeriod = 26, int senkouBDelayPeriod = 26)
116  : this(
117  $"ICHIMOKU({tenkanPeriod},{kijunPeriod},{senkouAPeriod},{senkouBPeriod},{senkouADelayPeriod},{senkouBDelayPeriod})",
118  tenkanPeriod,
119  kijunPeriod,
120  senkouAPeriod,
121  senkouBPeriod,
122  senkouADelayPeriod,
123  senkouBDelayPeriod
124  )
125  {
126  }
127 
128  /// <summary>
129  /// Creates a new IchimokuKinkoHyo indicator from the specific periods
130  /// </summary>
131  /// <param name="name">The name of this indicator</param>
132  /// <param name="tenkanPeriod">The Tenkan-sen period</param>
133  /// <param name="kijunPeriod">The Kijun-sen period</param>
134  /// <param name="senkouAPeriod">The Senkou A Span period</param>
135  /// <param name="senkouBPeriod">The Senkou B Span period</param>
136  /// <param name="senkouADelayPeriod">The Senkou A Span delay</param>
137  /// <param name="senkouBDelayPeriod">The Senkou B Span delay</param>
138  public IchimokuKinkoHyo(string name, int tenkanPeriod = 9, int kijunPeriod = 26, int senkouAPeriod = 26, int senkouBPeriod = 52, int senkouADelayPeriod = 26, int senkouBDelayPeriod = 26)
139  : base(name)
140  {
141  WarmUpPeriod = Math.Max(tenkanPeriod + senkouADelayPeriod, kijunPeriod + senkouADelayPeriod);
142  WarmUpPeriod = Math.Max(WarmUpPeriod, senkouBPeriod + senkouBDelayPeriod);
143 
144  TenkanMaximum = new Maximum(name + "_TenkanMax", tenkanPeriod);
145  TenkanMinimum = new Minimum(name + "_TenkanMin", tenkanPeriod);
146  KijunMaximum = new Maximum(name + "_KijunMax", kijunPeriod);
147  KijunMinimum = new Minimum(name + "_KijunMin", kijunPeriod);
148  SenkouBMaximum = new Maximum(name + "_SenkouBMaximum", senkouBPeriod);
149  SenkouBMinimum = new Minimum(name + "_SenkouBMinimum", senkouBPeriod);
150  DelayedTenkanSenkouA = new Delay(name + "DelayedTenkan", senkouADelayPeriod);
151  DelayedKijunSenkouA = new Delay(name + "DelayedKijun", senkouADelayPeriod);
152  DelayedMaximumSenkouB = new Delay(name + "DelayedMax", senkouBDelayPeriod);
153  DelayedMinimumSenkouB = new Delay(name + "DelayedMin", senkouBDelayPeriod);
154  Chikou = new Delay(name + "_Chikou", senkouADelayPeriod);
155 
157  name + "_SenkouA",
158  input => SenkouA.IsReady ? (DelayedTenkanSenkouA.Current.Value + DelayedKijunSenkouA.Current.Value) / 2 : decimal.Zero,
160  () =>
161  {
162  Tenkan.Reset();
163  Kijun.Reset();
164  });
165 
167  name + "_SenkouB",
168  input => SenkouB.IsReady ? (DelayedMaximumSenkouB.Current.Value + DelayedMinimumSenkouB.Current.Value) / 2 : decimal.Zero,
170  () =>
171  {
172  Tenkan.Reset();
173  Kijun.Reset();
174  });
175 
177  name + "_Tenkan",
178  input => Tenkan.IsReady ? (TenkanMaximum.Current.Value + TenkanMinimum.Current.Value) / 2 : decimal.Zero,
179  tenkan => TenkanMaximum.IsReady && TenkanMinimum.IsReady,
180  () =>
181  {
182  TenkanMaximum.Reset();
183  TenkanMinimum.Reset();
184  });
185 
187  name + "_Kijun",
188  input => Kijun.IsReady ? (KijunMaximum.Current.Value + KijunMinimum.Current.Value) / 2 : decimal.Zero,
189  kijun => KijunMaximum.IsReady && KijunMinimum.IsReady,
190  () =>
191  {
192  KijunMaximum.Reset();
193  KijunMinimum.Reset();
194  });
195  }
196 
197  /// <summary>
198  /// Returns true if all of the sub-components of the Ichimoku indicator is ready
199  /// </summary>
200  public override bool IsReady => Tenkan.IsReady && Kijun.IsReady && SenkouA.IsReady && SenkouB.IsReady;
201 
202  /// <summary>
203  /// Required period, in data points, for the indicator to be ready and fully initialized.
204  /// </summary>
205  public int WarmUpPeriod { get; }
206 
207  /// <summary>
208  /// Computes the next value of this indicator from the given state
209  /// </summary>
210  /// <param name="input">The input given to the indicator</param>
211  protected override decimal ComputeNextValue(IBaseDataBar input)
212  {
213  TenkanMaximum.Update(input.Time, input.High);
214  TenkanMinimum.Update(input.Time, input.Low);
215  Tenkan.Update(input.Time, input.Close);
216  if (Tenkan.IsReady) DelayedTenkanSenkouA.Update(input.Time, Tenkan.Current.Value);
217 
218  KijunMaximum.Update(input.Time, input.High);
219  KijunMinimum.Update(input.Time, input.Low);
220  Kijun.Update(input.Time, input.Close);
221  if (Kijun.IsReady) DelayedKijunSenkouA.Update(input.Time, Kijun.Current.Value);
222 
223  SenkouA.Update(input.Time, input.Close);
224 
225  SenkouB.Update(input.Time, input.Close);
226  SenkouBMaximum.Update(input.Time, input.High);
227  if (SenkouBMaximum.IsReady) DelayedMaximumSenkouB.Update(input.Time, SenkouBMaximum.Current.Value);
228  SenkouBMinimum.Update(input.Time, input.Low);
229  if (SenkouBMinimum.IsReady) DelayedMinimumSenkouB.Update(input.Time, SenkouBMinimum.Current.Value);
230 
231  Chikou.Update(input.Time, input.Close);
232 
233  return input.Close;
234  }
235 
236  /// <summary>
237  /// Resets this indicator to its initial state
238  /// </summary>
239  public override void Reset()
240  {
241  base.Reset();
242  TenkanMaximum.Reset();
243  TenkanMinimum.Reset();
244  Tenkan.Reset();
245  KijunMaximum.Reset();
246  KijunMinimum.Reset();
247  Kijun.Reset();
250  SenkouA.Reset();
251  SenkouBMaximum.Reset();
252  SenkouBMinimum.Reset();
255  Chikou.Reset();
256  SenkouB.Reset();
257  }
258  }
259 }