Exemple #1
0
        public async Task <IActionResult> Create([Bind("ID,Balance,Name,UnitSize,ExpectedPriceChange,RiskFreeInterestRate")] TradingAccount tradingAccount)
        {
            if (ModelState.IsValid)
            {
                _context.Add(tradingAccount);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(tradingAccount));
        }
Exemple #2
0
        public async Task <IActionResult> Create([Bind("ID,Ask,Bid,Contents,Delta,Quantity,Strike")] Option option)
        {
            if (ModelState.IsValid)
            {
                _context.Add(option);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(option));
        }
Exemple #3
0
        public async Task <IActionResult> Upload([Bind("Contents")] CsvUpload csvUpload)
        {
            if (ModelState.IsValid)
            {
                _context.Options.Clear();

                TradingAccount tradingAccount = _context.TradingAccounts.FirstOrDefault();

                String[] split           = csvUpload.Contents.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                Decimal  underlyingPrice = Decimal.Parse(split[0]);

                using (TextReader tr = new StringReader(String.Join(Environment.NewLine, split.Skip(1)).Replace(",,", String.Empty).Replace("%", String.Empty)))
                {
                    var csv = new CsvReader(tr, new CsvConfiguration()
                    {
                        HasHeaderRecord = true
                    });
                    while (csv.Read())
                    {
                        Option callOption = new Option();
                        callOption.OptionType        = OptionTypes.Call;
                        callOption.Delta             = csv.GetField <Decimal>(0);
                        callOption.ImpliedVolatility = csv.GetField <Decimal>(1) / 100m;
                        callOption.Bid             = csv.GetField <Decimal>(2);
                        callOption.Ask             = csv.GetField <Decimal>(5);
                        callOption.Expiry          = csv.GetField <DateTime>(7);
                        callOption.Strike          = csv.GetField <Decimal>(8);
                        callOption.UnderlyingPrice = underlyingPrice;

                        callOption.FillCalculatedFields(tradingAccount);

                        _context.Options.Add(callOption);

                        Option putOption = new Option();
                        putOption.OptionType        = OptionTypes.Put;
                        putOption.Delta             = Math.Abs(csv.GetField <Decimal>(13));
                        putOption.ImpliedVolatility = csv.GetField <Decimal>(14) / 100m;
                        putOption.Bid             = csv.GetField <Decimal>(9);
                        putOption.Ask             = csv.GetField <Decimal>(11);
                        putOption.Expiry          = csv.GetField <DateTime>(7);
                        putOption.Strike          = csv.GetField <Decimal>(8);
                        putOption.UnderlyingPrice = underlyingPrice;

                        putOption.FillCalculatedFields(tradingAccount);

                        _context.Options.Add(putOption);
                    }

                    await _context.SaveChangesAsync();
                }

                return(RedirectToAction("Index", "Home"));
            }
            return(View(csvUpload));
        }
Exemple #4
0
        public async Task <IActionResult> Index(String sortProperty, String sortDirection, Decimal?expectedPriceChange)
        {
            OptionsLayout optionsLayout = new OptionsLayout();

            TradingAccount tradingAccount = await _context.TradingAccounts.FirstOrDefaultAsync();

            if (expectedPriceChange.HasValue)
            {
                tradingAccount.ExpectedPriceChange = expectedPriceChange.Value;

                foreach (Option option in await _context.Options.ToListAsync())
                {
                    option.FillCalculatedFields(tradingAccount);
                }

                await _context.SaveChangesAsync();
            }

            optionsLayout.TradingAccount = tradingAccount;

            if (String.IsNullOrEmpty(sortProperty))
            {
                sortProperty = "BlackScholesPLPercent";
            }

            if (String.IsNullOrEmpty(sortDirection))
            {
                sortDirection = "desc";
            }

            ViewData["sortProperty"]  = sortProperty;
            ViewData["sortDirection"] = sortDirection;

            optionsLayout.Puts  = _context.Options.Where(x => x.OptionType == OptionTypes.Put && x.BlackScholesPLPercent > 0).OrderBy(sortProperty, sortDirection).ToList();
            optionsLayout.Calls = _context.Options.Where(x => x.OptionType == OptionTypes.Call && x.BlackScholesPLPercent > 0).OrderBy(sortProperty, sortDirection).ToList();

            return(View(optionsLayout));
        }