using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using TradeIdeas.MiscSupport;
using TradeIdeas.TIProData;
using TradeIdeas.TIProData.Configuration;
using TradeIdeas.TIProData.Interfaces;
using TradeIdeas.TIProGUI;
using TradeIdeas.XML;
namespace TIProDevExtension
{
public partial class HistogramHistoryForm : Form
{
private enum HistogramHistoryTimeInterval { Daily, Hourly, FiveMinutes };
///
/// Top list config string
///
public string Config
{
get { return textBox1.Text; }
}
///
/// The start date
///
public DateTime StartDate
{
get { return dateTimePickerStart.Value; }
}
///
/// The end date
///
public DateTime EndDate
{
get { return dateTimePickerEnd.Value; }
}
///
/// The time interval to be used for the request. Can be Daily, Hourly, or every 5 minutes.
///
private HistogramHistoryTimeInterval TimeInterval
{
get
{
if (radioButtonHour.Checked)
return HistogramHistoryTimeInterval.Hourly;
else if (radioButtonFiveMinutes.Checked)
return HistogramHistoryTimeInterval.FiveMinutes;
else
return HistogramHistoryTimeInterval.Daily;
}
}
///
/// The minimum time of day to use for the requests. Date part of this datetime object should be ignored.
///
public DateTime MinTimeOfDay
{
get { return dateTimePickerMinTOD.Value; }
}
///
/// The maximum time of day to use for the requests. Date part of this datetime object should be ignored.
///
public DateTime MaxTimeOfDay
{
get { return dateTimePickerMaxTOD.Value; }
}
///
/// CSV file to output histogram data.
///
public string FileName
{
get { return labelFile.Text; }
}
private IConnectionMaster _connectionMaster;
public HistogramHistoryForm(IConnectionMaster connectionMaster)
{
_connectionMaster = connectionMaster;
InitializeComponent();
StartPosition = FormStartPosition.CenterParent;
RetrieveFilters();
openFileDialog1.FileName = GuiEnvironment.DataDirectory + "\\output.csv";
}
private List _filters = new List();
//private bool _formLoaded = false;
///
/// Requests config information from the server.
///
private void RetrieveFilters()
{
ConfigurationWindowManager configManager = new ConfigurationWindowManager();
configManager.LoadFromServer(_connectionMaster, ConfigurationType.TopList, OnConfigLoaded, "");
}
private void OnConfigLoaded(ConfigurationWindowManager configManager)
{
_filters = configManager.FiltersInOrder.OrderBy(x => x.LongDescription).ToList();
this.InvokeIfRequired(delegate()
{
//_formLoaded = true;
AddFilters();
});
}
private void AddFilters()
{
this.InvokeIfRequired(delegate ()
{
comboBoxFilters.DataSource = new BindingSource(_filters, null);
comboBoxFilters.DisplayMember = "LongDescription";
comboBoxFilters.ValueMember = "InternalCode";
});
}
private void buttonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void buttonOk_Click(object sender, EventArgs e)
{
textBoxLog.Clear();
DoRequest();
}
private void buttonChooseFile_Click(object sender, EventArgs e)
{
if (labelFile.Text != "None")
openFileDialog1.FileName = labelFile.Text;
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
labelFile.Text = openFileDialog1.FileName;
}
private void DoRequest()
{
Filter filter = (Filter)comboBoxFilters.SelectedItem;
DateTime thisTime = StartDate.Date + new TimeSpan(dateTimePickerMinTOD.Value.Hour, dateTimePickerMinTOD.Value.Minute, dateTimePickerMinTOD.Value.Second);
string baseConfig = textBox1.Text + "&show0=" + filter.InternalCode;
while (thisTime < EndDate)
{
string requestConfig = baseConfig + "&exact_time=" + ServerFormats.ToTimeT(thisTime);
Dictionary message = TradeIdeas.ServerConnection.TalkWithServer.CreateMessage(
"command", "symbol_count_request",
"long_form", requestConfig);
_connectionMaster.SendManager.SendMessage(message, Response);
textBoxLog.Text = thisTime.ToString() + " " + requestConfig + Environment.NewLine + textBoxLog.Text;
thisTime = GetNextTime(thisTime, TimeInterval, MinTimeOfDay, MaxTimeOfDay);
}
}
private DateTime GetNextTime(DateTime current, HistogramHistoryTimeInterval interval, DateTime minTod, DateTime maxTod)
{
DateTime toReturn = current;
if (interval == HistogramHistoryTimeInterval.FiveMinutes || interval == HistogramHistoryTimeInterval.Hourly)
{
if (interval == HistogramHistoryTimeInterval.FiveMinutes)
toReturn = current.AddMinutes(5);
else
toReturn = current.AddMinutes(60);
if (toReturn.TimeOfDay > maxTod.TimeOfDay)
toReturn = toReturn.AddDays(1).Date + new TimeSpan(minTod.Hour, minTod.Minute, minTod.Second);
}
else
toReturn = current.AddDays(1);
if (toReturn.DayOfWeek == DayOfWeek.Saturday)
toReturn = toReturn.AddDays(2);
if (toReturn.DayOfWeek == DayOfWeek.Sunday)
toReturn = toReturn.AddDays(1);
return toReturn;
}
private void Response(byte[] body, object clientId)
{
if (null == body)
{
}
else
{
string debugView = System.Text.Encoding.UTF8.GetString(body);
XmlNode message = XmlHelper.Get(body).Node(0);
File.AppendAllText(FileName, debugView + Environment.NewLine);
}
}
}
}