Skip to content
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
6 changes: 0 additions & 6 deletions Common/Packets/BacktestResultPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,6 @@ public class BacktestResult : Result
/// </summary>
public Dictionary<string, AlgorithmPerformance> RollingWindow { get; set; } = new Dictionary<string, AlgorithmPerformance>();

/// <summary>
/// Rolling window detailed statistics.
/// </summary>
public AlgorithmPerformance TotalPerformance { get; set; }

/// <summary>
/// Default Constructor
/// </summary>
Expand All @@ -226,7 +221,6 @@ public BacktestResult()
public BacktestResult(BacktestResultParameters parameters) : base(parameters)
{
RollingWindow = parameters.RollingWindow;
TotalPerformance = parameters.TotalPerformance;
}
}
} // End of Namespace:
8 changes: 1 addition & 7 deletions Common/Packets/BacktestResultParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
using QuantConnect.Orders;
using QuantConnect.Statistics;
using System.Collections.Generic;
using QuantConnect.Securities.Positions;

namespace QuantConnect.Packets
{
Expand All @@ -32,10 +31,6 @@ public class BacktestResultParameters : BaseResultParameters
/// </summary>
public Dictionary<string, AlgorithmPerformance> RollingWindow { get; set; }

/// <summary>
/// Rolling window detailed statistics.
/// </summary>
public AlgorithmPerformance TotalPerformance { get; set; }
/// <summary>
/// Creates a new instance
/// </summary>
Expand All @@ -49,10 +44,9 @@ public BacktestResultParameters(IDictionary<string, Chart> charts,
AlgorithmPerformance totalPerformance = null,
AlgorithmConfiguration algorithmConfiguration = null,
IDictionary<string, string> state = null)
: base(charts, orders, profitLoss, statistics, runtimeStatistics, orderEvents, algorithmConfiguration, state)
: base(charts, orders, profitLoss, statistics, runtimeStatistics, orderEvents, totalPerformance, algorithmConfiguration, state)
{
RollingWindow = rollingWindow;
TotalPerformance = totalPerformance;
}
}
}
10 changes: 9 additions & 1 deletion Common/Packets/BaseResultParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
*
*/

using System;
using QuantConnect.Orders;
using QuantConnect.Statistics;
using System;
using System.Collections.Generic;

namespace QuantConnect.Packets
Expand Down Expand Up @@ -65,6 +66,11 @@ public class BaseResultParameters
/// </summary>
public AlgorithmConfiguration AlgorithmConfiguration { get; set; }

/// <summary>
/// Rolling window detailed statistics.
/// </summary>
public AlgorithmPerformance TotalPerformance { get; set; }

/// <summary>
/// Creates a new instance
/// </summary>
Expand All @@ -74,6 +80,7 @@ public BaseResultParameters(IDictionary<string, Chart> charts,
IDictionary<string, string> statistics,
IDictionary<string, string> runtimeStatistics,
List<OrderEvent> orderEvents,
AlgorithmPerformance totalPerformance = null,
AlgorithmConfiguration algorithmConfiguration = null,
IDictionary<string, string> state = null)
{
Expand All @@ -85,6 +92,7 @@ public BaseResultParameters(IDictionary<string, Chart> charts,
OrderEvents = orderEvents;
AlgorithmConfiguration = algorithmConfiguration;
State = state;
TotalPerformance = totalPerformance;
}
}
}
8 changes: 4 additions & 4 deletions Common/Packets/LiveResultPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
*
*/

using System;
using System.Linq;
using Newtonsoft.Json;
using QuantConnect.Orders;
using QuantConnect.Logging;
using QuantConnect.Orders;
using QuantConnect.Securities;
using System;
using System.Collections.Generic;
using System.Linq;

namespace QuantConnect.Packets
{
Expand Down Expand Up @@ -109,7 +109,7 @@ public static LiveResultPacket CreateEmpty(LiveNodePacket job)
return new LiveResultPacket(job, new LiveResult(new LiveResultParameters(
new Dictionary<string, Chart>(), new Dictionary<int, Order>(), new Dictionary<DateTime, decimal>(),
new Dictionary<string, Holding>(), new CashBook(), new Dictionary<string, string>(),
new SortedDictionary<string, string>(), new List<OrderEvent>(), new Dictionary<string, string>(),
new SortedDictionary<string, string>(), new List<OrderEvent>(), null, new Dictionary<string, string>(),
new AlgorithmConfiguration(), new Dictionary<string, string>())));
}
} // End Queue Packet:
Expand Down
6 changes: 4 additions & 2 deletions Common/Packets/LiveResultParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
*
*/

using System;
using QuantConnect.Orders;
using QuantConnect.Securities;
using QuantConnect.Statistics;
using System;
using System.Collections.Generic;

namespace QuantConnect.Packets
Expand Down Expand Up @@ -52,10 +53,11 @@ public LiveResultParameters(IDictionary<string, Chart> charts,
IDictionary<string, string> statistics,
IDictionary<string, string> runtimeStatistics,
List<OrderEvent> orderEvents,
AlgorithmPerformance totalPerformance = null,
IDictionary<string, string> serverStatistics = null,
AlgorithmConfiguration algorithmConfiguration = null,
IDictionary<string, string> state = null)
: base(charts, orders, profitLoss, statistics, runtimeStatistics, orderEvents, algorithmConfiguration, state)
: base(charts, orders, profitLoss, statistics, runtimeStatistics, orderEvents, totalPerformance, algorithmConfiguration, state)
{
Holdings = holdings;
CashBook = cashBook;
Expand Down
10 changes: 9 additions & 1 deletion Common/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
* limitations under the License.
*/

using System;
using Newtonsoft.Json;
using QuantConnect.Orders;
using QuantConnect.Packets;
using QuantConnect.Statistics;
using System;
using System.Collections.Generic;

namespace QuantConnect
Expand Down Expand Up @@ -83,6 +84,12 @@ public class Result
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public AlgorithmConfiguration AlgorithmConfiguration { get; set; }

/// <summary>
/// Rolling window detailed statistics.
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public AlgorithmPerformance TotalPerformance { get; set; }

/// <summary>
/// Creates new empty instance
/// </summary>
Expand All @@ -103,6 +110,7 @@ public Result(BaseResultParameters parameters)
OrderEvents = parameters.OrderEvents;
AlgorithmConfiguration = parameters.AlgorithmConfiguration;
State = parameters.State;
TotalPerformance = parameters.TotalPerformance;
}
}
}
32 changes: 31 additions & 1 deletion Common/Statistics/Trade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* limitations under the License.
*/

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

Expand All @@ -23,10 +24,39 @@ namespace QuantConnect.Statistics
/// </summary>
public class Trade
{
private List<Symbol> _symbols;

/// <summary>
/// The symbol of the traded instrument
/// </summary>
public Symbol Symbol { get; set; }
[Obsolete("Use Symbols property instead")]
[JsonIgnore]
public Symbol Symbol
{
get
{
return _symbols != null && _symbols.Count > 0 ? _symbols[0] : Symbol.Empty;
}
private set
{
_symbols = new List<Symbol>() { value };
}
}

/// <summary>
/// Just needed so that "Symbol" is never serialized but can be deserialized, if present, for backward compatibility
/// </summary>
[JsonProperty("Symbol")]
private Symbol SymbolForDeserialization { set => Symbol = value; }

/// <summary>
/// The symbol associated to the traded instruments
/// </summary>
public List<Symbol> Symbols
{
get { return _symbols; }
set { _symbols = value; }
}

/// <summary>
/// The date and time the trade was opened
Expand Down
12 changes: 6 additions & 6 deletions Common/Statistics/TradeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ private void ProcessFillUsingFillToFill(OrderEvent fill, decimal orderFee, decim
{
new Trade
{
Symbol = fill.Symbol,
Symbols = [fill.Symbol],
EntryTime = fill.UtcTime,
EntryPrice = fill.FillPrice,
Direction = fill.FillQuantity > 0 ? TradeDirection.Long : TradeDirection.Short,
Expand All @@ -251,7 +251,7 @@ private void ProcessFillUsingFillToFill(OrderEvent fill, decimal orderFee, decim
// execution has same direction of trade
position.PendingTrades.Add(new Trade
{
Symbol = fill.Symbol,
Symbols = [fill.Symbol],
EntryTime = fill.UtcTime,
EntryPrice = fill.FillPrice,
Direction = fill.FillQuantity > 0 ? TradeDirection.Long : TradeDirection.Short,
Expand Down Expand Up @@ -295,7 +295,7 @@ private void ProcessFillUsingFillToFill(OrderEvent fill, decimal orderFee, decim

var newTrade = new Trade
{
Symbol = trade.Symbol,
Symbols = trade.Symbols,
EntryTime = trade.EntryTime,
EntryPrice = trade.EntryPrice,
Direction = trade.Direction,
Expand Down Expand Up @@ -329,7 +329,7 @@ private void ProcessFillUsingFillToFill(OrderEvent fill, decimal orderFee, decim
{
new Trade
{
Symbol = fill.Symbol,
Symbols =[fill.Symbol],
EntryTime = fill.UtcTime,
EntryPrice = fill.FillPrice,
Direction = fill.FillQuantity > 0 ? TradeDirection.Long : TradeDirection.Short,
Expand Down Expand Up @@ -412,7 +412,7 @@ private void ProcessFillUsingFlatToFlat(OrderEvent fill, decimal orderFee, decim
var direction = Math.Sign(fill.FillQuantity) < 0 ? TradeDirection.Long : TradeDirection.Short;
var trade = new Trade
{
Symbol = fill.Symbol,
Symbols = [fill.Symbol],
EntryTime = entryTime,
EntryPrice = entryAveragePrice,
Direction = direction,
Expand Down Expand Up @@ -515,7 +515,7 @@ private void ProcessFillUsingFlatToReduced(OrderEvent fill, decimal orderFee, de
var direction = totalExecutedQuantity < 0 ? TradeDirection.Long : TradeDirection.Short;
var trade = new Trade
{
Symbol = fill.Symbol,
Symbols = [fill.Symbol],
EntryTime = entryTime,
EntryPrice = entryPrice,
Direction = direction,
Expand Down
26 changes: 21 additions & 5 deletions Engine/Results/LiveTradingResultHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,8 @@ private void Update()

//Add the algorithm statistics first.

var summary = GenerateStatisticsResults(performanceCharts).Summary;
var runtimeStatistics = GetAlgorithmRuntimeStatistics(summary);

var statistics = GenerateStatisticsResults(performanceCharts);
var runtimeStatistics = GetAlgorithmRuntimeStatistics(statistics.Summary);

// since we're sending multiple packets, let's do it async and forget about it
// chart data can get big so let's break them up into groups
Expand Down Expand Up @@ -248,7 +247,9 @@ private void Update()

var deltaStatistics = new Dictionary<string, string>();
var orders = new Dictionary<int, Order>(TransactionHandler.Orders);
var complete = new LiveResultPacket(_job, new LiveResult(new LiveResultParameters(chartComplete, orders, Algorithm.Transactions.TransactionRecord, holdings, Algorithm.Portfolio.CashBook, deltaStatistics, runtimeStatistics, orderEvents, serverStatistics, state: GetAlgorithmState())));
var complete = new LiveResultPacket(_job, new LiveResult(new LiveResultParameters(chartComplete, orders,
Algorithm.Transactions.TransactionRecord, holdings, Algorithm.Portfolio.CashBook, deltaStatistics,
runtimeStatistics, orderEvents, statistics.TotalPerformance, serverStatistics, state: GetAlgorithmState())));
StoreResult(complete);
_nextChartsUpdate = DateTime.UtcNow.Add(ChartUpdateInterval);
Log.Debug("LiveTradingResultHandler.Update(): End-store result");
Expand Down Expand Up @@ -761,7 +762,7 @@ protected void SendFinalResult()
result = new LiveResultPacket(_job,
new LiveResult(new LiveResultParameters(charts, orders, profitLoss, new Dictionary<string, Holding>(),
Algorithm.Portfolio.CashBook, statisticsResults.Summary, runtime, GetOrderEventsToStore(),
algorithmConfiguration: AlgorithmConfiguration.Create(Algorithm, null), state: endState)));
algorithmConfiguration: AlgorithmConfiguration.Create(Algorithm, null), state: endState, totalPerformance: statisticsResults.TotalPerformance)));
}
else
{
Expand Down Expand Up @@ -854,8 +855,15 @@ protected override void StoreResult(Packet packet)
// swap out our charts with the sampled data
minuteCharts.Remove(PortfolioMarginKey);
live.Results.Charts = minuteCharts;

var totalPerformance = live.Results.TotalPerformance;
live.Results.TotalPerformance = null; // we don't need to save this in minute data

SaveResults(CreateKey("minute"), live.Results);

// restore total performance
live.Results.TotalPerformance = totalPerformance;

// 10 minute resolution data, save today
var tenminuteSampler = new SeriesSampler(TimeSpan.FromMinutes(10));
var tenminuteCharts = tenminuteSampler.SampleCharts(live.Results.Charts, start, stop);
Expand Down Expand Up @@ -983,6 +991,14 @@ private static void Truncate(LiveResult result, DateTime start, DateTime stop)
(x.LastUpdateTime != null && x.LastUpdateTime >= start && x.LastUpdateTime <= stop)
).ToDictionary(x => x.Id);

var closedTrades = result.TotalPerformance?.ClosedTrades;
if (closedTrades != null && closedTrades.Count > 0)
{
result.TotalPerformance.ClosedTrades = closedTrades
.Where(x => x.ExitTime >= start && x.ExitTime <= stop)
.ToList();
}

//Log.Trace("LiveTradingResultHandler.Truncate: Truncate Outgoing: " + result.Charts["Strategy Equity"].Series["Equity"].Values.Count);
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/Common/Statistics/PortfolioStatisticsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private List<Trade> CreateITMOptionAssignment(bool win)
{
new Trade
{
Symbol = Symbols.SPY_C_192_Feb19_2016,
Symbols = [Symbols.SPY_C_192_Feb19_2016],
EntryTime = time,
EntryPrice = 80m,
Direction = TradeDirection.Long,
Expand All @@ -193,7 +193,7 @@ private List<Trade> CreateITMOptionAssignment(bool win)
},
new Trade
{
Symbol = Symbols.SPY,
Symbols =[Symbols.SPY],
EntryTime = time.AddMinutes(20),
EntryPrice = 192m,
Direction = TradeDirection.Long,
Expand Down
Loading
Loading