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 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" }; 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) { } }