using System;
using MarketData.Utils;

namespace MarketData.Generator.Momentum
{
  public class MGConfiguration
  {
// Operational Settings
    public bool Verbose{get;set;}       
// Basic settings            
    public int HoldingPeriod{get;set;}             
    public int MaxPositions{get;set;}              
    public String NoTradeSymbols{get;set;}    
    public String NoTradeFinancialSymbols{get;set;}          
    public double InitialCash{get;set;}

// Fundamental screenings
    public double MarketCapLowerLimit{get;set;}

    public bool UsePEScreen{get;set;}      // If set this filter will ignore any security that is either missing a PE or if the PE is present but less than 0

    public bool UseMaxPEScreen{get;set;}   // control Max PE range check
    public double MaxPE{get;set;}         // if UseMaxPECheck is set this setting will ignore any security who's PE is greater.  If PE is missing candidate will be accepted
    public bool StrictMaxPE{get;set;}     // if TRUE then UseMaxPEScreen is STRICT otherwise UseMaxPEScreen is SOFT RULE where >MaxPE is AVOIDED but used to fill the quota.  The quota is filled by ranking highPECandidates by PE and taking the lower PE's up to the quota.  Soft rule yields best result

// EBITDA screen
    public bool UseEBITDAScreen{get;set;}
    public bool UseRevenuePerShareScreen{get;set;}

//  If slope Beta Check is true then we compare the fundamental Beta to the threshhold value.  If Beta>Threshhold AND the slope from the Benchmark LowPrice over BetaDays is <0 we reject
    public String Benchmark{get;set;}  
    public bool UseLowSlopeBetaCheck{get;set;}        
    public int LowSlopeBetaDays{get;set;}          
    public double LowSlopeBetaThreshhold{get;set;} 

// MACD Settings : If MACD is being used then the process configures the MACD as per setup and eliminates candidates with a weak sell/strong sell signal in the signal days setting.
    public bool UseMACD{get;set;}
    public String MACDSetup="(12,26,9)";                // (8,17,9) is another alternative
    public int MACDSignalDays{get;set;}
    public bool MACDRejectWeakSellSignals{get;set;}
    public bool MACDRejectStrongSellSignals{get;set;}

// Stochastics Settings : If Stochastics is being used then the process eliminates candidates with a weak sell/strong sell in the signal days setting.
    public bool UseStochastics{get;set;}
    public int StochasticsSignalDays{get;set;}
    public bool StochasticsRejectWeakSells{get;set;}
    public bool StochasticsRejectStrongSells{get;set;}

// FallbackCandidate : If this setting is true then the FallbackCandidate is purchased if there are no candidates that qualify in a given cycle
    public bool UseFallbackCandidate{get;set;}
    public String FallbackCandidate{get;set;}
    public String FallbackCandidateBestOf{get;set;}  // if this is set then the fallback candidate is selected as the best 252 day return 

// Benchmark mode : If this is set to true then purchases the benchmark symbol instead of the momentum candidates
    public bool BenchmarkMode{get;set;}
    public String BenchmarkModeSymbol{get;set;}

// QualityIndicator
    public String QualityIndicatorType{get;set;}   //  this can be either IDINDICATOR or SCOREINDICATOR.  The SCOREINDICATOR was adopted from CMMomentum model

// IncludeTradeMasterForSymbolsHeld :
   public bool IncludeTradeMasterForSymbolsHeld{get;set;} // if this is set to true then use both ActivePositions within the model as well as the master trades table to determine held positions. 

    public MGConfiguration()
    {    
      Verbose=true;                                      // user verbose output
      BenchmarkMode=false;                               // set this to true if you want to run the model using just the benchmark symbol to buy.
      BenchmarkModeSymbol="SPY";                         // SPY is the default symbol to buy when testing                    
      HoldingPeriod=3;                                   // 3 is the default
      MaxPositions=3;                                    // 3 is the default
      NoTradeSymbols="GBTC,YOKU,PNY,RFMD,ASAZY";         // ASAZY came up as candidate during 3/30 run but not available on Robinhood
      NoTradeFinancialSymbols="U.S. Private Equity,U.S. Financials,U.S. Financial Services,U.S. Banking and Investment Services,Trading-Miscellaneous,Trading--Miscellaneous,Trading--Leveraged Equity,Trading--Leveraged Debt,Trading--Leveraged Commodities,Trading--Inverse Equity,Trading--Inverse Commodities,Tactical Allocation,Specialty Finance,Japan Financials,Savings & Cooperative Banks,Option Writing,Insurance Brokers,Insurance - Specialty,Insurance - Reinsurance,Insurance - Property & Casualty,Insurance - Life,Insurance - Diversified,Global Private Equity,Global Financials,Financial Services,Financial Exchanges,Financial,China Financials,Banks - Regional - US,Banks - Regional - Latin America,Banks - Global,Asset Management,Credit Services";
      Benchmark="SPY";                                   // SPY is the default
// Candidate selection settings
      MarketCapLowerLimit=1000000000;                    // 1B is the default
// PEScreen is off by default                        
      UsePEScreen=false;                                 // false is the default.  This setting yields the most optimal performance in backtests.  Checks for existance of PE and if exists ensures >0
// UseMaxPEScreen, MaxPE, and StrictPEExclusion 
      UseMaxPEScreen=true;
      MaxPE=40;
      StrictMaxPE=false;

      UseEBITDAScreen=true;                              // true is the default
      UseRevenuePerShareScreen=true;                     // true is the default
      UseLowSlopeBetaCheck=true;                         // true is the default.  this yields the most optimal performance in backtests
      LowSlopeBetaDays=15;                               // 15 is the default. This yields the most optimal performance in backtests
      LowSlopeBetaThreshhold=1.00;                       // (1.00) is the default This yields the most optimal performance in backtests
      UseMACD=true;                                      // true is the default
      MACDSetup="(12,26,9)";                             // (12,26,9) 
      MACDSignalDays=12;                                 // 12 is the default
      MACDRejectStrongSellSignals=false;                 // false is the default
      MACDRejectWeakSellSignals=true;                    // true is the default
      UseStochastics=true;                               // true is the default
      StochasticsSignalDays=3;                           // 3 is the default
      StochasticsRejectStrongSells=true;                 // true is the default
      StochasticsRejectWeakSells=true;                   // true is the default

// Fallback candidate settings
      UseFallbackCandidate=true;                         // True is the default
      FallbackCandidate="SHV";                           // "SHV" ICE U.S. Treasury Short Bond Index, "AGG" Barclays U.S. Aggregate Bond Index - AGG can have a slighty better return but is more volatile
      FallbackCandidateBestOf="SHV,AGG,ACWX";            // if set then the fallback candidate is selected as the best 252 day return in this comma seperated list (i.e.) "SHV,ACWX,AGG" 
// Set the QualityIndicator type to the IDIndicator
      QualityIndicatorType=QualityIndicator.ToString(QualityIndicator.QualityType.IDIndicator);

      IncludeTradeMasterForSymbolsHeld=false;
    }
    public void DisplayHeader()
    {
      MDTrace.WriteLine(LogLevel.DEBUG,"Setting,Value");
    }
    public NVPCollection ToNVPCollection()
    {
      NVPCollection nvpCollection=new NVPCollection();
      nvpCollection.Add(new NVP("Verbose",Verbose.ToString()));
      nvpCollection.Add(new NVP("BenchmarkMode",BenchmarkMode.ToString()));
      nvpCollection.Add(new NVP("BenchmarkModeSymbol",BenchmarkModeSymbol.ToString()));
      nvpCollection.Add(new NVP("HoldingPeriod",HoldingPeriod.ToString()));
      nvpCollection.Add(new NVP("MaxPositions",MaxPositions.ToString()));
      nvpCollection.Add(new NVP("NoTradeSymbols",NoTradeSymbols.ToString()));
      nvpCollection.Add(new NVP("NoTradeFinancialSymbols",NoTradeFinancialSymbols.ToString()));
      nvpCollection.Add(new NVP("Benchmark",Benchmark.ToString()));
      nvpCollection.Add(new NVP("MarketCapLowerLimit",MarketCapLowerLimit.ToString()));
      nvpCollection.Add(new NVP("UsePEScreen",UsePEScreen.ToString()));
      nvpCollection.Add(new NVP("UseEBITDAScreen",UseEBITDAScreen.ToString()));
      nvpCollection.Add(new NVP("UseRevenuePerShareScreen",UseRevenuePerShareScreen.ToString()));
      nvpCollection.Add(new NVP("UseLowSlopeBetaCheck",UseLowSlopeBetaCheck.ToString()));
      nvpCollection.Add(new NVP("LowSlopeBetaDays",LowSlopeBetaDays.ToString()));
      nvpCollection.Add(new NVP("LowSlopeBetaThreshhold",LowSlopeBetaThreshhold.ToString()));
      nvpCollection.Add(new NVP("UseMACD",UseMACD.ToString()));
      nvpCollection.Add(new NVP("MACDSetup",MACDSetup.ToString()));
      nvpCollection.Add(new NVP("MACDSignalDays",MACDSignalDays.ToString()));
      nvpCollection.Add(new NVP("MACDRejectStrongSellSignals",MACDRejectStrongSellSignals.ToString()));
      nvpCollection.Add(new NVP("MACDRejectWeakSellSignals",MACDRejectWeakSellSignals.ToString()));
      nvpCollection.Add(new NVP("UseStochastics",UseStochastics.ToString()));
      nvpCollection.Add(new NVP("StochasticsSignalDays",StochasticsSignalDays.ToString()));
      nvpCollection.Add(new NVP("StochasticsRejectStrongSells",StochasticsRejectStrongSells.ToString()));
      nvpCollection.Add(new NVP("StochasticsRejectWeakSells",StochasticsRejectWeakSells.ToString()));
      nvpCollection.Add(new NVP("UseFallbackCandidate",UseFallbackCandidate.ToString()));
      nvpCollection.Add(new NVP("FallbackCandidate",FallbackCandidate.ToString()));
      nvpCollection.Add(new NVP("FallbackCandidateBestOf",FallbackCandidateBestOf.ToString()));
      nvpCollection.Add(new NVP("UseMaxPEScreen",UseMaxPEScreen.ToString()));
      nvpCollection.Add(new NVP("MaxPE",MaxPE.ToString()));
      nvpCollection.Add(new NVP("StrictMaxPE",StrictMaxPE.ToString()));
      nvpCollection.Add(new NVP("QualityIndicatorType",QualityIndicatorType.ToString()));
      nvpCollection.Add(new NVP("IncludeTradeMasterForSymbolsHeld",IncludeTradeMasterForSymbolsHeld.ToString()));
      return nvpCollection;
    }
    public static MGConfiguration FromNVPCollection(NVPCollection nvpCollection)
    {
      MGConfiguration mgConfiguration=new MGConfiguration();
      NVPDictionary nvpDictionary=nvpCollection.ToDictionary();
      mgConfiguration.Verbose=nvpDictionary["Verbose"].Get<Boolean>();
      mgConfiguration.BenchmarkMode=nvpDictionary["BenchmarkMode"].Get<Boolean>();
      mgConfiguration.BenchmarkModeSymbol=nvpDictionary["BenchmarkModeSymbol"].Get<String>();
      mgConfiguration.HoldingPeriod=nvpDictionary["HoldingPeriod"].Get<int>();
      mgConfiguration.MaxPositions=nvpDictionary["MaxPositions"].Get<int>();
      mgConfiguration.NoTradeSymbols=nvpDictionary["NoTradeSymbols"].Get<String>();
      mgConfiguration.NoTradeFinancialSymbols=nvpDictionary["NoTradeFinancialSymbols"].Get<String>();
      mgConfiguration.Benchmark=nvpDictionary["Benchmark"].Get<String>();
      mgConfiguration.MarketCapLowerLimit=nvpDictionary["MarketCapLowerLimit"].Get<double>();
      mgConfiguration.UsePEScreen=nvpDictionary["UsePEScreen"].Get<Boolean>();
      mgConfiguration.UseMaxPEScreen=nvpDictionary["UseMaxPEScreen"].Get<Boolean>();
      mgConfiguration.MaxPE=nvpDictionary["MaxPE"].Get<double>();
      mgConfiguration.StrictMaxPE=nvpDictionary["StrictMaxPE"].Get<Boolean>();
      mgConfiguration.UseEBITDAScreen=nvpDictionary["UseEBITDAScreen"].Get<Boolean>();
      mgConfiguration.UseRevenuePerShareScreen=nvpDictionary["UseRevenuePerShareScreen"].Get<Boolean>();
      mgConfiguration.UseLowSlopeBetaCheck=nvpDictionary["UseLowSlopeBetaCheck"].Get<Boolean>();
      mgConfiguration.LowSlopeBetaDays=nvpDictionary["LowSlopeBetaDays"].Get<int>();
      mgConfiguration.LowSlopeBetaThreshhold=nvpDictionary["LowSlopeBetaThreshhold"].Get<double>();
      mgConfiguration.UseMACD=nvpDictionary["UseMACD"].Get<Boolean>();
      mgConfiguration.MACDSetup=nvpDictionary["MACDSetup"].Get<String>();
      mgConfiguration.MACDSignalDays=nvpDictionary["MACDSignalDays"].Get<int>();
      mgConfiguration.MACDRejectStrongSellSignals=nvpDictionary["MACDRejectStrongSellSignals"].Get<Boolean>();
      mgConfiguration.MACDRejectWeakSellSignals=nvpDictionary["MACDRejectWeakSellSignals"].Get<Boolean>();
      mgConfiguration.UseStochastics=nvpDictionary["UseStochastics"].Get<Boolean>();
      mgConfiguration.StochasticsSignalDays=nvpDictionary["StochasticsSignalDays"].Get<int>();
      mgConfiguration.StochasticsRejectStrongSells=nvpDictionary["StochasticsRejectStrongSells"].Get<Boolean>();
      mgConfiguration.StochasticsRejectWeakSells=nvpDictionary["StochasticsRejectWeakSells"].Get<Boolean>();
      mgConfiguration.UseFallbackCandidate=nvpDictionary["UseFallbackCandidate"].Get<Boolean>();
      mgConfiguration.FallbackCandidate=nvpDictionary["FallbackCandidate"].Get<String>();
      mgConfiguration.FallbackCandidateBestOf=nvpDictionary["FallbackCandidateBestOf"].Get<String>();

      if(nvpDictionary.ContainsKey("QualityIndicatorType")) mgConfiguration.QualityIndicatorType=nvpDictionary["QualityIndicatorType"].Get<String>();
      else mgConfiguration.QualityIndicatorType=QualityIndicator.ToString(QualityIndicator.QualityType.IDIndicator);

      if(nvpDictionary.ContainsKey("IncludeTradeMasterForSymbolsHeld")) mgConfiguration.IncludeTradeMasterForSymbolsHeld=nvpDictionary["IncludeTradeMasterForSymbolsHeld"].Get<bool>();
      else mgConfiguration.IncludeTradeMasterForSymbolsHeld=false;
      return mgConfiguration;
    }
    public void DisplayConfiguration()
    {
      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Verbose,{0}",Verbose));
      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Holding Period,{0}",HoldingPeriod));
      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("MaxPositions,{0}",MaxPositions));
      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("NoTradeSymbols,{0}",NoTradeSymbols));
      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("NoTradeFinancialSymbols,{0}",NoTradeFinancialSymbols));
      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Benchmark,{0}",Benchmark));
      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("MarketCapLowerLimit,{0}",MarketCapLowerLimit));

      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("UsePEScreen,{0}",UsePEScreen));
      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("UseMaxPEScreen,{0}",UseMaxPEScreen));
      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("MaxPE,{0}",MaxPE));
      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("StrictMaxPE,{0}",StrictMaxPE));

      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("UseEBITDAScreen,{0}",UseEBITDAScreen));
      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("UseRevenuePerShareScreen,{0}",UseRevenuePerShareScreen));

      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("UseLowSlopeBetaCheck,{0}",UseLowSlopeBetaCheck));
      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("LowSlopeBetaDays,{0}",LowSlopeBetaDays));
      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("LowSlopeBetaThreshhold,{0}",LowSlopeBetaThreshhold));

      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("UseMACD,{0}",UseMACD));
      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("MACDSetup,{0}",MACDSetup));
      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("MACDSignalDays,{0}",MACDSignalDays));
      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("MACDRejectStrongSellSignals,{0}",MACDRejectStrongSellSignals));
      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("MACDRejectWeakSellSignals,{0}",MACDRejectWeakSellSignals));

      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("UseStochastics,{0}",UseStochastics));
      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("StochasticsSignalDays,{0}",StochasticsSignalDays));
      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("StochasticsRejectStrongSells,{0}",StochasticsRejectStrongSells));
      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("StochasticsRejectWeakSells,{0}",StochasticsRejectWeakSells));

      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("UseFallbackCandidate,{0}",UseFallbackCandidate));
      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("FallbackCandidate,{0}",FallbackCandidate));
      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("FallbackCandidateBestOf,{0}",FallbackCandidateBestOf));

      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("BenchmarkMode,{0}",BenchmarkMode));
      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("BenchmarkSymbol,{0}",BenchmarkModeSymbol));

      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("QualityIndicatorType,{0}",QualityIndicatorType));

      MDTrace.WriteLine(LogLevel.DEBUG,String.Format("IncludeTradeMasterForSymbolsHeld,{0}",IncludeTradeMasterForSymbolsHeld));
    }
  }
}
