17 using System.Collections.Generic;
18 using System.Globalization;
20 using Newtonsoft.Json;
21 using Newtonsoft.Json.Linq;
51 public override void WriteJson(JsonWriter writer,
object value, JsonSerializer serializer)
54 if (ReferenceEquals(optimizationBacktest,
null))
return;
56 writer.WriteStartObject();
58 if (!
string.IsNullOrEmpty(optimizationBacktest.Name))
60 writer.WritePropertyName(
"name");
61 writer.WriteValue(optimizationBacktest.Name);
64 if (!
string.IsNullOrEmpty(optimizationBacktest.BacktestId))
66 writer.WritePropertyName(
"id");
67 writer.WriteValue(optimizationBacktest.BacktestId);
69 writer.WritePropertyName(
"progress");
70 writer.WriteValue(optimizationBacktest.Progress);
72 writer.WritePropertyName(
"exitCode");
73 writer.WriteValue(optimizationBacktest.ExitCode);
76 if (optimizationBacktest.StartDate !=
default)
78 writer.WritePropertyName(
"startDate");
79 writer.WriteValue(optimizationBacktest.StartDate.ToStringInvariant(
DateFormat.
UI));
82 if (optimizationBacktest.EndDate !=
default)
84 writer.WritePropertyName(
"endDate");
85 writer.WriteValue(optimizationBacktest.EndDate.ToStringInvariant(
DateFormat.
UI));
88 if (optimizationBacktest.OutOfSampleMaxEndDate !=
null)
90 writer.WritePropertyName(
"outOfSampleMaxEndDate");
91 writer.WriteValue(optimizationBacktest.OutOfSampleMaxEndDate.ToStringInvariant(
DateFormat.
UI));
93 writer.WritePropertyName(
"outOfSampleDays");
94 writer.WriteValue(optimizationBacktest.OutOfSampleDays);
97 if (!optimizationBacktest.Statistics.IsNullOrEmpty())
99 writer.WritePropertyName(
"statistics");
100 writer.WriteStartArray();
101 foreach (var keyValuePair
in optimizationBacktest.Statistics.OrderBy(pair => pair.Key))
103 switch (keyValuePair.Key)
111 var statistic = keyValuePair.Value.Replace(
"%",
string.Empty);
114 writer.WriteValue(result);
117 writer.WriteEndArray();
120 if (optimizationBacktest.ParameterSet !=
null)
122 writer.WritePropertyName(
"parameterSet");
123 serializer.Serialize(writer, optimizationBacktest.ParameterSet.Value);
126 if (optimizationBacktest.Equity !=
null)
128 writer.WritePropertyName(
"equity");
130 var equity = JsonConvert.SerializeObject(optimizationBacktest.Equity.Values);
131 writer.WriteRawValue(equity);
134 writer.WriteEndObject();
147 public override object ReadJson(JsonReader reader, Type objectType,
object existingValue, JsonSerializer serializer)
149 var jObject = JObject.Load(reader);
151 var name = jObject[
"name"].Value<
string>();
152 var hostName = jObject[
"hostName"]?.Value<
string>();
153 var backtestId = jObject[
"id"].Value<
string>();
154 var progress = jObject[
"progress"].Value<decimal>();
155 var exitCode = jObject[
"exitCode"].Value<
int>();
157 var outOfSampleDays = jObject[
"outOfSampleDays"]?.Value<
int>() ??
default;
158 var startDate = jObject[
"startDate"]?.Value<DateTime?>() ??
default;
159 var endDate = jObject[
"endDate"]?.Value<DateTime?>() ??
default;
160 var outOfSampleMaxEndDate = jObject[
"outOfSampleMaxEndDate"]?.Value<DateTime>();
162 var jStatistics = jObject[
"statistics"];
163 Dictionary<string, string> statistics =
default;
164 if (jStatistics !=
null)
166 statistics =
new Dictionary<string, string>
195 var parameterSet = serializer.Deserialize<
ParameterSet>(jObject[
"parameterSet"].CreateReader());
198 if (jObject[
"equity"] !=
null)
200 foreach (var point
in JsonConvert.DeserializeObject<List<Candlestick>>(jObject[
"equity"].ToString()))
202 equity.AddPoint(point);
214 StartDate = startDate,
215 OutOfSampleDays = outOfSampleDays,
216 OutOfSampleMaxEndDate = outOfSampleMaxEndDate,
219 return optimizationBacktest;