private void InitParametersRoundDigits() { ParametersRoundDigits = new int[steps.Length]; for (int i = 0; i < ParametersRoundDigits.Length; i++) { ParametersRoundDigits[i] = (int)GeneMath.Round(1 / (steps[i] * 10), 0).ToString().Length; } }
private void LoadTradeData(string tradeLogFilename) { InitParametersRoundDigits(); if (!File.Exists(tradeLogFilename)) { return; } if (!Directory.Exists("bin")) { Directory.CreateDirectory("bin"); } var indexes = GetIndexes(); var cultureInfo = CultureInfo.InvariantCulture; var firstDealDate = DateTime.MinValue; var lastDealDate = DateTime.MinValue; allDates = new Dictionary <DateTime, float>(); { #region Collecting aggregated variables values var tempAgr = new ConcurrentDictionary <string, SortedDictionary <long, int>[]>(); using (StreamReader reader = new StreamReader(new FileStream(tradeLogFilename, FileMode.Open, FileAccess.Read, FileShare.Read, 1024 * 1024 * 512))) { var first = true; reader.ReadLine(); var header = reader.ReadLine().Split(new char[] { ';' }); while (!reader.EndOfStream) { var data = reader.ReadLine().Split(new char[] { ';' }); DateTime date; // if trade date/time can't be parsed correctly, than skip that trade if (!DateTime.TryParse(data[OpenTime], cultureInfo, DateTimeStyles.AssumeLocal, out date)) { continue; } if (!allDates.ContainsKey(date.Date)) { allDates.Add(date.Date, 0); } // skip trade when it's open date/time not within specified interval if (Parameters.UseTradeLogDateTimeFilter && (date < Parameters.TradeLogFilterStartDateTime || date > Parameters.TradeLogFilterEndDateTime)) { continue; } if (first) { firstDealDate = date; first = false; } lastDealDate = date; var variables = new long[indexes.Length];//points является отдельной переменной и не входит в группу переменных variables string agrExName = ""; for (int index = 0; index < indexes.Length; index++) { if (indexes[index].Type == IndexType.Absolute) { switch (indexes[index].IndexName) { case "Points": break; case "FPwr": variables[index] = GetPowerLong(data[OpenMass], data[OpenAccel], steps[index]); break; case "APwr": variables[index] = GetPowerLong(data[OpenAgrExMass], data[OpenAgrExAccel], steps[index]); break; case "BPwr": variables[index] = GetPowerLong(data[BBOAgrMass], data[BBOAgrAccel], steps[index]); break; case "SPwr": variables[index] = GetPowerLong(data[SumAgrMass], data[AveAgrAccel], steps[index]); break; case "FAng": variables[index] = GetAngleLong(data[OpenMass], data[OpenAccel], steps[index]); break; case "AAng": variables[index] = GetAngleLong(data[OpenAgrExMass], data[OpenAgrExAccel], steps[index]); break; case "BAng": variables[index] = GetAngleLong(data[BBOAgrMass], data[BBOAgrAccel], steps[index]); break; case "SAng": variables[index] = GetAngleLong(data[SumAgrMass], data[AveAgrAccel], steps[index]); break; case "OpenTrendMid": double val; Utils.TryParse(data[OpenTrendMid], out val); if (val < 0) { agrExName = data[AgrEx] + 'd'; string makePositiveTrend = data[indexes[index].Column].Remove(0, 1); Utils.ParseLongDualCulture(makePositiveTrend, steps[index], out variables[index]); } else { agrExName = data[AgrEx] + 'u'; Utils.ParseLongDualCulture(data[indexes[index].Column], steps[index], out variables[index]); } break; default: Utils.ParseLongDualCulture(data[indexes[index].Column], steps[index], out variables[index]); break; } } else { float val, divisorVal; Utils.ParseFloatDualCulture(data[indexes[index].Column], out val); Utils.ParseFloatDualCulture(data[indexes[index].DivisorColumn], out divisorVal); if (divisorVal != 0) { variables[index] = (int)GeneMath.Round(val / divisorVal * (1 / steps[index])); } else { variables[index] = (int)GeneMath.Round(val * 100 * (1 / steps[index])); } } } var symbolExchenge = data[symbol].Replace('/', '_') + "_" + agrExName; SortedDictionary <long, int>[] symAgr; if (!tempAgr.ContainsKey(symbolExchenge)) { symAgr = new SortedDictionary <long, int> [indexes.Length]; tempAgr[symbolExchenge] = symAgr; for (int i = 0; i < variables.Length; i++) { symAgr[i] = new SortedDictionary <long, int>(); } } else { symAgr = tempAgr[symbolExchenge]; } for (int i = 0; i < variables.Length; i++) { var varAgr = symAgr[i]; if (!varAgr.ContainsKey(variables[i])) { varAgr[variables[i]] = 1; } else { varAgr[variables[i]]++; } } } } #endregion #region Aggregated values serialization var serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); foreach (var symExPair in tempAgr) { var symAgr = new AgrFeedData(); symAgr.Data = new SortedDictionary <int, long> [indexes.Length]; symAgr.VariablesBitLength = new int[symExPair.Value.Length]; var varIndex = 0; foreach (var varValues in symExPair.Value) { var varAgr = new SortedDictionary <int, long>(); symAgr.Data[varIndex] = varAgr; varValues.Add(varValues.Keys.ElementAt(0) - 1, 0); varValues.Add(varValues.Keys.ElementAt(varValues.Keys.Count - 1) + 1, 0); int i = 0; var coef = varValues.Count == 1 ? 1 : GetNextPowerof2Dec1(varValues.Count - 1) / (float)(varValues.Count - 1); var maxKey = 0; var lastIndex = -1; var lastValue = 0L; foreach (var valuePair in varValues) { var index = (int)Math.Round(i * coef); if (index - lastIndex > 1) { varAgr[index - 1] = lastValue; } varAgr[index] = valuePair.Key; i++; maxKey = index; lastIndex = index; lastValue = valuePair.Key; } foreach (var indexedPair in varAgr) { varValues[indexedPair.Value] = indexedPair.Key; } var bitsCount = GetBitsCount(maxKey); symAgr.VariablesBitLength[varIndex] = (int)bitsCount; varIndex++; } try { using (var stream = new FileStream($"bin\\{symExPair.Key}.bin", FileMode.Create)) { serializer.Serialize(stream, symAgr); } } catch (Exception ex) { MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace); } } #endregion var logDaysCount = lastDealDate.Date.Subtract(firstDealDate.Date).Days; var pointsDivizor = logDaysCount * ((Parameters.DatePriority - 1) == 0 ? 1 : Parameters.DatePriority - 1); #region Tradelog parsing using (StreamReader reader = new StreamReader(new FileStream(tradeLogFilename, FileMode.Open, FileAccess.Read, FileShare.Read, 1024 * 1024 * 512))) { reader.ReadLine(); var header = reader.ReadLine().Split(new char[] { ';' }); while (!reader.EndOfStream) { var data = reader.ReadLine().Split(new char[] { ';' }); DateTime date; // if trade date/time can't be parsed correctly, than skip that trade if (!DateTime.TryParse(data[OpenTime], cultureInfo, DateTimeStyles.AssumeLocal, out date)) { continue; } // skip trade when it's open date/time not within specified interval if (Parameters.UseTradeLogDateTimeFilter && (date < Parameters.TradeLogFilterStartDateTime || date > Parameters.TradeLogFilterEndDateTime)) { continue; } TradeInfo info = new TradeInfo(); info.openDate = date; var pointsDays = date.Date.Subtract(firstDealDate.Date).Days; var pointsMultiplier = (pointsDays / pointsDivizor + 1); Utils.ParseFloatDualCulture(data[MAE], out info.MAE); Utils.ParseFloatDualCulture(data[MFE], out info.MFE); info.variables = new int[indexes.Length];//points является отдельной переменной и не входит в группу переменных variables var variables = new long[indexes.Length]; string agrExName = ""; float dealPoints = 0; for (int index = 0; index < indexes.Length; index++) { if (indexes[index].Type == IndexType.Absolute) { switch (indexes[index].IndexName) { case "Points": float commission; Utils.ParseFloatDualCulture(data[indexes[index].Column], out info.Points); Utils.ParseFloatDualCulture(data[Profit], out info.Profit); Utils.ParseFloatDualCulture(data[Commission], out commission); if (info.Profit != 0) { info.PointsWithCommision = info.Points; if (!Parameters.BTlog) { dealPoints = info.Points * (1 + commission / info.Profit) > 50 && Parameters.ProfitLimit ? 50 : info.Points * (1 + commission / info.Profit); } else { dealPoints = info.Points + commission > 50 && Parameters.ProfitLimit ? 50 : info.Points + commission; } info.Points = dealPoints * pointsMultiplier; } break; case "FPwr": variables[index] = GetPowerLong(data[OpenMass], data[OpenAccel], steps[index]); break; case "APwr": variables[index] = GetPowerLong(data[OpenAgrExMass], data[OpenAgrExAccel], steps[index]); break; case "BPwr": variables[index] = GetPowerLong(data[BBOAgrMass], data[BBOAgrAccel], steps[index]); break; case "SPwr": variables[index] = GetPowerLong(data[SumAgrMass], data[AveAgrAccel], steps[index]); break; case "FAng": variables[index] = GetAngleLong(data[OpenMass], data[OpenAccel], steps[index]); break; case "AAng": variables[index] = GetAngleLong(data[OpenAgrExMass], data[OpenAgrExAccel], steps[index]); break; case "BAng": variables[index] = GetAngleLong(data[BBOAgrMass], data[BBOAgrAccel], steps[index]); break; case "SAng": variables[index] = GetAngleLong(data[SumAgrMass], data[AveAgrAccel], steps[index]); break; case "OpenTrendMid": double val; Utils.TryParse(data[OpenTrendMid], out val); if (val < 0) { agrExName = data[AgrEx] + 'd'; string makePositiveTrend = data[indexes[index].Column].Remove(0, 1); Utils.ParseLongDualCulture(makePositiveTrend, steps[index], out variables[index]); } else { agrExName = data[AgrEx] + 'u'; Utils.ParseLongDualCulture(data[indexes[index].Column], steps[index], out variables[index]); } break; case "OpenSlip": Utils.ParseFloatDualCulture(data[indexes[index].Column], out info.OpenSlip); Utils.ParseLongDualCulture(data[indexes[index].Column], steps[index], out variables[index]); break; default: Utils.ParseLongDualCulture(data[indexes[index].Column], steps[index], out variables[index]); break; } } else { float val, divisorVal; Utils.ParseFloatDualCulture(data[indexes[index].Column], out val); Utils.ParseFloatDualCulture(data[indexes[index].DivisorColumn], out divisorVal); if (divisorVal != 0) { variables[index] = (int)GeneMath.Round(val / divisorVal * (1 / steps[index])); } else { variables[index] = (int)GeneMath.Round(val * 100 * (1 / steps[index])); } } } var symbolExchenge = data[symbol].Replace('/', '_') + "_" + agrExName; var symAgr = tempAgr[symbolExchenge]; for (int index = 0; index < info.variables.Length; index++) { info.variables[index] = symAgr[index][variables[index]]; } info.isArbitrage = Parameters.UseAllFilters ? info.Points > 0 : (((float)Math.Abs(info.MFE / info.MAE) > 2 || info.MAE == 0) && info.Points > 0); List <TradeInfo> agrTrades = GetTradesList(data[symbol], agrExName); agrTrades.Add(info); bool isLastDeal = false; if (reader.EndOfStream) { isLastDeal = true; } if (Parameters.isVolumeSet) { GetDayPoints(data[symbol], agrExName, date.Date, dealPoints, isLastDeal); GetDayProfitExpectancy(data[symbol], agrExName, date.Date, info.MAE, info.MFE, dealPoints, isLastDeal); } if (Parameters.isMaxDDSet) { DateTime tradeClosed; // if trade date/time can't be parsed correctly, than skip that trade if (DateTime.TryParse(data[9], cultureInfo, DateTimeStyles.AssumeLocal, out tradeClosed)) { GetDealPoints(data[symbol], agrExName, tradeClosed, dealPoints); } } } } #endregion } GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true); }
public int[][] initialPositionOpt(int k) { int i, j, m, n, counter; int[,] basic; int[][] result; // разобраться - возможно параметр таки только целый бывает float l = Parameters.VariableNumberOfSlots; basic = new int[(int)GeneMath.Round((float)(l + (l - 1) * l / 2)), 2]; counter = 0; for (i = 0; i < l; i++) { for (j = 0; j < l - i; j++) { basic[counter, 0] = i; basic[counter, 1] = j; counter++; } } int size = 0; for (i = 0; i < k; i++) { counter = 0; int jMax = (int)GeneMath.Round(GeneMath.Exp(GeneMath.Log(l + (l - 1) * l / 2) * i)); for (j = 1; j <= jMax; j++) { int mMax = (int)GeneMath.Round((float)(l + (l - 1) * l / 2)) - 1; for (m = 0; m <= mMax; m++) { int nMax = (int)(GeneMath.Round(GeneMath.Exp(GeneMath.Log(l + (l - 1) * l / 2) * (k - 1 - i))) - 1); for (n = 0; n <= nMax; n++) { counter++; } } } size = size > counter ? size : counter; } result = new int[size][]; for (i = 0; i < k; i++) { int jMax = (int)GeneMath.Round(GeneMath.Exp(GeneMath.Log(l + (l - 1) * l / 2) * i)); counter = 0; for (j = 1; j <= jMax; j++) { int mMax = (int)GeneMath.Round((float)(l + (l - 1) * l / 2)) - 1; for (m = 0; m <= mMax; m++) { int basicM0 = basic[m, 0]; int basicM1 = basic[m, 1]; int nMax = ((int)GeneMath.Round(GeneMath.Exp(GeneMath.Log(l + (l - 1) * l / 2) * (k - 1 - i)))) - 1; for (n = 0; n <= nMax; n++) { if (i == 0) { result[counter] = new int[2]; result[counter][0] = basicM0; result[counter][1] = basicM1; } else { int[] tmp = result[counter]; result[counter] = new int[tmp.Length + 2]; tmp.CopyTo(result[counter], 0); result[counter][tmp.Length] = basicM0; result[counter][tmp.Length + 1] = basicM1; } counter++; } } } } return(result); }