Beispiel #1
0
        static IntervalData getIntervalData()
        {
            //var newInterval = new IntervalData
            //{
            //    interval = _random.Next(10, 200),
            //    bigSmaLen = _random.Next(90, 200),
            //    smallSmaLen = _random.Next(5, 100),
            //    basePriceSmaLen = _random.Next(1, 5)
            //};


            var newInterval = new IntervalData
            {
                interval        = _random.Next(10, 240),
                bigSmaLen       = _random.Next(151, 300),
                smallSmaLen     = _random.Next(5, 150),
                basePriceSmaLen = _random.Next(6, 12)
            };


            lock (_TriedListLock)
            {
                var beforeAdding = _TriedIntervalList.Count();

                _TriedIntervalList.Add(newInterval);

                var afterAdding = _TriedIntervalList.Count();

                while (beforeAdding == afterAdding)
                {
                    newInterval = new IntervalData
                    {
                        interval        = _random.Next(10, 200),
                        bigSmaLen       = _random.Next(90, 200),
                        smallSmaLen     = _random.Next(5, 100),
                        basePriceSmaLen = _random.Next(1, 5)
                    };


                    beforeAdding = _TriedIntervalList.Count();

                    _TriedIntervalList.Add(newInterval);

                    afterAdding = _TriedIntervalList.Count();
                }
            }


            return(newInterval);
        }
Beispiel #2
0
        public static IntervalRange GetaHalfRange(IntervalRange inputCurRange, IntervalData inputCurInterval)
        {
            var mid             = (inputCurRange.interval_max - inputCurRange.interval_min) / 2;
            var curBest         = inputCurInterval.interval;
            var newMin_Interval = curBest - (mid / 2);
            var newMax_Interval = curBest + (mid / 2);

            mid     = (inputCurRange.bigSmaLen_max - inputCurRange.bigSmaLen_min) / 2;
            curBest = inputCurInterval.bigSmaLen;
            var newBigsmaMin = curBest - (mid / 2);
            var newBigsmaMax = curBest + (mid / 2);

            mid     = (inputCurRange.smallSmaLen_max - inputCurRange.smallSmaLen_min) / 2;
            curBest = inputCurInterval.smallSmaLen;
            var newSmallSmaLen_min = curBest - (mid / 2);
            var newSmallSmaLen_max = curBest + (mid / 2);

            mid     = (inputCurRange.SignalLen_max - inputCurRange.SignalLen_min) / 2;
            curBest = inputCurInterval.SignalLen;
            var newSignalLen_min = curBest - (mid / 2);
            var newSignalLen_max = curBest + (mid / 2);


            return(new IntervalRange
            {
                interval_min = (newMin_Interval < 1) ? 1 : newMin_Interval,
                interval_max = (newMax_Interval < 1) ? 1 : newMax_Interval,

                bigSmaLen_min = (newBigsmaMin < 1) ? 1 : newBigsmaMin,
                bigSmaLen_max = (newBigsmaMax < 1) ? 1 : newBigsmaMax,

                smallSmaLen_min = (newSmallSmaLen_min < 1) ? 1 : newSmallSmaLen_min,
                smallSmaLen_max = (newSmallSmaLen_max < 1) ? 1 : newSmallSmaLen_max,

                SignalLen_min = (newSignalLen_min < 1) ? 1 : newSignalLen_min,
                SignalLen_max = (newSignalLen_max < 1) ? 1 : newSignalLen_max
            });
        }
Beispiel #3
0
        private static List <ResultData> RunSim_ThreadSafe(ref TickerClient inputTicker, DateTime startDt, DateTime endDt, int inputSimCount)
        {
            int lastCommonInterval = 0;
            int lastBigSma         = 0;
            int lastSmallSma       = 0;

            Simulator2 S = null;


            TickerClient Ticker = inputTicker;


            DateTime autoStartDate = startDt;


            DateTime autoEndDate = endDt;


            List <ResultData> resultList = new List <ResultData>();


            int simCount = inputSimCount;


            //Parallel.For(0, simCount, i =>
            for (int i = 0; i < simCount; i++)
            {
                IntervalData interval = null;
                interval = getIntervalData();
                try
                {
                    if (S == null)
                    {
                        S             = new Simulator2(ref Ticker, ProductName, interval.interval, interval.bigSmaLen, interval.smallSmaLen, interval.basePriceSmaLen, true);
                        S.GraphWindow = _GraphingWindow;
                    }
                    else
                    {
                        if (!(lastCommonInterval == interval.interval && lastBigSma == interval.bigSmaLen && lastSmallSma == interval.smallSmaLen))
                        {
                            S.Dispose();
                            S             = null;
                            S             = new Simulator2(ref Ticker, ProductName, interval.interval, interval.bigSmaLen, interval.smallSmaLen, interval.basePriceSmaLen);
                            S.GraphWindow = _GraphingWindow;
                        }
                    }

                    var plResult = S.Calculate(autoStartDate, autoEndDate, false, true);

                    resultList.Add(new ResultData {
                        Pl = plResult, intervals = interval
                    });


                    lastCommonInterval = interval.interval;
                    lastBigSma         = interval.bigSmaLen;
                    lastSmallSma       = interval.smallSmaLen;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("invalid input / error in calc");
                }
            }

            return(resultList);
        }
Beispiel #4
0
        public static void AutoSimulate2()
        {
            _TriedIntervalList.Clear();

            TickerClient Ticker = new TickerClient(ProductName);

            //wait for ticker to get ready
            Thread.Sleep(2 * 1000);



            DateTime dtStart = DateTime.MinValue;

            while (dtStart == DateTime.MinValue)
            {
                Console.WriteLine("Enter simulation start date:");
                var inDt = Console.ReadLine();
                DateTime.TryParse(inDt, out dtStart);
            }


            DateTime autoStartDate = dtStart;// new DateTime(2017, 10, 1);

            //DateTime autoEndDate = new DateTime(2018, 2, 13);//DateTime.Now;


            DateTime dtEnd = DateTime.MinValue;

            while (dtEnd == DateTime.MinValue)
            {
                Console.WriteLine("Enter simulation end date, enter n to use todays date:");
                var inDt = Console.ReadLine();

                if (inDt == "n")
                {
                    dtEnd = DateTime.Now;
                    break;
                }
                DateTime.TryParse(inDt, out dtEnd);
            }


            DateTime autoEndDate = dtEnd; //DateTime.Now;


            List <ResultData> allCombinedResultList = new List <ResultData>();



            var startTime = DateTime.Now;

            int simCount = 0;


            int Each_Sim_Count = 50;//50;

            while (simCount == 0)
            {
                Console.WriteLine("Enter number of simulation count, " + Each_Sim_Count + " minimun");
                simCount = Convert.ToInt32(Console.ReadLine());
            }


            List <Task> allSimTasks = new List <Task>();


            var eachBatchCount = 1000;


            var lastBatchCompletionTime = DateTime.Now;

            Int32 SLEEP_TIME_SEC = 30 * 1000;


            for (int batch = eachBatchCount; batch < simCount + eachBatchCount; batch += eachBatchCount)
            {
                Console.WriteLine("starting batch: " + (batch - eachBatchCount) + " - " + batch);

                if (batch > eachBatchCount)
                {
                    Thread.Sleep(SLEEP_TIME_SEC);
                }

                var threadCount = Math.Ceiling((eachBatchCount / (double)Each_Sim_Count));


                for (int i = 0; i < threadCount; i++)
                {
                    var curTask = Task.Factory.StartNew(() =>
                    {
                        var returnedResult = RunSim_ThreadSafe(ref Ticker, autoStartDate, autoEndDate, Each_Sim_Count);
                        allCombinedResultList.AddRange(returnedResult);
                    });

                    allSimTasks.Add(curTask);
                }



                Task.WaitAll(allSimTasks.ToArray());


                if (batch >= eachBatchCount)
                {
                    var timeTakenLastBatch = (DateTime.Now - lastBatchCompletionTime).TotalMinutes;
                    Console.WriteLine("Time taken to complete last batch (min): " + timeTakenLastBatch);

                    var expectedCompletionTime = (timeTakenLastBatch + ((SLEEP_TIME_SEC / 1000) / 60)) * ((simCount - batch) / eachBatchCount);
                    Console.WriteLine("Expected completion time: " + expectedCompletionTime + " min, " + DateTime.Now.AddMinutes(expectedCompletionTime));
                    lastBatchCompletionTime = DateTime.Now;
                }
            }



            Console.WriteLine("\nTop 5 profit results\n");

            IntervalData bestValues = null;

            for (int i = 0; i < 5; i++)
            {
                var best = allCombinedResultList.Where(d => d.Pl == allCombinedResultList.Max(a => a.Pl)).First();

                if (bestValues == null)
                {
                    bestValues = best.intervals;
                }

                var resMsg = "best result: " + i + "\n"
                             + "PL: " + best.Pl + "\n"
                             + "Interval: " + best.intervals.interval + "\n"
                             + "Base price sma: " + best.intervals.basePriceSmaLen + "\n"
                             + "big sma: " + best.intervals.bigSmaLen + "\n"
                             + "small sma: " + best.intervals.smallSmaLen + "\n";


                Logger.WriteLog("\n" + resMsg);

                allCombinedResultList.Remove(best);
            }



            var S = new Simulator2(ref Ticker, ProductName, bestValues.interval, bestValues.bigSmaLen, bestValues.smallSmaLen, bestValues.basePriceSmaLen, false);

            S.GraphWindow = _GraphingWindow;
            S.Calculate(autoStartDate, autoEndDate, true, true);



            //Console.WriteLine("\nTop 5 loss results\n");
            //for (int i = 0; i < 5; i++)
            //{
            //    var best = allCombinedResultList.Where(d => d.Pl == allCombinedResultList.Min(a => a.Pl)).First();
            //    var resMsg = "best result: " + i + "\n"
            //        + "PL: " + best.Pl + "\n"
            //        + "Interval: " + best.intervals.interval + "\n"
            //        + "Base price sma: " + best.intervals.basePriceSmaLen + "\n"
            //        + "big sma: " + best.intervals.bigSmaLen + "\n"
            //        + "small sma: " + best.intervals.smallSmaLen + "\n";


            //    Logger.WriteLog("\n" + resMsg);

            //    allCombinedResultList.Remove(best);

            //}


            Console.WriteLine("time taken for " + allSimTasks.Count() * Each_Sim_Count + " iterations (min): " + (DateTime.Now - startTime).TotalMinutes);

            //Console.ReadLine();
        }