Files
TradeBlotter/ViewModels/StochasticsViewModel.cs
2024-05-24 15:12:29 -04:00

238 lines
7.7 KiB
C#

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Threading.Tasks;
using MarketData;
using MarketData.Utils;
using MarketData.MarketDataModel;
using MarketData.Generator;
using MarketData.DataAccess;
using TradeBlotter.DataAccess;
using TradeBlotter.Model;
using TradeBlotter.Command;
using Microsoft.Research.DynamicDataDisplay.DataSources;
using TradeBlotter.Cache;
namespace TradeBlotter.ViewModels
{
public class StochasticsViewModel : WorkspaceViewModel
{
private Stochastics stochastics;
private String symbol;
private String companyName;
private Prices prices = null;
private List<String> symbols;
private List<String> watchLists;
private String selectedWatchList;
private RelayCommand refreshCommand;
private List<Int32> dayCounts;
private Int32 selectedDayCount;
private bool isLegendVisible = true;
private bool busyIndicator = false;
public StochasticsViewModel()
{
base.DisplayName = "StochasticsView";
watchLists = WatchListDA.GetWatchLists();
watchLists.Insert(0, Constants.CONST_ALL);
selectedWatchList = watchLists.Find(x => x.Equals("Valuations"));
symbols = WatchListDA.GetWatchList(selectedWatchList);
dayCounts = new List<Int32>();
dayCounts.Add(60);
dayCounts.Add(90);
dayCounts.Add(180);
dayCounts.Add(360);
dayCounts.Add(720);
dayCounts.Add(1440);
dayCounts.Add(3600);
selectedDayCount = dayCounts[1];
PropertyChanged += OnStochasticsViewModelPropertyChanged;
}
public override SaveParameters GetSaveParameters()
{
SaveParameters saveParams = new SaveParameters();
if (null == symbol) return null;
saveParams.Add(new KeyValuePair<String, String>("Type", GetType().Namespace + "." + GetType().Name));
saveParams.Add(new KeyValuePair<String, String>("SelectedSymbol", symbol));
saveParams.Add(new KeyValuePair<String, String>("SelectedWatchList", selectedWatchList));
saveParams.Add(new KeyValuePair<String, String>("SelectedDayCount", selectedDayCount.ToString()));
saveParams.Add(new KeyValuePair<String, String>("IsLegendVisible", isLegendVisible.ToString()));
return saveParams;
}
public override void SetSaveParameters(SaveParameters saveParameters)
{
try
{
symbol = (from KeyValuePair<String, String> item in saveParameters where item.Key.Equals("SelectedSymbol") select item).FirstOrDefault().Value;
selectedWatchList = (from KeyValuePair<String, String> item in saveParameters where item.Key.Equals("SelectedWatchList") select item).FirstOrDefault().Value;
base.OnPropertyChanged("SelectedWatchList");
selectedDayCount = Int32.Parse((from KeyValuePair<String, String> item in saveParameters where item.Key.Equals("SelectedDayCount") select item).FirstOrDefault().Value);
try { isLegendVisible = Boolean.Parse((from KeyValuePair<String, String> item in saveParameters where item.Key.Equals("IsLegendVisible") select item).FirstOrDefault().Value); }
catch (Exception) { ;}
Referer=saveParameters.Referer;
base.OnPropertyChanged("SelectedSymbol");
}
catch (Exception exception)
{
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception:{0}",exception.ToString()));
}
}
public override bool CanPersist()
{
return true;
}
public bool BusyIndicator
{
get { return busyIndicator; }
set { busyIndicator = value; base.OnPropertyChanged("BusyIndicator"); }
}
private void OnStochasticsViewModelPropertyChanged(object sender, PropertyChangedEventArgs eventArgs)
{
if (eventArgs.PropertyName.Equals("SelectedSymbol")||(eventArgs.PropertyName.Equals("SelectedDayCount")&&null!=symbol))
{
BusyIndicator = true;
Task workerTask = Task.Factory.StartNew(() =>
{
base.DisplayName = "Stochastic("+symbol+")";
base.OnPropertyChanged("DisplayName");
prices = PricingDA.GetPrices(symbol, selectedDayCount);
companyName = PricingDA.GetNameForSymbol(symbol);
stochastics = StochasticsGenerator.GenerateStochastics(prices);
});
workerTask.ContinueWith((continuation) =>
{
BusyIndicator = false;
base.OnPropertyChanged("PcntK");
base.OnPropertyChanged("PcntD");
base.OnPropertyChanged("Title");
base.OnPropertyChanged("LegendVisible");
});
}
else if (eventArgs.PropertyName.Equals("SelectedWatchList"))
{
// if (selectedWatchList.Equals(Constants.CONST_ALL)) symbols = PricingDA.GetSymbols();
if (selectedWatchList.Equals(Constants.CONST_ALL)) symbols = SymbolCache.GetInstance().GetSymbols();
else symbols = WatchListDA.GetWatchList(selectedWatchList);
base.OnPropertyChanged("Symbols");
}
}
public CompositeDataSource PcntK
{
get
{
CompositeDataSource compositeDataSource = StochasticsModel.PcntK(stochastics);
return compositeDataSource;
}
}
public CompositeDataSource PcntD
{
get
{
CompositeDataSource compositeDataSource = StochasticsModel.PcntD(stochastics);
return compositeDataSource;
}
}
public override String Title
{
get
{
if (null == companyName || null == prices || 0 == prices.Count) return "";
StringBuilder sb=new StringBuilder();
sb.Append(companyName).Append(" (").Append(symbol).Append(") ").Append(Utility.DateTimeToStringMMHDDHYYYY(prices[prices.Count - 1].Date)).Append(" Thru ").Append(Utility.DateTimeToStringMMHDDHYYYY(prices[0].Date));
sb.Append(" (").Append(Utility.FormatPercent(stochastics[0].PK/100.00)).Append(")");
return sb.ToString();
}
}
public Boolean Visibility
{
get { return false; }
}
public Boolean CheckBoxLegendVisible
{
get
{
return isLegendVisible;
}
set
{
isLegendVisible = value;
base.OnPropertyChanged("CheckBoxLegendVisible");
base.OnPropertyChanged("LegendVisible");
}
}
public String LegendVisible
{
get
{
if (isLegendVisible) return "true";
return "false";
}
set
{
isLegendVisible = Boolean.Parse(value);
base.OnPropertyChanged("LegendVisible");
}
}
public List<Int32> DayCounts
{
get { return dayCounts; }
}
public Int32 SelectedDayCount
{
get { return selectedDayCount; }
set { selectedDayCount = value; base.OnPropertyChanged("SelectedDayCount"); }
}
public List<String> Symbols
{
get
{
return symbols;
}
}
public String SelectedSymbol
{
get { return symbol; }
set
{
if (value == symbol || String.IsNullOrEmpty(value)) return;
symbol = value;
base.OnPropertyChanged("SelectedSymbol");
}
}
public List<String> WatchListNames
{
get
{
return watchLists;
}
set { ;}
}
public String SelectedWatchList
{
get { return selectedWatchList; }
set { selectedWatchList = value; base.OnPropertyChanged("SelectedWatchList"); }
}
private void Refresh()
{
base.OnPropertyChanged("SelectedSymbol");
}
private Boolean CanRefresh
{
get { return true; }
}
public ICommand RefreshCommand
{
get
{
if (null == refreshCommand) refreshCommand = new RelayCommand(param => Refresh(), param => CanRefresh);
return refreshCommand;
}
}
}
}