Beispiel #1
0
        private void RadButton2_Click_1(object sender, EventArgs e)
        {
            // ConcurrentBag<List<Model.PNL>> consolidated = new ConcurrentBag<List<Model.PNL>>();
            try
            {
                Parallel.ForEach((radDropDownList1.Items), (x) =>
                {
                    ProgressDelegate myProgres = ShowMyProgress;
                    List <Model.Idea> myideas  = Common.GetIdeas();

                    Model.Idea selectedIdea = myideas.Where(a => a.Name == x.Text).First();
                    StockOHLC stockOHLC     = new StockOHLC();
                    int year = 2019;
                    //for (int year = 2015; year <= 2019; year++)
                    //{
                    //Load Data
                    Task <Dictionary <string, List <Model.Candle> > > loadmydata = Task.Run <Dictionary <string, List <Model.Candle> > >(() => stockOHLC.GetOHLC(new DateTime(year, Convert.ToInt32(ddlStartMonth.SelectedItem.Text), Convert.ToInt32(ddlStartDate.SelectedItem.Text)), new DateTime(year + 1, Convert.ToInt32(ddlEndMonth.SelectedItem.Text), Convert.ToInt32(ddlEndDate.SelectedItem.Text)), selectedIdea.Interval, myProgres));

                    //Apply indicators

                    loadmydata.ContinueWith((t0) =>
                    {
                        SetText("Applying indicators");
                        Task <Dictionary <string, List <Model.Candle> > > withIndicators = Task.Run <Dictionary <string, List <Model.Candle> > >(() => TechnicalIndicators.AddIndicators(t0.Result, selectedIdea.TI, new DateTime(year, Convert.ToInt32(ddlStartMonth.SelectedItem.Text), Convert.ToInt32(ddlStartDate.SelectedItem.Text)), new DateTime(Convert.ToInt32(ddlEndYear.SelectedItem.Text), Convert.ToInt32(ddlEndMonth.SelectedItem.Text), Convert.ToInt32(ddlEndDate.SelectedItem.Text))));
                        Task getTradingStocks = withIndicators.ContinueWith((t1) =>
                        {
                            Task <Dictionary <Guid, Model.StrategyModel> > getTradedStocks = Task.Run <Dictionary <Guid, Model.StrategyModel> >(() => stockOHLC.GetTopMostSolidGapOpenerDayWise(t1.Result, selectedIdea, myProgres));
                            Task tradeMyStocks = getTradedStocks.ContinueWith((t2) =>
                            {
                                Task <List <Model.PNL> > calculation = Task <List <Model.PNL> > .Run(() => stockOHLC.TradeStocks(t2.Result, t1.Result, selectedIdea, myProgres));
                                calculation.ContinueWith((t3) =>
                                {
                                    consolidated.Add(t3.Result);
                                    //SetDataSource(t3.Result);
                                    //SetText("Idea ran successfully");
                                });
                            });
                        });
                    });
                    //}
                });
                //while(consolidated.Count()< radDropDownList1.Items.Count)
                //{
                //    Thread.Sleep(10000);
                //}
            }
            catch (Exception ex)
            {
                File.AppendAllText(@"C:\Jai Sri Thakur Ji\Nifty Analysis\errors.txt", Environment.NewLine);
                File.AppendAllText(@"C:\Jai Sri Thakur Ji\Nifty Analysis\errors.txt", System.Reflection.MethodBase.GetCurrentMethod() + " :- " + ex.Message);
                File.AppendAllText(@"C:\Jai Sri Thakur Ji\Nifty Analysis\errors.txt", System.Reflection.MethodBase.GetCurrentMethod() + " :- " + ex.StackTrace);
                File.AppendAllText(@"C:\Jai Sri Thakur Ji\Nifty Analysis\errors.txt", System.Reflection.MethodBase.GetCurrentMethod() + " :- " + ex.InnerException);
            }
        }
Beispiel #2
0
        private void btnLoad_Click(object sender, EventArgs e)
        {
            l.Clear();
            //rgvStocks.DataSource = null;
            try
            {
                ProgressDelegate  myProgres = ShowMyProgress;
                List <Model.Idea> myideas   = null;
                if (checkBox1.Checked)
                {
                    myideas = Common.GetIdeas().OrderBy(a => a.runOrder).ToList();
                }
                else
                {
                    myideas = Common.GetIdeas().Where(a => a.Name == radDropDownList1.SelectedItem.Text).ToList();
                }

                List <Task> allTask = new List <Task>();
                //Model.Idea selectedIdea = myideas.Where(a => a.Name == radDropDownList1.SelectedItem.Text).First();
                foreach (var selectedIdea in myideas.OrderBy(a => a.runOrder))
                {
                    if (selectedIdea.Name == "Dual_Time_Frame_Momentum")
                    {
                        StockOHLC stockOHLC = new StockOHLC();

                        //Load Data
                        Task <Dictionary <string, List <Model.Candle> > > loadmydata = Task.Run <Dictionary <string, List <Model.Candle> > >(() => stockOHLC.GetOHLC(new DateTime(Convert.ToInt32(ddlStartYear.SelectedItem.Text), Convert.ToInt32(ddlStartMonth.SelectedItem.Text), Convert.ToInt32(ddlStartDate.SelectedItem.Text)), new DateTime(Convert.ToInt32(ddlEndYear.SelectedItem.Text), Convert.ToInt32(ddlEndMonth.SelectedItem.Text), Convert.ToInt32(ddlEndDate.SelectedItem.Text)), selectedIdea.Interval, myProgres));
                        allTask.Add(loadmydata);
                        loadmydata.ContinueWith((t0) =>
                        {
                            SetText("Applying stochastic indicators to small timeframe");
                            Task <Dictionary <string, List <Model.Candle> > > withIndicators = Task.Run <Dictionary <string, List <Model.Candle> > >(() => TechnicalIndicators.AddIndicators(t0.Result, selectedIdea.TI, new DateTime(Convert.ToInt32(ddlStartYear.SelectedItem.Text), Convert.ToInt32(ddlStartMonth.SelectedItem.Text), Convert.ToInt32(ddlStartDate.SelectedItem.Text)), new DateTime(Convert.ToInt32(ddlEndYear.SelectedItem.Text), Convert.ToInt32(ddlEndMonth.SelectedItem.Text), Convert.ToInt32(ddlEndDate.SelectedItem.Text))));
                            withIndicators.ContinueWith((t1) =>
                            {
                                Task <Dictionary <string, List <Model.Candle> > > loadmydata2 = Task.Run <Dictionary <string, List <Model.Candle> > >(() => stockOHLC.GetOHLC(new DateTime(Convert.ToInt32(ddlStartYear.SelectedItem.Text), Convert.ToInt32(ddlStartMonth.SelectedItem.Text), Convert.ToInt32(ddlStartDate.SelectedItem.Text)), new DateTime(Convert.ToInt32(ddlEndYear.SelectedItem.Text), Convert.ToInt32(ddlEndMonth.SelectedItem.Text), Convert.ToInt32(ddlEndDate.SelectedItem.Text)), selectedIdea.Interval2, myProgres));
                                allTask.Add(loadmydata2);
                                loadmydata2.ContinueWith((t2) =>
                                {
                                    SetText("Applying stochastic indicators to large time frame");
                                    Task <Dictionary <string, List <Model.Candle> > > withIndicators2 = Task.Run <Dictionary <string, List <Model.Candle> > >(() => TechnicalIndicators.AddIndicators(t2.Result, selectedIdea.TI, new DateTime(Convert.ToInt32(ddlStartYear.SelectedItem.Text), Convert.ToInt32(ddlStartMonth.SelectedItem.Text), Convert.ToInt32(ddlStartDate.SelectedItem.Text)), new DateTime(Convert.ToInt32(ddlEndYear.SelectedItem.Text), Convert.ToInt32(ddlEndMonth.SelectedItem.Text), Convert.ToInt32(ddlEndDate.SelectedItem.Text))));
                                    allTask.Add(withIndicators2);

                                    withIndicators2.ContinueWith((t3) =>
                                    {
                                        Task <Dictionary <Guid, Model.StrategyModel> > getTradedStocks = Task.Run <Dictionary <Guid, Model.StrategyModel> >(() => stockOHLC.ApplyDualMomentumStrategyModel(DateTime.Now, t1.Result, t3.Result, selectedIdea, myProgres));
                                        allTask.Add(getTradedStocks);
                                        Task tradeMyStocks = getTradedStocks.ContinueWith((t4) =>
                                        {
                                            var stocksList = t4.Result;
                                            List <PNL> p   = new List <PNL>();
                                            foreach (var s in stocksList)
                                            {
                                                p.Add(new PNL {
                                                    Stock = s.Value.Stock, Date = s.Value.Date, Direction = Enum.GetName(typeof(Model.Trade), s.Value.Trade)
                                                });
                                            }
                                            l.Add(new CustomizedPNL {
                                                order = selectedIdea.runOrder, selectedIdea = selectedIdea, Strategyoutput = p
                                            });
                                        });
                                    });
                                });
                            });
                        });
                    }
                    else
                    {
                        StockOHLC stockOHLC = new StockOHLC();

                        //Load Data
                        Task <Dictionary <string, List <Model.Candle> > > loadmydata = Task.Run <Dictionary <string, List <Model.Candle> > >(() => stockOHLC.GetOHLC(new DateTime(Convert.ToInt32(ddlStartYear.SelectedItem.Text), Convert.ToInt32(ddlStartMonth.SelectedItem.Text), Convert.ToInt32(ddlStartDate.SelectedItem.Text)), new DateTime(Convert.ToInt32(ddlEndYear.SelectedItem.Text), Convert.ToInt32(ddlEndMonth.SelectedItem.Text), Convert.ToInt32(ddlEndDate.SelectedItem.Text)), selectedIdea.Interval, myProgres));
                        allTask.Add(loadmydata);
                        //loadmydata.Wait();
                        //Apply indicators
                        loadmydata.ContinueWith((t0) =>
                        {
                            SetText("Applying indicators");
                            Task <Dictionary <string, List <Model.Candle> > > withIndicators = Task.Run <Dictionary <string, List <Model.Candle> > >(() => TechnicalIndicators.AddIndicators(t0.Result, selectedIdea.TI, new DateTime(Convert.ToInt32(ddlStartYear.SelectedItem.Text), Convert.ToInt32(ddlStartMonth.SelectedItem.Text), Convert.ToInt32(ddlStartDate.SelectedItem.Text)), new DateTime(Convert.ToInt32(ddlEndYear.SelectedItem.Text), Convert.ToInt32(ddlEndMonth.SelectedItem.Text), Convert.ToInt32(ddlEndDate.SelectedItem.Text))));
                            allTask.Add(withIndicators);
                            Task getTradingStocks = withIndicators.ContinueWith((t1) =>
                            {
                                Task <Dictionary <Guid, Model.StrategyModel> > getTradedStocks = Task.Run <Dictionary <Guid, Model.StrategyModel> >(() => stockOHLC.GetTopMostSolidGapOpenerDayWise(t1.Result, selectedIdea, myProgres));
                                allTask.Add(getTradedStocks);
                                Task tradeMyStocks = getTradedStocks.ContinueWith((t2) =>
                                {
                                    Task <List <Model.PNL> > calculation = Task <List <Model.PNL> > .Run(() => stockOHLC.TradeStocks(t2.Result, t1.Result, selectedIdea, myProgres));
                                    allTask.Add(getTradedStocks);
                                    calculation.ContinueWith((t3) =>
                                    {
                                        l.Add(new CustomizedPNL {
                                            order = selectedIdea.runOrder, selectedIdea = selectedIdea, Strategyoutput = t3.Result
                                        });
                                    });
                                    //calculation.Wait();
                                });
                            });
                        });


                        //loadmydata.Wait();
                        //Task.WaitAll();
                    }
                    Task.Factory.ContinueWhenAll(allTask.ToArray(), FinalWork);
                }
            }
            catch (Exception ex)
            {
                File.AppendAllText(@"C:\Jai Sri Thakur Ji\Nifty Analysis\errors.txt", Environment.NewLine);
                File.AppendAllText(@"C:\Jai Sri Thakur Ji\Nifty Analysis\errors.txt", System.Reflection.MethodBase.GetCurrentMethod() + " :- " + ex.Message);
                File.AppendAllText(@"C:\Jai Sri Thakur Ji\Nifty Analysis\errors.txt", System.Reflection.MethodBase.GetCurrentMethod() + " :- " + ex.StackTrace);
                File.AppendAllText(@"C:\Jai Sri Thakur Ji\Nifty Analysis\errors.txt", System.Reflection.MethodBase.GetCurrentMethod() + " :- " + ex.InnerException);
            }
        }