Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asset plot improvement #7699

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Common/BaseSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public abstract class BaseSeries
/// </summary>
public int Index { get; set; }

/// <summary>
/// Defines the visual Z index of the series on the chart.
/// </summary>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public int? ZIndex { get; set; }

/// <summary>
/// Chart type for the series:
/// </summary>
Expand Down Expand Up @@ -259,8 +265,6 @@ public enum SeriesType
/// Heatmap Plot (9) -- NOTE: 8 is reserved
Heatmap = 9,
/// Scatter 3D Plot (10)
Scatter3d,
/// A stock plot
StockPlot
Scatter3d
}
}
2 changes: 1 addition & 1 deletion Common/CandlestickSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public override ISeriesPoint ConsolidateChartPoints()
/// <returns></returns>
public override BaseSeries Clone(bool empty = false)
{
var series = new CandlestickSeries(Name, Index, Unit);
var series = new CandlestickSeries(Name, Index, Unit) { ZIndex = ZIndex };

if (!empty)
{
Expand Down
18 changes: 17 additions & 1 deletion Common/Chart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ public class Chart
/// List of Series Objects for this Chart:
public Dictionary<string, BaseSeries> Series = new Dictionary<string, BaseSeries>();

/// <summary>
/// Associated symbol if any, making this an asset plot
/// </summary>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public Symbol Symbol { get; set; }

/// <summary>
/// Default constructor for chart:
/// </summary>
Expand All @@ -60,9 +66,19 @@ public Chart(string name, ChartType type = ChartType.Overlay)
/// Constructor for a chart
/// </summary>
/// <param name="name">String name of the chart</param>
public Chart(string name)
public Chart(string name) : this(name, null)
{
}

/// <summary>
/// Constructor for a chart
/// </summary>
/// <param name="name">String name of the chart</param>
/// <param name="symbol">Associated symbol if any</param>
public Chart(string name, Symbol symbol)
{
Name = name;
Symbol = symbol;
Series = new Dictionary<string, BaseSeries>();
}

Expand Down
7 changes: 7 additions & 0 deletions Common/Orders/OrderJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ public static Order CreateOrderFromJObject(JObject jObject)
order.OrderSubmissionData = new OrderSubmissionData(bidPrice, askPrice, lastPrice);
}

var priceAdjustmentMode = jObject["PriceAdjustmentMode"];
if (priceAdjustmentMode != null && priceAdjustmentMode.Type != JTokenType.Null)
{
var value = priceAdjustmentMode.Value<int>();
order.PriceAdjustmentMode = (DataNormalizationMode)value;
}

var lastFillTime = jObject["LastFillTime"];
var lastUpdateTime = jObject["LastUpdateTime"];
var canceledTime = jObject["CanceledTime"];
Expand Down
3 changes: 2 additions & 1 deletion Common/Series.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ public override BaseSeries Clone(bool empty = false)
var series = new Series(Name, SeriesType, Index, Unit)
{
Color = Color,
ScatterMarkerSymbol = ScatterMarkerSymbol
ZIndex = ZIndex,
ScatterMarkerSymbol = ScatterMarkerSymbol,
};

if (!empty)
Expand Down
19 changes: 17 additions & 2 deletions Common/Util/SeriesJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
*/

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Drawing;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;

namespace QuantConnect.Util
{
Expand Down Expand Up @@ -52,6 +52,12 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
writer.WritePropertyName("SeriesType");
writer.WriteValue(baseSeries.SeriesType);

if (baseSeries.ZIndex.HasValue)
{
writer.WritePropertyName("ZIndex");
writer.WriteValue(baseSeries.ZIndex.Value);
}

switch (value)
{
case Series series:
Expand Down Expand Up @@ -103,13 +109,21 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
var seriesType = (SeriesType)jObject["SeriesType"].Value<int>();
var values = (JArray)jObject["Values"];

int? zindex = null;
var jZIndex = jObject["ZIndex"];
if (jZIndex != null && jZIndex.Type != JTokenType.Null)
{
zindex = jZIndex.Value<int>();
}

if (seriesType == SeriesType.Candle)
{
return new CandlestickSeries()
{
Name = name,
Unit = unit,
Index = index,
ZIndex = zindex,
SeriesType = seriesType,
Values = values.ToObject<List<Candlestick>>(serializer).Where(x => x != null).Cast<ISeriesPoint>().ToList()
};
Expand All @@ -120,6 +134,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
Name = name,
Unit = unit,
Index = index,
ZIndex = zindex,
SeriesType = seriesType,
Color = jObject["Color"].ToObject<Color>(serializer),
ScatterMarkerSymbol = jObject["ScatterMarkerSymbol"].ToObject<ScatterMarkerSymbol>(serializer),
Expand Down
36 changes: 36 additions & 0 deletions Tests/Common/CandlestickSeriesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using NUnit.Framework;

namespace QuantConnect.Tests.Common
{
[TestFixture]
public class CandlestickSeriesTest
{
[Test]
public void Clone()
{
var series = new CandlestickSeries("A", 8, "TT") { ZIndex = 98 };
var result = (CandlestickSeries)series.Clone();

Assert.AreEqual(series.Name, result.Name);
Assert.AreEqual(series.Unit, result.Unit);
Assert.AreEqual(series.SeriesType, result.SeriesType);
Assert.AreEqual(series.Index, result.Index);
Assert.AreEqual(series.ZIndex, result.ZIndex);
}
}
}
4 changes: 3 additions & 1 deletion Tests/Common/Orders/OrderJsonConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,8 @@ public void RoundTripUsingJsonConverter(string timeInForceStr)
CanceledTime = DateTime.UtcNow,
Status = OrderStatus.Filled,
OrderSubmissionData = new OrderSubmissionData(321.47m, 321.48m, 321.49m),
Properties = { TimeInForce = timeInForce }
Properties = { TimeInForce = timeInForce },
PriceAdjustmentMode = DataNormalizationMode.Adjusted
};

var converter = new OrderJsonConverter();
Expand Down Expand Up @@ -541,6 +542,7 @@ public void RoundTripUsingJsonConverter(string timeInForceStr)
Assert.AreEqual(expected.OrderSubmissionData.AskPrice, actual.OrderSubmissionData.AskPrice);
Assert.AreEqual(expected.OrderSubmissionData.BidPrice, actual.OrderSubmissionData.BidPrice);
Assert.AreEqual(expected.OrderSubmissionData.LastPrice, actual.OrderSubmissionData.LastPrice);
Assert.AreEqual(expected.PriceAdjustmentMode, actual.PriceAdjustmentMode);
}

[Test]
Expand Down
20 changes: 18 additions & 2 deletions Tests/Common/SeriesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

using System;
using System.Linq;
using System.Drawing;
using NUnit.Framework;

namespace QuantConnect.Tests.Common
Expand All @@ -26,10 +27,25 @@ public class SeriesTests
public void RespectsMostRecentTimeOnDuplicatePoints()
{
var series = new Series();
series.AddPoint(DateTime.Today, 1m);
series.AddPoint(DateTime.Today, 2m);
series.AddPoint(new DateTime(2023, 2, 2), 1m);
series.AddPoint(new DateTime(2023, 2, 2), 2m);
Assert.AreEqual(1, series.Values.Count);
Assert.AreEqual(2m, series.GetValues<ChartPoint>().Single().y);
}

[Test]
public void Clone()
{
var series = new Series("A", SeriesType.Line, "TT", Color.AliceBlue, ScatterMarkerSymbol.Circle) { ZIndex = 98, Index = 8 };
var result = (Series)series.Clone();

Assert.AreEqual(series.Name, result.Name);
Assert.AreEqual(series.Unit, result.Unit);
Assert.AreEqual(series.SeriesType, result.SeriesType);
Assert.AreEqual(series.Color.ToArgb(), result.Color.ToArgb());
Assert.AreEqual(series.ScatterMarkerSymbol, result.ScatterMarkerSymbol);
Assert.AreEqual(series.Index, result.Index);
Assert.AreEqual(series.ZIndex, result.ZIndex);
}
}
}
9 changes: 6 additions & 3 deletions Tests/Common/Util/SeriesJsonConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ namespace QuantConnect.Tests.Common.Util
[TestFixture]
public class SeriesJsonConverterTests
{
[Test]
public void SerializeDeserializeReturnsSameSeriesValue()
[TestCase(null)]
[TestCase(87)]
public void SerializeDeserializeReturnsSameSeriesValue(int? zIndex)
{
var date = new DateTime(2050, 1, 1, 1, 1, 1);
var series = new Series("Pepito Grillo", SeriesType.Bar, "%", Color.Blue, ScatterMarkerSymbol.Diamond);
var series = new Series("Pepito Grillo", SeriesType.Bar, "%", Color.Blue, ScatterMarkerSymbol.Diamond) { ZIndex = zIndex, Index = 6 };
series.AddPoint(date, 1);
series.AddPoint(date.AddSeconds(1), 2);

Expand All @@ -50,6 +51,8 @@ public void SerializeDeserializeReturnsSameSeriesValue()
Assert.AreEqual(series.SeriesType, result.SeriesType);
Assert.AreEqual(series.Color.ToArgb(), result.Color.ToArgb());
Assert.AreEqual(series.ScatterMarkerSymbol, result.ScatterMarkerSymbol);
Assert.AreEqual(series.ZIndex, result.ZIndex);
Assert.AreEqual(series.Index, result.Index);
}

[Test]
Expand Down
Loading