Beispiel #1
0
        public static void Start()
        {
            Console.WriteLine("Starting Simulator 2");

            ShowGraphingForm();

            while (true)
            {
                var am = "";
                while (!(am == "a" || am == "m"))
                {
                    Console.WriteLine("Enter m for manual a for automatic");
                    am = Console.ReadLine();
                }


                if (am == "m")
                {
                    //Simulator2.ManualSimulate2();
                    Simulator2.ManualSimulate2();
                }
                else
                {
                    //Simulator2.AutoSimulate2();
                    Simulator2.AutoSimulate2();
                }
            }
        }
Beispiel #2
0
        public static void ManualSimulate2()
        {
            int lastCommonInterval = 0;
            int lastBigSma         = 0;
            int lastSmallSma       = 0;

            Simulator2 S = null;

            var          ProductName = "LTC-USD";
            TickerClient Ticker      = new TickerClient(ProductName);

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



            try
            {
                Console.WriteLine("Enter simulation start date:");
                var      inDt = Console.ReadLine();
                DateTime dt;
                DateTime.TryParse(inDt, out dt);

                Console.WriteLine("Enter Common time interval in minutes");
                var inputCommonInterval = Convert.ToInt16(Console.ReadLine());


                Console.WriteLine("Enter base price sma len: ");
                var basePriceSma = Convert.ToInt16(Console.ReadLine());

                Console.WriteLine("Enter big sma length");
                var inputBigSmaLen = Convert.ToInt16(Console.ReadLine());

                Console.WriteLine("Enter small sma length");
                var inputSmallSmaLen = Convert.ToInt16(Console.ReadLine());

                //Console.WriteLine("Enter signal len: ");
                //var inputSmaLen = Convert.ToInt16(Console.ReadLine());



                if (S == null)
                {
                    S             = new Simulator2(ref Ticker, ProductName, inputCommonInterval, inputBigSmaLen, inputSmallSmaLen, basePriceSma, true);
                    S.GraphWindow = _GraphingWindow;
                }
                else
                {
                    if (!(lastCommonInterval == inputCommonInterval && lastBigSma == inputBigSmaLen && lastSmallSma == inputSmallSmaLen))
                    {
                        S.Dispose();
                        S             = null;
                        S             = new Simulator2(ref Ticker, ProductName, inputCommonInterval, inputBigSmaLen, inputSmallSmaLen, basePriceSma);
                        S.GraphWindow = _GraphingWindow;
                    }
                }

                S.Calculate(dt, DateTime.Now, true, true);

                lastCommonInterval = inputCommonInterval;
                lastBigSma         = inputBigSmaLen;
                lastSmallSma       = inputSmallSmaLen;
            }
            catch (Exception ex)
            {
                Console.WriteLine("invalid input / error in calc: " + ex.Message);
            }
        }
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();
        }