コード例 #1
0
        public string[] getOptimizedIndicators(List <string> indicatorsToTry, IndicatorSelector selector, int threads)
        {
            ended = false;
            indicatorToTryIndex  = 0;
            indicatorsToTryCount = indicatorsToTry.Count;

            Logger.log("Start testing indicators");
            List <Thread> ths = new List <Thread>();

            for (int i = 0; i < threads; i++)
            {
                ths.Add(new Thread(delegate() { optimizeInternally(indicatorsToTry, selector); }));
            }

            foreach (Thread t in ths)
            {
                t.Start();
            }

            foreach (Thread t in ths)
            {
                if (t.IsAlive)
                {
                    t.Join();
                }
            }

            return(selector.getResultingCandidates());
        }
コード例 #2
0
        private void optimizeInternally(List <string> indicatorsToTry, IndicatorSelector selector)
        {
            while (ended == false)
            {
                try
                {
                    //generator.getGeneratedIndicator(Convert.ToInt32(outcomeTimeframe / 1000 / 15), Convert.ToInt32(outcomeTimeframe * 100 / 1000));
                    int index = getNextIndex();
                    if (index >= indicatorsToTry.Count)
                    {
                        break;
                    }

                    WalkerIndicator   wi = IndicatorGenerator.getIndicatorByString(indicatorsToTry[index]);
                    LearningIndicator li = new LearningIndicator(wi, priceData, outcomeCodeData, outcomeData, outcomeTimeframe, outcomeCodePercent, minPercentThreshold, learningIndicatorSteps, true);

                    selector.pushIndicatorStatistics(li);
                }
                catch (TooLittleValidDataException e)
                {
                    //Logger.log("E:" + e.Message);
                }
                catch (TooLittleStatesException e)
                {
                    //Logger.log("E:" + e.Message);
                }
                catch (Exception e)
                {
                    Logger.log("FATAL:" + e.Message);
                }
            }

            ended = true;
        }
コード例 #3
0
        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;
        }