/// <summary>
        /// Get stock historical price from Yahoo Finance
        /// </summary>
        /// <param name="symbol">Stock ticker symbol</param>
        /// <param name="start">Starting datetime</param>
        /// <param name="end">Ending datetime</param>
        /// <returns>List of history price</returns>
        public async Task <StockReportDto> GetReportViaYahoo(string symbol, DateTime start, DateTime end, string eventCode)
        {
            var historyPrices = new List <StockBarDto>();

            try
            {
                var csvData = GetRaw(symbol, start, end, eventCode);
                if (csvData != null)
                {
                    historyPrices = Parse(csvData);
                }
            }
            catch (Exception ex)
            {
                this._consoleHubProxy.WriteLine(ConsoleWriteLineInput.Create($"Unable to retrieve bars for: {symbol}"));

                return(new StockReportDto {
                    StartDate = start, EndDate = end, StockBars = historyPrices, FailedToRetrieveBars = true
                });
            }

            return(new StockReportDto {
                StartDate = start, EndDate = end, StockBars = historyPrices
            });
        }
Esempio n. 2
0
        public async Task <List <TosMarketDto> > LoadAndScrape(bool scrape = true)
        {
            List <TosMarketDto> markets = new List <TosMarketDto>();

            try
            {
                using (TextReader reader = File.OpenText("Files\\TOS_AllFutures.csv"))
                {
                    var csv = new CsvReader(reader);
                    csv.Configuration.RegisterClassMap <TosMarketDtoMap>();
                    markets = csv.GetRecords <TosMarketDto>().ToList();
                    markets.RemoveAll(x => x.Symbol == "GLB");
                }

                if (scrape)
                {
                    await Task.WhenAll(markets.Select(x => Scrape(x)));
                }
            }
            catch (Exception ex)
            {
                this._consoleHubProxy.WriteLine(ConsoleWriteLineInput.Create($"Exception: {ex.Message} {Environment.NewLine} Stacktrace: {ex.StackTrace}"));
            }

            return(markets);
        }
        private async Task <StockUpdatePriceAndDatesDto> ScrapePriceAndDates(Stock stock)
        {
            this._consoleHubProxy.WriteLine(ConsoleWriteLineInput.Create($"ScrapePriceAndDates for : {stock.Symbol}"));

            StockUpdatePriceAndDatesDto dto = stock.MapTo <StockUpdatePriceAndDatesDto>();

            using (WebClient webClient = new WebClient())
            {
                String html = await webClient.DownloadStringTaskAsync(String.Format(YahooUrl, stock.Symbol));

                var matches = _regexExDividendDate.Matches(html);

                dto.ExDividendDate = null;
                DateTime exDividendDate = DateTime.MinValue;
                if (matches.Count > 0 && DateTime.TryParse(matches[0].Groups["ExDividendDate"].Value, out exDividendDate))
                {
                    dto.ExDividendDate = exDividendDate;
                }

                matches         = _regexTargetPrice.Matches(html);
                dto.TargetPrice = null;
                Decimal targetPrice = 0m;
                if (matches.Count > 0 && Decimal.TryParse(matches[0].Groups["TargetPrice"].Value, out targetPrice))
                {
                    dto.TargetPrice = targetPrice;
                }

                matches = _regexNextEarningsDate.Matches(html);
                dto.NextEarningsDate = null;
                DateTime nextEarningsDate = DateTime.MinValue;
                if (matches.Count > 0 && DateTime.TryParse(matches[0].Groups["NextEarningsDate"].Value, out nextEarningsDate))
                {
                    dto.NextEarningsDate = nextEarningsDate;
                }

                matches   = _regexPrice.Matches(html);
                dto.Price = 0m;
                Decimal price = 0m;
                if (matches.Count > 0 && Decimal.TryParse(matches[0].Groups["Price"].Value, out price))
                {
                    dto.Price = price;
                }

                matches       = _regexAvgVolume.Matches(html);
                dto.AvgVolume = 0;
                int avgVolume = 0;
                if (matches.Count > 0 && Int32.TryParse(matches[0].Groups["AvgVolume"].Value.Replace(",", String.Empty), out avgVolume))
                {
                    dto.AvgVolume = avgVolume;
                }

                if (dto.AvgVolume.HasValue)
                {
                    dto.ADV = dto.AvgVolume.Value * dto.Price;
                }
            }


            return(dto);
        }
Esempio n. 4
0
        public void UpdateCoveredStockPositions(TradeFromPasteDto dto)
        {
            try
            {
                List <TradeDto> trades;

                using (var unitOfWork = this.UnitOfWorkManager.Begin())
                {
                    trades = _objectMapper.Map <List <TradeDto> >(_repository.GetAllIncluding(x => x.CoveredCallOption, x => x.Stock).Where(x => x.TradingAccountId == dto.TradingAccountId &&
                                                                                                                                            x.ExitReason == TradeExitReasons.None && x.CoveredCallOptionId.HasValue).ToList());
                    unitOfWork.Complete();
                }

                dto.ToUpdateCoveredCalls(trades);

                foreach (TradeDto trade in trades)
                {
                    using (var unitOfWork = this.UnitOfWorkManager.Begin())
                    {
                        SaveOptionTrade(trade, dto.Date);
                        unitOfWork.Complete();
                    }
                }
            }
            catch (Exception ex)
            {
                this._consoleHubProxy.WriteLine(ConsoleWriteLineInput.Create($"Exception: {ex.Message} {Environment.NewLine} Stacktrace: {ex.StackTrace}"));
            }
        }
        public void RecognizeText(int id)
        {
            Screenshot screenshot = this._repository.Get(id);

            //var i = new System.Drawing.Bitmap(new MemoryStream(screenshot.Data));
            //i.Save("image.bmp");

            using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default))
            {
                using (var image = new System.Drawing.Bitmap(new MemoryStream(screenshot.Data)))
                {
                    using (var pix = PixConverter.ToPix(image))
                    {
                        using (var page = engine.Process(pix))
                        {
                            var text = page.GetText();
                            Console.WriteLine("Mean confidence: {0}", page.GetMeanConfidence());

                            Console.WriteLine("Text (GetText): \r\n{0}", text);
                            Console.WriteLine("Text (iterator):");
                            using (var iter = page.GetIterator())
                            {
                                iter.Begin();

                                do
                                {
                                    do
                                    {
                                        do
                                        {
                                            do
                                            {
                                                if (iter.IsAtBeginningOf(PageIteratorLevel.Block))
                                                {
                                                    Console.WriteLine("<BLOCK>");
                                                }

                                                var t = iter.GetText(PageIteratorLevel.Word);
                                                this._consoleHubProxy.WriteLine(ConsoleWriteLineInput.Create(t));
                                                Console.Write(" ");

                                                if (iter.IsAtFinalOf(PageIteratorLevel.TextLine, PageIteratorLevel.Word))
                                                {
                                                    Console.WriteLine();
                                                }
                                            } while (iter.Next(PageIteratorLevel.TextLine, PageIteratorLevel.Word));

                                            if (iter.IsAtFinalOf(PageIteratorLevel.Para, PageIteratorLevel.TextLine))
                                            {
                                                Console.WriteLine();
                                            }
                                        } while (iter.Next(PageIteratorLevel.Para, PageIteratorLevel.TextLine));
                                    } while (iter.Next(PageIteratorLevel.Block, PageIteratorLevel.Para));
                                } while (iter.Next(PageIteratorLevel.Block));
                            }
                        }
                    }
                }
            }
        }
        private async Task <StockDto> ReportWrapper(Stock stock, DateTime date, int period, int lookback)
        {
            StockDto dto = stock.MapTo <StockDto>();

            dto.StockReports = await GetReports(stock.Symbol, date, period, lookback);

            this._consoleHubProxy.WriteLine(ConsoleWriteLineInput.Create($"Generating reports for : {stock.Symbol}"));

            return(dto);
        }
Esempio n. 7
0
        public void Simulate(List <Trade> sample, List <Market> markets, IConsoleHubProxy consoleHubProxy)
        {
            Random random     = new Random(Clock.Now.Millisecond);
            int    sampleSize = sample.Count;
            List <MonteCarloSimulationIteration> iterations = new List <MonteCarloSimulationIteration>();

            for (int i = 0; i < this.NumberOfIterations; i++)
            {
                consoleHubProxy.WriteLine(ConsoleWriteLineInput.Create($"Simulation iteration {i+1} of {this.NumberOfIterations}"));

                MonteCarloSimulationIteration iteration = new MonteCarloSimulationIteration();
                for (int j = 0; j < this.NumberOfTradesPerIteration; j++)
                {
                    MonteCarloSimulationTrade trade = new MonteCarloSimulationTrade {
                        NetProfit = sample[random.Next(sampleSize)].ProfitLossPerContract
                    };
                    iteration.Trades.Add(trade);
                    trade.CumulativeProfit = iteration.Trades.Sum(x => x.NetProfit);
                    trade.Drawdown         = trade.CumulativeProfit - iteration.Trades.Max(x => x.CumulativeProfit);

                    if (trade.NetProfit < 0)
                    {
                        if (iteration.Trades.Count == 1)
                        {
                            trade.ConsecutiveLosses = 1;
                        }
                        else
                        {
                            trade.ConsecutiveLosses = iteration.Trades[iteration.Trades.Count - 2].ConsecutiveLosses + 1;
                        }
                    }
                }

                iteration.CumulativeProfit  = iteration.Trades.Last().CumulativeProfit;
                iteration.MaxDrawdown       = iteration.Trades.Min(x => x.Drawdown);
                iteration.ConsecutiveLosses = iteration.Trades.Max(x => x.ConsecutiveLosses);
                iterations.Add(iteration);
            }

            this.CumulativeProfit = Extensions.Percentile <Decimal>(iterations.Select(x => x.CumulativeProfit).ToList(), 1.0m - this.CumulativeProfitK);
            this.TradingEdge      = this.CumulativeProfit > 0;

            this.ConsecutiveLosses = Extensions.Percentile <int>(iterations.Select(x => x.ConsecutiveLosses).ToList(), this.ConsecutiveLossesK);
            this.MaxDrawdown       = Extensions.Percentile <Decimal>(iterations.Select(x => x.MaxDrawdown).ToList(), 1.0m - this.MaxDrawdownK);
            this.OneContractFunds  = this.RuinPoint + (this.MaxDrawdownMultiple * Math.Abs(this.MaxDrawdown));
            this.MaxContracts      = (int)Math.Floor(this.AccountSize / this.OneContractFunds);

            foreach (Market market in markets)
            {
                this.MarketMaxContractsList.Add(new MarketMaxContracts {
                    Symbol = market.Symbol, Size = (int)Math.Floor(this.AccountSize / (market.InitialMargin + 10m + (this.MaxDrawdownMultiple * Math.Abs(this.MaxDrawdown))))
                });
            }
        }
        public void WriteLine(ConsoleWriteLineInput consoleWriteLineInput)
        {
            //var client = new RestClient(this._settingManager.GetSettingValue("Path"));
            var client  = new RestClient(ConfigurationManager.AppSettings["Path"]);
            var request = new RestRequest("ConsoleSignalR/WriteLine", Method.POST);

            request.RequestFormat = DataFormat.Json;
            request.AddJsonBody(new { input = consoleWriteLineInput });

            RestResponse response = (RestResponse)client.Execute(request);
            //});
        }
        /// <summary>
        /// Get raw stock historical price from Yahoo Finance
        /// </summary>
        /// <param name="symbol">Stock ticker symbol</param>
        /// <param name="start">Starting datetime</param>
        /// <param name="end">Ending datetime</param>
        /// <returns>Raw history price string</returns>
        public string GetRaw(string symbol, DateTime start, DateTime end, string eventCode)
        {
            string csvData = null;

            try
            {
                var url = "https://query1.finance.yahoo.com/v7/finance/download/{0}?period1={1}&period2={2}&interval=1d&events={3}&crumb={4}";

                //if no token found, refresh it
                if (string.IsNullOrEmpty(Token.Cookie) | string.IsNullOrEmpty(Token.Crumb))
                {
                    if (!Token.Refresh(symbol))
                    {
                        return(GetRaw(symbol, start, end, eventCode));
                    }
                }

                url = string.Format(url, symbol, Math.Round(Time.DateTimeToUnixTimeStamp(start), 0), Math.Round(Time.DateTimeToUnixTimeStamp(end), 0), eventCode, Token.Crumb);
                using (var wc = new WebClient())
                {
                    wc.Headers.Add(HttpRequestHeader.Cookie, Token.Cookie);
                    csvData = wc.DownloadString(url);
                }
            }
            catch (WebException webEx)
            {
                var response = (HttpWebResponse)webEx.Response;

                //Re-fecthing token
                if (response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    //Debug.Print(webEx.Message);
                    //Token.Reset();
                    //Debug.Print("Re-fetch");
                    return(GetRaw(symbol, start, end, eventCode));
                }
                throw;
            }
            catch (Exception ex)
            {
                this._consoleHubProxy.WriteLine(ConsoleWriteLineInput.Create($"Exception: {ex.Message} {Environment.NewLine} Stacktrace: {ex.StackTrace}"));
            }

            return(csvData);
        }
Esempio n. 10
0
 public void AddTradeFromPaste(TradeFromPasteDto dto)
 {
     try
     {
         foreach (TradeDto trade in dto.ToFutureTradeDto(_marketRepository.GetAllList()))
         {
             using (var unitOfWork = this.UnitOfWorkManager.Begin())
             {
                 Save(trade);
                 unitOfWork.Complete();
             }
         }
     }
     catch (Exception ex)
     {
         this._consoleHubProxy.WriteLine(ConsoleWriteLineInput.Create($"Exception: {ex.Message} {Environment.NewLine} Stacktrace: {ex.StackTrace}"));
     }
 }
Esempio n. 11
0
 public void OpenCoveredStockPositions(TradeFromPasteDto dto)
 {
     try
     {
         foreach (TradeDto trade in dto.ToOpenCoveredCalls(_stockRepository.GetAllList()))
         {
             using (var unitOfWork = this.UnitOfWorkManager.Begin())
             {
                 SaveOptionTrade(trade, dto.Date);
                 unitOfWork.Complete();
             }
         }
     }
     catch (Exception ex)
     {
         this._consoleHubProxy.WriteLine(ConsoleWriteLineInput.Create($"Exception: {ex.Message} {Environment.NewLine} Stacktrace: {ex.StackTrace}"));
     }
 }
        /// <summary>
        /// Parse raw historical price data into list
        /// </summary>
        /// <param name="csvData"></param>
        /// <returns></returns>
        private List <StockBarDto> Parse(string csvData)
        {
            var hps = new List <StockBarDto>();

            try
            {
                var rows = csvData.Split(Convert.ToChar(10));

                //row(0) was ignored because is column names
                //data is read from oldest to latest
                for (var i = 1; i <= rows.Length - 1; i++)
                {
                    var row = rows[i];
                    if (string.IsNullOrEmpty(row))
                    {
                        continue;
                    }

                    var cols = row.Split(',');
                    if (cols[1] == "null")
                    {
                        continue;
                    }

                    var hp = new StockBarDto
                    {
                        Date = DateTime.Parse(cols[0]),
                        Open = Convert.ToDecimal(cols[1]),
                        High = Convert.ToDecimal(cols[2]),
                        Low  = Convert.ToDecimal(cols[3]),
                        //Close = Convert.ToDecimal(cols[4]),
                        Close = Convert.ToDecimal(cols[5])
                    };

                    hps.Add(hp);
                }
            }
            catch (Exception ex)
            {
                this._consoleHubProxy.WriteLine(ConsoleWriteLineInput.Create($"Exception: {ex.Message} {Environment.NewLine} Stacktrace: {ex.StackTrace}"));
            }

            return(hps);
        }
        public async Task GenerateReports(GenerateStockReportsInput input)
        {
            try
            {
                //this._sqlExecuter.Execute("DELETE FROM [GuerillaTrader].[dbo].[StockReports]");

                List <Stock> stocks = new List <Stock>();

                using (var unitOfWork = this.UnitOfWorkManager.Begin())
                {
                    stocks = this._repository.GetAllIncluding(x => x.StockReports.Select(y => y.StockBars)).ToList();
                    unitOfWork.Complete();
                }

                int pageSize  = 50;
                int pageCount = (int)Math.Ceiling(((Double)stocks.Count / (Double)pageSize));

                for (int i = 0; i < pageCount; i++)
                {
                    this._consoleHubProxy.WriteLine(ConsoleWriteLineInput.Create($"Page {i + 1} of {pageCount}..."));

                    using (var unitOfWork = this.UnitOfWorkManager.Begin())
                    {
                        List <StockDto> dtos = (await Task.WhenAll(stocks.Skip(i * pageSize).Take(pageSize).Select(x => ReportWrapper(x, input.StartDate, input.Period, input.Lookback)).ToArray())).ToList();
                        foreach (StockDto dto in dtos)
                        {
                            dto.SetStats(input.Period);
                            Save(dto);
                        }
                        unitOfWork.Complete();
                    }

                    this._consoleHubProxy.WriteLine(ConsoleWriteLineInput.Create($"Page {i + 1} of {pageCount} finished."));
                }
            }
            catch (Exception ex)
            {
                this._consoleHubProxy.WriteLine(ConsoleWriteLineInput.Create($"Exception: {ex.Message} {Environment.NewLine} Stacktrace: {ex.StackTrace}"));
            }
        }
        public void UpdateTaxProperties()
        {
            List <TaxRateStockDto> taxRateStocks = new List <TaxRateStockDto>();

            using (TextReader reader = File.OpenText("Files\\SP500TaxRates.csv"))
            {
                var csv = new CsvReader(reader);
                csv.Configuration.RegisterClassMap <TaxRateStockDtoMap>();
                taxRateStocks = csv.GetRecords <TaxRateStockDto>().ToList();
            }

            List <Stock> stocks = _repository.GetAllList();

            foreach (Stock stock in stocks)
            {
                TaxRateStockDto dto = taxRateStocks.FirstOrDefault(x => x.CompanyName.StartsWith(stock.Name, StringComparison.InvariantCulture));
                if (dto == null)
                {
                    this._consoleHubProxy.WriteLine(ConsoleWriteLineInput.Create($"{stock.Name}: {stock.Symbol}"));
                }
            }
        }
        public async Task UpdatePriceAndDates()
        {
            try
            {
                List <Stock> stocks = new List <Stock>();

                using (var unitOfWork = this.UnitOfWorkManager.Begin())
                {
                    //stocks = this._repository.GetAll().Where(x => x.Symbol == "GOOGL").ToList();
                    stocks = this._repository.GetAll().ToList();
                    unitOfWork.Complete();
                }

                int pageSize  = 50;
                int pageCount = (int)Math.Ceiling(((Double)stocks.Count / (Double)pageSize));

                for (int i = 0; i < pageCount; i++)
                {
                    this._consoleHubProxy.WriteLine(ConsoleWriteLineInput.Create($"Page {i + 1} of {pageCount}..."));

                    using (var unitOfWork = this.UnitOfWorkManager.Begin())
                    {
                        List <StockUpdatePriceAndDatesDto> dtos = (await Task.WhenAll(stocks.Skip(i * pageSize).Take(pageSize).Select(x => ScrapePriceAndDates(x)).ToArray())).ToList();
                        foreach (StockUpdatePriceAndDatesDto dto in dtos)
                        {
                            Save(dto);
                        }
                        unitOfWork.Complete();
                    }

                    this._consoleHubProxy.WriteLine(ConsoleWriteLineInput.Create($"Page {i + 1} of {pageCount} finished."));
                }
            }
            catch (Exception ex)
            {
                this._consoleHubProxy.WriteLine(ConsoleWriteLineInput.Create($"Exception: {ex.Message} {Environment.NewLine} Stacktrace: {ex.StackTrace}"));
            }
        }
 public ActionResult WriteLine(ConsoleWriteLineInput input)
 {
     this._consoleHub.WriteLine(input);
     return(this.Json(new { success = true }));
 }
Esempio n. 17
0
 public void WriteLine(ConsoleWriteLineInput input)
 {
     Clients.All.writeLine(input.Line);
 }
        private async Task <StockReportDto> GetReportViaGoogle(String symbol, DateTime start, DateTime end)
        {
            try
            {
                var startdate = ((ConvertMonths)start.Month).ToString()
                                + @"+" + start.Day.ToString()
                                + @"%2C+" + start.Year.ToString();
                var enddate = ((ConvertMonths)end.Month).ToString()
                              + @"+" + end.Day.ToString()
                              + @"%2C+" + end.Year.ToString();

                // Create the Google formatted URL.
                var url = string.Format(UrlPrototypeDaily, symbol, startdate, enddate);

                // Download the data from Google.
                string[] lines;
                using (var client = new WebClient())
                {
                    var data = client.DownloadString(url);
                    lines = data.Split('\n');
                }

                // first line is header
                var currentLine = 1;

                List <StockBarDto> bars = new List <StockBarDto>();

                while (currentLine < lines.Length - 1)
                {
                    // Format: Date,Open,High,Low,Close,Volume
                    var columns = lines[currentLine].Split(',');

                    // date format: DD-Mon-YY, e.g. 27-Sep-16
                    var DMY = columns[0].Split('-');

                    // date = 20160927
                    var day   = DMY[0].ToInt32();
                    var month = (int)Enum.Parse(typeof(ConvertMonths), DMY[1]);
                    var year  = (DMY[2].ToInt32() > 70) ? 1900 + DMY[2].ToInt32() : 2000 + DMY[2].ToInt32();
                    var time  = new DateTime(year, month, day, 0, 0, 0);

                    // occasionally, the columns will have a '-' instead of a proper value
                    List <Decimal?> ohlc = new List <Decimal?>()
                    {
                        columns[1] != "-" ? (Decimal?)columns[1].ToDecimal() : null,
                        columns[2] != "-" ? (Decimal?)columns[2].ToDecimal() : null,
                        columns[3] != "-" ? (Decimal?)columns[3].ToDecimal() : null,
                        columns[4] != "-" ? (Decimal?)columns[4].ToDecimal() : null
                    };

                    if (ohlc.Where(val => val == null).Count() > 0)
                    {
                        // let's try hard to fix any issues as good as we can
                        // this code assumes that there is at least 1 good value
                        if (ohlc[1] == null)
                        {
                            ohlc[1] = ohlc.Where(val => val != null).Max();
                        }
                        if (ohlc[2] == null)
                        {
                            ohlc[2] = ohlc.Where(val => val != null).Min();
                        }
                        if (ohlc[0] == null)
                        {
                            ohlc[0] = ohlc.Where(val => val != null).Average();
                        }
                        if (ohlc[3] == null)
                        {
                            ohlc[3] = ohlc.Where(val => val != null).Average();
                        }

                        //Log.Error(string.Format("Corrupt bar on {0}: {1},{2},{3},{4}. Saved as {5},{6},{7},{8}.",
                        //    columns[0], columns[1], columns[2], columns[3], columns[4],
                        //    ohlc[0], ohlc[1], ohlc[2], ohlc[3]));
                    }

                    //long volume = columns[5].ToInt64();

                    bars.Add(new StockBarDto {
                        Date = time, Open = (Decimal)ohlc[0], High = (Decimal)ohlc[1], Low = (Decimal)ohlc[2], Close = (Decimal)ohlc[3]
                    });

                    currentLine++;
                }

                return(new StockReportDto {
                    StartDate = start, EndDate = end, StockBars = bars
                });
            }
            catch
            {
                this._consoleHubProxy.WriteLine(ConsoleWriteLineInput.Create($"Unable to retrieve bars for: {symbol}"));
                return(new StockReportDto {
                    StartDate = start, EndDate = end, StockBars = new List <StockBarDto>(), FailedToRetrieveBars = true
                });
            }
        }