我已声明我的名单如下
private List<QuickTradeObject> AllTrades = new List<QuickTradeObject>();
当我反序列化json时,出现强制转换错误。我该如何解决?
using(StreamReader reader = new StreamReader(filename))
{
string jsonString = reader.ReadToEnd();
AllTrades = JsonConvert.DeserializeObject<QuickTradeObject>(jsonString);
}
NinjaScript文件错误代码行列 PlotTrades.cs无法将类型'NinjaTrader.NinjaScript.Indicators.QuickTradeObject'隐式转换为'System.Collections.Generic.List'CS0029 72 16
#region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Gui.SuperDom;
using NinjaTrader.Gui.Tools;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.Core.FloatingPoint;
using NinjaTrader.NinjaScript.DrawingTools;
#endregion
using Newtonsoft.Json;
using System.IO;
//This namespace holds Indicators in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Indicators
{
public class PlotTrades : Indicator
{
private Series<double> EntryPrice;
private Series<double> ExitPrice;
private Series<DateTime> EntryTime;
private Series<DateTime> ExitTime;
private Series<string> TradeDirection;
private List<QuickTradeObject> AllTrades = new List<QuickTradeObject>();
private string filename=@"c:\temp\2019-10-12--08-58-41EURUSDVShapedBreakout.json.json";
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Indicator here.";
Name = "PlotTrades";
Calculate = Calculate.OnBarClose;
IsOverlay = true;
DisplayInDataBox = true;
DrawOnPricePanel = true;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = true;
ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
//Disable this property if your indicator requires custom values that cumulate with each new market data event.
//See Help Guide for additional information.
IsSuspendedWhileInactive = true;
AddLine(Brushes.Orange, 1, "Equity");
}
else if (State == State.Configure)
{
}
else if (State == State.DataLoaded)
{
EntryPrice = new Series<double>(this);
ExitPrice = new Series<double>(this);
EntryTime = new Series<DateTime>(this);
ExitTime = new Series<DateTime>(this);
TradeDirection = new Series<string>(this);
using(StreamReader reader = new StreamReader(filename))
{
string jsonString = reader.ReadToEnd();
AllTrades = JsonConvert.DeserializeObject<QuickTradeObject>(jsonString);
}
Print( AllTrades.ToString());
}
}
protected override void OnBarUpdate()
{
//Add your custom indicator logic here.
}
}
public class QuickTradeObject
{
public DateTime sessionDate;
public int TradeNumber;
public DateTime entryTime;
public DateTime exitTime;
public DateTime halfExitTime;
public double entryPrice;
public double halfExitPrice;
public double fullExitPrice;
public string Direction;
public double highestHigh;
public double lowestLow;
public double gains;
public double risk;
public double mfe;
public double mae;
public double todayTarget;
public double todayTargetRemainingatStart;
public double todayTargetRemainingatEnd;
public double cumulativeGainsToday;
public double cumulativeMAEtoday;
public double cumulativeMFEtoday;
public bool MeetTargetToday;
public double reEntryTimeinMinutes;
public double timeInTrade;
public bool reEnteredSameDirection;
public bool CrossedWeekend;
public bool trigger50Percenttail;
public bool pulledBackin5;
public bool movedBackin5;
public bool movedStop45;
public double MaxMoveBeforePullback;
public double MaxMoveAgainstAfterPullback;
public DateTime pulledBackin5Time;
public DateTime movedBackinTime;
public string url;
}
}
答案 0 :(得分:0)
使用以下内容修复了该问题 AllTrades = JsonConvert.DeserializeObject>(jsonString);