protected void WriteTradeTriggers() { int count = trade_triggers.Count; var csv = new StringBuilder(); for (int i = 0; i < count; i++) { TradeTriggerByDate res = trade_triggers[i]; var newLine = string.Format("{0};{1};{2}", res.Symbol, res.TradeDate.ToShortDateString(), res.DaysToHold); csv.AppendLine(newLine); } //mut.WaitOne(); //wait until safe to enter; prior thread has completed writing. try { String file_name = output_folder + "triggers_strategy" + Strategy_Num.ToString() + "_" + TestingStartDate.ToString("ddMMMyyyy") + "to" + TestingEndDate.ToString("ddMMMyyyy") + "_" + uid + ".csv"; File.WriteAllText(file_name, csv.ToString()); //mut.ReleaseMutex(); } catch (System.Exception exp) { Log("File write error for file name '" + "' Error '" + exp.Message + "'", LogLevel.Warning); } }
protected void WriteTradeTriggers() { int count = trade_triggers.Count; var csv = new StringBuilder(); for (int i = 0; i < count; i++) { TradeTriggerByDate res = trade_triggers[i]; var newLine = string.Format("{0};{1};{2}", res.Symbol, res.TradeDate.ToShortDateString(), res.DaysToHold); csv.AppendLine(newLine); } //mut.WaitOne(); //wait until safe to enter; prior thread has completed writing. try { File.WriteAllText("c:\\temp\\knn\\triggers.csv", csv.ToString()); //mut.ReleaseMutex(); } catch (System.Exception exp) { Log("File write error for file name '" + "' Error '" + exp.Message + "'", LogLevel.Warning); } }
protected void LoadTriggers() { for (int i = 0; i < symbol_list.Count; i++) { trade_triggers.Add(new List <TradeTriggerByDate>()); } string trigger_file_path = "c:\\temp\\knn\\triggers.csv"; String trigger_config_file_path = "c:\\temp\\knn\\triggers_config.txt"; System.IO.StreamReader file3 = new System.IO.StreamReader(trigger_config_file_path); String line3 = file3.ReadLine(); if (line3 != null) { trigger_file_path = line3; } file3.Close(); System.IO.StreamReader file4 = new System.IO.StreamReader(trigger_file_path); string line4; while ((line4 = file4.ReadLine()) != null) { string[] values = line4.Split(';'); if (values.Length == 3) { TradeTriggerByDate trig = new TradeTriggerByDate(); trig.Symbol = values[0]; trig.TradeDate = Convert.ToDateTime(values[1]); trig.DaysToHold = Convert.ToInt32(values[2]); trig.Status = "NotExecuted"; int symbol_index = symbol_to_index[trig.Symbol]; trade_triggers[symbol_index].Add(trig); } } file4.Close(); }
protected override void OnBarUpdate() { int BarIndex = BarsInProgress; bool bFinishedProcessingAllData = false; Boolean bLastSymbol = BarIndex == (symbol_list.Count - 1); DateTime current_bar_date = Times[BarIndex][0].Date; if (bLastSymbol && (((CurrentBar + 1) == Count) || (current_bar_date == TestingEndDate))) //we are on the last bar to process { Debug.WriteLine("bFinished trigger"); bFinishedProcessingAllData = true; } String debug_txt1 = "Date=" + Times[BarIndex][0].Date.ToShortDateString() + "BarIndex=" + BarIndex.ToString() + "CurrentBar=" + CurrentBar.ToString() + "Count=" + Count.ToString(); Debug.WriteLine(debug_txt1); //NOTE: Index 1 is always S&P 500 if ((BarIndex >= 2 /*1*/) && (BarIndex < (symbol_list.Count))) //Start at index=1 since we want to ignore the primary/dummy instrument { DateTime date_to_process = Times[BarIndex][0].Date; //need to wait FutureValueDaysToLookAhead days before we can make calcs if ((date_to_process >= TestingStartDate) && (date_to_process <= TestingEndDate)) // && (CurrentBar >= (20))) //give 20 days of buffer { int SP500_index = 1; TradeScannerResult scan_result = new TradeScannerResult(); double OpenPriceToday = Opens[BarIndex][0]; double ClosePriceToday = Closes[BarIndex][0]; double SP500_Open = Opens[SP500_index][0]; double SP500_Close = Closes[SP500_index][0]; scan_result.Date = date_to_process; scan_result.Symbol = symbol_list[BarIndex]; scan_result.Description = company_name_list[BarIndex]; scan_result.Open = OpenPriceToday; scan_result.Close = ClosePriceToday; scan_result.PercentChange_Today = (((ClosePriceToday / OpenPriceToday) - 1.0) * 100.0); scan_result.SP500_PercentChange_Today = (((SP500_Close / SP500_Open) - 1.0) * 100.0); scan_result.Compare_SP500_Today = scan_result.PercentChange_Today - scan_result.SP500_PercentChange_Today; scan_result.PercentChange_1Day = PctChange(BarIndex, 1); scan_result.PercentChange_2Day = PctChange(BarIndex, 2); scan_result.PercentChange_5Day = PctChange(BarIndex, 5); scan_result.PercentChange_10Day = PctChange(BarIndex, 10); scan_result.PercentChange_15Day = PctChange(BarIndex, 15); scan_result.PercentChange_20Day = PctChange(BarIndex, 20); scan_result.Compare_SP500_1Day = scan_result.PercentChange_1Day - PctChange(SP500_index, 1); scan_result.Compare_SP500_2Day = scan_result.PercentChange_2Day - PctChange(SP500_index, 2); scan_result.Compare_SP500_5Day = scan_result.PercentChange_5Day - PctChange(SP500_index, 5); scan_result.Compare_SP500_10Day = scan_result.PercentChange_10Day - PctChange(SP500_index, 10); scan_result.Compare_SP500_15Day = scan_result.PercentChange_15Day - PctChange(SP500_index, 15); scan_result.Compare_SP500_20Day = scan_result.PercentChange_20Day - PctChange(SP500_index, 20); scan_result.Green_5Day = GetPercentOfGreenDays(BarIndex, 5); scan_result.Green_10Day = GetPercentOfGreenDays(BarIndex, 10); scan_result.Green_15Day = GetPercentOfGreenDays(BarIndex, 15); scan_result.Green_20Day = GetPercentOfGreenDays(BarIndex, 20); PopulateParamatersForIndicator(IndicatorEnum.indicator_BOLLINGER); //Indicator_FutureValueChange_Pair indicator_pair = GetIndicatorValue(indicator_list[i], BarIndex, 0, date_to_process); double indicator_value_today = 0.0; scan_result.Indicator_Name = "BOLL"; scan_result.Strategy_Name = Strategy_Num.ToString(); double boll_0 = ComputeBollinger(BarIndex, 0); double boll_1 = ComputeBollinger(BarIndex, 1); double boll_2 = ComputeBollinger(BarIndex, 2); double boll_3 = ComputeBollinger(BarIndex, 3); double boll_4 = ComputeBollinger(BarIndex, 4); double boll_5 = ComputeBollinger(BarIndex, 5); double boll_10 = ComputeBollinger(BarIndex, 10); double boll_15 = ComputeBollinger(BarIndex, 15); double boll_20 = ComputeBollinger(BarIndex, 20); indicator_value_today = boll_0; scan_result.Indicator_Today = indicator_value_today; double boll_0_chg = boll_1 - boll_0; double boll_min = -1.0; double boll_max = -0.75; bool boll_in_range = (boll_0 > boll_min) && (boll_0 < boll_max); double boll_min_change = 0.05; double boll_max_change = 0.25; bool boll_change_in_range = (boll_0_chg > boll_min_change) && (boll_0_chg < boll_max_change); bool boll_declining_3dys = (boll_3 > boll_2) && (boll_2 > boll_1) && (boll_0 > boll_1); scan_result.Indicator_Change_1Day = indicator_value_today - boll_1; scan_result.Indicator_Change_2Day = indicator_value_today - boll_2; scan_result.Indicator_Change_5Day = indicator_value_today - boll_5; scan_result.Indicator_Change_10Day = indicator_value_today - boll_10; scan_result.Indicator_Change_15Day = indicator_value_today - boll_15; scan_result.Indicator_Change_20Day = indicator_value_today - boll_20; double RSI_val = RSI(BarsArray[BarIndex], 14, 3)[0]; //get the indicator val from n days ago scan_result.RSI = RSI_val; bool bBuy = false; switch (Strategy_Num) { case 1: //declining boll for 3, then up spike if (boll_in_range && boll_change_in_range) { //strat1 (declining boll for 3, then up spike) if (boll_declining_3dys) { Console.WriteLine("\nstrat1\n"); bBuy = true; } } break; case 2: //boll up spike if (boll_in_range && boll_change_in_range) { Console.WriteLine("\nstrat2\n"); bBuy = true; } break; case 3: //boll crossing -1 threshold, but not too far if ((boll_1 < -1.1) && (boll_0 > -0.9) && (boll_0 < -0.7)) { Console.WriteLine("\nstrat3\n"); bBuy = true; } break; default: break; } String signal_name = scan_result.Symbol + "-" + Strategy_Num.ToString() + "-" + DaysToHold.ToString(); //Check to see if time to sell if (Trade_Status[BarIndex] == "InProgress") { //if ((date_to_process > trig.TradeDate)) // && (date_to_process <= trig.TradeDate.AddDays(trig.DaysToHold))) //{ DaysSincePurchase[BarIndex]++; if (DaysSincePurchase[BarIndex] == DaysToHold) //Use input parameter rather than reading from file trig.DaysToHold //Set Exit for end of nth day { Trade_Status[BarIndex] = "Idle"; ExitLong(BarIndex, NumShares[BarIndex], signal_name, signal_name); DaysSincePurchase[BarIndex] = -1; } //} } else if (bBuy) { if (scan_result.Close < AmountPerTrade) //if price is greater than max per trade, then we cannot purchase { TradeTriggerByDate trig = new TradeTriggerByDate(); trig.Symbol = scan_result.Symbol; trig.TradeDate = scan_result.Date; trig.DaysToHold = DaysToHold; trade_triggers.Add(trig); Trade_Status[BarIndex] = "InProgress"; DaysSincePurchase[BarIndex] = 0; //trig.Status = "InProgress"; NumShares[BarIndex] = (int)(AmountPerTrade / scan_result.Close); EnterLong(BarIndex, NumShares[BarIndex], signal_name); trade_scanner_results.Add(scan_result); } } } } if (bFinishedProcessingAllData == true) { uid = rnd.Next(100000, 999999).ToString(); WriteTradeScannerResults(); WriteTradeTriggers(); } }
protected override void OnBarUpdate() { int BarIndex = BarsInProgress; bool bFinishedProcessingAllData = false; Boolean bLastSymbol = BarIndex == (symbol_list.Count - 1); DateTime current_bar_date = Times[BarIndex][0].Date; if (bLastSymbol && (((CurrentBar + 1) == Count) || (current_bar_date == TestingEndDate))) //we are on the last bar to process { Debug.WriteLine("bFinished trigger"); bFinishedProcessingAllData = true; } String debug_txt1 = "Date=" + Times[BarIndex][0].Date.ToShortDateString() + "BarIndex=" + BarIndex.ToString() + "CurrentBar=" + CurrentBar.ToString() + "Count=" + Count.ToString(); Debug.WriteLine(debug_txt1); //NOTE: Index 1 is always S&P 500 if ((BarIndex >= 1 /*1*/) && (BarIndex < (symbol_list.Count))) //Start at index=1 since we want to ignore the primary/dummy instrument { DateTime date_to_process = Times[BarIndex][0].Date; //need to wait FutureValueDaysToLookAhead days before we can make calcs if ((date_to_process >= TestingStartDate) && (date_to_process <= TestingEndDate)) // && (CurrentBar >= (20))) //give 20 days of buffer { int SP500_index = 1; if (BarIndex == SP500_index) { Date_Price_Pair date_price = new Date_Price_Pair(); date_price.Date = date_to_process; date_price.Price = Closes[BarIndex][0]; SP500_historical_price_list.Add(date_price); } else //non S&P500 symbols { TradeScannerResult scan_result = new TradeScannerResult(); double OpenPriceToday = Opens[BarIndex][0]; double ClosePriceToday = Closes[BarIndex][0]; double SP500_Open = Opens[SP500_index][0]; double SP500_Close = Closes[SP500_index][0]; try { scan_result.Date = date_to_process; scan_result.Symbol = symbol_list[BarIndex]; scan_result.Description = company_name_list[BarIndex]; scan_result.Open = OpenPriceToday; scan_result.Close = ClosePriceToday; scan_result.PercentChange_Today = (((ClosePriceToday / OpenPriceToday) - 1.0) * 100.0); scan_result.SP500_PercentChange_Today = (((SP500_Close / SP500_Open) - 1.0) * 100.0); scan_result.Compare_SP500_Today = scan_result.PercentChange_Today - scan_result.SP500_PercentChange_Today; scan_result.PercentChange_1Day = PctChange(BarIndex, 1); scan_result.PercentChange_2Day = PctChange(BarIndex, 2); scan_result.PercentChange_5Day = PctChange(BarIndex, 5); scan_result.PercentChange_10Day = PctChange(BarIndex, 10); scan_result.PercentChange_15Day = PctChange(BarIndex, 15); scan_result.PercentChange_20Day = PctChange(BarIndex, 20); scan_result.Compare_SP500_1Day = scan_result.PercentChange_1Day - PctChange(SP500_index, 1); scan_result.Compare_SP500_2Day = scan_result.PercentChange_2Day - PctChange(SP500_index, 2); scan_result.Compare_SP500_5Day = scan_result.PercentChange_5Day - PctChange(SP500_index, 5); scan_result.Compare_SP500_10Day = scan_result.PercentChange_10Day - PctChange(SP500_index, 10); scan_result.Compare_SP500_15Day = scan_result.PercentChange_15Day - PctChange(SP500_index, 15); scan_result.Compare_SP500_20Day = scan_result.PercentChange_20Day - PctChange(SP500_index, 20); scan_result.Green_5Day = GetPercentOfGreenDays(BarIndex, 5); scan_result.Green_10Day = GetPercentOfGreenDays(BarIndex, 10); scan_result.Green_15Day = GetPercentOfGreenDays(BarIndex, 15); scan_result.Green_20Day = GetPercentOfGreenDays(BarIndex, 20); PopulateParamatersForIndicator(IndicatorEnum.indicator_BOLLINGER); //Indicator_FutureValueChange_Pair indicator_pair = GetIndicatorValue(indicator_list[i], BarIndex, 0, date_to_process); double indicator_value_today = 0.0; scan_result.Indicator_Name = "BOLL"; scan_result.Strategy_Name = Strategy_Description; // Strategy_Num.ToString(); double boll_0 = ComputeBollinger(BarIndex, 0); double boll_1 = ComputeBollinger(BarIndex, 1); double boll_2 = ComputeBollinger(BarIndex, 2); double boll_3 = ComputeBollinger(BarIndex, 3); double boll_4 = ComputeBollinger(BarIndex, 4); double boll_5 = ComputeBollinger(BarIndex, 5); double boll_10 = ComputeBollinger(BarIndex, 10); double boll_15 = ComputeBollinger(BarIndex, 15); double boll_20 = ComputeBollinger(BarIndex, 20); indicator_value_today = boll_0; scan_result.Indicator_Today = indicator_value_today; double boll_0_chg = boll_1 - boll_0; double boll_min = -1.0; double boll_max = -0.75; bool boll_in_range = (boll_0 > boll_min) && (boll_0 < boll_max); double boll_min_change = 0.05; double boll_max_change = 0.25; bool boll_change_in_range = (boll_0_chg > boll_min_change) && (boll_0_chg < boll_max_change); bool boll_declining_3dys = (boll_3 > boll_2) && (boll_2 > boll_1) && (boll_0 > boll_1); scan_result.Indicator_Change_1Day = indicator_value_today - boll_1; scan_result.Indicator_Change_2Day = indicator_value_today - boll_2; scan_result.Indicator_Change_5Day = indicator_value_today - boll_5; scan_result.Indicator_Change_10Day = indicator_value_today - boll_10; scan_result.Indicator_Change_15Day = indicator_value_today - boll_15; scan_result.Indicator_Change_20Day = indicator_value_today - boll_20; double RSI_val = RSI(BarsArray[BarIndex], 14, 3)[0]; //get the indicator val from n days ago scan_result.RSI = RSI_val; bool bBuy = false; switch (Strategy_Num) { case 1: //declining boll for 3, then up spike if (boll_in_range && boll_change_in_range && boll_declining_3dys) { bBuy = true; } break; case 2: //boll up spike if (boll_in_range && boll_change_in_range) { bBuy = true; } break; case 3: //boll crossing -1 threshold, but not too far if ((boll_1 < -1.1) && nt.InRange(boll_0, -0.9, -0.7)) { bBuy = true; } break; case 4: //boll in range, today's change < -0.5% and SP500 change > 0% if (boll_in_range && (scan_result.PercentChange_Today < -0.5) && (scan_result.SP500_PercentChange_Today > 0.0)) { bBuy = true; } break; case 5: //strat3 + Today 0.75-1.25 and SP500Change 0.5 - 1.0 if ((boll_1 < -1.1) && nt.InRange(boll_0, -0.9, -0.7) && nt.InRange(scan_result.PercentChange_Today, 0.75, 1.25) && nt.InRange(scan_result.SP500_PercentChange_Today, 0.5, 1.0)) { bBuy = true; } break; case 6: //boll up spike + Today < -1.5 + SP500Change < -0.75 if (boll_in_range && boll_change_in_range && (scan_result.PercentChange_Today < -1.5) && (scan_result.SP500_PercentChange_Today < -0.75)) { bBuy = true; } break; case 7: //strat3 + Today 0.5-2.0 and SP500Change 0.5 - 3.0 if ((boll_1 < -1.1) && nt.InRange(boll_0, -0.9, -0.7) && nt.InRange(scan_result.PercentChange_Today, 0.5, 2.0) && nt.InRange(scan_result.SP500_PercentChange_Today, 0.5, 3.0)) { bBuy = true; } break; case 8: //strat7 + 5-day indicator change -1.5 to -0.5 if ((boll_1 < -1.1) && nt.InRange(boll_0, -0.9, -0.7) && nt.InRange(scan_result.PercentChange_Today, 0.5, 2.0) && nt.InRange(scan_result.SP500_PercentChange_Today, 0.5, 3.0) && nt.InRange(scan_result.Indicator_Change_5Day, -1.5, -0.5)) { bBuy = true; } break; case 9: //strat7 but use 0.0 for min of %ChangeToday and SP500ChangeToday if ((boll_1 < -1.1) && nt.InRange(boll_0, -0.9, -0.7) && nt.InRange(scan_result.PercentChange_Today, 0.0, 2.0) && nt.InRange(scan_result.SP500_PercentChange_Today, 0.0, 3.0)) { bBuy = true; } break; case 10: //strat7 but remove SP500%Change condition if ((boll_1 < -1.1) && nt.InRange(boll_0, -0.9, -0.7) && nt.InRange(scan_result.PercentChange_Today, 0.5, 2.0)) { bBuy = true; } break; case 11: //strat7 but change boll range max to -0.5 if ((boll_1 < -1.1) && nt.InRange(boll_0, -0.9, -0.5) && nt.InRange(scan_result.PercentChange_Today, 0.5, 2.0) && nt.InRange(scan_result.SP500_PercentChange_Today, 0.5, 3.0)) { bBuy = true; } break; case 12: //strat7 but change boll range min/max to -0.95/-0.75 if ((boll_1 < -1.1) && nt.InRange(boll_0, -0.95, -0.75) && nt.InRange(scan_result.PercentChange_Today, 0.5, 2.0) && nt.InRange(scan_result.SP500_PercentChange_Today, 0.5, 3.0)) { bBuy = true; } break; case 13: //strat12 but also add condition for CompSP500_today > 0.15 if ((boll_1 < -1.1) && nt.InRange(boll_0, -0.95, -0.75) && nt.InRange(scan_result.PercentChange_Today, 0.5, 2.0) && nt.InRange(scan_result.SP500_PercentChange_Today, 0.5, 3.0) && (scan_result.Compare_SP500_Today > 0.15)) { bBuy = true; } break; case 14: //Just testing SP500%change range if (nt.InRange(scan_result.SP500_PercentChange_Today, 0.5, 3.0)) { bBuy = true; } break; case 15: //strat7, but use 0.0 for SP500 min if ((boll_1 < -1.1) && nt.InRange(boll_0, -0.9, -0.7) && nt.InRange(scan_result.PercentChange_Today, 0.5, 2.0) && nt.InRange(scan_result.SP500_PercentChange_Today, 0.0, 3.0)) { bBuy = true; } break; case 16: //strat7, but use -1.0 for boll_1 min and -0.5 for boll_0 max if ((boll_1 < -1.0) && nt.InRange(boll_0, -0.9, -0.5) && nt.InRange(scan_result.PercentChange_Today, 0.5, 2.0) && nt.InRange(scan_result.SP500_PercentChange_Today, 0.5, 3.0)) { bBuy = true; } break; case 17: //strat7 but but use -1.0 for boll_1 min and -0.5 for boll_0 max, use 0.25 for today min, and remove SP500 change if ((boll_1 < -1.0) && nt.InRange(boll_0, -0.9, -0.5) && nt.InRange(scan_result.PercentChange_Today, 0.25, 2.0)) { bBuy = true; } break; case 18: //strat7 but but use -1.2 for boll_1 min and -0.95 for boll_0 max, use 0.25 for today min, and remove SP500 change if ((boll_1 < -1.2) && nt.InRange(boll_0, -0.95, -0.5) && nt.InRange(scan_result.PercentChange_Today, 0.25, 2.0)) { bBuy = true; } break; case 19: //just dropped to range of -1.0 to -0.8 if ((boll_1 > -0.5) && nt.InRange(boll_0, -1.0, -0.8)) { bBuy = true; } break; case 20: //recovery from below -0.9 if ((boll_1 < -0.9) && nt.InRange(boll_0, -0.7, -0.3)) { bBuy = true; } break; default: break; } String signal_name = scan_result.Symbol + "-" + Strategy_Num.ToString() + "-" + DaysToHold.ToString() + "-" + StopLossPercent.ToString(); //Check to see if time to sell if (Trade_Status[BarIndex] == "InProgress") { //if ((date_to_process > trig.TradeDate)) // && (date_to_process <= trig.TradeDate.AddDays(trig.DaysToHold))) //{ DaysSincePurchase[BarIndex]++; if (DaysSincePurchase[BarIndex] == DaysToHold) //Use input parameter rather than reading from file trig.DaysToHold //Set Exit for end of nth day { Trade_Status[BarIndex] = "Idle"; ExitLong(BarIndex, NumShares[BarIndex], signal_name, signal_name); DaysSincePurchase[BarIndex] = -1; } //} } else if (bBuy) { if (scan_result.Close < AmountPerTrade) //if price is greater than max per trade, then we cannot purchase { TradeTriggerByDate trig = new TradeTriggerByDate(); trig.Symbol = scan_result.Symbol; trig.TradeDate = scan_result.Date; trig.DaysToHold = DaysToHold; trade_triggers.Add(trig); Trade_Status[BarIndex] = "InProgress"; DaysSincePurchase[BarIndex] = 0; //trig.Status = "InProgress"; NumShares[BarIndex] = (int)(AmountPerTrade / scan_result.Close); EnterLong(BarIndex, NumShares[BarIndex], signal_name); trade_scanner_results.Add(scan_result); } } } catch { Console.WriteLine("Caught exception"); } } } } if (bFinishedProcessingAllData == true) { uid = rnd.Next(100000, 999999).ToString(); results_folder = results_folder + "_" + uid + "\\"; DirectoryInfo di2 = Directory.CreateDirectory(results_folder); WriteTradeScannerResults(); //WriteTradeTriggers(); WriteSP500HistoricalData(); } }
protected override void OnBarUpdate() { //if (BarsInProgress == 0) //{ int BarIndex = BarsInProgress; bool bFinishedProcessingAllData = false; if (((CurrentBar + 1) == Count) && BarIndex == (symbol_list.Count - 1)) //we are on the last bar to process { bFinishedProcessingAllData = true; } if ((BarIndex >= 1) && (BarIndex < (symbol_list.Count))) //Start at index=1 since we want to ignore the primary/dummy instrument { DateTime date_to_process = Times[BarIndex][0].Date; //need to wait FutureValueDaysToLookAhead days before we can make calcs string symbol_name = symbol_list[BarIndex]; double current_price = Closes[BarIndex][0]; List <TradeTriggerByDate> triggers = trade_triggers[BarIndex]; for (int i = 0; i < triggers.Count; i++) { TradeTriggerByDate trig = triggers[i]; string trade_id_str = symbol_name; string trade_exit_str = "Exit " + trade_id_str; //if (DaysSincePurchase[BarIndex] >= 0) //Trade in-progress if (trig.Status == "InProgress") { if ((date_to_process > trig.TradeDate)) // && (date_to_process <= trig.TradeDate.AddDays(trig.DaysToHold))) { DaysSincePurchase[BarIndex]++; if (DaysSincePurchase[BarIndex] == DaysToHold) //Use input parameter rather than reading from file trig.DaysToHold //Set Exit for end of nth day { trig.Status = "Completed"; Trade_Status[BarIndex] = "Idle"; ExitLong(BarIndex, NumShares[BarIndex], trade_exit_str, trade_id_str); DaysSincePurchase[BarIndex] = -1; } } } //else if ((trig.TradeDate == date_to_process) && (Trade_Status[BarIndex] != "InProgress")) //prevent multiple entries for same symbol else if ((trig.TradeDate == date_to_process) && (Trade_Status[BarIndex] != "InProgress")) //prevent multiple entries for same symbol { if (current_price < AmountPerTrade) //if price is greater than max per trade, then we cannot purchase { Trade_Status[BarIndex] = "InProgress"; DaysSincePurchase[BarIndex] = 0; trig.Status = "InProgress"; NumShares[BarIndex] = (int)(AmountPerTrade / current_price); EnterLong(BarIndex, NumShares[BarIndex], trade_id_str); scanner_results.Add(trig.Symbol + " " + trig.TradeDate.ToShortDateString() + " " + DaysToHold.ToString()); } } } } if (bFinishedProcessingAllData) { WriteStringListToCSV(scanner_results, "c:\\temp\\knn\\scanner_results.txt"); } //} }
protected override void OnBarUpdate() { int BarIndex = BarsInProgress; bool bFinishedProcessingAllData = false; Boolean bLastSymbol = BarIndex == (symbol_list.Count - 1); DateTime current_bar_date = Times[BarIndex][0].Date; if (bLastSymbol && (((CurrentBar + 1) == Count) || (current_bar_date == TestingEndDate))) //we are on the last bar to process { Debug.WriteLine("bFinished trigger"); bFinishedProcessingAllData = true; } String debug_txt1 = "Date=" + Times[BarIndex][0].Date.ToShortDateString() + "BarIndex=" + BarIndex.ToString() + "CurrentBar=" + CurrentBar.ToString() + "Count=" + Count.ToString(); Debug.WriteLine(debug_txt1); //NOTE: Index 1 is always S&P 500 if ((BarIndex >= 2 /*1*/) && (BarIndex < (symbol_list.Count))) //Start at index=1 since we want to ignore the primary/dummy instrument { DateTime date_to_process = Times[BarIndex][0].Date; //need to wait FutureValueDaysToLookAhead days before we can make calcs //if (date_to_process == TestingEndDate) if ((date_to_process <= TestingEndDate) && (CurrentBar >= (20))) //give 20 days of buffer { //Convert.ToSingle int SP500_index = 1; TradeScannerResult scan_result = new TradeScannerResult(); double OpenPriceToday = Opens[BarIndex][0]; double ClosePriceToday = Closes[BarIndex][0]; double SP500_Open = Opens[SP500_index][0]; double SP500_Close = Closes[SP500_index][0]; scan_result.Date = date_to_process; scan_result.Symbol = symbol_list[BarIndex]; scan_result.Description = company_name_list[BarIndex]; scan_result.Open = OpenPriceToday; scan_result.Close = ClosePriceToday; scan_result.PercentChange_Today = (((ClosePriceToday / OpenPriceToday) - 1.0) * 100.0); scan_result.SP500_PercentChange_Today = (((SP500_Close / SP500_Open) - 1.0) * 100.0); scan_result.Compare_SP500_Today = scan_result.PercentChange_Today - scan_result.SP500_PercentChange_Today; scan_result.PercentChange_1Day = PctChange(BarIndex, 1); scan_result.PercentChange_2Day = PctChange(BarIndex, 2); scan_result.PercentChange_5Day = PctChange(BarIndex, 5); scan_result.PercentChange_10Day = PctChange(BarIndex, 10); scan_result.PercentChange_15Day = PctChange(BarIndex, 15); scan_result.PercentChange_20Day = PctChange(BarIndex, 20); scan_result.Compare_SP500_1Day = scan_result.PercentChange_1Day - PctChange(SP500_index, 1); scan_result.Compare_SP500_2Day = scan_result.PercentChange_2Day - PctChange(SP500_index, 2); scan_result.Compare_SP500_5Day = scan_result.PercentChange_5Day - PctChange(SP500_index, 5); scan_result.Compare_SP500_10Day = scan_result.PercentChange_10Day - PctChange(SP500_index, 10); scan_result.Compare_SP500_15Day = scan_result.PercentChange_15Day - PctChange(SP500_index, 15); scan_result.Compare_SP500_20Day = scan_result.PercentChange_20Day - PctChange(SP500_index, 20); scan_result.Green_5Day = GetPercentOfGreenDays(BarIndex, 5); scan_result.Green_10Day = GetPercentOfGreenDays(BarIndex, 10); scan_result.Green_15Day = GetPercentOfGreenDays(BarIndex, 15); scan_result.Green_20Day = GetPercentOfGreenDays(BarIndex, 20); //public string Indicator_Name { get; set; } //public float Indicator_Today { get; set; } //public float Indicator_Change_1Day { get; set; } //See how indicator has changed over past N days //public float Indicator_Change_2Day { get; set; } //public float Indicator_Change_5Day { get; set; } //public float Indicator_Change_10Day { get; set; } //public float Indicator_Change_15Day { get; set; } //public float Indicator_Change_20Day { get; set; } //public int Green_5Day { get; set; } //% of Green days in the past N days //public int Green_10Day { get; set; } //public int Green_15Day { get; set; } //public int Green_20Day { get; set; } for (int i = 0; i < indicator_list.Count; i++) { PopulateParamatersForIndicator(indicator_list[i]); //Indicator_FutureValueChange_Pair indicator_pair = GetIndicatorValue(indicator_list[i], BarIndex, 0, date_to_process); double indicator_value_today = 0.0; switch (indicator_list[i]) { case IndicatorEnum.indicator_BOLLINGER: scan_result.Indicator_Name = "BOLL"; double boll_0 = ComputeBollinger(BarIndex, 0); double boll_1 = ComputeBollinger(BarIndex, 1); double boll_2 = ComputeBollinger(BarIndex, 2); double boll_3 = ComputeBollinger(BarIndex, 3); double boll_4 = ComputeBollinger(BarIndex, 4); double boll_5 = ComputeBollinger(BarIndex, 5); double boll_10 = ComputeBollinger(BarIndex, 10); double boll_15 = ComputeBollinger(BarIndex, 15); double boll_20 = ComputeBollinger(BarIndex, 20); indicator_value_today = boll_0; scan_result.Indicator_Today = indicator_value_today; double boll_0_chg = boll_1 - boll_0; double boll_min = -1.0; double boll_max = -0.75; bool boll_in_range = (boll_0 > boll_min) && (boll_0 < boll_max); double boll_min_change = 0.05; double boll_max_change = 0.25; bool boll_change_in_range = (boll_0_chg > boll_min_change) && (boll_0_chg < boll_max_change); bool boll_declining_3dys = (boll_3 > boll_2) && (boll_2 > boll_1) && (boll_0 > boll_1); scan_result.Indicator_Change_1Day = indicator_value_today - boll_1; scan_result.Indicator_Change_2Day = indicator_value_today - boll_2; scan_result.Indicator_Change_5Day = indicator_value_today - boll_5; scan_result.Indicator_Change_10Day = indicator_value_today - boll_10; scan_result.Indicator_Change_15Day = indicator_value_today - boll_15; scan_result.Indicator_Change_20Day = indicator_value_today - boll_20; //if (boll_in_range && boll_change_in_range) //{ // //strat1 (declining boll for 3, then up spike) // if (boll_declining_3dys) // { // Console.WriteLine("\nstrat1\n"); // TradeTriggerByDate trig = new TradeTriggerByDate(); // trig.Symbol = scan_result.Symbol; // trig.TradeDate = scan_result.Date; // trig.DaysToHold = 5; // trade_triggers.Add(trig); // } // //strat2 (just boll up spike) // else // { // Console.WriteLine("\nstrat2\n"); // TradeTriggerByDate trig = new TradeTriggerByDate(); // trig.Symbol = scan_result.Symbol; // trig.TradeDate = scan_result.Date; // trig.DaysToHold = 5; // trade_triggers.Add(trig); // } //} if ((boll_1 < -1.1) && (boll_0 > -0.9) && (boll_0 < -0.7)) { Console.WriteLine("\nstrat3\n"); TradeTriggerByDate trig = new TradeTriggerByDate(); trig.Symbol = scan_result.Symbol; trig.TradeDate = scan_result.Date; trig.DaysToHold = 5; trade_triggers.Add(trig); } break; default: break; } } trade_scanner_results.Add(scan_result); } } if (bFinishedProcessingAllData == true) { WriteTradeScannerResults(); WriteTradeTriggers(); } }