Код:
#region Using declarations
using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.IO;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Gui.Tools;
using NinjaTrader.NinjaScript;
using System.Text.RegularExpressions;
#endregion
namespace NinjaTrader.Gui.NinjaScript
{
public class NRDToCSV : AddOnBase
{
private NTMenuItem menuItem;
private NTMenuItem existingMenuItemInControlCenter;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Name = "NRDToCSV";
Description = "*.nrd to *.csv market replay files convertion";
}
}
protected override void OnWindowCreated(Window window)
{
ControlCenter cc = window as ControlCenter;
if (cc == null) return;
existingMenuItemInControlCenter = cc.FindFirst("ControlCenterMenuItemTools") as NTMenuItem;
if (existingMenuItemInControlCenter == null) return;
menuItem = new NTMenuItem { Header = "NRD to CSV", Style = Application.Current.TryFindResource("MainMenuItem") as Style };
existingMenuItemInControlCenter.Items.Add(menuItem);
menuItem.Click += OnMenuItemClick;
}
protected override void OnWindowDestroyed(Window window)
{
if (menuItem != null && window is ControlCenter)
{
if (existingMenuItemInControlCenter != null && existingMenuItemInControlCenter.Items.Contains(menuItem))
existingMenuItemInControlCenter.Items.Remove(menuItem);
menuItem.Click -= OnMenuItemClick;
menuItem = null;
}
}
private void OnMenuItemClick(object sender, RoutedEventArgs e)
{
Core.Globals.RandomDispatcher.BeginInvoke(new Action(() => new NRDToCSVWindow().Show()));
}
}
public class NRDToCSVWindow : NTWindow, IWorkspacePersistence
{
private TextBox tbNrdRootDir;
private TextBox tbCsvRootDir;
private Button bConvert;
private TextBox tbOutput;
public NRDToCSVWindow()
{
Caption = "NRD to CSV";
Width = 512;
Height = 640;
Content = BuildContent();
Loaded += (o, e) =>
{
if (WorkspaceOptions == null)
WorkspaceOptions = new WorkspaceOptions("NRDToCSV-" + Guid.NewGuid().ToString("N"), this);
};
Closing += (o, e) =>
{
if (bConvert != null)
bConvert.Click -= OnConvertButtonClick;
};
}
private DependencyObject BuildContent()
{
double margin = (double) FindResource("MarginBase");
tbNrdRootDir = new TextBox() {
IsReadOnly = true,
Margin = new Thickness(margin, 0, margin, margin),
Text = Path.Combine(NinjaTrader.Core.Globals.UserDataDir, "db", "replay"),
};
Label lNrdRootDir = new Label() {
Foreground = FindResource("FontLabelBrush") as Brush,
Margin = new Thickness(margin, 0, margin, 0),
Content = "_NRD Replays root directory:" ,
};
tbCsvRootDir = new TextBox() {
Margin = new Thickness(margin, 0, margin, margin),
Text = Path.Combine(NinjaTrader.Core.Globals.UserDataDir, "db", "replay.csv"),
};
Label lCsvRootDir = new Label() {
Foreground = FindResource("FontLabelBrush") as Brush,
Margin = new Thickness(margin, margin, margin, 0),
Content = "Converted _CSV root directory:",
};
bConvert = new Button()
{
Margin = new Thickness(margin),
IsDefault = true,
Content = "_Convert",
};
bConvert.Click += OnConvertButtonClick;
tbOutput = new TextBox() {
IsReadOnly = true,
HorizontalScrollBarVisibility = ScrollBarVisibility.Auto,
VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
Margin = new Thickness(margin),
};
Grid grid = new Grid() { Background = new SolidColorBrush(Colors.Transparent) };
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto } );
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto } );
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto } );
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto } );
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto } );
grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) } );
Grid.SetRow(lNrdRootDir, 0);
Grid.SetRow(tbNrdRootDir, 1);
Grid.SetRow(lCsvRootDir, 2);
Grid.SetRow(tbCsvRootDir, 3);
Grid.SetRow(bConvert, 4);
Grid.SetRow(tbOutput, 5);
grid.Children.Add(lNrdRootDir);
grid.Children.Add(tbNrdRootDir);
grid.Children.Add(lCsvRootDir);
grid.Children.Add(tbCsvRootDir);
grid.Children.Add(bConvert);
grid.Children.Add(tbOutput);
return grid;
}
private void OnConvertButtonClick(object sender, RoutedEventArgs e)
{
if (tbOutput == null) return;
tbOutput.Clear();
string nrdDir = tbNrdRootDir.Text;
string csvDir = tbCsvRootDir.Text;
if (!Directory.Exists(nrdDir)) {
tbOutput.AppendText(string.Format("ERROR: The NRD root directory \"{0}\" not found\r\n", nrdDir));
return;
}
if (!Directory.Exists(csvDir)) {
try
{
Directory.CreateDirectory(csvDir);
}
catch (Exception error)
{
tbOutput.AppendText(string.Format("ERROR: Unable to create the CSV root directory \"{0}\": {1}\r\n", csvDir, error.ToString()));
}
return;
}
string[] subDirectoryEntries = Directory.GetDirectories(nrdDir);
if (subDirectoryEntries.Length == 0) {
tbOutput.AppendText(string.Format("ERROR: The NRD root directory \"{0}\" is empty\r\n", nrdDir));
return;
}
foreach (string subDirectory in subDirectoryEntries)
ProcessDirectory(subDirectory, csvDir);
tbOutput.AppendText("Conversion complete\r\n");
}
private void ProcessDirectory(string nrdDirectory, string csvDir)
{
string[] fileEntries = Directory.GetFiles(nrdDirectory, "*.nrd");
if (fileEntries.Length == 0)
{
tbOutput.AppendText(string.Format("WARNING: No *.nrd files found in \"{0}\" directory. Skipped\r\n", nrdDirectory));
return;
}
// int i = 0;
foreach (string fileName in fileEntries)
{
string fullName = Path.GetFileName(Path.GetDirectoryName(fileName));
Collection instruments = NinjaTrader.Cbi.InstrumentList.GetInstruments(fullName);
if (instruments.Count == 0) {
tbOutput.AppendText(string.Format("Unable to find an instrument name \"{0}\"\r\n", fullName));
continue;
} else if (instruments.Count > 1) {
tbOutput.AppendText(string.Format("More than one instrument was identified for name \"{0}\"\r\n", fullName));
continue;
}
string name = Path.GetFileNameWithoutExtension(fileName);
DateTime date = new DateTime(
Convert.ToInt16(name.Substring(0, 4)),
Convert.ToInt16(name.Substring(4, 2)),
Convert.ToInt16(name.Substring(6, 2)));
foreach (Cbi.Instrument instrument in instruments) {
string csvFileName = string.Format("{0}.csv", Path.Combine(csvDir, instrument.FullName, name));
string csvFileDir = Path.GetDirectoryName(csvFileName);
if (!Directory.Exists(csvFileDir))
Directory.CreateDirectory(csvFileDir);
tbOutput.AppendText(string.Format("Convert \"{0}\\{1}.nrd\" to \"{2}\\{3}.csv\": {4:yyyy-mm-dd}\r\n",
fullName, name, instrument.FullName, name, date));
MarketReplay.DumpMarketDepth(instrument, date.AddDays(1), date.AddDays(1), csvFileName);
}
// if (i++ == 1) break;
}
}
public void Restore(XDocument document, XElement element) { }
public void Save(XDocument document, XElement element) { }
public WorkspaceOptions WorkspaceOptions { get; set; }
}
}