Exemple #1
0
        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;
            }

            //NOTE: Index 1 is always S&P 500
            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++)
                        {
                            PopulateParamatersForIndicator(indicator_list[i]);
                            Indicator_FutureValueChange_Pair indicator_pair = GetIndicatorValue(indicator_list[i], BarIndex, FutureValueDaysToLookAhead, date_to_process);

                            if (bWithinTrainingPeriod)
                            {
                                master_list_training[i][BarIndex].Add(indicator_pair);
                            }
                            else if (bWithinTestingPeriod)
                            {
                                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);

                                PopulateParamatersForIndicator(indicator_list[i]);
                                Indicator_FutureValueChange_Pair indicator_pair = GetIndicatorValue(indicator_list[i], bar_index, j, date_to_process);

                                //Add to the "Testing" lists since these should be the ones capturing most recent values
                                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_with_date + "control_file" + unique_id + ".txt";
                String consolidated_txt_file_path = output_folder_with_date + "consolidated_report" + unique_id + ".txt";
                String consolidated_csv_file_path = output_folder_with_date + "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);

                    PopulateParamatersForIndicator(indicator_list[j]);
                    BuildIndicatorAndDescriptionStrings(indicator_list[j]);

                    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_with_date + 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(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);

                        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");
                        }
                    }
                }
                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 + " " + output_folder_with_date + "master_report.csv" + " " + output_folder_with_date + "master_trig.csv" + " false" + " false" + mode + " " + output_folder_with_date + "master_sorted.csv" + " " + GeneratePlotsForBestSolutions.ToString() + " " + NumPlotsToGenerate.ToString();
                Debug.WriteLine("Python args" + args);
                CallPythonScript(output_folder + "\\Data_Processing46.py", args);
                mut_python.ReleaseMutex();
                Debug.WriteLine("After Python mutex");
            }
        }
Exemple #2
0
        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)
            {
            }
        }
Exemple #3
0
        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
                Description = "test";
                Name        = "PlotGenerator1";
                Calculate   = Calculate.OnEachTick; //This will let us get the last Bar

                EndDate = DateTime.Today;
                SortedMasterFilePath = "c:\\temp\\knn\\";
                NumPlotsToGenerate   = 20;


                // 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)
            {
            }
            else if (State == State.DataLoaded)
            {
                mut_python.WaitOne();
                string args = SortedMasterFilePath + EndDate.ToString("ddMMMyyyy") + "\\master_sorted.csv" + " " + NumPlotsToGenerate.ToString();
                Debug.WriteLine("Python args" + args);
                CallPythonScript("c:\\temp\\knn" + "\\Data_Processing46.py", args);
                mut_python.ReleaseMutex();
                Debug.WriteLine("After Python mutex");
            }
            else if ((State == State.Transition) || (State == State.Terminated))  //finished processing historical data (and ready for real-time)
            {
            }
        }