protected String GenerateConfigFile(String filePath, String symbol, String training_file_path, String testing_file_path, String unique_id) { String output_file_base = filePath + "_results_"; String config_file_name = filePath + "_config" + unique_id + ".txt"; List <String> labels = new List <String> { "KNN_num_neighbors", "num_training_pts", "samples_per_group", "window_size", "avg_target", "good_target", "bad_target", "min_samples", "thresh1", "thresh2", "future_lookahead", "s_name", "description", "training_file", "training_start", "training_end", "testing_file", "testing_start", "testing_end", "output_csv_file", "output_txt_file", "output_pdf_file" }; List <String> values = new List <String> { KNN_num_neighbors.ToString(), num_training_pts.ToString(), samples_per_group.ToString(), window_size.ToString(), avg_target.ToString(), good_target.ToString(), bad_target.ToString(), min_samples.ToString(), thresh1.ToString(), thresh2.ToString(), FutureValueDaysToLookAhead.ToString(), symbol, description, training_file_path, TrainingStartDate.ToShortDateString(), TrainingEndDate.ToShortDateString(), testing_file_path, TestingStartDate.ToShortDateString(), TestingEndDate.ToShortDateString(), output_file_base + "data" + unique_id + ".csv", output_file_base + "summary" + unique_id + ".txt", output_file_base + "plot" + unique_id + ".pdf", }; List <String> types = new List <String> { "int", "int", "int", "int", "float", "float", "float", "int", "float", "float", "int", "str", "str", "str", "str", "str", "str", "str", "str", "str", "str", "str" }; var cfg = new StringBuilder(); for (int i = 0; i < labels.Count; i++) { cfg.AppendLine(labels[i] + "=" + values[i] + "#" + types[i]); } File.WriteAllText(config_file_name, cfg.ToString()); return(config_file_name); }
protected override void OnBarUpdate() { //if (CurrentBar > (Count - 2)) //This means we have processed all historical data (https://ninjatrader.com/support/forum/showthread.php?t=66713) //{ // Random rnd = new Random(); // String unique_id = "_" + rnd.Next(100000, 999999).ToString(); // creates a number between 100k and 999k // String control_file_path = output_folder + "control_file" + unique_id + ".txt"; // String consolidated_txt_file_path = output_folder + "consolidated_report" + unique_id + ".txt"; // String consolidated_csv_file_path = output_folder + "consolidated_report" + unique_id + ".csv"; // switch (indicator_to_use) // { // case IndicatorEnum.indicator_MACD: // if (Sanitize == false) // description = "MACD"; // else // description = "M"; // description = description + "(" + Param1.ToString() + "-" + Param2.ToString() + "-" + Param3.ToString() + ")"; // break; // case IndicatorEnum.indicator_RSI: // if (Sanitize == false) // description = "RSI"; // else // description = "R"; // description = description + "(" + Param1.ToString() + "-" + Param2.ToString() + ")"; // break; // default: // description = "Unknown"; // break; // } // description = description + " - " + FutureValueDaysToLookAhead.ToString(); // if (Sanitize == false) // description = description + " days"; // description = description + " - " + unique_id.Substring(1); //remove preceding "_" // bool bFirstTime = true; // for (int i = 1; i < symbol_list.Length; i++) //Start at index=1 since we want to ignore the first primary/dummy instrument // { // String symbol_name = symbol_list[i]; // if (Sanitize == true) // symbol_name = symbol_list[i][0].ToString(); // String base_file_path_for_symbol = output_folder + symbol_name; //i.e. c:\temp\knn\AAPL // String training_file_path = base_file_path_for_symbol + "_training" + unique_id + ".csv"; // String testing_file_path = base_file_path_for_symbol + "_testing" + unique_id + ".csv"; // WriteListToCSV(list_Indicator_FutureValueChange_Training_ALL[i], training_file_path); // WriteListToCSV(list_Indicator_FutureValueChange_Testing_ALL[i], testing_file_path); // String config_file_name = ""; // config_file_name = GenerateConfigFile(base_file_path_for_symbol, symbol_name, training_file_path, testing_file_path, unique_id); // if (bFirstTime) //create new file // { // bFirstTime = false; // File.WriteAllText(control_file_path, config_file_name + "\r\n"); // } // else // append to existing file // { // File.AppendAllText(control_file_path, config_file_name + "\r\n"); // } // } // //Now we can call python for all symbols at once by passing in the control_file as cmd line arg // CallPythonScript(output_folder + "\\Data_Processing25.py", control_file_path + " " + consolidated_txt_file_path + " " + consolidated_csv_file_path + " true"); //} //double Current_Value = Closes[0][0]; //get the most recent Close data for the input symbol (index 0) //double Prev_Value = Closes[0][0]; ////double greg_indicator_value = GregIndicator1(BarsArray[0], 1.0)[0]; //if (CurrentBar >= DaysToLookBack) //make sure we have enough data loaded to look back n days //{ // Prev_Value = Closes[0][DaysToLookBack]; //} //// if (Current_Value > //// double Prev_Value = Close[CurrentBar - DaysToLookBack]; //double Percent_Change = (Current_Value / Prev_Value) - 1.0; //if (Buy_Price != 0.0) //{ // if ((Current_Value / Buy_Price - 1.0) > 0.02) //sell if gain > 2% // { // Buy_Price = 0.0; // //ExitLong(); // //ExitLong("Percent Drop", "Percent Drop"); // ExitLong(0, 100, "Exit Long from Percent Drop", "Percent Drop"); // //ExitLong("Selling - Went + 2%"); // //EnterShort("Selling - Went + 2%"); // } // //else if ((CurrentBar - BarBought) > 20) //sell after 20 days // //{ // // Buy_Price = 0.0; // // ExitLong(); // // //ExitLong("Selling - Waited 20 days"); // // //EnterShort("Selling - Waited 20 days"); // // } //} //if (Percent_Change < -0.02) //{ // //if (entryOrder == null) // //{ // EnterLong(0, 100, "Percent Drop"); // //} // //EnterLong(); // //EnterLong("Buying - Dropped 2%"); // //EnterLong(0, 100, "Percent Drop"); // //SetTrailStop(CalculationMode.Percent, 0.02); // //SetStopLoss(CalculationMode.Percent, 0.05); // Buy_Price = Current_Value; // BarBought = CurrentBar; // //SetTrailStop(CalculationMode.Percent, 0.01); //} int BarIndex = BarsInProgress; bool bFinishedProcessingAllData = false; if (((CurrentBar + 2) == 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 { if (CurrentBar >= (FutureValueDaysToLookAhead)) //just make sure we avoid out of bounds error in case we don't yet have enough data { //DateTime current_date = Times[0][0].Date; DateTime date_to_process = Times[BarIndex][FutureValueDaysToLookAhead].Date; //need to wait FutureValueDaysToLookAhead days before we can make calcs bool bWithinTrainingPeriod = (date_to_process >= TrainingStartDate) && (date_to_process <= TrainingEndDate); bool bWithinTestingPeriod = (date_to_process >= TestingStartDate) && (date_to_process <= TestingEndDate); if ((bWithinTrainingPeriod) || (bWithinTestingPeriod)) { //TimeSpan diff = TrainingEndDate - TrainingStartDate; //int training_days = (int)Math.Abs(Math.Round(diff.TotalDays)); double indicator_value = 0.0; switch (indicator_to_use) { case IndicatorEnum.indicator_MACD: indicator_value = MACD(BarsArray[BarIndex], Param1, Param2, Param3)[FutureValueDaysToLookAhead]; //get the indicator val from n days ago break; case IndicatorEnum.indicator_RSI: indicator_value = RSI(BarsArray[BarIndex], Param1, Param2)[FutureValueDaysToLookAhead]; //get the indicator val from n days ago break; case IndicatorEnum.indicator_BOLLINGER: double upper_band = Bollinger(BarsArray[BarIndex], (double)Param1, Param2).Upper[FutureValueDaysToLookAhead]; //get the indicator val from n days ago double middle_band = Bollinger(BarsArray[BarIndex], (double)Param1, Param2).Middle[FutureValueDaysToLookAhead]; //get the indicator val from n days ago double lower_band = Bollinger(BarsArray[BarIndex], (double)Param1, Param2).Lower[FutureValueDaysToLookAhead]; //get the indicator val from n days ago double current_price = Closes[BarIndex][FutureValueDaysToLookAhead]; double diff = current_price - middle_band; double band_range = upper_band - middle_band; indicator_value = diff / band_range; //how far current price is from the middle band (-1.0 means we're at the lower band, +1 means we're at the upper band) break; case IndicatorEnum.indicator_STOCHASTIC: //use the "D" value indicator_value = Stochastics(BarsArray[BarIndex], Param1, Param2, Param3).D[FutureValueDaysToLookAhead]; //get the indicator val from n days ago break; case IndicatorEnum.indicator_STOCHASTIC_RSI: indicator_value = StochRSI(BarsArray[BarIndex], Param1)[FutureValueDaysToLookAhead]; //get the indicator val from n days ago break; case IndicatorEnum.indicator_GREG: indicator_value = -999.999; // GregIndicator1(BarsArray[BarIndex], (float)Param1)[FutureValueDaysToLookAhead]; //get the indicator val from n days ago break; default: indicator_value = -999.99; break; } if (double.IsNaN(indicator_value)) { indicator_value = 0.0; } double future_price_change = 0.0; double future_price = Closes[BarIndex][0]; double start_price = Closes[BarIndex][FutureValueDaysToLookAhead]; future_price_change = ((future_price / start_price) - 1.0) * 100.0; Indicator_FutureValueChange_Pair indicator_pair = new Indicator_FutureValueChange_Pair(); indicator_pair.Date = date_to_process; indicator_pair.Price = start_price; indicator_pair.Indicator = indicator_value; indicator_pair.FutureValueChange = future_price_change; if (bWithinTrainingPeriod) { list_Indicator_FutureValueChange_Training.Add(indicator_pair); list_Indicator_FutureValueChange_Training_ALL[BarIndex].Add(indicator_pair); int count_training = list_Indicator_FutureValueChange_Training.Count; } else if (bWithinTestingPeriod) { list_Indicator_FutureValueChange_Testing.Add(indicator_pair); list_Indicator_FutureValueChange_Testing_ALL[BarIndex].Add(indicator_pair); int count_testing = list_Indicator_FutureValueChange_Testing.Count; } } } } if (bFinishedProcessingAllData) { String unique_id = "_" + rnd.Next(100000, 999999).ToString(); // creates a number between 100k and 999k String debug_txt = "FINISHEDPROCESSING min_samples=" + min_samples_pct.ToString() + " id=" + unique_id.ToString(); Debug.WriteLine(debug_txt); String control_file_path = output_folder + "control_file" + unique_id + ".txt"; String consolidated_txt_file_path = output_folder + "consolidated_report" + unique_id + ".txt"; String consolidated_csv_file_path = output_folder + "consolidated_report" + unique_id + ".csv"; switch (indicator_to_use) { case IndicatorEnum.indicator_MACD: if (Sanitize == false) { description = "MACD"; } else { description = "M"; } description = description + "(" + Param1.ToString() + "-" + Param2.ToString() + "-" + Param3.ToString() + ")"; break; case IndicatorEnum.indicator_RSI: if (Sanitize == false) { description = "RSI"; } else { description = "R"; } description = description + "(" + Param1.ToString() + "-" + Param2.ToString() + ")"; break; case IndicatorEnum.indicator_BOLLINGER: if (Sanitize == false) { description = "BOLL"; } else { description = "B"; } description = description + "(" + Param1.ToString() + "-" + Param2.ToString() + ")"; break; case IndicatorEnum.indicator_STOCHASTIC: if (Sanitize == false) { description = "STOCH"; } else { description = "S"; } description = description + "(" + Param1.ToString() + "-" + Param2.ToString() + "-" + Param3.ToString() + ")"; break; case IndicatorEnum.indicator_STOCHASTIC_RSI: if (Sanitize == false) { description = "STOCH_RSI"; } else { description = "SR"; } description = description + "(" + Param1.ToString() + ")"; break; default: description = "Unknown"; break; } description = description + " - " + FutureValueDaysToLookAhead.ToString(); if (Sanitize == false) { description = description + " days"; } description = description + " - " + unique_id.Substring(1); //remove preceding "_" bool bFirstTime = true; for (int i = 1; i < symbol_list.Count; i++) //Start at index=1 since we want to ignore the first primary/dummy instrument { String symbol_name = symbol_list[i]; String company_name = company_name_list[i]; if (Sanitize == true) { String symbol_name_tmp = string.Concat(symbol_list[i].Reverse()).ToLower(); symbol_name = char.ToUpper(symbol_name_tmp[0]) + symbol_name_tmp.Substring(1); company_name = "?"; } String base_file_path_for_symbol = output_folder + symbol_name; //i.e. c:\temp\knn\AAPL String training_file_path = base_file_path_for_symbol + "_training" + unique_id + ".csv"; String testing_file_path = base_file_path_for_symbol + "_testing" + unique_id + ".csv"; WriteListToCSV(list_Indicator_FutureValueChange_Training_ALL[i], training_file_path); WriteListToCSV(list_Indicator_FutureValueChange_Testing_ALL[i], testing_file_path); String config_file_name = ""; config_file_name = GenerateConfigFile(base_file_path_for_symbol, symbol_name, company_name, training_file_path, testing_file_path, unique_id); //mut.WaitOne(); if (bFirstTime) //create new file { bFirstTime = false; File.WriteAllText(control_file_path, config_file_name + "\r\n"); } else // append to existing file { File.AppendAllText(control_file_path, config_file_name + "\r\n"); } //mut.ReleaseMutex(); } //Now we can call python for all symbols at once by passing in the control_file as cmd line arg mut.WaitOne(); string args = control_file_path + " " + consolidated_txt_file_path + " " + consolidated_csv_file_path + " c:\\temp\\knn\\master_report.csv" + " false"; CallPythonScript(output_folder + "\\Data_Processing33.py", args); mut.ReleaseMutex(); } }
protected override void OnStateChange() { if (State == State.SetDefaults) { Description = "test"; Name = "KNN_Generator6"; DaysToLookBack = 1; FutureValueDaysToLookAhead = 5; TrainingStartDate = new DateTime(2017, 01, 01); TrainingEndDate = new DateTime(2017, 09, 30); TestingStartDate = new DateTime(2017, 10, 1); TestingEndDate = new DateTime(2017, 12, 31); //Custom params indicator_to_use = IndicatorEnum.indicator_MACD; KNN_num_neighbors = 3; num_training_pts = 200; samples_per_group = 10; window_size = 1; avg_target = 1.0f; good_target = 0.5f; bad_target = 0.25f; min_samples = 10; thresh1 = 1.0f; thresh2 = -1.0f; output_folder = "C:\\temp\\knn\\"; Param1 = 12; //Defaults for MACD Param2 = 26; Param3 = 9; Sanitize = false; // This strategy has been designed to take advantage of performance gains in Strategy Analyzer optimizations // See the Help Guide for additional information IsInstantiatedOnEachOptimizationIteration = false; } else if (State == State.Configure) { for (int i = 0; i < symbol_list.Length; i++) { //Don't add the dummy instrument (but we still need a placeholder list which is added below) if (i != 0) { AddDataSeries(symbol_list[i], Data.BarsPeriodType.Day, 1); } list_Indicator_FutureValueChange_Training_ALL.Add(new List <Indicator_FutureValueChange_Pair>()); list_Indicator_FutureValueChange_Testing_ALL.Add(new List <Indicator_FutureValueChange_Pair>()); } SetStopLoss(CalculationMode.Percent, 0.05); // Sets a 20 tick trailing stop for an open position //SetTrailStop(CalculationMode.Ticks, 20); } else if (State == State.DataLoaded) { double input_param = 2.0; greg1 = GregIndicator1(input_param); AddChartIndicator(greg1); } else if (State == State.Transition) //finished processing historical data (and ready for real-time) { //System.Windows.Forms.MessageBox.Show("test"); Random rnd = new Random(); String unique_id = "_" + rnd.Next(100000, 999999).ToString(); // creates a number between 100k and 999k String control_file_path = output_folder + "control_file" + unique_id + ".txt"; String consolidated_txt_file_path = output_folder + "consolidated_report" + unique_id + ".txt"; String consolidated_csv_file_path = output_folder + "consolidated_report" + unique_id + ".csv"; switch (indicator_to_use) { case IndicatorEnum.indicator_MACD: if (Sanitize == false) { description = "MACD"; } else { description = "M"; } description = description + "(" + Param1.ToString() + "-" + Param2.ToString() + "-" + Param3.ToString() + ")"; break; case IndicatorEnum.indicator_RSI: if (Sanitize == false) { description = "RSI"; } else { description = "R"; } description = description + "(" + Param1.ToString() + "-" + Param2.ToString() + ")"; break; default: description = "Unknown"; break; } description = description + " - " + FutureValueDaysToLookAhead.ToString(); if (Sanitize == false) { description = description + " days"; } description = description + " - " + unique_id.Substring(1); //remove preceding "_" bool bFirstTime = true; for (int i = 1; i < symbol_list.Length; i++) //Start at index=1 since we want to ignore the first primary/dummy instrument { String symbol_name = symbol_list[i]; if (Sanitize == true) { symbol_name = symbol_list[i][0].ToString(); } String base_file_path_for_symbol = output_folder + symbol_name; //i.e. c:\temp\knn\AAPL String training_file_path = base_file_path_for_symbol + "_training" + unique_id + ".csv"; String testing_file_path = base_file_path_for_symbol + "_testing" + unique_id + ".csv"; WriteListToCSV(list_Indicator_FutureValueChange_Training_ALL[i], training_file_path); //trying out +1!!! WriteListToCSV(list_Indicator_FutureValueChange_Testing_ALL[i], testing_file_path); String config_file_name = ""; config_file_name = GenerateConfigFile(base_file_path_for_symbol, symbol_name, training_file_path, testing_file_path, unique_id); if (bFirstTime) //create new file { bFirstTime = false; File.WriteAllText(control_file_path, config_file_name + "\r\n"); } else // append to existing file { File.AppendAllText(control_file_path, config_file_name + "\r\n"); } } //Now we can call python for all symbols at once by passing in the control_file as cmd line arg CallPythonScript(output_folder + "\\Data_Processing24.py", control_file_path + " " + consolidated_txt_file_path + " " + consolidated_csv_file_path + " true"); } }
protected override void OnStateChange() { if (State == State.SetDefaults) { if (bFirst) { bFirst = false; } String sp500_csv_file_path = "c:\\temp\\knn\\sp500.csv"; String sp500_config_file_path = "c:\\temp\\knn\\sp500_config_file.txt"; System.IO.StreamReader file1 = new System.IO.StreamReader(sp500_config_file_path); String line1 = file1.ReadLine(); if (line1 != null) { sp500_csv_file_path = line1; } file1.Close(); symbol_list = new List <String> { "DUMMY" }; company_name_list = new List <String> { "DUMMY" }; String line2; // Read the file and display it line by line. System.IO.StreamReader file2 = new System.IO.StreamReader(sp500_csv_file_path); while ((line2 = file2.ReadLine()) != null) { string[] values = line2.Split(','); if (values.Length == 2) { symbol_list.Add(values[0]); company_name_list.Add(values[1]); } } file2.Close(); Description = "test"; Name = "KNN_Generator10"; DaysToLookBack = 1; FutureValueDaysToLookAhead = 5; TrainingStartDate = new DateTime(2017, 01, 01); TrainingEndDate = new DateTime(2017, 12, 31); TestingStartDate = new DateTime(2018, 1, 1); TestingEndDate = DateTime.Today; //Custom params indicator_to_use = IndicatorEnum.indicator_MACD; KNN_num_neighbors = 3; num_training_pts = 200; num_groups = 20; stagger_factor = 0; window_size = 1; avg_target = 1.0f; good_target = 0.5f; bad_target = 0.20f; min_samples_pct = 0.06f; thresh1 = 1.0f; thresh2 = -1.0f; output_folder = "C:\\temp\\knn\\"; Param1 = 12; //Defaults for MACD Param2 = 26; Param3 = 9; Sanitize = false; bDataLoaded = false; // This strategy has been designed to take advantage of performance gains in Strategy Analyzer optimizations // See the Help Guide for additional information IsInstantiatedOnEachOptimizationIteration = true; //IsInstantiatedOnEachOptimizationIteration = false; } else if (State == State.Configure) { //System.Windows.Forms.MessageBox.Show("Configure"); bDataLoaded = false; for (int i = 0; i < symbol_list.Count; i++) { //Don't add the dummy instrument (but we still need a placeholder list which is added below) if (i != 0) { AddDataSeries(symbol_list[i], Data.BarsPeriodType.Day, 1); } list_Indicator_FutureValueChange_Training_ALL.Add(new List <Indicator_FutureValueChange_Pair>()); list_Indicator_FutureValueChange_Testing_ALL.Add(new List <Indicator_FutureValueChange_Pair>()); } SetStopLoss(CalculationMode.Percent, 0.05); // Sets a 20 tick trailing stop for an open position //SetTrailStop(CalculationMode.Ticks, 20); } else if (State == State.DataLoaded) { //String debug_txt = "DATALOADED min_samples=" + min_samples.ToString(); //Debug.WriteLine(debug_txt); //double input_param = 2.0; //greg1 = GregIndicator1(input_param); //AddChartIndicator(greg1); bDataLoaded = true; } else if ((bDataLoaded == true) && ((State == State.Transition) || (State == State.Terminated))) //finished processing historical data (and ready for real-time) { if (false) //list_Indicator_FutureValueChange_Training_ALL.Count > 0) { //String debug_txt = "TERMINATED min_samples=" + min_samples.ToString(); //Debug.WriteLine(debug_txt); //if (bExecutedPython == false) //{ // bExecutedPython = true; //System.Threading.Thread.Sleep(2000); //System.Windows.Forms.MessageBox.Show("test"); Random rnd = new Random(); String unique_id = "_" + rnd.Next(100000, 999999).ToString(); // creates a number between 100k and 999k String control_file_path = output_folder + "control_file" + unique_id + ".txt"; String consolidated_txt_file_path = output_folder + "consolidated_report" + unique_id + ".txt"; String consolidated_csv_file_path = output_folder + "consolidated_report" + unique_id + ".csv"; switch (indicator_to_use) { case IndicatorEnum.indicator_MACD: if (Sanitize == false) { description = "MACD"; } else { description = "M"; } description = description + "(" + Param1.ToString() + "-" + Param2.ToString() + "-" + Param3.ToString() + ")"; break; case IndicatorEnum.indicator_RSI: if (Sanitize == false) { description = "RSI"; } else { description = "R"; } description = description + "(" + Param1.ToString() + "-" + Param2.ToString() + ")"; break; case IndicatorEnum.indicator_BOLLINGER: if (Sanitize == false) { description = "BOLL"; } else { description = "B"; } break; case IndicatorEnum.indicator_STOCHASTIC: if (Sanitize == false) { description = "STOCH"; } else { description = "S"; } description = description + "(" + Param1.ToString() + "-" + Param2.ToString() + ")"; break; default: description = "Unknown"; break; } description = description + " - " + FutureValueDaysToLookAhead.ToString(); if (Sanitize == false) { description = description + " days"; } description = description + " - " + unique_id.Substring(1); //remove preceding "_" bool bFirstTime = true; for (int i = 1; i < symbol_list.Count; i++) //Start at index=1 since we want to ignore the first primary/dummy instrument { String symbol_name = symbol_list[i]; if (Sanitize == true) { symbol_name = symbol_list[i][0].ToString(); } String base_file_path_for_symbol = output_folder + symbol_name; //i.e. c:\temp\knn\AAPL String training_file_path = base_file_path_for_symbol + "_training" + unique_id + ".csv"; String testing_file_path = base_file_path_for_symbol + "_testing" + unique_id + ".csv"; WriteListToCSV(list_Indicator_FutureValueChange_Training_ALL[i], training_file_path); WriteListToCSV(list_Indicator_FutureValueChange_Testing_ALL[i], testing_file_path); String config_file_name = ""; config_file_name = GenerateConfigFile(base_file_path_for_symbol, symbol_name, company_name_list[i], training_file_path, testing_file_path, unique_id); if (bFirstTime) //create new file { bFirstTime = false; File.WriteAllText(control_file_path, config_file_name + "\r\n"); } else // append to existing file { File.AppendAllText(control_file_path, config_file_name + "\r\n"); } } //Now we can call python for all symbols at once by passing in the control_file as cmd line arg //CallPythonScript(output_folder + "\\Data_Processing25.py", control_file_path + " " + consolidated_txt_file_path + " " + consolidated_csv_file_path + " true"); //bDataLoaded = false; } // } } }
protected String GenerateConfigFile(String filePath, String symbol, String company, String training_file_path, String testing_file_path, String unique_id) { String output_file_base = filePath + "_results_"; String config_file_name = filePath + "_config" + unique_id + ".txt"; List <String> labels = new List <String> { "KNN_num_neighbors", "num_training_pts", "num_groups", "stagger_factor", "window_size", "avg_target", "good_target", "bad_target", "min_samples_pct", "thresh1", "thresh2", "future_lookahead", "s_name", "co_name", "description", "training_file", "training_start", "training_end", "testing_file", "testing_start", "testing_end", "output_csv_file", "output_txt_file", "output_pdf_file" }; List <String> values = new List <String> { KNN_num_neighbors.ToString(), num_training_pts.ToString(), num_groups.ToString(), stagger_factor.ToString(), window_size.ToString(), avg_target.ToString(), good_target.ToString(), bad_target.ToString(), min_samples_pct.ToString(), thresh1.ToString(), thresh2.ToString(), FutureValueDaysToLookAhead.ToString(), symbol, company, description, training_file_path, TrainingStartDate.ToShortDateString(), TrainingEndDate.ToShortDateString(), testing_file_path, TestingStartDate.ToShortDateString(), TestingEndDate.ToShortDateString(), output_file_base + "data" + unique_id + ".csv", output_file_base + "summary" + unique_id + ".txt", output_file_base + "plot" + unique_id + ".pdf", }; List <String> types = new List <String> { "int", "int", "int", "int", "int", "float", "float", "float", "float", "float", "float", "int", "str", "str", "str", "str", "str", "str", "str", "str", "str", "str", "str", "str" }; var cfg = new StringBuilder(); for (int i = 0; i < labels.Count; i++) { cfg.AppendLine(labels[i] + "=" + values[i] + "#" + types[i]); } //mut.WaitOne(); //wait until safe to enter; prior thread has completed writing. try { File.WriteAllText(config_file_name, cfg.ToString()); //mut.ReleaseMutex(); } catch (System.Exception exp) { Log("File write error for file name '" + filePath + "' Error '" + exp.Message + "'", LogLevel.Warning); } return(config_file_name); }
public void BuildIndicatorAndDescriptionStrings(IndicatorEnum this_ind) //, int Param1, int Param2, int Param3) { switch (this_ind) { case IndicatorEnum.indicator_MACD: if (Sanitize == false) { description = "MACD"; } else { description = "M"; } indicator = description; description = description + "(" + Param1.ToString() + "-" + Param2.ToString() + "-" + Param3.ToString() + ")"; break; case IndicatorEnum.indicator_RSI: if (Sanitize == false) { description = "RSI"; } else { description = "R"; } indicator = description; description = description + "(" + Param1.ToString() + "-" + Param2.ToString() + ")"; break; case IndicatorEnum.indicator_BOLLINGER: if (Sanitize == false) { description = "BOLL"; } else { description = "B"; } indicator = description; description = description + "(" + Param1.ToString() + "-" + Param2.ToString() + ")"; break; case IndicatorEnum.indicator_STOCHASTIC: if (Sanitize == false) { description = "STOCH"; } else { description = "S"; } indicator = description; description = description + "(" + Param1.ToString() + "-" + Param2.ToString() + "-" + Param3.ToString() + ")"; break; case IndicatorEnum.indicator_STOCHASTIC_RSI: if (Sanitize == false) { description = "STOCH_RSI"; } else { description = "SR"; } indicator = description; description = description + "(" + Param1.ToString() + ")"; break; default: description = "Unknown"; indicator = description; break; } description = description + " - " + FutureValueDaysToLookAhead.ToString(); if (Sanitize == false) { description = description + " days"; } description = description + " - " + uid; }
protected String GenerateConfigFile(String filePath, String symbol, String company, String training_file_path, String testing_file_path, String unique_id) { String output_file_base = filePath + "_results_"; String config_file_name = filePath + "_config" + unique_id + ".txt"; List <List <String> > param_list = new List <List <String> >(); param_list.Add(new List <String>() { "uid", uid, "str" }); param_list.Add(new List <String>() { "KNN_num_neighbors", KNN_num_neighbors.ToString(), "int" }); param_list.Add(new List <String>() { "num_training_pts", num_training_pts.ToString(), "int" }); param_list.Add(new List <String>() { "num_groups", num_groups.ToString(), "int" }); param_list.Add(new List <String>() { "stagger_factor", stagger_factor.ToString(), "int" }); param_list.Add(new List <String>() { "window_size", window_size.ToString(), "int" }); param_list.Add(new List <String>() { "avg_target", avg_target.ToString(), "float" }); param_list.Add(new List <String>() { "good_target", good_target.ToString(), "float" }); param_list.Add(new List <String>() { "bad_target", bad_target.ToString(), "float" }); param_list.Add(new List <String>() { "min_samples_pct", min_samples_pct.ToString(), "float" }); param_list.Add(new List <String>() { "thresh1", thresh1.ToString(), "float" }); param_list.Add(new List <String>() { "thresh2", thresh2.ToString(), "float" }); param_list.Add(new List <String>() { "future_lookahead", FutureValueDaysToLookAhead.ToString(), "int" }); param_list.Add(new List <String>() { "s_name", symbol, "str" }); param_list.Add(new List <String>() { "co_name", company, "str" }); param_list.Add(new List <String>() { "description", description, "str" }); param_list.Add(new List <String>() { "indicator", indicator, "str" }); param_list.Add(new List <String>() { "param1", Param1.ToString(), "int" }); param_list.Add(new List <String>() { "param2", Param2.ToString(), "int" }); param_list.Add(new List <String>() { "param3", Param3.ToString(), "int" }); param_list.Add(new List <String>() { "training_file", training_file_path, "str" }); param_list.Add(new List <String>() { "training_start", TrainingStartDate.ToShortDateString(), "str" }); param_list.Add(new List <String>() { "training_end", TrainingEndDate.ToShortDateString(), "str" }); param_list.Add(new List <String>() { "testing_file", testing_file_path, "str" }); param_list.Add(new List <String>() { "testing_start", TestingStartDate.ToShortDateString(), "str" }); param_list.Add(new List <String>() { "testing_end", TestingEndDate.ToShortDateString(), "str" }); param_list.Add(new List <String>() { "output_csv_file", output_file_base + "data" + unique_id + ".csv", "str" }); param_list.Add(new List <String>() { "output_txt_file", output_file_base + "summary" + unique_id + ".txt", "str" }); param_list.Add(new List <String>() { "output_pdf_file", output_file_base + "plot" + unique_id + ".pdf", "str" }); var cfg = new StringBuilder(); for (int i = 0; i < param_list.Count; i++) { cfg.AppendLine(param_list[i][0] + "=" + param_list[i][1] + "#" + param_list[i][2]); } //mut.WaitOne(); //wait until safe to enter; prior thread has completed writing. try { File.WriteAllText(config_file_name, cfg.ToString()); //mut.ReleaseMutex(); } catch (System.Exception exp) { Log("File write error for file name '" + filePath + "' Error '" + exp.Message + "'", LogLevel.Warning); } return(config_file_name); }
protected override void OnStateChange() { if (State == State.SetDefaults) { Description = "test"; Name = "KNN_Generator3"; DaysToLookBack = 1; FutureValueDaysToLookAhead = 5; TrainingStartDate = new DateTime(2017, 01, 01); TrainingEndDate = new DateTime(2017, 09, 30); TestingStartDate = new DateTime(2017, 10, 1); TestingEndDate = new DateTime(2017, 12, 31); //Custom params indicator_to_use = IndicatorEnum.indicator_MACD; KNN_num_neighbors = 3; num_training_pts = 200; samples_per_group = 10; window_size = 1; avg_target = 1.0f; good_target = 0.5f; bad_target = 0.25f; min_samples = 10; thresh1 = 1.0f; thresh2 = -1.0f; output_folder = "C:\\temp\\knn\\"; // This strategy has been designed to take advantage of performance gains in Strategy Analyzer optimizations // See the Help Guide for additional information IsInstantiatedOnEachOptimizationIteration = false; } else if (State == State.Configure) { for (int i = 1; i < symbol_list.Length; i++) //start at 1 since SLB already added by default { // Add an MSFT 1 day Bars object to the strategy AddDataSeries(symbol_list[i], Data.BarsPeriodType.Day, 1); //private List<Indicator_FutureValueChange_Pair> list_Indicator_FutureValueChange_Training = new List<Indicator_FutureValueChange_Pair>(); list_Indicator_FutureValueChange_Training_ALL.Add(new List <Indicator_FutureValueChange_Pair>()); list_Indicator_FutureValueChange_Testing_ALL.Add(new List <Indicator_FutureValueChange_Pair>()); } //Add extra one???? list_Indicator_FutureValueChange_Training_ALL.Add(new List <Indicator_FutureValueChange_Pair>()); list_Indicator_FutureValueChange_Testing_ALL.Add(new List <Indicator_FutureValueChange_Pair>()); SetStopLoss(CalculationMode.Percent, 0.05); // Sets a 20 tick trailing stop for an open position //SetTrailStop(CalculationMode.Ticks, 20); } else if (State == State.DataLoaded) { double input_param = 2.0; greg1 = GregIndicator1(input_param); AddChartIndicator(greg1); } else if (State == State.Transition) //finished processing historical data (and ready for real-time) { //System.Windows.Forms.MessageBox.Show("test"); String control_file_path = output_folder + "control_file.txt"; switch (indicator_to_use) { case IndicatorEnum.indicator_MACD: if (bSanitize == false) { description = "MACD"; } else { description = "M"; } break; case IndicatorEnum.indicator_RSI: if (bSanitize == false) { description = "RSI"; } else { description = "R"; } break; default: description = "Unknown"; break; } description = description + " - " + FutureValueDaysToLookAhead.ToString(); if (bSanitize == false) { description = description + " days"; } bool bFirstTime = true; for (int i = 1; i < symbol_list.Length; i++) { String base_file_path_for_symbol = output_folder + symbol_list[i]; //i.e. c:\temp\knn\AAPL String training_file_path = base_file_path_for_symbol + "_training.csv"; String testing_file_path = base_file_path_for_symbol + "_testing.csv"; WriteListToCSV(list_Indicator_FutureValueChange_Training_ALL[i], training_file_path); //trying out +1!!! WriteListToCSV(list_Indicator_FutureValueChange_Testing_ALL[i], testing_file_path); //WriteListToCSV(list_Indicator_FutureValueChange_Training_ALL[i], training_file_path); //WriteListToCSV(list_Indicator_FutureValueChange_Testing_ALL[i], testing_file_path); String config_file_name = ""; config_file_name = GenerateConfigFile(base_file_path_for_symbol, symbol_list[i], training_file_path, testing_file_path); if (bFirstTime) //create new file { bFirstTime = false; File.WriteAllText(control_file_path, config_file_name + "\r\n"); } else // append to existing file { File.AppendAllText(control_file_path, config_file_name + "\r\n"); } } //Now we can call python for all symbols at once by passing in the control_file as cmd line arg CallPythonScript(output_folder + "\\Data_Processing18.py", control_file_path); } }
protected override void OnBarUpdate() { if (OnlyGeneratePlots) { return; } int BarIndex = BarsInProgress; bool bFinishedProcessingAllData = false; String debug_txt1 = "Date=" + Times[BarIndex][0].Date.ToShortDateString() + "BarIndex=" + BarIndex.ToString() + "CurrentBar=" + CurrentBar.ToString() + "Count=" + Count.ToString(); Debug.WriteLine(debug_txt1); DateTime current_bar_date = Times[BarIndex][0].Date; Boolean bLastSymbol = BarIndex == (symbol_list.Count - 1); if (current_bar_date > TestingEndDate) { return; } else if (bLastSymbol && (((CurrentBar + 1) == Count) || (current_bar_date == TestingEndDate))) //we are on the last bar to process { Debug.WriteLine("bFinished trigger"); bFinishedProcessingAllData = true; } if (((ProcessAllTickers == true) && (BarIndex >= 1) && (BarIndex < (symbol_list.Count))) || //Start at index=1 since we want to ignore the primary/dummy instrument ((ProcessAllTickers == false) && (BarIndex == 0))) { if (CurrentBar >= (FutureValueDaysToLookAhead)) //just make sure we avoid out of bounds error in case we don't yet have enough data { DateTime date_to_process = Times[BarIndex][FutureValueDaysToLookAhead].Date; //need to wait FutureValueDaysToLookAhead days before we can make calcs bool bWithinTrainingPeriod = (date_to_process >= TrainingStartDate) && (date_to_process <= TrainingEndDate); bool bWithinTestingPeriod = (date_to_process >= TestingStartDate) && (date_to_process <= TestingEndDate); if ((bWithinTrainingPeriod) || (bWithinTestingPeriod)) { for (int i = 0; i < indicator_list.Count; i++) { Indicator_FutureValueChange_Pair indicator_pair = GetIndicatorValue(indicator_list[i], BarIndex, FutureValueDaysToLookAhead, date_to_process); if (bWithinTrainingPeriod) { //list_Indicator_FutureValueChange_Training.Add(indicator_pair); //list_Indicator_FutureValueChange_Training_ALL[BarIndex].Add(indicator_pair); master_list_training[i][BarIndex].Add(indicator_pair); } else if (bWithinTestingPeriod) { //list_Indicator_FutureValueChange_Testing.Add(indicator_pair); //list_Indicator_FutureValueChange_Testing_ALL[BarIndex].Add(indicator_pair); master_list_testing[i][BarIndex].Add(indicator_pair); } } } } } if (bFinishedProcessingAllData) { String debug_txt2 = "finished processing all data"; Debug.WriteLine(debug_txt2); for (int bar_index = 1; bar_index < symbol_list.Count; bar_index++) { for (int i = 0; i < indicator_list.Count; i++) { int num_bars_for_symbol = Times[bar_index].Count; if (CurrentBar < num_bars_for_symbol) //Check for missing data { for (int j = (FutureValueDaysToLookAhead - 1); j >= 0; j--) { DateTime date_to_process = Times[bar_index][j].Date; debug_txt2 = "date_to_process=" + date_to_process.ToShortDateString(); Debug.WriteLine(debug_txt2); Indicator_FutureValueChange_Pair indicator_pair = GetIndicatorValue(indicator_to_use, bar_index, j, date_to_process); //Add to the "Testing" lists since these should be the ones capturing most recent values //list_Indicator_FutureValueChange_Testing.Add(indicator_pair); //list_Indicator_FutureValueChange_Testing_ALL[bar_index].Add(indicator_pair); master_list_testing[i][bar_index].Add(indicator_pair); } } } } uid = rnd.Next(100000, 999999).ToString(); String unique_id = "_" + uid; // creates a number between 100k and 999k String control_file_path = output_folder + "control_file" + unique_id + ".txt"; String consolidated_txt_file_path = output_folder + "consolidated_report" + unique_id + ".txt"; String consolidated_csv_file_path = output_folder + "consolidated_report" + unique_id + ".csv"; bool bFirstTime = true; int start_index = 1; int max_index = symbol_list.Count; if (ProcessAllTickers == false) { start_index = 0; max_index = 1; } for (int j = 0; j < indicator_list.Count; j++) { uid = rnd.Next(100000, 999999).ToString(); unique_id = "_" + uid; // creates a number between 100k and 999k String debug_txt = "FINISHEDPROCESSING min_samples=" + min_samples_pct.ToString() + " id=" + unique_id.ToString(); Debug.WriteLine(debug_txt); switch (indicator_list[j]) { case IndicatorEnum.indicator_MACD: if (Sanitize == false) { description = "MACD"; } else { description = "M"; } indicator = description; description = description + "(" + Param1.ToString() + "-" + Param2.ToString() + "-" + Param3.ToString() + ")"; break; case IndicatorEnum.indicator_RSI: if (Sanitize == false) { description = "RSI"; } else { description = "R"; } indicator = description; description = description + "(" + Param1.ToString() + "-" + Param2.ToString() + ")"; break; case IndicatorEnum.indicator_BOLLINGER: if (Sanitize == false) { description = "BOLL"; } else { description = "B"; } indicator = description; description = description + "(" + Param1.ToString() + "-" + Param2.ToString() + ")"; break; case IndicatorEnum.indicator_STOCHASTIC: if (Sanitize == false) { description = "STOCH"; } else { description = "S"; } indicator = description; description = description + "(" + Param1.ToString() + "-" + Param2.ToString() + "-" + Param3.ToString() + ")"; break; case IndicatorEnum.indicator_STOCHASTIC_RSI: if (Sanitize == false) { description = "STOCH_RSI"; } else { description = "SR"; } indicator = description; description = description + "(" + Param1.ToString() + ")"; break; default: description = "Unknown"; indicator = description; break; } description = description + " - " + FutureValueDaysToLookAhead.ToString(); if (Sanitize == false) { description = description + " days"; } description = description + " - " + unique_id.Substring(1); //remove preceding "_" for (int i = start_index; i < max_index; i++) //Start at index=1 since we want to ignore the first primary/dummy instrument { String symbol_name = symbol_list[i]; if (ProcessAllTickers == false) { symbol_name = master_symbol_name; } String company_name = company_name_list[i]; if (Sanitize == true) { String symbol_name_tmp = string.Concat(symbol_name.Reverse()).ToLower(); symbol_name = char.ToUpper(symbol_name_tmp[0]) + symbol_name_tmp.Substring(1); company_name = "?"; } String base_file_path_for_symbol = output_folder + symbol_name; //i.e. c:\temp\knn\AAPL String training_file_path = base_file_path_for_symbol + "_training" + unique_id + ".csv"; String testing_file_path = base_file_path_for_symbol + "_testing" + unique_id + ".csv"; //WriteListToCSV(list_Indicator_FutureValueChange_Training_ALL[i], training_file_path); //WriteListToCSV(list_Indicator_FutureValueChange_Testing_ALL[i], testing_file_path); WriteListToCSV(master_list_training[j][i], training_file_path); WriteListToCSV(master_list_testing[j][i], testing_file_path); String config_file_name = ""; config_file_name = GenerateConfigFile(base_file_path_for_symbol, symbol_name, company_name, training_file_path, testing_file_path, unique_id); //mut.WaitOne(); if (bFirstTime) //create new file { bFirstTime = false; File.WriteAllText(control_file_path, config_file_name + "\r\n"); } else // append to existing file { File.AppendAllText(control_file_path, config_file_name + "\r\n"); } //mut.ReleaseMutex(); } } Debug.WriteLine("Before Python mutex"); //Now we can call python for all symbols at once by passing in the control_file as cmd line arg mut_python.WaitOne(); string mode = " BEST"; if (FindBestSolutions == false) { mode = " RECENT"; } string args = control_file_path + " " + consolidated_txt_file_path + " " + consolidated_csv_file_path + " c:\\temp\\knn\\master_report.csv" + " c:\\temp\\knn\\master_trig.csv" + " false" + " false" + mode + " c:\\temp\\knn\\master_sorted.csv" + " " + GeneratePlotsForBestSolutions.ToString() + " " + NumPlotsToGenerate.ToString(); Debug.WriteLine("Python args" + args); CallPythonScript(output_folder + "\\Data_Processing45.py", args); mut_python.ReleaseMutex(); Debug.WriteLine("After Python mutex"); } }
protected override void OnBarUpdate() { int BarIndex = BarsInProgress; bool bFinishedProcessingAllData = false; if (((CurrentBar + 2) == Count) && BarIndex == (symbol_list.Count - 1)) //we are on the last bar to process { bFinishedProcessingAllData = true; } if (((ProcessAllTickers == true) && (BarIndex >= 1) && (BarIndex < (symbol_list.Count))) || //Start at index=1 since we want to ignore the primary/dummy instrument ((ProcessAllTickers == false) && (BarIndex == 0))) { if (CurrentBar >= (FutureValueDaysToLookAhead)) //just make sure we avoid out of bounds error in case we don't yet have enough data { DateTime current_bar_date = Times[BarIndex][0].Date; DateTime date_to_process = Times[BarIndex][FutureValueDaysToLookAhead].Date; //need to wait FutureValueDaysToLookAhead days before we can make calcs bool bWithinTrainingPeriod = (date_to_process >= TrainingStartDate) && (date_to_process <= TrainingEndDate); bool bWithinTestingPeriod = (date_to_process >= TestingStartDate) && (date_to_process <= TestingEndDate); if ((bWithinTrainingPeriod) || (bWithinTestingPeriod)) { //TimeSpan diff = TrainingEndDate - TrainingStartDate; //int training_days = (int)Math.Abs(Math.Round(diff.TotalDays)); double indicator_value = 0.0; switch (indicator_to_use) { case IndicatorEnum.indicator_MACD: indicator_value = MACD(BarsArray[BarIndex], Param1, Param2, Param3)[FutureValueDaysToLookAhead]; //get the indicator val from n days ago break; case IndicatorEnum.indicator_RSI: indicator_value = RSI(BarsArray[BarIndex], Param1, Param2)[FutureValueDaysToLookAhead]; //get the indicator val from n days ago break; case IndicatorEnum.indicator_BOLLINGER: double upper_band = Bollinger(BarsArray[BarIndex], (double)Param1, Param2).Upper[FutureValueDaysToLookAhead]; //get the indicator val from n days ago double middle_band = Bollinger(BarsArray[BarIndex], (double)Param1, Param2).Middle[FutureValueDaysToLookAhead]; //get the indicator val from n days ago double lower_band = Bollinger(BarsArray[BarIndex], (double)Param1, Param2).Lower[FutureValueDaysToLookAhead]; //get the indicator val from n days ago double current_price = Closes[BarIndex][FutureValueDaysToLookAhead]; double diff = current_price - middle_band; double band_range = upper_band - middle_band; indicator_value = diff / band_range; //how far current price is from the middle band (-1.0 means we're at the lower band, +1 means we're at the upper band) break; case IndicatorEnum.indicator_STOCHASTIC: //use the "D" value indicator_value = Stochastics(BarsArray[BarIndex], Param1, Param2, Param3).D[FutureValueDaysToLookAhead]; //get the indicator val from n days ago break; case IndicatorEnum.indicator_STOCHASTIC_RSI: indicator_value = StochRSI(BarsArray[BarIndex], Param1)[FutureValueDaysToLookAhead]; //get the indicator val from n days ago break; case IndicatorEnum.indicator_GREG: indicator_value = -999.999; // GregIndicator1(BarsArray[BarIndex], (float)Param1)[FutureValueDaysToLookAhead]; //get the indicator val from n days ago break; default: indicator_value = -999.99; break; } if (double.IsNaN(indicator_value)) { indicator_value = 0.0; } double future_price_change = 0.0; double future_price = Closes[BarIndex][0]; double start_price = Closes[BarIndex][FutureValueDaysToLookAhead]; future_price_change = ((future_price / start_price) - 1.0) * 100.0; Indicator_FutureValueChange_Pair indicator_pair = new Indicator_FutureValueChange_Pair(); indicator_pair.Date = date_to_process; indicator_pair.Price = start_price; indicator_pair.Indicator = indicator_value; indicator_pair.FutureValueChange = future_price_change; if (bWithinTrainingPeriod) { list_Indicator_FutureValueChange_Training.Add(indicator_pair); list_Indicator_FutureValueChange_Training_ALL[BarIndex].Add(indicator_pair); int count_training = list_Indicator_FutureValueChange_Training.Count; } else if (bWithinTestingPeriod) { list_Indicator_FutureValueChange_Testing.Add(indicator_pair); list_Indicator_FutureValueChange_Testing_ALL[BarIndex].Add(indicator_pair); int count_testing = list_Indicator_FutureValueChange_Testing.Count; } } } } else { } if (bFinishedProcessingAllData) { for (int bar_index = 1; bar_index < symbol_list.Count; bar_index++) { for (int j = FutureValueDaysToLookAhead; j >= 0; j--) { DateTime date_to_process = Times[bar_index][j].Date; double indicator_value = 0.0; switch (indicator_to_use) { case IndicatorEnum.indicator_MACD: indicator_value = MACD(BarsArray[bar_index], Param1, Param2, Param3)[j]; //get the indicator val from n days ago break; case IndicatorEnum.indicator_RSI: indicator_value = RSI(BarsArray[bar_index], Param1, Param2)[0]; //get the indicator val from n days ago break; case IndicatorEnum.indicator_BOLLINGER: double upper_band = Bollinger(BarsArray[bar_index], (double)Param1, Param2).Upper[j]; //get the indicator val from n days ago double middle_band = Bollinger(BarsArray[bar_index], (double)Param1, Param2).Middle[j]; //get the indicator val from n days ago double lower_band = Bollinger(BarsArray[bar_index], (double)Param1, Param2).Lower[j]; //get the indicator val from n days ago double current_price = Closes[bar_index][j]; double diff = current_price - middle_band; double band_range = upper_band - middle_band; indicator_value = diff / band_range; //how far current price is from the middle band (-1.0 means we're at the lower band, +1 means we're at the upper band) break; case IndicatorEnum.indicator_STOCHASTIC: //use the "D" value indicator_value = Stochastics(BarsArray[bar_index], Param1, Param2, Param3).D[j]; //get the indicator val from n days ago break; case IndicatorEnum.indicator_STOCHASTIC_RSI: indicator_value = StochRSI(BarsArray[bar_index], Param1)[j]; //get the indicator val from n days ago break; case IndicatorEnum.indicator_GREG: indicator_value = -999.999; // GregIndicator1(BarsArray[BarIndex], (float)Param1)[FutureValueDaysToLookAhead]; //get the indicator val from n days ago break; default: indicator_value = -999.99; break; } if (double.IsNaN(indicator_value)) { indicator_value = 0.0; } double future_price_change = 0.0; double future_price = Closes[bar_index][j]; double start_price = Closes[bar_index][j]; future_price_change = ((future_price / start_price) - 1.0) * 100.0; Indicator_FutureValueChange_Pair indicator_pair = new Indicator_FutureValueChange_Pair(); indicator_pair.Date = date_to_process; indicator_pair.Price = start_price; indicator_pair.Indicator = indicator_value; indicator_pair.FutureValueChange = future_price_change; //if (bWithinTrainingPeriod) //{ // list_Indicator_FutureValueChange_Training.Add(indicator_pair); // list_Indicator_FutureValueChange_Training_ALL[BarIndex].Add(indicator_pair); // int count_training = list_Indicator_FutureValueChange_Training.Count; //} //else if (bWithinTestingPeriod) //{ list_Indicator_FutureValueChange_Testing.Add(indicator_pair); list_Indicator_FutureValueChange_Testing_ALL[bar_index].Add(indicator_pair); int count_testing = list_Indicator_FutureValueChange_Testing.Count; //} } } //if (CurrentBar >= (FutureValueDaysToLookAhead)) //just make sure we avoid out of bounds error in case we don't yet have enough data //{ // DateTime current_bar_date = Times[bar_index][0].Date; // DateTime date_to_process = Times[bar_index][0].Date; //need to wait FutureValueDaysToLookAhead days before we can make calcs // bool bWithinTrainingPeriod = (date_to_process >= TrainingStartDate) && (date_to_process <= TrainingEndDate); // bool bWithinTestingPeriod = (date_to_process >= TestingStartDate) && (date_to_process <= TestingEndDate); // if ((bWithinTrainingPeriod) || (bWithinTestingPeriod)) // { // //TimeSpan diff = TrainingEndDate - TrainingStartDate; // //int training_days = (int)Math.Abs(Math.Round(diff.TotalDays)); // } //} //////// uid = rnd.Next(100000, 999999).ToString(); String unique_id = "_" + uid; // creates a number between 100k and 999k String debug_txt = "FINISHEDPROCESSING min_samples=" + min_samples_pct.ToString() + " id=" + unique_id.ToString(); Debug.WriteLine(debug_txt); String control_file_path = output_folder + "control_file" + unique_id + ".txt"; String consolidated_txt_file_path = output_folder + "consolidated_report" + unique_id + ".txt"; String consolidated_csv_file_path = output_folder + "consolidated_report" + unique_id + ".csv"; switch (indicator_to_use) { case IndicatorEnum.indicator_MACD: if (Sanitize == false) { description = "MACD"; } else { description = "M"; } indicator = description; description = description + "(" + Param1.ToString() + "-" + Param2.ToString() + "-" + Param3.ToString() + ")"; break; case IndicatorEnum.indicator_RSI: if (Sanitize == false) { description = "RSI"; } else { description = "R"; } indicator = description; description = description + "(" + Param1.ToString() + "-" + Param2.ToString() + ")"; break; case IndicatorEnum.indicator_BOLLINGER: if (Sanitize == false) { description = "BOLL"; } else { description = "B"; } indicator = description; description = description + "(" + Param1.ToString() + "-" + Param2.ToString() + ")"; break; case IndicatorEnum.indicator_STOCHASTIC: if (Sanitize == false) { description = "STOCH"; } else { description = "S"; } indicator = description; description = description + "(" + Param1.ToString() + "-" + Param2.ToString() + "-" + Param3.ToString() + ")"; break; case IndicatorEnum.indicator_STOCHASTIC_RSI: if (Sanitize == false) { description = "STOCH_RSI"; } else { description = "SR"; } indicator = description; description = description + "(" + Param1.ToString() + ")"; break; default: description = "Unknown"; indicator = description; break; } description = description + " - " + FutureValueDaysToLookAhead.ToString(); if (Sanitize == false) { description = description + " days"; } description = description + " - " + unique_id.Substring(1); //remove preceding "_" bool bFirstTime = true; int start_index = 1; int max_index = symbol_list.Count; if (ProcessAllTickers == false) { start_index = 0; max_index = 1; } for (int i = start_index; i < max_index; i++) //Start at index=1 since we want to ignore the first primary/dummy instrument { String symbol_name = symbol_list[i]; if (ProcessAllTickers == false) { symbol_name = master_symbol_name; } String company_name = company_name_list[i]; if (Sanitize == true) { String symbol_name_tmp = string.Concat(symbol_name.Reverse()).ToLower(); symbol_name = char.ToUpper(symbol_name_tmp[0]) + symbol_name_tmp.Substring(1); company_name = "?"; } String base_file_path_for_symbol = output_folder + symbol_name; //i.e. c:\temp\knn\AAPL String training_file_path = base_file_path_for_symbol + "_training" + unique_id + ".csv"; String testing_file_path = base_file_path_for_symbol + "_testing" + unique_id + ".csv"; WriteListToCSV(list_Indicator_FutureValueChange_Training_ALL[i], training_file_path); WriteListToCSV(list_Indicator_FutureValueChange_Testing_ALL[i], testing_file_path); String config_file_name = ""; config_file_name = GenerateConfigFile(base_file_path_for_symbol, symbol_name, company_name, training_file_path, testing_file_path, unique_id); //mut.WaitOne(); if (bFirstTime) //create new file { bFirstTime = false; File.WriteAllText(control_file_path, config_file_name + "\r\n"); } else // append to existing file { File.AppendAllText(control_file_path, config_file_name + "\r\n"); } //mut.ReleaseMutex(); } //Now we can call python for all symbols at once by passing in the control_file as cmd line arg mut.WaitOne(); string mode = " BEST"; if (FindBestSolutions == false) { mode = " SINGLE"; } string args = control_file_path + " " + consolidated_txt_file_path + " " + consolidated_csv_file_path + " c:\\temp\\knn\\master_report.csv" + " c:\\temp\\knn\\master_trig.csv" + " false" + mode; CallPythonScript(output_folder + "\\Data_Processing39.py", args); mut.ReleaseMutex(); } }