private void CalcStoic_OnClick(object sender, RoutedEventArgs e) { try { DateTime Recent = EndDate.SelectedDate.Value; DateTime Early = StartDate.SelectedDate.Value; Stock k = AllStocks.Where(x => x.Ticker == StockList.Text).First(); int P = Convert.ToInt32(MAPeriod.Text); k.GetWeeklyAdjustment(); int recent = k.GetIndexWeekly(Recent); int early = k.GetIndexWeekly(Early); k.indicate = new Indicators(); k.indicate.EmaIndicators = new Indicators.Ema(early, recent, k.WeeklyHistory.Select(x => x.ClosingPrice).ToArray(), P); SSTOGraph Peter = new SSTOGraph(k.indicate.GetIntoChartFormat(k.WeeklyHistory.GetRange(early, recent - early).Select(x => x.ClosingPrice).ToList()), k.indicate.GetIntoChartFormat(k.indicate.EmaIndicators.EmaHist)); closeingP.Children.Add(Peter); TestHolding Benj = new TestHolding(); Benj.CashHeld = 2000; DateTime Start = new DateTime(2013, 01, 01); Benj = Strategies.TestThesis(AllStocks, Start, Benj); bool Continue = true; while (Continue) { if (Benj.TickHistSell.Last().SellDate < DateTime.Now.AddDays(-10) && Benj.CashHeld > 50) { Start = Benj.TickHistSell.Last().SellDate; Benj = Strategies.TestThesis(AllStocks, Start, Benj); } else { Continue = false; } } Benj.PrintData(); } catch { System.Windows.MessageBox.Show("ss", "ss", MessageBoxButton.OK, MessageBoxImage.Asterisk); } }
public static TestHolding TestThesis(List <Stock> StockMarket, DateTime startTime, TestHolding Holder) { foreach (var p in StockMarket) { p.GetWeeklyAdjustment(); p.CWP = p.GetIndexWeekly(startTime); p.CDP = p.GetIndexDaily(startTime); int recent = p.GetIndexWeekly(DateTime.Now); int early = p.GetIndexWeekly(startTime - TimeSpan.FromDays(365 * 10)); int RecDay = p.GetIndexDaily(DateTime.Now); int earlDay = p.GetIndexDaily(startTime - TimeSpan.FromDays(365 * 10)); p.indicate = new Indicators(); p.indicate.EmaIndicators = new Indicators.Ema(early, recent, p.WeeklyHistory.Select(x => x.ClosingPrice).ToArray(), 75); p.indicate.RSIIndicators = new Indicators.RSI(earlDay, RecDay, p.DailyHistory.Select(x => (double)x.Volume).ToArray(), 14); p.TestEmaFormat.AddRange(p.CleanTicTac(p.indicate.EmaIndicators.EmaHist)); p.TestRSIFormat.AddRange(p.CleanTicTac(p.indicate.RSIIndicators.RSIHist)); } List <Stock> ValidStocks = new List <Stock>(); List <Stock> InvestMECandidates = new List <Stock>(); foreach (var r in StockMarket) { if (r.TestEmaFormat.Count > r.CWP) { ValidStocks.Add(r); } } Stock InvestMe = new Stock(); while (InvestMECandidates.Count == 0) { InvestMECandidates = new List <Stock>(); InvestMECandidates.AddRange( ValidStocks.Where( x => x.TestEmaFormat[x.CWP] / (x.WeeklyHistory[x.CWP].ClosingPrice) < 1 && x.TestEmaFormat[x.CWP] / (x.WeeklyHistory[x.CWP].ClosingPrice) > 0.8 )); for (int l = InvestMECandidates.Count - 1; l >= 0; l--) { List <double> priorrange = new List <double>(); bool noZero = true; bool NotEnoughData = false; int zerocount = 0; while (noZero) { if (InvestMECandidates[l].TestEmaFormat[zerocount] != 0) { noZero = false; } else { if (InvestMECandidates[l].TestEmaFormat.Count - 1 <= zerocount) { NotEnoughData = true; noZero = false; } zerocount++; } } if (!NotEnoughData) { priorrange.AddRange(InvestMECandidates[l].TestEmaFormat.GetRange(zerocount, InvestMECandidates[l].CWP)); double gradient = (priorrange.Last() - priorrange.First()) / priorrange.Count; if (gradient < -0.05) { InvestMECandidates.Remove(InvestMECandidates[l]); } } else { InvestMECandidates.Remove(InvestMECandidates[l]); } } if (InvestMECandidates.Count == 0) { for (int i = ValidStocks.Count - 1; i >= 0; i--) { ValidStocks[i].CWP = ValidStocks[i].CWP + 1; if (ValidStocks[i].WeeklyHistory.Count - 1 < ValidStocks[i].CWP) { ValidStocks.Remove(ValidStocks[i]); } } } if (ValidStocks.Count == 0) { Holder.Sell("No Opportunities", null, DateTime.Now); return(Holder); } } Random randd = new Random(); int qw = randd.Next(0, InvestMECandidates.Count); InvestMe = InvestMECandidates[qw]; startTime = InvestMe.WeeklyHistory[InvestMe.CWP].TradeDate; Holder.Buy(InvestMe, startTime); int periodCount = 0; double originalEMA = InvestMe.TestEmaFormat[InvestMe.CWP]; double BuyPrice = InvestMe.DailyHistory[InvestMe.CDP].High; bool PriceAboveEMa = true; bool TakeReturns = true; bool StopLoss = true; bool c4 = true; bool SlowEarner = true; bool BigDiff = true; startTime = startTime.Add(TimeSpan.FromDays(7)); while (PriceAboveEMa && TakeReturns && StopLoss && c4 && SlowEarner && BigDiff) { startTime = startTime.Add(TimeSpan.FromDays(1)); InvestMe.CDP = InvestMe.GetIndexDaily(startTime); InvestMe.CWP = InvestMe.GetIndexWeekly(startTime); PriceAboveEMa = InvestMe.WeeklyHistory[InvestMe.CWP].ClosingPrice > (InvestMe.TestEmaFormat[InvestMe.CWP] * 0.9); TakeReturns = InvestMe.DailyHistory[InvestMe.CDP].ClosingPrice / BuyPrice < 2; //c3 = InvestMe.DailyHistory[BuyPeriod].ClosingPrice/ // InvestMe.DailyHistory[BuyPeriod + 1].ClosingPrice > 1.005; c4 = startTime < DateTime.Now.AddDays(-5); //SlowEarner = (InvestMe.DailyHistory[InvestMe.CDP].ClosingPrice - BuyPrice) / periodCount < /*10*/ *(InvestMe.TestEmaFormat[InvestMe.CWP] -originalEMA)/periodCount ; BigDiff = InvestMe.DailyHistory[InvestMe.CDP].ClosingPrice / InvestMe.TestEmaFormat[InvestMe.CWP] < 1.3; StopLoss = InvestMe.DailyHistory[InvestMe.CDP].ClosingPrice / BuyPrice > 0.9; periodCount++; } string reason = ""; if (!PriceAboveEMa) { reason += "Closing price was lower than 1 times EMA. "; } if (!TakeReturns) { reason += "Closing price return exceeded 300%. "; } if (!StopLoss) { reason += "Closing price was less than 95% of buy price. "; } if (!c4) { reason += "End of investment period. "; } if (!SlowEarner) { reason += " Return was too low over the investment period. "; } if (!BigDiff) { reason += " Ratio of close to ema was big"; } Holder.Sell(reason, InvestMe, InvestMe.WeeklyHistory[InvestMe.CWP].TradeDate); return(Holder); }
public MainWindow() { InitializeComponent(); EndDate.SelectedDate = DateTime.Now; StartDate.SelectedDate = DateTime.Now.AddYears(-5); Startup NewProgram = new Startup(); AllStocks = NewProgram.InitialiseStocks(false); //AllStocks = NewProgram.UpdatePrices(AllStocks); TestHolding Benj = new TestHolding(); Benj.CashHeld = 2000; DateTime Start = new DateTime(2014, 01, 01); Benj = Strategies.TestThesis(AllStocks, Start, Benj); bool Continue = true; while (Continue) { if (Benj.TickHistSell.Last().SellDate < DateTime.Now.AddDays(-10) && Benj.CashHeld > 50) { Start = Benj.TickHistSell.Last().SellDate; Benj = Strategies.TestThesis(AllStocks, Start, Benj); } else { Continue = false; } } Benj.PrintData(); StockList.ItemsSource = AllStocks.Select(x => x.Ticker).Distinct(); //DateTime Recent = new DateTime(2016, 5, 13); //DateTime Early = new DateTime(2016, 2, 1); //List<double> Results = new List<double>(); //List<List<double>> ListResults = new List<List<double>>(); //foreach (var p in AllStocks) //{ // int r1 = p.GetIndex(Recent); // int e1 = p.GetIndex(Early); // p.indicate.EmaIndicators = new Indicators.Ema(r1, e1, // p.History.Select(x => x.ClosingPrice).ToArray(), 10); //} //List<KeyValuePair<int, double>> ff = AllStocks[0].indicate.GetIntoChartFormat(AllStocks[0].History.Where(x => x.TradeDate > DateTime.Now.AddYears(-1)).Select(x => (double)x.ClosingPrice).Reverse().ToList()); //Chart Close = new Chart(ff); //closeingP.Children.Add(Close); //int optinTimePeriod = 15; //int OptinFastPeriod = 5; //int OptinSlowPeriod = 20; //foreach(var c in AllStocks) //{ // int recent = c.GetIndex(DateTime.Now); // int early = c.GetIndex(DateTime.Now.AddMonths(-4)); // double[] OP = c.History.GetRange(recent, early - recent).Select(x => x.OpeningPrice).ToArray(); // double[] CP = c.History.GetRange(recent, early - recent).Select(x => x.ClosingPrice).ToArray(); // double[] HP = c.History.GetRange(recent, early - recent).Select(x => x.High).ToArray(); // double[] LP = c.History.GetRange(recent, early - recent).Select(x => x.Low).ToArray(); // double[] Vol = c.History.GetRange(recent, early - recent).Select(x => (double)x.Volume).ToArray(); // c.indicate.AcosIndicators = new Indicators.Acos(recent, early, OP); // c.indicate.ADDIndicators = new Indicators.ADD(recent, early, OP, CP); // c.indicate.ADIndicators = new Indicators.AD(recent, early, HP, LP, CP, Vol); // c.indicate.AdOscIndicators = new Indicators.AdOsc(recent, early, HP, LP, CP, Vol,OptinFastPeriod, OptinSlowPeriod); // c.indicate.AdxRIndicators = new Indicators.AdxR(recent, early, HP, LP, CP, optinTimePeriod); // c.indicate.ApoIndicators = new Indicators.Apo(recent, early, CP, OptinFastPeriod, OptinSlowPeriod); //} }