Lean  $LEAN_TAG$
CandlestickJsonConverter.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 
16 using System;
17 using Newtonsoft.Json;
18 using Newtonsoft.Json.Linq;
19 
20 namespace QuantConnect.Util
21 {
22  /// <summary>
23  /// Candlestick Json Converter
24  /// </summary>
25  public class CandlestickJsonConverter : JsonConverter
26  {
27  /// <summary>
28  /// Write Series to Json
29  /// </summary>
30  /// <param name="writer">The Json Writer to use</param>
31  /// <param name="value">The value to written to Json</param>
32  /// <param name="serializer">The Json Serializer to use</param>
33  public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
34  {
35  // Candlesticks will be written as a single array of 5 values: [time, open, high, low, close]
36 
37  var candlestick = value as Candlestick;
38  if (candlestick == null)
39  {
40  return;
41  }
42 
43  writer.WriteStartArray();
44 
45  writer.WriteValue(candlestick.LongTime);
46  writer.WriteValue(candlestick.Open);
47  writer.WriteValue(candlestick.High);
48  writer.WriteValue(candlestick.Low);
49  writer.WriteValue(candlestick.Close);
50 
51  writer.WriteEndArray();
52  }
53 
54  /// <summary>
55  /// Json reader implementation which handles backwards compatiblity for old equity chart points
56  /// </summary>
57  public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
58  {
59  if(reader.TokenType == JsonToken.StartObject)
60  {
61  var chartPoint = serializer.Deserialize<ChartPoint>(reader);
62  if(chartPoint == null)
63  {
64  return null;
65  }
66  return new Candlestick(chartPoint.X, chartPoint.Y, chartPoint.Y, chartPoint.Y, chartPoint.Y);
67  }
68  var jArray = JArray.Load(reader);
69  if(jArray.Count <= 2)
70  {
71  var chartPoint = jArray.ToObject<ChartPoint>();
72  if (chartPoint == null)
73  {
74  return null;
75  }
76  return new Candlestick(chartPoint.X, chartPoint.Y, chartPoint.Y, chartPoint.Y, chartPoint.Y);
77  }
78  return new Candlestick(jArray[0].Value<long>(), jArray[1].Value<decimal?>(), jArray[2].Value<decimal?>(),
79  jArray[3].Value<decimal?>(), jArray[4].Value<decimal?>());
80  }
81 
82  /// <summary>
83  /// Determine if this Converter can convert this type
84  /// </summary>
85  /// <param name="objectType">Type that we would like to convert</param>
86  /// <returns>True if <see cref="Series"/></returns>
87  public override bool CanConvert(Type objectType)
88  {
89  return objectType == typeof(Candlestick);
90  }
91 
92  /// <summary>
93  /// This converter wont be used to read JSON. Will throw exception if manually called.
94  /// </summary>
95  public override bool CanRead => true;
96  }
97 }