public void getOutcome_Test_MoreComplex_Negative() { //Generate a input double[][] inputs = new double[100][]; DateTime dt = DateTime.Now.ToUniversalTime(); for (int i = 0; i < inputs.Length; i++) { //Date bid ask volume dt = dt.AddMilliseconds(1000); inputs[i] = new double[] { Timestamp.dateTimeToMilliseconds(dt), inputs.Length - i, inputs.Length - i, 0 }; } double successRate; double[][] outcomes = OutcomeGenerator.getOutcome(inputs, 1000 * 10, out successRate); int foundNaNs = 0; for (int i = 0; i < outcomes.Length; i++) { if (outcomes[i] != null) { Assert.AreEqual(inputs.Length - (i + 9), outcomes[i][(int)OutcomeMatrixIndices.Actual]); Assert.AreEqual(inputs.Length - (i + 9), outcomes[i][(int)OutcomeMatrixIndices.Min]); Assert.AreEqual(inputs.Length - (i + 1), outcomes[i][(int)OutcomeMatrixIndices.Max]); } else { foundNaNs++; } } Assert.AreEqual(10, foundNaNs); Assert.AreEqual(null, outcomes[outcomes.Length - 1]); }
public void getOutcome_Test_OnlyOneValue() { //Generate a input double[][] inputs = new double[100][]; DateTime dt = DateTime.Now.ToUniversalTime(); for (int i = 0; i < inputs.Length; i++) { //Date bid ask volume dt = dt.AddMilliseconds(1000); inputs[i] = new double[] { Timestamp.dateTimeToMilliseconds(dt), 2, 2, 0 }; } double successRate; double[][] outcomes = OutcomeGenerator.getOutcome(inputs, 1000 * 10, out successRate); int foundNaNs = 0; foreach (double[] row in outcomes) { if (row != null) { Assert.AreEqual(2, row[(int)OutcomeMatrixIndices.Actual]); Assert.AreEqual(2, row[(int)OutcomeMatrixIndices.Min]); Assert.AreEqual(2, row[(int)OutcomeMatrixIndices.Max]); } else { foundNaNs++; } } Assert.AreEqual(10, foundNaNs); Assert.AreEqual(null, outcomes[outcomes.Length - 1]); }
public void updateIndicators(long timeframeToLookBack, long timeframeToLookBackForIndicatorInit, IndicatorSelector indicatorSelector) { List <double[]> selectedPriceData = new List <double[]>(); for (int i = priceData.Count - 1; i > 0; i--) { if (Convert.ToInt64(priceData[i][(int)PriceDataIndeces.Date]) > timestampNow - timeframeToLookBack) { selectedPriceData.Insert(0, priceData[i]); //Todo: List direction correct? } else { break; } } double[][] selectedPriceDataArray = selectedPriceData.ToArray(); double s; double[][] outcomeData = OutcomeGenerator.getOutcome(selectedPriceDataArray, outcomeTimeframe, out s); if (s < 0.6) { throw new Exception("s < o.6: " + s); } //bool[][] outcomeCodeData = OutcomeGenerator.getOutcomeCode(selectedPriceDataArray, outcomeData, outcomeCodePercent, out s); bool[][] outcomeCodeFirstData = OutcomeGenerator.getOutcomeCodeFirst(selectedPriceDataArray, outcomeTimeframe, outcomeCodePercent, out s); if (s < 0.6) { throw new Exception("s < o.6: " + s); } string[] indicatorIds; //This part can be skipped by caching todo: get from outside double hash = outcomeTimeframe + selectedPriceData[0].Sum() + selectedPriceData[selectedPriceData.Count - 1].Sum() + selectedPriceData[selectedPriceData.Count / 2].Sum(); string optimalIndicatorsFileName = cachePath + "/" + "optimalIndicatorsIn_" + hash + "_" + selectedPriceData[selectedPriceData.Count - 1][(int)PriceDataIndeces.Date] + "_" + timeframeToLookBack + "_" + outcomeCodePercent + ".txt"; if (cachePath != null && File.Exists(optimalIndicatorsFileName)) { indicatorIds = File.ReadAllText(optimalIndicatorsFileName).Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); Logger.log("Loaded optimal indicators from file: " + optimalIndicatorsFileName); } else { //Shuffle okay indicators? todo: Logger.log("Generated optimal indicators"); IndicatorOptimizer optimizer = new IndicatorOptimizer(selectedPriceDataArray, outcomeData, outcomeCodeFirstData, outcomeTimeframe, outcomeCodePercent, minPercentThreshold, learningIndicatorSteps); indicatorIds = optimizer.getOptimizedIndicators(okayIndicators, indicatorSelector, 8); File.WriteAllLines(optimalIndicatorsFileName, indicatorIds); } Logger.log("Selected indicators: "); List <LearningIndicator> lis = new List <LearningIndicator>(); foreach (string str in indicatorIds) { Logger.log(str); WalkerIndicator ind = IndicatorGenerator.getIndicatorByString(str); if (ind.getName() != str) { throw new Exception(str + "!=" + ind.getName()); } lis.Add(new LearningIndicator(ind, selectedPriceDataArray, outcomeCodeFirstData, outcomeData, outcomeTimeframe, outcomeCodePercent, minPercentThreshold, learningIndicatorSteps, false)); } SignalMachine sm = new AlternativeSignalMachine(lis.ToArray()); //Todo: make accessable copy? Logger.log("SM STATE: ##################" + Environment.NewLine + sm.getStateMessage()); //Make them up to date List <double[]> selectedPriceDataForIndicatorInit = new List <double[]>(); for (int i = priceData.Count - 1; i > 0; i--) { if (Convert.ToInt64(priceData[i][(int)PriceDataIndeces.Date]) > timestampNow - timeframeToLookBackForIndicatorInit) { selectedPriceDataForIndicatorInit.Insert(0, priceData[i]); //Todo: List direction correct? } else { break; } } foreach (double[] row in selectedPriceDataForIndicatorInit) { sm.pushPrice(row); } this.signalMachine = sm; }
private void FindOkayIndicatorsForm_Load(object sender, EventArgs e) { timer1.Start(); string okayIndicatorsFile = "okayIndicators" + outcomeTimeframe + ".txt"; if (File.Exists(okayIndicatorsFile)) { List <string> lines = File.ReadLines(okayIndicatorsFile).ToList(); foreach (string line in lines) { if (line != "" && line != null && line != " ") { found.Add(line, true); } } } DataLoader dl = new DataLoader(Config.DataPath + pair); double[][] priceData = dl.getArray(1000 * 60 * 60 * 24 * 30l, timeframeToTest, minTimestep); //One month, but the second. Every 10 secs double success; bool[][] outcomeCodeFirstData = OutcomeGenerator.getOutcomeCodeFirst(priceData, outcomeTimeframe, outcomeCodePercent, out success); if (success < 0.7) { throw new Exception("OutcomeCode low success: " + success); } double[][] outcomeData = OutcomeGenerator.getOutcome(priceData, outcomeTimeframe, out success); if (success < 0.7) { throw new Exception("Outcome low success: " + success); } IndicatorGenerator generator = new IndicatorGenerator(); new Thread(delegate() { while (true) { WalkerIndicator ind = generator.getGeneratedIndicator(Convert.ToInt32((outcomeTimeframe / 1000) / 10), Convert.ToInt32((outcomeTimeframe / 1000) * 100)); try { if (found.ContainsKey(ind.getName()) == false) { tried++; new LearningIndicator(ind, priceData, outcomeCodeFirstData, outcomeData, outcomeTimeframe, outcomeCodePercent, minPercentThreshold, learningIndicatorSteps, false); File.AppendAllText(okayIndicatorsFile, ind.getName() + Environment.NewLine); found.Add(ind.getName(), true); } } catch (Exception) { } } }).Start(); }