/// <summary> /// Получение входных данных. /// </summary> private async Task <TestResultRepositiry> GetInputData() { runTime = String.Empty; TimeSpan _runTime; DateTime _startRun = DateTime.Now; List <Task> _tasks = new List <Task>(); string[] filePathes = StaticService.GetPathFiles(folderInput, patern1); testResultRepo = new TestResultRepositiry(); // очищаем папки StaticService.DeleteAllFile(folderOutTestResult, patern1); StaticService.DeleteAllFile(folderOutTestTradesSimple, patern1); StaticService.DeleteAllFile(folderOutTestTradesOPT, patern1); foreach (string _path in filePathes) // проходим по файлам { _tasks.Add(TrendAlgoritmTest(_path)); } await Task.WhenAll(_tasks); StaticService.Serializes(testResultRepo, folderOutTestResult + "!_actul_result"); _runTime = DateTime.Now - _startRun; runTime = String.Format("{0:00}:{1:00}:{2:00}.{3:000}", _runTime.Hours, _runTime.Minutes, _runTime.Seconds, _runTime.Milliseconds); return(testResultRepo); }
/// <summary> /// Расчет Болинжера по _countPeriod и _kstd /// </summary> private async Task CreateRepositoryBolinger(MarketTradesRepository _mtr, int _countPeriod, double _kstd, string _fileNameInp) { double High_line = 0; double Midl_line = 0; double Low_line = 0; double price; List <double> priceList = new List <double>(); TradesBolingerRepository _trdBReposit = new TradesBolingerRepository(); await Task.Run(() => { foreach (ParametrMarketTrades item in _mtr) { price = (double)item.PriceTrades; priceList.Add(price); if (priceList.Count == _countPeriod) { StaticCalculations.BollingerBands(priceList, _kstd, ref High_line, ref Midl_line, ref Low_line); priceList.RemoveAt(0); _trdBReposit.Add(new ParametrTradesBolinger(item.DateTimeTrades, item.NumberTrades, item.PriceTrades, item.SeccodeTrades, High_line, Low_line, Midl_line)); } } string _fileName = _fileNameInp + StaticService.GenerateKey(_countPeriod, _kstd); StaticService.Serializes(_trdBReposit, folderOut + _fileName); }); }
/// <summary> /// Task для конвертации в .dat /// </summary> private Task TaskConvert(byte[] _result, string _id) { StaticService.LogFileWrite(DateTime.Now.ToLongTimeString() + " " + _id, "runtime.txt", true); return(Task.Run(() => { string _text = Encoding.UTF8.GetString(_result); string[] _separator = new string[3] { "\r\n", "\n", "\r" }; string[] _mapStr = _text.Split(_separator, StringSplitOptions.RemoveEmptyEntries); string _date = String.Empty; string _time = String.Empty; decimal _price = 0; MarketTradesRepository marketTradRepo = new MarketTradesRepository(); int _iD = 0; foreach (string _line in _mapStr) { string[] _splites = Regex.Split(_line, paternRegEx1); _iD++; _date = _splites[0]; _time = _splites[1]; try { _price = Convert.ToDecimal(_splites[2]); } catch (Exception ex) { MessageBox.Show(ex.Message); } marketTradRepo.Add(new ParametrMarketTrades(_iD.ToString(), _date, _time, 0, _price, "")); } if (marketTradRepo.Count > 0) { StaticService.Serializes(marketTradRepo, folder + marketTradRepo[0].DateTimeTrades.ToShortDateString()); } StaticService.LogFileWrite(DateTime.Now.ToLongTimeString() + " " + _id, "runtime.txt", true); })); }
/// <summary> /// Синхронная конвертация в .dat /// </summary> private void ConvertMy() { StreamReader file; string[] filePathes = StaticService.GetPathFiles(folder, patern1); foreach (string _path in filePathes) // проходим по файлам { MarketTradesRepository marketTradRepo; string _date = String.Empty; string _time = String.Empty; decimal _price = 0; using (file = new StreamReader(_path)) { string line; marketTradRepo = new MarketTradesRepository(); int _id = 0; while ((line = file.ReadLine()) != null) // проходим по стркам { string[] _splites = Regex.Split(line, paternRegEx1); _id++; _date = _splites[0]; _time = _splites[1]; try { _price = Convert.ToDecimal(_splites[2]); } catch (Exception ex) { MessageBox.Show(ex.Message); } marketTradRepo.Add(new ParametrMarketTrades(_id.ToString(), _date, _time, 0, _price, "")); } } if (marketTradRepo.Count > 0) { StaticService.Serializes(marketTradRepo, folder + marketTradRepo[0].DateTimeTrades.ToShortDateString()); } } }
/// <summary> /// Открываем лонги, если цена выше самой верхней линии. /// Открываем шорты, если цена ниже самой нижней линии. /// Закрытие: переворачиваемся. /// </summary> private Task TrendAlgoritmTest(string _path) { return(Task.Run(() => { TradesBolingerRepository _trdBRepo = null; string file_name = "test_result.txt"; try { _trdBRepo = (TradesBolingerRepository)StaticService.Deserializes(_path); } catch (Exception) { StaticService.LogFileWrite(_path, "error_log.txt", true); } string _key = Path.GetFileNameWithoutExtension(_path); string _dateRes = StaticService.GetComment(_key, SettingsClass.PaternDate1); string _settBB = StaticService.GetComment(_key, SettingsClass.Patern); TestTradesCollection testTradColl = new TestTradesCollection(); TestTradesCollection testTradCollOPT = new TestTradesCollection(); int tp = 0; double profitPortfolio = 0; // прибыль портфеля на каждую сделку int countTrades = 0; double maxProfit = 0; double minProfit = 0; int countProfitTrades = 0; int countLossTrades = 0; // OPT double profitPortfolioOpt = 0; int countTradesOpt = 0; double maxProfitOpt = 0; double minProfitOpt = 0; int countProfitTradesOpt = 0; int countLossTradesOpt = 0; // end opt StaticService.LogFileWrite("\n ---------" + _key + "---------", file_name, true); if (_trdBRepo != null) { foreach (ParametrTradesBolinger pcT in _trdBRepo) { //StaticClassService.LogFileWrite(pcT.DateTimeTrades + "\t" + pcT.NumberTrades + "\t" + pcT.PriceTrades + "\t" + pcT.SeccodeTrades + "\t" + pcT.LineUp + "\t" + pcT.LineDown + "\t" + pcT.LineMidl, file_name, true); DateTime dateTimeTrade = pcT.DateTimeTrades; if (dateTimeTrade.TimeOfDay < new TimeSpan(19, 0, 0)) { double price = (double)pcT.PriceTrades; //---Здесь вызываем Класс нужного алгоритма--- Algoritms.BollingerCrossing.Algoritm(pcT, testTradColl, testTradCollOPT, file_name, price, dateTimeTrade, ref tp, ref countTrades, ref countTradesOpt, ref profitPortfolio, ref profitPortfolioOpt, ref maxProfit, ref maxProfitOpt, ref minProfit, ref minProfitOpt, ref countProfitTrades, ref countProfitTradesOpt, ref countLossTrades, ref countLossTradesOpt); //-------------------------------------------- } } } // save result testResultRepo.Add(new ParametrTestResult(_key, _dateRes, _settBB, profitPortfolio, countTrades, maxProfit, minProfit, countProfitTrades, countLossTrades)); testResultRepo.Add(new ParametrTestResult(_key + "OPT", _dateRes, _settBB, profitPortfolioOpt, countTradesOpt, maxProfitOpt, minProfitOpt, countProfitTradesOpt, countLossTradesOpt)); // Opt // serialize TestTradesCollection end TestTradesCollection'OPT' StaticService.Serializes(testTradColl, folderOutTestTradesSimple + _key); StaticService.Serializes(testTradCollOPT, folderOutTestTradesOPT + _key); })); }