diff --git a/App.config b/App.config index 2dc2ba4..5e7ff26 100644 --- a/App.config +++ b/App.config @@ -1,9 +1,9 @@ - - - + + + diff --git a/MarketDataLib/DataAccess/PortfolioDA.cs b/MarketDataLib/DataAccess/PortfolioDA.cs index c229b6a..0e4cbc1 100644 --- a/MarketDataLib/DataAccess/PortfolioDA.cs +++ b/MarketDataLib/DataAccess/PortfolioDA.cs @@ -58,7 +58,7 @@ namespace MarketData.DataAccess } public static bool InsertUpdateStopLimit(StopLimit stopLimit) { - if(null==stopLimit||null==stopLimit.Symbol||double.IsNaN(stopLimit.StopPrice))return false; + if(null==stopLimit || null==stopLimit.Symbol || double.IsNaN(stopLimit.StopPrice))return false; if(!HasStopLimit(stopLimit.Symbol))return InsertStopLimit(stopLimit); return UpdateStopLimit(stopLimit); } diff --git a/MarketDataLib/DataAccess/PricingDA.cs b/MarketDataLib/DataAccess/PricingDA.cs index 6b93c90..63113b2 100644 --- a/MarketDataLib/DataAccess/PricingDA.cs +++ b/MarketDataLib/DataAccess/PricingDA.cs @@ -1046,6 +1046,66 @@ namespace MarketData.DataAccess if (null != sqlConnection) sqlConnection.Close(); } } + + public static Dictionary GetLatestPrices(List symbols) + { + MySqlConnection sqlConnection = null; + MySqlDataReader sqlDataReader = null; + MySqlCommand sqlCommand=null; + String strQuery = null; + Dictionary latestPrices = new Dictionary(); + + try + { + StringBuilder sb = new StringBuilder(); + sqlConnection = SqlUtils.CreateMySqlConnection(MainDataSource.Instance.LocateDataSource("market_data")); + sb.Append($"SELECT B.date, B.symbol,B.open,B.high,B.low,B.close,B.volume,B.adjclose,B.source from "); + sb.Append($"(SELECT date, symbol, open, high, low, close, volume, adjclose, SOURCE, ROW_NUMBER() OVER ("); + sb.Append($"PARTITION BY symbol ORDER BY DATE DESC) AS rownum from prices "); + sb.Append($"WHERE symbol IN {SqlUtils.CreateInClause(symbols)})B where B.rownum=1"); + strQuery = sb.ToString(); ; + sqlCommand = new MySqlCommand(strQuery, sqlConnection); + sqlCommand.CommandTimeout = SqlUtils.COMMAND_TIMEOUT; + sqlDataReader = sqlCommand.ExecuteReader(); + while(sqlDataReader.Read()) + { + Price price = new Price(); + if(!sqlDataReader.IsDBNull(0))price.Date = sqlDataReader.GetDateTime(0); + else throw new Exception("Date cannot be null"); + if(!sqlDataReader.IsDBNull(1))price.Symbol = sqlDataReader.GetString(1); + else throw new Exception("Symbol cannot be null"); + if(!sqlDataReader.IsDBNull(2))price.Open = sqlDataReader.GetDouble(2); + else price.Open=double.NaN; + if(!sqlDataReader.IsDBNull(3))price.High = sqlDataReader.GetDouble(3); + else price.High=double.NaN; + if(!sqlDataReader.IsDBNull(4))price.Low = sqlDataReader.GetDouble(4); + else price.Low=double.NaN; + if(!sqlDataReader.IsDBNull(5))price.Close = sqlDataReader.GetDouble(5); + else price.Close=double.NaN; + if(!sqlDataReader.IsDBNull(6))price.Volume = sqlDataReader.GetInt64(6); + else price.Volume=0; + if(!sqlDataReader.IsDBNull(7))price.AdjClose = sqlDataReader.GetDouble(7); + else price.AdjClose=double.NaN; + if(!sqlDataReader.IsDBNull(8))price.Source = (Price.PriceSource)Utility.CharToNum(sqlDataReader.GetChar(8)); + else price.Source=Price.PriceSource.Other; + if(!latestPrices.ContainsKey(price.Symbol))latestPrices.Add(price.Symbol, price); + } + return latestPrices; + } + catch (Exception exception) + { + MDTrace.WriteLine(LogLevel.DEBUG,exception); + MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Query was {0}",strQuery)); + return latestPrices; + } + finally + { + if(null!=sqlCommand)sqlCommand.Dispose(); + if (null != sqlDataReader) {sqlDataReader.Close();sqlDataReader.Dispose();} + if (null != sqlConnection) sqlConnection.Close(); + } + } + public static Prices GetPrices(String symbol) { MySqlConnection sqlConnection = null; diff --git a/MarketDataLib/Generator/MGSHMomentum/MGSHBacktest.cs b/MarketDataLib/Generator/MGSHMomentum/MGSHBacktest.cs index 04f3788..dc09b2b 100644 --- a/MarketDataLib/Generator/MGSHMomentum/MGSHBacktest.cs +++ b/MarketDataLib/Generator/MGSHMomentum/MGSHBacktest.cs @@ -197,8 +197,13 @@ namespace MarketData.Generator.MGSHMomentum double totalTrades=sessionParams.AllPositions.Count; double winningTrades=sessionParams.AllPositions.Where(x => x.GainLoss>=0.00).Count(); double losingTrades=sessionParams.AllPositions.Where(x => x.GainLoss<0.00).Count(); - double averageWinningTrade=sessionParams.AllPositions.Where(x => x.GainLoss>=0.00).Average(x => x.GainLossPcnt)*100.00; - double averageLosingTrade=sessionParams.AllPositions.Where(x => x.GainLoss<0.00).Average(x => x.GainLossPcnt)*100.00; + MGSHPositions winningTradeList = new MGSHPositions(sessionParams.AllPositions.Where(x => x.GainLoss>=0.00).ToList()); + double averageWinningTrade = winningTradeList.Count>0?winningTradeList.Average(x => x.GainLossPcnt)*100.00:0.00; +// double averageWinningTrade=sessionParams.AllPositions.Where(x => x.GainLoss>=0.00).Average(x => x.GainLossPcnt)*100.00; + MGSHPositions losingTradeList = new MGSHPositions(sessionParams.AllPositions.Where(x => x.GainLoss<0.00).ToList()); + double averageLosingTrade = losingTradeList.Count>0?losingTradeList.Average(x => x.GainLossPcnt)*100.00:0.00; +// double averageLosingTrade=sessionParams.AllPositions.Where(x => x.GainLoss<0.00).Average(x => x.GainLossPcnt)*100.00; + double percentWinningTrades=(winningTrades/(double)sessionParams.AllPositions.Count)*100.00; double percentLosingTrades=(losingTrades/(double)sessionParams.AllPositions.Count)*100.00; double expectation=(percentWinningTrades*averageWinningTrade)/(percentLosingTrades*Math.Abs(averageLosingTrade));