コード例 #1
0
        protected void LoadTriggers()
        {
            for (int i = 0; i < symbol_list.Count; i++)
            {
                trade_triggers.Add(new List <TradeTrigger>());
            }

            string trigger_file_path        = "c:\\temp\\knn\\triggers.csv";
            String trigger_config_file_path = "c:\\temp\\knn\\triggers_config.txt";

            System.IO.StreamReader file3 = new System.IO.StreamReader(trigger_config_file_path);
            String line3 = file3.ReadLine();

            if (line3 != null)
            {
                trigger_file_path = line3;
            }
            file3.Close();

            System.IO.StreamReader file4 = new System.IO.StreamReader(trigger_file_path);
            string line4;

            while ((line4 = file4.ReadLine()) != null)
            {
                string[] values = line4.Split(';');
                if (values.Length == 9)
                {
                    TradeTrigger trig = new TradeTrigger();
                    trig.Symbol = values[0];

                    IndicatorEnum newIndicator;
                    Enum.TryParse(values[1], out newIndicator);
                    string indicator_str = newIndicator.ToString().Substring(10, newIndicator.ToString().Length - 10);

                    trig.Indicator  = newIndicator;
                    trig.StartRange = Convert.ToDouble(values[2]);
                    trig.EndRange   = Convert.ToDouble(values[3]);
                    trig.DaysToHold = Convert.ToInt32(values[4]);
                    trig.Param1     = Convert.ToInt32(values[5]);
                    trig.Param2     = Convert.ToInt32(values[6]);
                    trig.Param3     = Convert.ToInt32(values[7]);
                    trig.Identifier = values[8];

                    int symbol_index = symbol_to_index[trig.Symbol];

                    trade_triggers[symbol_index].Add(trig);
                }
            }
            file4.Close();
        }
コード例 #2
0
        protected override void OnBarUpdate()
        {
            int BarIndex = BarsInProgress;

            double indicator_value = 0.0;

            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
            {
                DateTime date_to_process = Times[BarIndex][0].Date;  //need to wait FutureValueDaysToLookAhead days before we can make calcs

                bool bWithinPeriod = (date_to_process >= StartDate) && (date_to_process <= EndDate);

                if (bWithinPeriod)
                {
                    string symbol_name = symbol_list[BarIndex];

                    double current_price = Closes[BarIndex][0];

                    List <TradeTrigger> triggers = trade_triggers[BarIndex];
                    for (int i = 0; i < triggers.Count; i++)
                    {
                        TradeTrigger trig           = triggers[i];
                        string       indicator_str  = trig.Indicator.ToString().Substring(10, trig.Indicator.ToString().Length - 10);
                        string       trade_id_str   = indicator_str + " " + symbol_name;
                        string       trade_exit_str = "Exit " + trade_id_str;

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

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

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

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

                        bool bIndicatorInRange = false;
                        if ((indicator_value > trig.StartRange) && (indicator_value < trig.EndRange))
                        {
                            bIndicatorInRange = true;
                            String results_string = date_to_process.ToShortDateString() + " " + trig.Symbol + " " + indicator_str +
                                                    " " + trig.Identifier + " " + trig.StartRange.ToString() + " " + trig.EndRange.ToString() +
                                                    " " + trig.Param1.ToString() + " " + trig.Param2.ToString() + " " + trig.Param3.ToString();
                            scanner_results.Add(results_string);
                        }

                        if (SimulateTrades == true)
                        {
                            if (DaysSincePurchase[BarIndex] >= 0)  //Trade in-progress
                            {
                                DaysSincePurchase[BarIndex]++;
                                if (DaysSincePurchase[BarIndex] == trig.DaysToHold)
                                {
                                    ExitLong(BarIndex, NumShares[BarIndex], trade_exit_str, trade_id_str);
                                    DaysSincePurchase[BarIndex] = -1;
                                }
                            }
                            else if (bIndicatorInRange)
                            {
                                DaysSincePurchase[BarIndex] = 0;
                                NumShares[BarIndex]         = (int)(AmountPerTrade / current_price);
                                EnterLong(BarIndex, NumShares[BarIndex], trade_id_str);
                            }
                        }
                    }
                }
            }

            if (bFinishedProcessingAllData)
            {
                WriteStringListToCSV(scanner_results, "c:\\temp\\knn\\scanner_results.txt");
            }
        }