//using System;
//using System.Collections.Generic;
//using System.Text;
//using MarketData.MarketDataModel;
//using MarketData.DataAccess;
//using MarketData.Utils;

//namespace MarketData.Generator
//{
//  public delegate void ProgressHandler(String symbol);
//  public class TradeSignalWatchGenerator
//  {
//    private TradeSignalWatchGenerator()
//    {
//    }
//    public static List<TradeResult> GenerateTradeSignalWatchList(String watchListName, String initialInvestment, String historicalDate, String macd,ProgressHandler progressHandler)
//    {
//      try
//      {
//        List<TradeResult> tradeResultList = new List<TradeResult>();
//        List<String> symbols = WatchListDA.GetWatchList(watchListName);
//        foreach (String symbol in symbols)
//        {
//          if (null != progressHandler) progressHandler(symbol);
//          TradeResult tradeResult = TradeSignalSymbolDateWithResult(symbol, initialInvestment, historicalDate, macd, null, null);
//          if(null!=tradeResult)tradeResultList.Add(tradeResult);
//        }
//        tradeResultList.Sort();
//        return tradeResultList;
//      }
//      catch (Exception exception)
//      {
//        MDTrace.WriteLine(LogLevel.DEBUG,exception);
//        return null;
//      }
//    }
//    private static TradeResult TradeSignalSymbolDateWithResult(String symbol, String initialInvestment, String strHistoricalDate, String macd, String strStopLossThreshold, String strStopLossDays)
//    {
//      try
//      {
//        TradeResult tradeResult = new TradeResult();
//        DateGenerator dateGenerator = new DateGenerator();
//        MACDSetup macdSetup = null;
//        double stopLossThreshold = 0.00;
//        int stopLossDays = 0;

//        symbol = symbol.ToUpper();
//        tradeResult.Symbol = symbol;
//        DateRange tradeDates = new DateRange(DateTime.Parse(strHistoricalDate), DateTime.Now);
//        if (null != strStopLossThreshold) stopLossThreshold = Double.Parse(strStopLossThreshold);
//        else stopLossThreshold = 0;
//        if (null != strStopLossDays) stopLossDays = Int32.Parse(strStopLossDays);
//        else stopLossDays = 0;
//        double initialCash = double.Parse(initialInvestment);
//        DateTime historicalDate = DateTime.Parse(strHistoricalDate);
//        DateTime latestPricingDate = PricingDA.GetLatestDate(symbol);
//        String companyName = PricingDA.GetNameForSymbol(symbol);
//        if (null == macd) macdSetup = new MACDSetup(8, 17, 9);
//        else macdSetup = new MACDSetup(macd);
//        historicalDate = dateGenerator.GenerateHistoricalDate(historicalDate, macdSetup.Slow);
//        tradeResult.Company = companyName;
//        tradeResult.MACDSetup = macdSetup;
//        tradeResult.StopLossThreshold = stopLossThreshold;
//        tradeResult.StopLossDays = stopLossDays;
//        if (latestPricingDate < historicalDate)
//        {
//          MDTrace.WriteLine(LogLevel.DEBUG,"The historical date you are trying to run is later than the latest date for which prices are available.");
//          return null;
//        }
//        tradeResult.InitialCash = initialCash;
//        Dictionary<DateTime, Price> pricesByDate = new Dictionary<DateTime, Price>();
//        Prices prices = PricingDA.GetPrices(symbol, DateTime.Now, historicalDate);
//        Price latestPrice = prices[0];
//        DateTime startDate = prices[0].Date;
//        DateTime endDate = prices[prices.Count - 1].Date;
//        tradeResult.StartDate = startDate;
//        tradeResult.HistoricalDate = historicalDate;
//        for (int index = 0; index < prices.Count; index++) pricesByDate.Add(prices[index].Date, prices[index]);
//        MACDSignals macdSignals = MACDGenerator.GenerateMACD(prices, macdSetup);
//        Signals signals = SignalGenerator.GenerateSignals(macdSignals);
//        Portfolio portfolio = SignalTrader.TradeSignals(tradeDates, signals, prices, initialCash, stopLossThreshold, stopLossDays);
//        tradeResult.AvailableCash = portfolio.AvailableCash;
//        double portfolioValue = portfolio.GetPortfolioValue(latestPrice);
//        double returnOnInvestment = (portfolioValue - initialCash) / initialCash;
//        tradeResult.TotalPortfolioValue = portfolioValue;
//        tradeResult.TotalGainLoss = portfolioValue - initialCash;
//        tradeResult.ROI = returnOnInvestment;
//        tradeResult.Trades = portfolio.Trades;
//        tradeResult.AverageGainLoss = portfolio.GetAverageGainLoss();
//        tradeResult.AverageHoldingDays = portfolio.GetAverageHoldingDays();
//        tradeResult.MinimumHoldingDays = portfolio.GetMinHoldingDays();
//        tradeResult.MaximumHoldingDays = portfolio.GetMaxHoldingDays();
//        return tradeResult;
//      }
//      catch (Exception exception)
//      {
//        MDTrace.WriteLine(LogLevel.DEBUG,exception.ToString());
//        return null;
//      }
//    }
//  }
//}
