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 override void OnStateChange() { if (State == State.SetDefaults) { if (bFirst) { bFirst = false; } symbol_list = new List <String> { "DUMMY", "^SP500" }; company_name_list = new List <String> { "DUMMY", "S&P 500" }; Trade_Status = new List <String> { "DUMMY", "DUMMY" }; DaysSincePurchase = new List <int> { -1, -1 }; NumShares = new List <int> { -1, -1 }; output_folder = "C:\\temp\\knn\\"; LoadTickers(); //load symbols from file Description = "test"; Name = "Trade_Scanner2"; Calculate = Calculate.OnEachTick; //This will let us get the last Bar TestingStartDate = DateTime.Today; TestingEndDate = DateTime.Today; //Trading params SimulateTrades = true; AmountPerTrade = 500.0f; StopLossPercent = 5; //use int so we can potentially change during optimization DaysToHold = 5; //Allows multiple stocks to be traded on same day EntryHandling = EntryHandling.UniqueEntries; //Custom params uid = ""; indicator = ""; master_symbol_name = ""; bDataLoaded = false; BOLL_param_string = "2;14;-1"; Strategy_Num = 1; // 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) { //Set our stop loss here SetStopLoss(CalculationMode.Percent, StopLossPercent / 100.0f); //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); } } for (int i = 0; i < indicator_list.Count; i++) { master_list_training.Add(new List <List <Indicator_FutureValueChange_Pair> >()); master_list_testing.Add(new List <List <Indicator_FutureValueChange_Pair> >()); for (int j = 0; j < symbol_list.Count; j++) { master_list_training[i].Add(new List <Indicator_FutureValueChange_Pair>()); master_list_testing[i].Add(new List <Indicator_FutureValueChange_Pair>()); } } } else if (State == State.DataLoaded) { //master_symbol_name = Instrument.MasterInstrument.Name; bDataLoaded = true; TrainingStartDate = TestingEndDate.AddYears(-1); TrainingEndDate = TestingEndDate.AddDays(-91); //TestingStartDate = TestingEndDate.AddDays(-90); output_folder_with_date = output_folder + TestingEndDate.ToString("ddMMMyyyy") + "\\"; DirectoryInfo di = Directory.CreateDirectory(output_folder_with_date); } else if ((bDataLoaded == true) && ((State == State.Transition) || (State == State.Terminated))) //finished processing historical data (and ready for real-time) { } }
protected void WriteTradeScannerResults() { int count = trade_scanner_results.Count; var csv = new StringBuilder(); csv.AppendLine("Date,Symbol,Co.,Open,Close,%Change,SP500%Change,CompSP500,%Change-1Day, %Change-2Day,%Change-5Day," + "%Change-10Day,%Change-15Day,%Change-20Day,CompSP500-1Day,CompSP500-2Day,CompSP500-5Day,CompSP500-10Day," + "CompSP500-15Day,CompSP500-20Day,IndName,IndToday,IndChange-1Day,IndChange-2Day,IndChange-5Day," + "IndChange-10Day,IndChange-15Day,IndChange-20Day,RSI,Strategy"); for (int i = 0; i < count; i++) { TradeScannerResult res = trade_scanner_results[i]; var newLine = string.Format("{0},{1},{2},{3},{4:0.00},{5:0.00},{6:0.00},{7:0.00}", res.Date.ToShortDateString(), res.Symbol, res.Description, res.Open, res.Close, res.PercentChange_Today, res.SP500_PercentChange_Today, res.Compare_SP500_Today); newLine = newLine + string.Format(",{0:0.00},{1:0.00},{2:0.00},{3:0.00},{4:0.00},{5:0.00}", res.PercentChange_1Day, res.PercentChange_2Day, res.PercentChange_5Day, res.PercentChange_10Day, res.PercentChange_15Day, res.PercentChange_20Day); newLine = newLine + string.Format(",{0:0.00},{1:0.00},{2:0.00},{3:0.00},{4:0.00},{5:0.00}", res.Compare_SP500_1Day, res.Compare_SP500_2Day, res.Compare_SP500_5Day, res.Compare_SP500_10Day, res.Compare_SP500_15Day, res.Compare_SP500_20Day); newLine = newLine + string.Format(",{0},{1:0.00},{2:0.00},{3:0.00},{4:0.00},{5:0.00},{6:0.00},{7:0.00},{8:0.00},{9}", res.Indicator_Name, res.Indicator_Today, res.Indicator_Change_1Day, res.Indicator_Change_2Day, res.Indicator_Change_5Day, res.Indicator_Change_10Day, res.Indicator_Change_15Day, res.Indicator_Change_20Day, res.RSI, res.Strategy_Name); csv.AppendLine(newLine); } //mut.WaitOne(); //wait until safe to enter; prior thread has completed writing. try { String file_name = output_folder + "scanner_results_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 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 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); }
protected override void OnStateChange() { if (State == State.SetDefaults) { if (bFirst) { bFirst = false; } symbol_list = new List <String> { "DUMMY", "^SP500" }; company_name_list = new List <String> { "DUMMY", "S&P 500" }; output_folder = "C:\\temp\\knn\\"; LoadTickers(); //load symbols from file Description = "test"; Name = "KNN_Generator16"; Calculate = Calculate.OnEachTick; //This will let us get the last Bar DaysToLookBack = 1; FutureValueDaysToLookAhead = 5; TestingEndDate = DateTime.Today; //Custom params uid = ""; indicator = ""; 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; Sanitize = false; ProcessAllTickers = true; FindBestSolutions = false; GeneratePlotsForBestSolutions = false; OnlyGeneratePlots = false; NumPlotsToGenerate = 20; master_symbol_name = ""; bDataLoaded = false; MACD_param_string = "12;26;9"; RSI_param_string = "14;3;-1"; BOLL_param_string = "2;14;-1"; STOCH_param_string = "7;14;3"; STOCH_RSI_param_string = "14;-1;-1"; // 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; if (!OnlyGeneratePlots) { 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); } } for (int i = 0; i < indicator_list.Count; i++) { master_list_training.Add(new List <List <Indicator_FutureValueChange_Pair> >()); master_list_testing.Add(new List <List <Indicator_FutureValueChange_Pair> >()); for (int j = 0; j < symbol_list.Count; j++) { master_list_training[i].Add(new List <Indicator_FutureValueChange_Pair>()); master_list_testing[i].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) { //master_symbol_name = Instrument.MasterInstrument.Name; bDataLoaded = true; TrainingStartDate = TestingEndDate.AddYears(-1); TrainingEndDate = TestingEndDate.AddDays(-91); TestingStartDate = TestingEndDate.AddDays(-90); output_folder_with_date = output_folder + TestingEndDate.ToString("ddMMMyyyy") + "\\"; DirectoryInfo di = Directory.CreateDirectory(output_folder_with_date); if (OnlyGeneratePlots) { mut_python.WaitOne(); string args = output_folder_with_date + "master_sorted.csv" + " " + NumPlotsToGenerate.ToString(); Debug.WriteLine("Python args" + args); CallPythonScript(output_folder + "\\Data_Processing46.py", args); mut_python.ReleaseMutex(); Debug.WriteLine("After Python mutex"); } } else if ((bDataLoaded == true) && ((State == State.Transition) || (State == State.Terminated))) //finished processing historical data (and ready for real-time) { } }
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); }