Skip to content

Commit

Permalink
Minor candlestick json converter backwards compatility fix (#7428)
Browse files Browse the repository at this point in the history
- Minor bug fix for candlestick json converter backwards compatibility,
  detected running the new report generator on an old lean backtest
  result json
  • Loading branch information
Martin-Molinero authored Aug 9, 2023
1 parent 1fd0dd3 commit d8c58fb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Common/ChartPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public decimal y
/// Shortcut for <see cref="x"/> for C# naming conventions
/// </summary>
[JsonIgnore]
public decimal X => x;
public long X => x;

/// <summary>
/// Shortcut for <see cref="y"/> for C# naming conventions
Expand Down
9 changes: 9 additions & 0 deletions Common/Util/CandlestickJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
/// </summary>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if(reader.TokenType == JsonToken.StartObject)
{
// backwards compatibility with old format
var jObject = JObject.Load(reader);

var chartPoint = new ChartPoint(jObject["x"].Value<long>(), jObject["y"].Value<decimal>());
return new Candlestick(chartPoint.X, chartPoint.Y, chartPoint.Y, chartPoint.Y, chartPoint.Y);
}

var jArray = JArray.Load(reader);
return new Candlestick(jArray[0].Value<long>(), jArray[1].Value<decimal>(), jArray[2].Value<decimal>(),
jArray[3].Value<decimal>(), jArray[4].Value<decimal>());
Expand Down
16 changes: 16 additions & 0 deletions Tests/Common/Util/CandlestickJsonConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,21 @@ public void SerializeDeserializeReturnsSameValue()
Assert.AreEqual(candlestick.Low, result.Low);
Assert.AreEqual(candlestick.Close, result.Close);
}

[Test]
public void BackwardsCompatility()
{
var dateTime = new DateTime(2023, 08, 01, 12, 11, 10);
var chartPoint = new ChartPoint(dateTime, 100);

var serializedChartPoint = JsonConvert.SerializeObject(chartPoint);
var result = (Candlestick)JsonConvert.DeserializeObject(serializedChartPoint, typeof(Candlestick));

Assert.AreEqual(chartPoint.Time, result.Time);
Assert.AreEqual(chartPoint.y, result.Open);
Assert.AreEqual(chartPoint.y, result.High);
Assert.AreEqual(chartPoint.y, result.Low);
Assert.AreEqual(chartPoint.y, result.Close);
}
}
}

0 comments on commit d8c58fb

Please sign in to comment.