public YahooDataSourceClient(string inputFile, string storageConnectionString) { var inputFileReader = new InputFileReader(inputFile); azureToYahooApiMapping = new Dictionary<string, string>(); foreach (var inputData in inputFileReader.ReadLine()) { azureToYahooApiMapping.Add(inputData.AzureTableStockCode, inputData.YahooOnlineApiStockCode); } this.storageConnectionString = storageConnectionString; }
static void Main(string[] args) { string passPhrase = GetEncryptionCertPrivateKey(); string storageAccountName = StringCipher.Decrypt(ConfigurationManager.AppSettings["EncryptedStorageAccountName"], passPhrase); string storageAccountkey = StringCipher.Decrypt(ConfigurationManager.AppSettings["EncryptedStorageKey"], passPhrase); string connectionString = string.Format("DefaultEndpointsProtocol=http;AccountName={0};AccountKey={1}", storageAccountName, storageAccountkey); Arguments arguments = new Arguments(args); if (args.Length == 0 || !string.IsNullOrEmpty(arguments["?"])) { PrintUsage(); return; } InputFileReader reader = null; string inputFile = arguments["InputFile"]; if (!string.IsNullOrEmpty(inputFile)) { reader = new InputFileReader(inputFile); } string dataSourceStr = "intelligent"; if (!string.IsNullOrEmpty(arguments["DataSource"])) dataSourceStr = arguments["DataSource"]; DataSource dataSource = (DataSource)Enum.Parse(typeof(DataSource), dataSourceStr, true); Console.WriteLine("Data source is set to {0}", dataSourceStr); StockProcessingAgent agent = new StockProcessingAgent(inputFile, connectionString, dataSource); string policyName = "BIAS4Policy"; if (!string.IsNullOrEmpty(arguments["Policy"])) policyName = arguments["Policy"]; Assembly assembly = Assembly.GetAssembly(typeof(ITradePolicy)); Type policyType = assembly.GetType("StockDatabase.Library.Policy." + policyName, true); ITradePolicy policy = (ITradePolicy)Activator.CreateInstance(policyType, new object[] { }); Console.WriteLine("Stock processing policy is set to {0}", policyName); bool noDividend = false; if (!string.IsNullOrEmpty(arguments["NoDividend"]) && string.Compare(arguments["NoDividend"], Boolean.TrueString, true) == 0) { noDividend = true; Console.WriteLine("Stock decesion option \"NoDividend\" is set."); } // upload stock data string uploadStockData = arguments["UploadStockData"]; if (string.Compare(uploadStockData, Boolean.TrueString, true) == 0) { foreach (var inputData in reader.ReadLine()) { agent.UpdateAzureStockData(inputData.AzureTableStockCode); } } // check win rate string checkWinRate = arguments["CheckWinRate"]; bool saveWinRateResult = false; if (!string.IsNullOrEmpty(arguments["SaveWinRateResult"]) && string.Compare(arguments["SaveWinRateResult"], Boolean.TrueString, true) == 0) saveWinRateResult = true; DateTime checkWinRateTimeSince; if (string.Compare(checkWinRate, Boolean.TrueString, true) == 0) { foreach (var inputData in reader.ReadLine()) { agent.CheckWinRate(inputData.AzureTableStockCode,policy,saveResult:saveWinRateResult, noDividend: noDividend); } } else if (DateTime.TryParse(checkWinRate,out checkWinRateTimeSince)) { foreach (var inputData in reader.ReadLine()) { agent.CheckWinRate(inputData.AzureTableStockCode, policy, dateTimeSince: checkWinRateTimeSince.ToString("yyyy-MM-dd"), saveResult: saveWinRateResult, noDividend: noDividend); } } // make stock decesion string makeStockDecesion = arguments["MakeStockDecesion"]; if (string.Compare(makeStockDecesion, "close", true) == 0 || string.Compare(makeStockDecesion, "realtime", true) == 0) { bool realTime = (string.Compare(makeStockDecesion, "realtime", true) == 0); string stockDecesionOutputFile = arguments["StockDecesionOutputFile"]; bool savePlannedDecesionBuyPrice = (string.Compare(arguments["SavePlannedDecesionBuyPrice"], Boolean.TrueString, true) == 0); StreamWriter writer = null; if (!string.IsNullOrEmpty(stockDecesionOutputFile)) writer = new StreamWriter(stockDecesionOutputFile, false, Encoding.Default); try { foreach (var inputData in reader.ReadLine()) { StockHoldingDecesionResult decesion = agent.MakeStockHoldingDecision(inputData.AzureTableStockCode, realTime, policy, savePlannedDecesionBuyPrice: savePlannedDecesionBuyPrice); Console.WriteLine("{0}: {1}", inputData.AzureTableStockCode, decesion.Decesion); if (writer != null) { StockWinRateData winRate = agent.GetStoredWinRateResult(inputData.AzureTableStockCode); writer.WriteLine("{0}#{1}#{2}#{3}#{4}#{5}#{6}#{7}", inputData.StockName + " " + inputData.AzureTableStockCode, decesion.Price, decesion.Decesion, winRate.RecentWinRate, winRate.RecentProfitRate, winRate.OverallWinRate, winRate.OverallProfitRate, decesion.LastRawDataDate.ToString("yyyy-MM-dd")); } } } finally { if (writer != null) { writer.Close(); writer.Dispose(); } } } // set stock hold state string setStockHoldState = arguments["SetStockHoldState"]; if (!string.IsNullOrEmpty(setStockHoldState)) { string[] data = setStockHoldState.Split(InputFileFormatter.DataSplitter); if (data.Length != 2 && data.Length != 3) { throw new InvalidOperationException("Unknown param format for SetStockHoldState"); } string azureTableStockCode = data[0]; bool holdState = bool.Parse(data[1]); DateTime affectDate = data.Length == 3 ? DateTimeOffset.Parse(data[2]).DateTime : DateTimeOffset.Parse(DateTime.Today.ToString("yyyy-MM-dd")).DateTime; agent.SetStockHoldState(azureTableStockCode, holdState, affectDate); } // set stock hold state string setPlannedBuyPrice = arguments["SetPlannedBuyPrice"]; if (!string.IsNullOrEmpty(setPlannedBuyPrice)) { string[] data = setPlannedBuyPrice.Split(InputFileFormatter.DataSplitter); if (data.Length != 2) { throw new InvalidOperationException("Unknown param format for SetPlannedBuyPrice"); } string azureTableStockCode = data[0]; double price = double.Parse(data[1]); agent.SetStockPannedDecesionBuyPrice(azureTableStockCode, price); } }