Example #1
0
        private Indicator_FutureValueChange_Pair GetIndicatorValue(IndicatorEnum indicator, int bar_index, int day_index, DateTime date_to_process)
        {
            double indicator_value = 0.0;

            switch (indicator)
            {
            case IndicatorEnum.indicator_MACD:
                indicator_value = MACD(BarsArray[bar_index], Param1, Param2, Param3)[day_index];      //get the indicator val from n days ago
                break;

            case IndicatorEnum.indicator_RSI:
                indicator_value = RSI(BarsArray[bar_index], Param1, Param2)[day_index];      //get the indicator val from n days ago
                break;

            case IndicatorEnum.indicator_BOLLINGER:
                double upper_band    = Bollinger(BarsArray[bar_index], (double)Param1, Param2).Upper[day_index];   //get the indicator val from n days ago
                double middle_band   = Bollinger(BarsArray[bar_index], (double)Param1, Param2).Middle[day_index];  //get the indicator val from n days ago
                double lower_band    = Bollinger(BarsArray[bar_index], (double)Param1, Param2).Lower[day_index];   //get the indicator val from n days ago
                double current_price = Closes[bar_index][day_index];
                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[day_index];      //get the indicator val from n days ago
                break;

            case IndicatorEnum.indicator_STOCHASTIC_RSI:
                indicator_value = StochRSI(BarsArray[bar_index], Param1)[day_index];      //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;
            }

            //S&P500 is always BarIndex 1
            int    SP500_Bar_Index           = 1;
            double SP500_future_price        = Closes[SP500_Bar_Index][0];
            double SP500_start_price         = Closes[SP500_Bar_Index][day_index];
            double SP500_future_price_change = ((SP500_future_price / SP500_start_price) - 1.0) * 100.0;

            double future_price        = Closes[bar_index][0];
            double start_price         = Closes[bar_index][day_index];
            double future_price_change = ((future_price / start_price) - 1.0) * 100.0;

            double price_change_compared_to_SP500 = future_price_change - SP500_future_price_change;

            Indicator_FutureValueChange_Pair indicator_pair = new Indicator_FutureValueChange_Pair();

            indicator_pair.Date              = date_to_process;
            indicator_pair.Price             = Convert.ToSingle(start_price);
            indicator_pair.Indicator         = Convert.ToSingle(indicator_value);
            indicator_pair.FutureValueChange = Convert.ToSingle(price_change_compared_to_SP500); // future_price_change;
            indicator_pair.SP500_Price       = Convert.ToSingle(SP500_start_price);

            return(indicator_pair);
        }
Example #2
0
        protected override void OnBarUpdate()
        {
            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;

            if ((BarIndex >= 1) && (BarIndex < (symbol_list.Length))) //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], 12, 26, 9)[FutureValueDaysToLookAhead];  //get the indicator val from n days ago
                            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], 14, 3)[FutureValueDaysToLookAhead];  //get the indicator val from n days ago
                            indicator_value = RSI(BarsArray[BarIndex], Param1, Param2)[FutureValueDaysToLookAhead];                                      //get the indicator val from n days ago
                            break;

                        case IndicatorEnum.indicator_GREG:
                            //indicator_value = GregIndicator1(BarsArray[BarIndex], 1.0)[FutureValueDaysToLookAhead];  //get the indicator val from n days ago
                            indicator_value = GregIndicator1(BarsArray[BarIndex], (float)Param1)[FutureValueDaysToLookAhead];                                      //get the indicator val from n days ago
                            break;

                        default:
                            indicator_value = -999.99;
                            break;
                        }

                        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;
                        }
                    }
                }
            }
        }
Example #3
0
        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();
            }
        }
Example #4
0
        protected override void OnBarUpdate()
        {
            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], 2.0)[0];

            if (CurrentBar >= DaysToLookBack)  //make sure we have enough data loaded to look back n days
            {
                Prev_Value = Closes[0][DaysToLookBack];
            }

            //Value[0] = CurrentBar == 0 ? 0 : Input[0] - Input[Math.Min(CurrentBar, Period)];

            //double Current_Value = Close[0];
            //double Prev_Value = Close[0];

            //double greg_indicator_value = greg1[0];

            //if (CurrentBar >= DaysToLookBack)  //make sure we have enough data loaded to look back n days
            //{
            //	Prev_Value = Close[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);
            }

            if (BarsInProgress == 0)                            //if we're looking at data for primary 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[0][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 ((minimum_date >= TrainingStartDate) && (minimum_date <= TrainingEndDate) || //if we are within training period
                    //    (minimum_date >= TestingStartDate) && (minimum_date <= TestingEndDate))    //or within the testing period
                    if ((bWithinTrainingPeriod) || (bWithinTestingPeriod))
                    {
                        //TimeSpan diff = TrainingEndDate - TrainingStartDate;
                        //int training_days = (int)Math.Abs(Math.Round(diff.TotalDays));

                        double indicator_value = MACD(BarsArray[0], 12, 26, 9)[FutureValueDaysToLookAhead];  //get the indicator val from n days ago
                        //double indicator_value = GregIndicator1(BarsArray[0], 2.0)[FutureValueDaysToLookAhead];  //get the indicator val from n days ago

                        double future_price_change = 0.0;
                        double future_price        = Closes[0][0];
                        double start_price         = Closes[0][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.Indicator         = indicator_value;
                        indicator_pair.FutureValueChange = future_price_change;

                        if (bWithinTrainingPeriod)
                        {
                            list_Indicator_FutureValueChange_Training.Add(indicator_pair);
                            int count_training = list_Indicator_FutureValueChange_Training.Count;
                        }
                        else if (bWithinTestingPeriod)
                        {
                            list_Indicator_FutureValueChange_Testing.Add(indicator_pair);
                            int count_testing = list_Indicator_FutureValueChange_Testing.Count;
                        }
                    }
                }
            }
        }
Example #5
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");
            }
        }
Example #6
0
        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();
            }
        }