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

254 lines
8.5 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 RSIViewModel : WorkspaceViewModel
{
private RSICollection rsiCollection;
private String symbol;
private String companyName;
private List<String> symbols;
private List<String> watchLists;
private String selectedWatchList;
private RelayCommand refreshCommand;
private List<Int32> dayCounts;
private List<Int32> rsiDayCounts;
private Int32 selectedDayCount;
private Int32 selectedRSIDayCount;
private bool isLegendVisible = true;
private bool busyIndicator = false;
public RSIViewModel()
{
base.DisplayName = "RSIView";
watchLists = WatchListDA.GetWatchLists();
watchLists.Insert(0, Constants.CONST_ALL);
selectedWatchList = watchLists.Find(x => x.Equals("Valuations"));
symbols = WatchListDA.GetWatchList(selectedWatchList);
rsiDayCounts=new List<Int32>();
rsiDayCounts.Add(3);
rsiDayCounts.Add(14);
dayCounts = new List<Int32>();
dayCounts.Add(30);
dayCounts.Add(60);
dayCounts.Add(90);
dayCounts.Add(180);
dayCounts.Add(360);
dayCounts.Add(720);
dayCounts.Add(1440);
dayCounts.Add(3600);
selectedDayCount = dayCounts[1];
selectedRSIDayCount=rsiDayCounts[1];
PropertyChanged += OnViewModelPropertyChanged;
}
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>("SelectedRSIDayCount", selectedRSIDayCount.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);
selectedRSIDayCount = Int32.Parse((from KeyValuePair<String, String> item in saveParameters where item.Key.Equals("SelectedRSIDayCount") 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 OnViewModelPropertyChanged(object sender, PropertyChangedEventArgs eventArgs)
{
if (eventArgs.PropertyName.Equals("SelectedSymbol")||((eventArgs.PropertyName.Equals("SelectedRSIDayCount")||eventArgs.PropertyName.Equals("SelectedDayCount"))&&null!=symbol))
{
BusyIndicator = true;
Task workerTask = Task.Factory.StartNew(() =>
{
base.DisplayName = "RSI("+symbol+")";
base.OnPropertyChanged("DisplayName");
rsiCollection=RSIGenerator.GenerateRSI(SelectedSymbol,selectedDayCount,selectedRSIDayCount);
companyName = PricingDA.GetNameForSymbol(symbol);
});
workerTask.ContinueWith((continuation) =>
{
BusyIndicator = false;
base.OnPropertyChanged("Pcnt");
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 Pcnt
{
get
{
CompositeDataSource compositeDataSource = RSIModel.Pcnt(rsiCollection);
return compositeDataSource;
}
}
public override String Title
{
get
{
if (null == companyName || null == rsiCollection || 0 == rsiCollection.Count) return "";
DateTime latestPricingDate=PricingDA.GetLatestDate(SelectedSymbol);
StringBuilder sb=new StringBuilder();
sb.Append(companyName).Append(" (").Append(symbol).Append(") ").Append(Utility.DateTimeToStringMMHDDHYYYY(latestPricingDate));
sb.Append(" (").Append(Utility.FormatNumber(rsiCollection[rsiCollection.Count-1].RSI,2)).Append("%)");
if(rsiCollection.Count<=1)return sb.ToString();
double changePercent=(rsiCollection[rsiCollection.Count-1].RSI-rsiCollection[rsiCollection.Count-2].RSI)/rsiCollection[rsiCollection.Count-1].RSI;
sb.Append(String.Format(" ({0}{1})",changePercent>=0.00?"+":"",Utility.FormatPercent(changePercent)));
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<Int32> RSIDayCounts
{
get { return rsiDayCounts; }
}
public Int32 SelectedRSIDayCount
{
get { return selectedRSIDayCount; }
set { selectedRSIDayCount = value; base.OnPropertyChanged("SelectedRSIDayCount"); }
}
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;
}
}
}
}