/// <summary> /// 组装TradeRecord实例 /// </summary> /// <param name="fields"></param> /// <returns></returns> private TradeRecord MapTradeDataToTradeRecord(string[] fields) { int tradeAmount = int.Parse(fields[0]); decimal tradePrice = decimal.Parse(fields[1]); var tradeRecord = new TradeRecord { TradeAmount = tradeAmount, TradePrice = tradePrice }; return(tradeRecord); }
public TradeRecord Map(string[] fields) { var sourceCurrencyCode = fields[0].Substring(0, 3); var destinationCurrencyCode = fields[0].Substring(3, 3); var tradeAmount = int.Parse(fields[1]); var tradePrice = decimal.Parse(fields[2]); var trade = new TradeRecord { SourceCurrency = sourceCurrencyCode, DestinationCurrency = destinationCurrencyCode, Lots = tradeAmount / LotSize, Price = tradePrice }; return(trade); }
private TradeRecord MapTradeDataToTradeRecord(string[] fields) { var sourceCurrencyCode = fields[0].Substring(0, 3); var destinationCurrencyCode = fields[0].Substring(3, 3); var tradeAmount = int.Parse(fields[1]); var tradePrice = decimal.Parse(fields[2]); const float LotSize = 100000f; var trade = new TradeRecord { SourceCurrency = sourceCurrencyCode, DestinationCurrency = destinationCurrencyCode, Lots = tradeAmount / LotSize, Price = tradePrice }; return(trade); }
/// <summary> /// Converts a string containing the trade data into a TradeRecord object /// </summary> /// <param name="fields"> The string must be split into three components before calling </param> /// <returns> A TradeRecord object containing the trade data</returns> private TradeRecord MapTradeDataToTradeRecord(String[] fields) { var sourceCurrencyCode = fields[0].Substring(0, 3); var destinationCurrencyCode = fields[0].Substring(3, 3); int tradeAmount = int.Parse(fields[1]); decimal tradePrice = decimal.Parse(fields[2]); // calculate values var trade = new TradeRecord(); trade.SourceCurrency = sourceCurrencyCode; trade.DestinationCurrency = destinationCurrencyCode; trade.Lots = tradeAmount / LotSize; trade.Price = tradePrice; return(trade); }
/// <summary> /// Takes a list of strings containing trade data and converts this into a list of TradeRecord objects /// </summary> /// <param name="lines"> The strings containing the trade data, each string should contain one trade in format of "GBPUSD,1000,1.51"</param> /// <returns> A list of TradeRecords, one record for each trade </returns> private IEnumerable <TradeRecord> ParseTrades(IEnumerable <string> lines) { List <TradeRecord> trades = new List <TradeRecord>(); int lineCount = 1; foreach (var line in lines) { String[] fields = line.Split(new char[] { ',' }); if (ValidateTradeData(fields, lineCount) == false) { continue; } TradeRecord trade = MapTradeDataToTradeRecord(fields); trades.Add(trade); lineCount++; } return(trades); }
public IEnumerable <TradeRecord> Parser(IEnumerable <string> tradeData) { var trades = new List <TradeRecord>(); var lineCount = 1; foreach (var line in tradeData) { var fields = line.Split(new char[] { ',' }); if (!iTradeValidator.Validate(fields, lineCount)) { continue; } TradeRecord trade = iTradeMapper.Map(fields); trades.Add(trade); lineCount++; } return(trades); }
public void ProcessTrades(Stream stream) { // read rows var lines = new List <string>(); using (var reader = new StreamReader(stream)) { string line; while ((line = reader.ReadLine()) != null) { lines.Add(line); } } var trades = new List <TradeRecord>(); // Validations var lineCount = 1; foreach (var line in lines) { var fields = line.Split(new char[] { ',' }); if (fields.Length != 3) { Console.WriteLine("WARN: Line {0} malformed. Only {1} field(s) found.", lineCount, fields.Length); continue; } if (fields[0].Length != 6) { Console.WriteLine("WARN: Trade currencies on line {0} malformed: '{1}'", lineCount, fields[0]); continue; } int tradeAmount; if (!int.TryParse(fields[1], out tradeAmount)) { Console.WriteLine("WARN: Trade amount on line {0} not a valid integer: '{1}'", lineCount, fields[1]); } decimal tradePrice; if (!decimal.TryParse(fields[2], out tradePrice)) { Console.WriteLine("WARN: Trade price on line {0} not a valid decimal: '{1}'", lineCount, fields[2]); } var sourceCurrencyCode = fields[0].Substring(0, 3); var destinationCurrencyCode = fields[0].Substring(3, 3); // calculate values var trade = new TradeRecord { SourceCurrency = sourceCurrencyCode, DestinationCurrency = destinationCurrencyCode, Lots = tradeAmount / LotSize, Price = tradePrice }; trades.Add(trade); lineCount++; } // Store data using (var connection = new System.Data.SqlClient.SqlConnection("Data Source=(local);Initial Catalog=TradeDatabase;Integrated Security=True;")) { connection.Open(); using (var transaction = connection.BeginTransaction()) { foreach (var trade in trades) { var command = connection.CreateCommand(); command.Transaction = transaction; command.CommandType = System.Data.CommandType.StoredProcedure; command.CommandText = "dbo.insert_trade"; command.Parameters.AddWithValue("@sourceCurrency", trade.SourceCurrency); command.Parameters.AddWithValue("@destinationCurrency", trade.DestinationCurrency); command.Parameters.AddWithValue("@lots", trade.Lots); command.Parameters.AddWithValue("@price", trade.Price); command.ExecuteNonQuery(); } transaction.Commit(); } connection.Close(); } Console.WriteLine("INFO: {0} trades processed", trades.Count); }
private TradeRecord MapTradeDataToTradeRecord(string[] fields) { var sourceCurrencyCode = fields[0].Substring(0, 3); var destinationCurrencyCode = fields[0].Substring(3, 3); var tradeAmount = int.Parse(fields[1]); var tradePrice = decimal.Parse(fields[2]); var trade = new TradeRecord { SourceCurrency = sourceCurrencyCode, DestinationCurrency = destinationCurrencyCode, Lots = tradeAmount / LotSize, Price = tradePrice }; return trade; }
public void ProcessTrades(Stream stream) { var lines = new List <string>(); using (var reader = new StreamReader(stream)) { string line; while ((line = reader.ReadLine()) != null) { lines.Add(line); } } var trades = new List <TradeRecord>(); var lineCount = 1; foreach (var line in lines) { var fields = line.Split(new char[] { ',' }); if (fields.Length != 3) { Console.WriteLine("WARN: Line {0} malformed. Only {1} fields found", lineCount, fields.Length); } int tradeAmount; if (!int.TryParse(fields[0], out tradeAmount)) { Console.WriteLine("WARN: Trade amount on line {0} not a valid integer :{1}", lineCount, fields[0]); } decimal tradePrice; if (!decimal.TryParse(fields[1], out tradePrice)) { Console.WriteLine("WARN: Trade Price on line {0} not a valid decimal :{1}", lineCount, fields[1]); } var tradeRecord = new TradeRecord { TradeAmount = tradeAmount, TradePrice = tradePrice }; trades.Add(tradeRecord); lineCount++; } using (var connection = new SqlConnection("DataSource=(local);Initial Catalog=TradeDataBase;Integrated Security = True;")) { connection.Open(); using (var transaction = connection.BeginTransaction()) { foreach (var trade in trades) { var command = connection.CreateCommand(); command.Transaction = transaction; command.CommandType = System.Data.CommandType.StoredProcedure; command.CommandText = "insert_trade"; command.Parameters.AddWithValue("@tradeamount", trade.TradeAmount); command.Parameters.AddWithValue("@tradeprice", trade.TradePrice); } transaction.Commit(); } connection.Close(); } Console.WriteLine("INFO: {0} trades processed", trades.Count); }
public void ProcessTrades(Stream stream) { // read rows var lines = new List<string>(); using (var reader = new StreamReader(stream)) { string line; while((line = reader.ReadLine()) != null) { lines.Add(line); } } var trades = new List<TradeRecord>(); var lineCount = 1; foreach (var line in lines) { var fields = line.Split(new char[] { ',' }); if(fields.Length != 3) { Console.WriteLine("WARN: Line {0} malformed. Only {1} field(s) found.", lineCount, fields.Length); continue; } if(fields[0].Length != 6) { Console.WriteLine("WARN: Trade currencies on line {0} malformed: '{1}'", lineCount, fields[0]); continue; } int tradeAmount; if(!int.TryParse(fields[1], out tradeAmount)) { Console.WriteLine("WARN: Trade amount on line {0} not a valid integer: '{1}'", lineCount, fields[1]); } decimal tradePrice; if(!decimal.TryParse(fields[2], out tradePrice)) { Console.WriteLine("WARN: Trade price on line {0} not a valid decimal: '{1}'", lineCount, fields[2]); } var sourceCurrencyCode = fields[0].Substring(0, 3); var destinationCurrencyCode = fields[0].Substring(3, 3); // calculate values var trade = new TradeRecord { SourceCurrency = sourceCurrencyCode, DestinationCurrency = destinationCurrencyCode, Lots = tradeAmount / LotSize, Price = tradePrice }; trades.Add(trade); lineCount++; } using (var connection = new System.Data.SqlClient.SqlConnection("Data Source=(local);Initial Catalog=TradeDatabase;Integrated Security=True;")) { connection.Open(); using(var transaction = connection.BeginTransaction()) { foreach(var trade in trades) { var command = connection.CreateCommand(); command.Transaction = transaction; command.CommandType = System.Data.CommandType.StoredProcedure; command.CommandText = "dbo.insert_trade"; command.Parameters.AddWithValue("@sourceCurrency", trade.SourceCurrency); command.Parameters.AddWithValue("@destinationCurrency", trade.DestinationCurrency); command.Parameters.AddWithValue("@lots", trade.Lots); command.Parameters.AddWithValue("@price", trade.Price); command.ExecuteNonQuery(); } transaction.Commit(); } connection.Close(); } Console.WriteLine("INFO: {0} trades processed", trades.Count); }