/*
* 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 System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using QuantConnect.Util;
namespace QuantConnect
{
///
/// Chart Series Object - Series data and properties for a chart:
///
[JsonConverter(typeof(SeriesJsonConverter))]
public class Series : BaseSeries
{
///
/// Color the series
///
[JsonConverter(typeof(ColorJsonConverter))]
public Color Color { get; set; } = Color.Empty;
///
/// Shape or symbol for the marker in a scatter plot
///
public ScatterMarkerSymbol ScatterMarkerSymbol { get; set; } = ScatterMarkerSymbol.None;
///
/// Default constructor for chart series
///
public Series() : base() { }
///
/// Constructor method for Chart Series
///
/// Name of the chart series
public Series(string name)
: base(name, SeriesType.Line)
{
}
///
/// Foundational constructor on the series class
///
/// Name of the series
/// Type of the series
/// Index position on the chart of the series
public Series(string name, SeriesType type, int index)
: this(name, type, index, "$")
{
}
///
/// Foundational constructor on the series class
///
/// Name of the series
/// Type of the series
/// Index position on the chart of the series
/// Unit for the series axis
public Series(string name, SeriesType type, int index, string unit)
: this(name, type, unit, Color.Empty, ScatterMarkerSymbol.None)
{
Index = index;
}
///
/// Constructor method for Chart Series
///
/// Name of the chart series
/// Type of the chart series
/// Unit of the series
public Series(string name, SeriesType type = SeriesType.Line, string unit = "$")
: this(name, type, unit, Color.Empty)
{
}
///
/// Constructor method for Chart Series
///
/// Name of the chart series
/// Type of the chart series
/// Unit of the series
/// Color of the series
public Series(string name, SeriesType type, string unit, Color color)
: this(name, type, unit, color, ScatterMarkerSymbol.None)
{
}
///
/// Constructor method for Chart Series
///
/// Name of the chart series
/// Type of the chart series
/// Unit of the series
/// Color of the series
/// Symbol for the marker in a scatter plot series
public Series(string name, SeriesType type, string unit, Color color, ScatterMarkerSymbol symbol = ScatterMarkerSymbol.None)
: base(name, type, 0, unit)
{
Color = color;
ScatterMarkerSymbol = symbol;
}
///
/// Add a new point to this series
///
/// Time of the chart point
/// Value of the chart point
public void AddPoint(DateTime time, decimal value)
{
ISeriesPoint point;
if (SeriesType == SeriesType.Scatter)
{
point = new ScatterChartPoint(time, value);
}
else
{
point = new ChartPoint(time, value);
}
AddPoint(point);
}
///
/// Add a new point to this series
///
/// The data point to add
public override void AddPoint(ISeriesPoint point)
{
if (point as ChartPoint == null)
{
throw new ArgumentException("Series.AddPoint requires a ChartPoint object");
}
base.AddPoint(point);
}
///
/// Add a new point to this series
///
/// The time of the data point
/// The values of the data point
public override void AddPoint(DateTime time, List values)
{
if (values.Count > 1)
{
throw new ArgumentException("Series.AddPoint requires a single value");
}
AddPoint(time, values.Count > 0 ? values[0] : 0);
}
///
/// Will sum up all chart points into a new single value, using the time of latest point
///
/// The new chart point
public override ISeriesPoint ConsolidateChartPoints()
{
if (Values.Count <= 0) return null;
var sum = 0m;
foreach (ChartPoint point in Values)
{
if(point.y.HasValue)
{
sum += point.y.Value;
}
}
var lastPoint = (ChartPoint)Values.Last();
return new ChartPoint(lastPoint.x, sum);
}
///
/// Return a new instance clone of this object
///
///
public override BaseSeries Clone(bool empty = false)
{
var series = new Series(Name, SeriesType, Index, Unit)
{
Color = Color,
ZIndex = ZIndex,
Tooltip = Tooltip,
IndexName = IndexName,
ScatterMarkerSymbol = ScatterMarkerSymbol,
};
if (!empty)
{
series.Values = CloneValues();
}
return series;
}
}
///
/// Shape or symbol for the marker in a scatter plot
///
[JsonConverter(typeof(StringEnumConverter))]
public enum ScatterMarkerSymbol
{
/// Circle symbol (0)
[EnumMember(Value = "none")]
None,
/// Circle symbol (1)
[EnumMember(Value = "circle")]
Circle,
/// Square symbol (2)
[EnumMember(Value = "square")]
Square,
/// Diamond symbol (3)
[EnumMember(Value = "diamond")]
Diamond,
/// Triangle symbol (4)
[EnumMember(Value = "triangle")]
Triangle,
/// Triangle-down symbol (5)
[EnumMember(Value = "triangle-down")]
TriangleDown
}
}