/// <summary>
        /// Calculation method
        /// </summary>
        /// <param name="initial"></param>
        /// <param name="exercise"></param>
        /// <param name="up"></param>
        /// <param name="down"></param>
        /// <param name="interest"></param>
        /// <param name="periods"></param>
        /// <param name="sims"></param>
        /// <param name="result"></param>
        private string CalculationTask(double initial,
                                       double exercise,
                                       double up,
                                       double down,
                                       double interest,
                                       long periods,
                                       long sims)
        {
            //Cancellation token
            CancellationTokenSource cts   = new CancellationTokenSource();
            CancellationToken       token = cts.Token;

            Task <string> T = Task.Factory.StartNew(() =>
            {
                Random rand = new Random();
                int start   = System.Environment.TickCount;

                //The most heavy function
                double price = AsianOptionsPricing.Simulation(token, rand, initial, exercise, up, down, interest, periods, sims);

                int stop = System.Environment.TickCount;

                double elapsedTimeInSecs = (stop - start) / 1000.0;

                string result = string.Format("{0:C}  [{1:#,##0.00} secs]",
                                              price, elapsedTimeInSecs);
                return(result);
            },
                                                    token
                                                    );

            return(T.Result);
        }
Esempio n. 2
0
        /// <summary>
        /// Main button to run the simulation.
        /// </summary>
        private void cmdPriceOption_Click(object sender, RoutedEventArgs e)
        {
            this.spinnerWait.Visibility = System.Windows.Visibility.Visible;
            this.spinnerWait.Spin       = true;

            double initial  = Convert.ToDouble(txtInitialPrice.Text);
            double exercise = Convert.ToDouble(txtExercisePrice.Text);
            double up       = Convert.ToDouble(txtUpGrowth.Text);
            double down     = Convert.ToDouble(txtDownGrowth.Text);
            double interest = Convert.ToDouble(txtInterestRate.Text);
            long   periods  = Convert.ToInt64(txtPeriods.Text);
            long   sims     = Convert.ToInt64(txtSimulations.Text);

            int count = System.Convert.ToInt32(this.lblCount.Content);

            count++;
            this.lblCount.Content = count.ToString();

            string result = "";

            //
            // Run simulation on a separate task to price option:
            //
            Task T = Task.Factory.StartNew(() =>
            {
                Random rand = new Random();
                int start   = System.Environment.TickCount;

                double price = AsianOptionsPricing.Simulation(rand, initial, exercise, up, down, interest, periods, sims);

                int stop = System.Environment.TickCount;

                double elapsedTimeInSecs = (stop - start) / 1000.0;

                result = string.Format("{0:C}  [{1:#,##0.00} secs]",
                                       price, elapsedTimeInSecs);
            }
                                           );

            //
            // Display the results once computation task is done, but ensure
            // task is run on UI's thread context so UI update is legal:
            //
            T.ContinueWith((antecedent) =>
            {
                this.lstPrices.Items.Insert(0, result);

                count = System.Convert.ToInt32(this.lblCount.Content);
                count--;
                this.lblCount.Content = count.ToString();

                if (count == 0)
                {
                    this.spinnerWait.Spin       = false;
                    this.spinnerWait.Visibility = System.Windows.Visibility.Collapsed;
                }
            },
                           TaskScheduler.FromCurrentSynchronizationContext()
                           );
        }
        private void cmdPriceOption_Click(object sender, RoutedEventArgs e)
        {
            //this.cmdPriceOption.IsEnabled = false;

            this.spinnerWait.Visibility = System.Windows.Visibility.Visible;
            this.spinnerWait.Spin       = true;

            double initial  = Convert.ToDouble(txtInitialPrice.Text);
            double exercise = Convert.ToDouble(txtExercisePrice.Text);
            double up       = Convert.ToDouble(txtUpGrowth.Text);
            double down     = Convert.ToDouble(txtDownGrowth.Text);
            double interest = Convert.ToDouble(txtInterestRate.Text);
            long   periods  = Convert.ToInt64(txtPeriods.Text);
            long   sims     = Convert.ToInt64(txtSimulations.Text);

            m_Counter++;
            this.lblCount.Content = m_Counter.ToString();

            //
            // Run simulation to price option:
            //
            string result = string.Empty;
            Task   T      = new Task(() =>
            {
                Random rand = new Random();
                int start   = System.Environment.TickCount;

                double price = AsianOptionsPricing.Simulation(rand, initial, exercise, up, down, interest, periods, sims);

                int stop = System.Environment.TickCount;

                double elapsedTimeInSecs = (stop - start) / 1000.0;

                result = string.Format("{0:C}  [{1:#,##0.00} secs]",
                                       price, elapsedTimeInSecs);
            });
            //antecedent
            Task t2 = T.ContinueWith((antecedent) =>
            {
                //
                // Display the results:
                //
                this.lstPrices.Items.Insert(0, result);

                m_Counter--;
                this.lblCount.Content = m_Counter.ToString();

                if (m_Counter == 0)
                {
                    this.spinnerWait.Spin       = false;
                    this.spinnerWait.Visibility = System.Windows.Visibility.Collapsed;
                }
                //this.cmdPriceOption.IsEnabled = true;
            },
                                     TaskScheduler.FromCurrentSynchronizationContext()
                                     );

            T.Start();
        }
Esempio n. 4
0
        /// <summary>
        /// Main button to run the simulation.
        /// </summary>
        private void cmdPriceOption_Click(object sender, RoutedEventArgs e)
        {
            //TODO create a command for this action.
            if (_viewModel.TaskCounter == 0)
            {
                spinnerWait.Visibility = Visibility.Visible;
                spinnerWait.Spin       = true;
            }
            _viewModel.TaskCounter += 1;

            var    param  = new AsianOptionsPricing.Parameter(_viewModel);
            string result = string.Empty;

            Task.Factory.StartNew(() =>
            {
                //
                // Run simulation to price options:
                //
                var rand  = new Random(Guid.NewGuid().GetHashCode());
                int start = Environment.TickCount;

                double price = AsianOptionsPricing.Simulation(rand, param);

                int stop = Environment.TickCount;

                double elapsedTimeInSecs = (stop - start) / 1000.0;

                // Standard Numeric Format Strings
                // https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#example
                var ci = new CultureInfo("en");
                result = $"{price.ToString("C", ci)} [{elapsedTimeInSecs:#,##0.00} secs]";
            }).ContinueWith((antecedent) =>
            {
                //
                // Display the results:
                //
                _viewModel.Results.Insert(0, result);

                _viewModel.TaskCounter -= 1;
                if (_viewModel.TaskCounter == 0)
                {
                    spinnerWait.Spin       = false;
                    spinnerWait.Visibility = Visibility.Collapsed;
                }
            }, TaskScheduler.FromCurrentSynchronizationContext());
        }
Esempio n. 5
0
        /// <summary>
        /// Main button to run the simulation.
        /// </summary>
        private void cmdPriceOption_Click(object sender, RoutedEventArgs e)
        {
            this.cmdPriceOption.IsEnabled = false;

            this.spinnerWait.Visibility = System.Windows.Visibility.Visible;
            this.spinnerWait.Spin       = true;

            double initial  = Convert.ToDouble(txtInitialPrice.Text);
            double exercise = Convert.ToDouble(txtExercisePrice.Text);
            double up       = Convert.ToDouble(txtUpGrowth.Text);
            double down     = Convert.ToDouble(txtDownGrowth.Text);
            double interest = Convert.ToDouble(txtInterestRate.Text);
            long   periods  = Convert.ToInt64(txtPeriods.Text);
            long   sims     = Convert.ToInt64(txtSimulations.Text);

            //
            // Run simulation to price option:
            //
            Random rand  = new Random();
            int    start = System.Environment.TickCount;

            double price = AsianOptionsPricing.Simulation(rand, initial, exercise, up, down, interest, periods, sims);

            int stop = System.Environment.TickCount;

            double elapsedTimeInSecs = (stop - start) / 1000.0;

            string result = string.Format("{0:C}  [{1:#,##0.00} secs]",
                                          price, elapsedTimeInSecs);

            //
            // Display the results:
            //
            this.lstPrices.Items.Insert(0, result);

            this.spinnerWait.Spin       = false;
            this.spinnerWait.Visibility = System.Windows.Visibility.Collapsed;

            this.cmdPriceOption.IsEnabled = true;
        }
Esempio n. 6
0
        /// <summary>
        /// Main button to run the simulation.
        /// </summary>
        private void cmdPriceOption_Click(object sender, RoutedEventArgs e)
        {
            //this.cmdPriceOption.IsEnabled = false;

            this.spinnerWait.Visibility = System.Windows.Visibility.Visible;
            this.spinnerWait.Spin       = true;

            double initial  = Convert.ToDouble(txtInitialPrice.Text);
            double exercise = Convert.ToDouble(txtExercisePrice.Text);
            double up       = Convert.ToDouble(txtUpGrowth.Text);
            double down     = Convert.ToDouble(txtDownGrowth.Text);
            double interest = Convert.ToDouble(txtInterestRate.Text);
            long   periods  = Convert.ToInt64(txtPeriods.Text);
            long   sims     = Convert.ToInt64(txtSimulations.Text);

            m_counter++;
            this.lblCount.Content        = m_counter.ToString();
            this.mnuFileCancel.IsEnabled = true;

            //m_cts = new CancellationTokenSource();
            CancellationToken token = m_cts.Token;
            //
            // Run simulation to price option:
            //
            //string result = "";
            Task <string> T = Task.Factory.StartNew <string>(() =>
            {
                Random rand = new Random();
                int start   = System.Environment.TickCount;

                double price = AsianOptionsPricing.Simulation(token, rand, initial, exercise, up, down, interest, periods, sims);

                int stop = System.Environment.TickCount;

                double elapsedTimeInSecs = (stop - start) / 1000.0;

                string result = string.Format("{0:C}  [{1:#,##0.00} secs]",
                                              price, elapsedTimeInSecs);
                return(result);
            }, token);

            //
            // Display the results:
            //
            Task T2 = T.ContinueWith((antecedent) =>
            {
                string result;
                try
                {
                    result = antecedent.Result;
                }
                catch (AggregateException ae)
                {
                    result = ae.InnerException is OperationCanceledException ? "<< canceled >>" : "<< error?! >>";
                }
                this.lstPrices.Items.Insert(0, result);
                m_counter--;
                this.lblCount.Content = m_counter.ToString();

                if (m_counter == 0)
                {
                    this.mnuFileCancel.IsEnabled = false;
                    this.spinnerWait.Spin        = false;
                    this.spinnerWait.Visibility  = System.Windows.Visibility.Collapsed;
                }

                //this.cmdPriceOption.IsEnabled = true;
            },
                                     TaskScheduler.FromCurrentSynchronizationContext()
                                     );
        }
Esempio n. 7
0
        /// <summary>
        /// Main button to run the simulation.
        /// </summary>
        private void cmdPriceOption_Click(object sender, RoutedEventArgs e)
        {
            this.spinnerWait.Visibility = System.Windows.Visibility.Visible;
            this.spinnerWait.Spin       = true;

            double initial  = Convert.ToDouble(txtInitialPrice.Text);
            double exercise = Convert.ToDouble(txtExercisePrice.Text);
            double up       = Convert.ToDouble(txtUpGrowth.Text);
            double down     = Convert.ToDouble(txtDownGrowth.Text);
            double interest = Convert.ToDouble(txtInterestRate.Text);
            long   periods  = Convert.ToInt64(txtPeriods.Text);
            long   sims     = Convert.ToInt64(txtSimulations.Text);

            int count = System.Convert.ToInt32(this.lblCount.Content);

            count++;
            this.lblCount.Content = count.ToString();

            this.mnuFileCancel.IsEnabled = true;

            //
            // Run simulation on a separate task to price option:
            //
            CancellationToken token = m_cts.Token;

            Task <string> T = new Task <string>(() =>
            {
                Random rand = new Random();
                int start   = System.Environment.TickCount;

                //
                // NOTE: we pass cancellation token to simulation code so it can check for
                // cancellation and respond accordingly.
                //
                double price = AsianOptionsPricing.Simulation(token, rand, initial, exercise, up, down, interest, periods, sims);

                int stop = System.Environment.TickCount;

                double elapsedTimeInSecs = (stop - start) / 1000.0;

                string result = string.Format("{0:C}  [{1:#,##0.00} secs]",
                                              price, elapsedTimeInSecs);

                return(result);
            },

                                                token // for cancellation:
                                                );

            // add task to list so we can cancel computation if necessary:
            m_running.Add(T);

            // and start it running:
            T.Start();

            //
            // Display the results once computation task is done, but ensure
            // task is run on UI's thread context so UI update is legal:
            //
            T.ContinueWith((antecedent) =>
            {
                string result;

                // computation task has finished, remove from cancellation list:
                m_running.Remove(antecedent);

                //
                // Properly handle exceptions from computation task, e.g. possible cancellation:
                //
                try
                {
                    result = antecedent.Result;
                }
                catch (AggregateException ae)
                {
                    if (ae.InnerException is OperationCanceledException)
                    {
                        result = "<<canceled>>";
                    }
                    else
                    {
                        result = string.Format("<<error: {0}>>", ae.InnerException.Message);
                    }
                }
                catch (Exception ex)
                {
                    result = string.Format("<<error: {0}>>", ex.Message);
                }

                this.lstPrices.Items.Insert(0, result);

                //
                // update rest of UI:
                //
                count = System.Convert.ToInt32(this.lblCount.Content);
                count--;
                this.lblCount.Content = count.ToString();

                if (count == 0)
                {
                    this.mnuFileCancel.IsEnabled = false;

                    this.spinnerWait.Spin       = false;
                    this.spinnerWait.Visibility = System.Windows.Visibility.Collapsed;
                }
            },

                           //
                           // must run on UI thread since updating GUI:
                           //
                           TaskScheduler.FromCurrentSynchronizationContext()
                           );
        }
Esempio n. 8
0
        /// <summary>
        /// Main button to run the simulation.
        /// </summary>
        private void cmdPriceOption_Click(object sender, RoutedEventArgs e)
        {
            this.spinnerWait.Visibility = System.Windows.Visibility.Visible;
            this.spinnerWait.Spin       = true;

            double initial  = Convert.ToDouble(txtInitialPrice.Text);
            double exercise = Convert.ToDouble(txtExercisePrice.Text);
            double up       = Convert.ToDouble(txtUpGrowth.Text);
            double down     = Convert.ToDouble(txtDownGrowth.Text);
            double interest = Convert.ToDouble(txtInterestRate.Text);
            long   periods  = Convert.ToInt64(txtPeriods.Text);
            long   sims     = Convert.ToInt64(txtSimulations.Text);

            int count = System.Convert.ToInt32(this.lblCount.Content);

            count++;
            this.lblCount.Content = count.ToString();

            //By default it is false
            this.mnuFileCancel.IsEnabled = true;

            CancellationToken token = cts.Token;
            //
            // Run simulation on a separate task to price option:
            //
            Task <string> T = Task.Factory.StartNew <string>(() =>
            {
                Random rand = new Random();
                int start   = System.Environment.TickCount;

                double price = AsianOptionsPricing.Simulation(token, rand, initial, exercise, up, down, interest, periods, sims);

                int stop = System.Environment.TickCount;

                double elapsedTimeInSecs = (stop - start) / 1000.0;

                string result = string.Format("{0:C}  [{1:#,##0.00} secs]",
                                              price, elapsedTimeInSecs);

                return(result);
            },
                                                             token);

            //
            // Display the results once computation task is done, but ensure
            // task is run on UI's thread context so UI update is legal:
            //
            Task T2 = T.ContinueWith((antecedent) =>
            {
                string result;
                try
                {
                    result = antecedent.Result;
                }
                catch (AggregateException ae)
                {
                    if (ae.InnerException is OperationCanceledException)
                    {
                        result = "<< canceled >>";
                    }
                    else
                    {
                        result = "<< error? >>";
                    }
                }


                this.lstPrices.Items.Insert(0, result);

                count = System.Convert.ToInt32(this.lblCount.Content);
                count--;
                this.lblCount.Content = count.ToString();

                if (count == 0)
                {
                    this.mnuFileCancel.IsEnabled = false;
                    this.spinnerWait.Spin        = false;
                    this.spinnerWait.Visibility  = System.Windows.Visibility.Collapsed;
                }
            },
                                     TaskScheduler.FromCurrentSynchronizationContext()
                                     );
        }