private void btnGo_Click(object sender, EventArgs e) { zedGraphControl1.GraphPane.GraphObjList.RemoveAll(t => t is LineObj); SimpleSearchSettings settings = new SimpleSearchSettings(); settings.fallSearchPeriodInDays = int.Parse(tbFallSearchPeriod.Text); settings.riseSearchPeriodInDays = int.Parse(tbRiseSearchPeriod.Text); settings.requiredChangeRate = decimal.Parse(tbRateChange.Text); settings.numerOfRepeats = int.Parse(tbRepeats.Text); string symbol = cbSelectSymbol.SelectedItem as string; IEnumerable<Price> data = this.GetShareData(symbol); IEnumerable<SplitEvent> splitData = this.GetSplitData(symbol); data = HistoricalPriceReader.Convert(data, splitData).ToArray(); SimpleSearch processor = new SimpleSearch(); processor.Process(data.ToArray(), settings); DisplaySimpleSearchResults(processor.Results); }
private void Form1_Load(object sender, EventArgs e) { var path = Path.Combine(Directory.GetCurrentDirectory(), "settings.txt"); using (var file = File.OpenText(path)) { this.simpleSearchSettings= JsonConvert.DeserializeObject<SimpleSearchSettings>(file.ReadLine()); file.Close(); } tbAllSharesFallSearchPeriod.Text = tbFallSearchPeriod.Text = simpleSearchSettings.fallSearchPeriodInDays.ToString(); tbAllSharesRiseSearchPeriod.Text = tbRiseSearchPeriod.Text = simpleSearchSettings.riseSearchPeriodInDays.ToString(); tbAllSharesRateChange.Text = tbRateChange.Text = simpleSearchSettings.requiredChangeRate.ToString(); tbAllSharesRepeats.Text = tbRepeats.Text = simpleSearchSettings.numerOfRepeats.ToString(); this.symbols = ShareIndexReader.GetSymbols("https://s3.amazonaws.com/quandl-static-content/Ticker+CSV%27s/Indicies/FTSE100.csv"); this.cbSelectSymbol.Items.AddRange(symbols.ToArray()); this.cbSelectSymbol.SelectedIndex = symbols.ToList().IndexOf("VOD.L"); this.cbGraphDisplay.SelectedIndex = 0; zedGraphControl1.GraphPane.Title.IsVisible = false; string symbol = this.cbSelectSymbol.SelectedItem as string; DisplayGraph(this.GetShareData(symbol), this.GetSplitData(symbol)); PopulateSplitListView(this.GetSplitData(symbol)); }
public void Process(Price[] shareData, SimpleSearchSettings settings) { this.Results = new List<List<Result>>(); int count = shareData.Count(); Price? lastSellPoint = null; List<Result> currentRun = new List<Result>(); int buyPointIndex = 0; decimal? phase2Slope = null, phase2Intercept = null; while (buyPointIndex < count) { if (currentRun.Count < settings.numerOfRepeats) { var buyPoint = shareData[buyPointIndex]; var sellPoint = buyPoint; bool found = false; for (int sellPointIndex = buyPointIndex + 1; sellPointIndex < count && (sellPoint.date - buyPoint.date).TotalDays < settings.riseSearchPeriodInDays; sellPointIndex++) { sellPoint = shareData[sellPointIndex]; if (((sellPoint.closePrice - buyPoint.closePrice) * 100 / buyPoint.closePrice) > settings.requiredChangeRate) { currentRun.Add(new Result() { buy = buyPoint, sell = sellPoint, state = ResultState.OK }); lastSellPoint = sellPoint; buyPointIndex = sellPointIndex + 1; phase2Slope = phase2Intercept = null; found = true; break; } } bool withinFallBuyPeriod = lastSellPoint == null || (buyPoint.date - lastSellPoint.Value.date).TotalDays < settings.fallSearchPeriodInDays; if (!found && !withinFallBuyPeriod) { currentRun.Clear(); lastSellPoint = null; } } else { bool foundBuyPoint = false, foundSellPoint = false; do { double[] x = currentRun.Select(c => (double)c.buy.date.Ticks).ToArray(); double[] y = currentRun.Select(c => (double)c.buy.closePrice).ToArray(); double slope, intercept; MathExtension.GenerateLinearBestFit(x, y, out slope, out intercept); phase2Slope = (decimal)slope; phase2Intercept = (decimal)intercept; foundBuyPoint = foundSellPoint = false; Price buyPoint, sellPoint; do { buyPoint = shareData[buyPointIndex++]; foundBuyPoint = (buyPoint.closePrice <= (phase2Slope * buyPoint.date.Ticks + phase2Intercept)); } while (buyPointIndex < count-1 && !foundBuyPoint && (buyPoint.date - lastSellPoint.Value.date).TotalDays < settings.fallSearchPeriodInDays); sellPoint = buyPoint; if (foundBuyPoint) { for (int sellPointIndex = buyPointIndex; sellPointIndex < count && !foundSellPoint && (sellPoint.date - buyPoint.date).TotalDays < settings.riseSearchPeriodInDays; sellPointIndex++) { sellPoint = shareData[sellPointIndex]; if (((sellPoint.closePrice - buyPoint.closePrice) * 100 / buyPoint.closePrice) > settings.requiredChangeRate) { foundSellPoint = true; buyPointIndex = sellPointIndex; break; } } currentRun.Add(new Result() { buy = buyPoint, sell = sellPoint, state = foundSellPoint ? ResultState.OK : ResultState.FailedFindingSellPoint }); } } while (foundSellPoint); if (currentRun.Count >= settings.numerOfRepeats) Results.Add(currentRun); currentRun = new List<Result>(); } buyPointIndex++; } }
private void btnSimpleSearchGentics_Click(object sender, EventArgs e) { SimpleSearchSettings currentSettings = new SimpleSearchSettings(), bestSettings; int currentFailedSells, bestFailedSells; int currentOkSells, bestOkSells; Random rnd = new Random(); string symbol = cbSelectSymbol.SelectedItem as string; Price[] data = HistoricalPriceReader.Convert(this.GetShareData(symbol), this.GetSplitData(symbol)).ToArray(); bestSettings = this.simpleSearchSettings; SimpleSearch search = new SimpleSearch(); search.Process(data, this.simpleSearchSettings); var results = search.Results; bestFailedSells = results.Count(r => r.Last().state == ResultState.FailedFindingSellPoint); bestOkSells = results.Count(r => r.Last().state == ResultState.OK); for (int c = 365; c > 0; c--) { currentSettings.fallSearchPeriodInDays = rnd.Next(1, c); currentSettings.riseSearchPeriodInDays = rnd.Next(1, c); currentSettings.requiredChangeRate = rnd.Next(1, c); currentSettings.numerOfRepeats = this.simpleSearchSettings.numerOfRepeats; search.Process(data, currentSettings); results = search.Results; currentFailedSells = results.Count(r => r.Last().state == ResultState.FailedFindingSellPoint); currentOkSells = results.Count(r => r.Last().state == ResultState.OK); if (currentOkSells > 0 && (currentFailedSells < bestFailedSells || (currentFailedSells == bestFailedSells && currentOkSells > bestOkSells))) { bestSettings = (SimpleSearchSettings)currentSettings.Clone(); bestFailedSells = currentFailedSells; bestOkSells = currentOkSells; } } tbFallSearchPeriod.Text = bestSettings.fallSearchPeriodInDays.ToString(); tbRiseSearchPeriod.Text = bestSettings.riseSearchPeriodInDays.ToString(); tbRateChange.Text = bestSettings.requiredChangeRate.ToString(); tbRepeats.Text = bestSettings.numerOfRepeats.ToString(); search.Process(data, bestSettings); DisplaySimpleSearchResults(search.Results); }
public void Process(Price[] shareData, SimpleSearchSettings settings) { this.Results = new List <List <Result> >(); int count = shareData.Count(); Price?lastSellPoint = null; List <Result> currentRun = new List <Result>(); int buyPointIndex = 0; decimal? phase2Slope = null, phase2Intercept = null; while (buyPointIndex < count) { if (currentRun.Count < settings.numerOfRepeats) { var buyPoint = shareData[buyPointIndex]; var sellPoint = buyPoint; bool found = false; for (int sellPointIndex = buyPointIndex + 1; sellPointIndex < count && (sellPoint.date - buyPoint.date).TotalDays < settings.riseSearchPeriodInDays; sellPointIndex++) { sellPoint = shareData[sellPointIndex]; if (((sellPoint.closePrice - buyPoint.closePrice) * 100 / buyPoint.closePrice) > settings.requiredChangeRate) { currentRun.Add(new Result() { buy = buyPoint, sell = sellPoint, state = ResultState.OK }); lastSellPoint = sellPoint; buyPointIndex = sellPointIndex + 1; phase2Slope = phase2Intercept = null; found = true; break; } } bool withinFallBuyPeriod = lastSellPoint == null || (buyPoint.date - lastSellPoint.Value.date).TotalDays < settings.fallSearchPeriodInDays; if (!found && !withinFallBuyPeriod) { currentRun.Clear(); lastSellPoint = null; } } else { bool foundBuyPoint = false, foundSellPoint = false; do { double[] x = currentRun.Select(c => (double)c.buy.date.Ticks).ToArray(); double[] y = currentRun.Select(c => (double)c.buy.closePrice).ToArray(); double slope, intercept; MathExtension.GenerateLinearBestFit(x, y, out slope, out intercept); phase2Slope = (decimal)slope; phase2Intercept = (decimal)intercept; foundBuyPoint = foundSellPoint = false; Price buyPoint, sellPoint; do { buyPoint = shareData[buyPointIndex++]; foundBuyPoint = (buyPoint.closePrice <= (phase2Slope * buyPoint.date.Ticks + phase2Intercept)); } while (buyPointIndex < count - 1 && !foundBuyPoint && (buyPoint.date - lastSellPoint.Value.date).TotalDays < settings.fallSearchPeriodInDays); sellPoint = buyPoint; if (foundBuyPoint) { for (int sellPointIndex = buyPointIndex; sellPointIndex < count && !foundSellPoint && (sellPoint.date - buyPoint.date).TotalDays < settings.riseSearchPeriodInDays; sellPointIndex++) { sellPoint = shareData[sellPointIndex]; if (((sellPoint.closePrice - buyPoint.closePrice) * 100 / buyPoint.closePrice) > settings.requiredChangeRate) { foundSellPoint = true; buyPointIndex = sellPointIndex; break; } } currentRun.Add(new Result() { buy = buyPoint, sell = sellPoint, state = foundSellPoint ? ResultState.OK : ResultState.FailedFindingSellPoint }); } } while (foundSellPoint); if (currentRun.Count >= settings.numerOfRepeats) { Results.Add(currentRun); } currentRun = new List <Result>(); } buyPointIndex++; } }