Files
TradeBlotter/UIUtils/ClosePositionDialogNoStop.xaml.cs

209 lines
5.4 KiB
C#

using MarketData.DataAccess;
using MarketData.Generator.Interface;
using MarketData.MarketDataModel;
using MarketData.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using Position=MarketData.Generator.CMTrend.Position;
namespace TradeBlotter.UIUtils
{
/// <summary>
/// Interaction logic for ClosePositionDialogNoStop.xaml
/// </summary>
public partial class ClosePositionDialogNoStop : Window
{
private ManualResetEvent manualResetEvent=new ManualResetEvent(false);
private String Symbol{get;set;}
private DateTime PurchaseDate { get; set; }
private DateTime SellDate{get;set;}
private double SellPrice { get; set; }
private bool Result { get; set; }
private ClosePositionDialogNoStop(String title,IPurePosition position)
{
this.Background=new SolidColorBrush(Colors.AliceBlue);
this.Owner=Application.Current.MainWindow;
InitializeComponent();
this.Loaded+=new RoutedEventHandler(ClosePositionDialogNoStop_Loaded);
this.Closing+=Window_Closing;
dpPurchaseDate.SelectedDateChanged+=dpPurchaseDate_SelectionChanged;
dpSellDate.SelectedDateChanged+=dpPurchaseDate_SelectionChanged;
Title=title;
Symbol=position.Symbol;
PurchaseDate=position.PurchaseDate;
if(Utility.IsEpoch(position.SellDate))
{
SellDate = DateTime.Now;
}
else
{
SellDate=position.SellDate;
}
SellPrice=position.CurrentPrice;
txtSymbol.Text=Symbol;
dpPurchaseDate.SelectedDate=PurchaseDate;
dpSellDate.SelectedDate=SellDate;
UpdateSellPrice();
UpdateCompanyName();
}
public ManualResetEvent Event
{
get
{
return manualResetEvent;
}
}
public static IPurePosition Prompt(string title,IPurePosition position)
{
ClosePositionDialogNoStop closePositionDialog=new ClosePositionDialogNoStop(title,position);
try { closePositionDialog.ShowDialog(); }
catch(Exception) { return null; }
closePositionDialog.Event.WaitOne();
if( closePositionDialog.Result)
{
position.SellDate=closePositionDialog.SellDate;
position.CurrentPrice=closePositionDialog.SellPrice;
return position;
}
return null;
}
void ClosePositionDialogNoStop_Loaded(object sender,RoutedEventArgs e)
{
txtSellPrice.Focus();
}
private void btnOk_Click(object sender,RoutedEventArgs e)
{
Result=Validate();
if(!Result)
{
SystemSounds.Beep.Play();
return;
}
Close();
Event.Set();
return;
}
private void txtSellPrice_OnLostFocusHandler(Object sender,RoutedEventArgs routedEventArgs)
{
HandleSellPriceEvents();
}
private void txtSellPrice_OnKeyDownHandler(object sender,KeyEventArgs e)
{
if(e.Key==Key.Return)
{
HandleSellPriceEvents();
}
}
private void HandleSellPriceEvents()
{
double value=0.00;
value=Utility.ParseCurrency(txtSellPrice.Text);
if(SellPrice.Equals(value)) return;
SellPrice=value;
UpdateSellPrice();
}
private void dpPurchaseDate_SelectionChanged(object sender,RoutedEventArgs e)
{
if(null==dpPurchaseDate.SelectedDate) return;
PurchaseDate=dpPurchaseDate.SelectedDate.Value;
}
private void dpSellDate_SelectionChanged(object sender,RoutedEventArgs e)
{
if(null==dpSellDate.SelectedDate) return;
SellDate=dpSellDate.SelectedDate.Value;
}
private void Window_Closing(object sender,System.ComponentModel.CancelEventArgs e)
{
Event.Set();
}
private void btnCancel_Click(object sender,RoutedEventArgs e)
{
Result=false;
Close();
Event.Set();
}
private bool Validate()
{
DateGenerator dateGenerator=new DateGenerator();
if(null==Symbol)
{
SetMessage("Invalid Symbol.");
return false;
}
if(Utility.IsEpoch(PurchaseDate))
{
SetMessage("Invalid Purchase Date.");
return false;
}
if(Utility.IsEpoch(SellDate))
{
SetMessage("Invalid Sell Date.");
return false;
}
if(!dateGenerator.IsMarketOpen(SellDate))
{
SetMessage("Market closed on Sell Date.");
return false;
}
if(!dateGenerator.IsMarketOpen(PurchaseDate))
{
SetMessage("Market closed on Purchase Date.");
return false;
}
if(double.IsNaN(SellPrice))
{
SetMessage("Invalid Sell Price.");
return false;
}
SetMessage("");
return true;
}
private void UpdateSellPrice()
{
txtSellPrice.Text=Utility.FormatCurrency(SellPrice,3);
}
private void UpdateCompanyName()
{
CompanyProfile companyProfile=CompanyProfileDA.GetCompanyProfile(Symbol);
if(null==companyProfile||null==companyProfile.CompanyName) return;
lblCompanyName.Content=companyProfile.CompanyName.ToUpper();
}
private void SetMessage(String message)
{
lblMessage.Content=message;
}
}
}