コード例 #1
0
ファイル: Trade_Scanner3.cs プロジェクト: grc-stk/NT
        protected void WriteSP500HistoricalData()
        {
            int count = SP500_historical_price_list.Count;

            var csv = new StringBuilder();

            csv.AppendLine("Date,P");

            for (int i = 0; i < count; i++)
            {
                Date_Price_Pair dp = SP500_historical_price_list[i];

                var newLine = string.Format("{0},{1:0.00}", dp.Date.ToShortDateString(), dp.Price);

                csv.AppendLine(newLine);
            }

            //mut.WaitOne(); //wait until safe to enter; prior thread has completed writing.
            try
            {
                String file_name = results_folder + "sp.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);
            }
        }
コード例 #2
0
ファイル: Trade_Scanner3.cs プロジェクト: grc-stk/NT
        protected override void OnBarUpdate()
        {
            int BarIndex = BarsInProgress;

            bool bFinishedProcessingAllData = false;

            Boolean bLastSymbol = BarIndex == (symbol_list.Count - 1);

            DateTime current_bar_date = Times[BarIndex][0].Date;

            if (bLastSymbol &&
                (((CurrentBar + 1) == Count) || (current_bar_date == TestingEndDate)))  //we are on the last bar to process
            {
                Debug.WriteLine("bFinished trigger");
                bFinishedProcessingAllData = true;
            }

            String debug_txt1 = "Date=" + Times[BarIndex][0].Date.ToShortDateString() + "BarIndex=" + BarIndex.ToString() + "CurrentBar=" + CurrentBar.ToString() + "Count=" + Count.ToString();

            Debug.WriteLine(debug_txt1);

            //NOTE: Index 1 is always S&P 500
            if ((BarIndex >= 1 /*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

                if ((date_to_process >= TestingStartDate) && (date_to_process <= TestingEndDate)) //  && (CurrentBar >= (20)))  //give 20 days of buffer
                {
                    int SP500_index = 1;
                    if (BarIndex == SP500_index)
                    {
                        Date_Price_Pair date_price = new Date_Price_Pair();
                        date_price.Date  = date_to_process;
                        date_price.Price = Closes[BarIndex][0];
                        SP500_historical_price_list.Add(date_price);
                    }
                    else  //non S&P500 symbols
                    {
                        TradeScannerResult scan_result = new TradeScannerResult();

                        double OpenPriceToday  = Opens[BarIndex][0];
                        double ClosePriceToday = Closes[BarIndex][0];
                        double SP500_Open      = Opens[SP500_index][0];
                        double SP500_Close     = Closes[SP500_index][0];

                        try
                        {
                            scan_result.Date                      = date_to_process;
                            scan_result.Symbol                    = symbol_list[BarIndex];
                            scan_result.Description               = company_name_list[BarIndex];
                            scan_result.Open                      = OpenPriceToday;
                            scan_result.Close                     = ClosePriceToday;
                            scan_result.PercentChange_Today       = (((ClosePriceToday / OpenPriceToday) - 1.0) * 100.0);
                            scan_result.SP500_PercentChange_Today = (((SP500_Close / SP500_Open) - 1.0) * 100.0);
                            scan_result.Compare_SP500_Today       = scan_result.PercentChange_Today - scan_result.SP500_PercentChange_Today;

                            scan_result.PercentChange_1Day  = PctChange(BarIndex, 1);
                            scan_result.PercentChange_2Day  = PctChange(BarIndex, 2);
                            scan_result.PercentChange_5Day  = PctChange(BarIndex, 5);
                            scan_result.PercentChange_10Day = PctChange(BarIndex, 10);
                            scan_result.PercentChange_15Day = PctChange(BarIndex, 15);
                            scan_result.PercentChange_20Day = PctChange(BarIndex, 20);

                            scan_result.Compare_SP500_1Day  = scan_result.PercentChange_1Day - PctChange(SP500_index, 1);
                            scan_result.Compare_SP500_2Day  = scan_result.PercentChange_2Day - PctChange(SP500_index, 2);
                            scan_result.Compare_SP500_5Day  = scan_result.PercentChange_5Day - PctChange(SP500_index, 5);
                            scan_result.Compare_SP500_10Day = scan_result.PercentChange_10Day - PctChange(SP500_index, 10);
                            scan_result.Compare_SP500_15Day = scan_result.PercentChange_15Day - PctChange(SP500_index, 15);
                            scan_result.Compare_SP500_20Day = scan_result.PercentChange_20Day - PctChange(SP500_index, 20);

                            scan_result.Green_5Day  = GetPercentOfGreenDays(BarIndex, 5);
                            scan_result.Green_10Day = GetPercentOfGreenDays(BarIndex, 10);
                            scan_result.Green_15Day = GetPercentOfGreenDays(BarIndex, 15);
                            scan_result.Green_20Day = GetPercentOfGreenDays(BarIndex, 20);


                            PopulateParamatersForIndicator(IndicatorEnum.indicator_BOLLINGER);
                            //Indicator_FutureValueChange_Pair indicator_pair = GetIndicatorValue(indicator_list[i], BarIndex, 0, date_to_process);

                            double indicator_value_today = 0.0;

                            scan_result.Indicator_Name = "BOLL";
                            scan_result.Strategy_Name  = Strategy_Description; // Strategy_Num.ToString();

                            double boll_0  = ComputeBollinger(BarIndex, 0);
                            double boll_1  = ComputeBollinger(BarIndex, 1);
                            double boll_2  = ComputeBollinger(BarIndex, 2);
                            double boll_3  = ComputeBollinger(BarIndex, 3);
                            double boll_4  = ComputeBollinger(BarIndex, 4);
                            double boll_5  = ComputeBollinger(BarIndex, 5);
                            double boll_10 = ComputeBollinger(BarIndex, 10);
                            double boll_15 = ComputeBollinger(BarIndex, 15);
                            double boll_20 = ComputeBollinger(BarIndex, 20);

                            indicator_value_today       = boll_0;
                            scan_result.Indicator_Today = indicator_value_today;

                            double boll_0_chg = boll_1 - boll_0;

                            double boll_min             = -1.0;
                            double boll_max             = -0.75;
                            bool   boll_in_range        = (boll_0 > boll_min) && (boll_0 < boll_max);
                            double boll_min_change      = 0.05;
                            double boll_max_change      = 0.25;
                            bool   boll_change_in_range = (boll_0_chg > boll_min_change) && (boll_0_chg < boll_max_change);
                            bool   boll_declining_3dys  = (boll_3 > boll_2) && (boll_2 > boll_1) && (boll_0 > boll_1);

                            scan_result.Indicator_Change_1Day  = indicator_value_today - boll_1;
                            scan_result.Indicator_Change_2Day  = indicator_value_today - boll_2;
                            scan_result.Indicator_Change_5Day  = indicator_value_today - boll_5;
                            scan_result.Indicator_Change_10Day = indicator_value_today - boll_10;
                            scan_result.Indicator_Change_15Day = indicator_value_today - boll_15;
                            scan_result.Indicator_Change_20Day = indicator_value_today - boll_20;

                            double RSI_val = RSI(BarsArray[BarIndex], 14, 3)[0];  //get the indicator val from n days ago
                            scan_result.RSI = RSI_val;

                            bool bBuy = false;
                            switch (Strategy_Num)
                            {
                            case 1:     //declining boll for 3, then up spike
                                if (boll_in_range && boll_change_in_range)
                                {
                                    //strat1 (declining boll for 3, then up spike)
                                    if (boll_declining_3dys)
                                    {
                                        Console.WriteLine("\nstrat1\n");
                                        bBuy = true;
                                    }
                                }
                                break;

                            case 2:      //boll up spike
                                if (boll_in_range && boll_change_in_range)
                                {
                                    Console.WriteLine("\nstrat2\n");
                                    bBuy = true;
                                }
                                break;

                            case 3:      //boll crossing -1 threshold, but not too far
                                if ((boll_1 < -1.1) && (boll_0 > -0.9) && (boll_0 < -0.7))
                                {
                                    Console.WriteLine("\nstrat3\n");
                                    bBuy = true;
                                }
                                break;

                            case 4:      //boll in range, today's change < -0.5% and SP500 change > 0%
                                if (boll_in_range && (scan_result.PercentChange_Today < -0.5) && (scan_result.SP500_PercentChange_Today > 0.0))
                                {
                                    Console.WriteLine("\nstrat4\n");
                                    bBuy = true;
                                }
                                break;

                            case 5:      //strat3 + Today 0.75-1.25 and SP500Change 0.5 - 1.0
                                if ((boll_1 < -1.1) && (boll_0 > -0.9) && (boll_0 < -0.7) && (scan_result.PercentChange_Today > 0.75) &&
                                    (scan_result.PercentChange_Today < 1.25) &&
                                    (scan_result.SP500_PercentChange_Today > 0.5) &&
                                    (scan_result.SP500_PercentChange_Today < 1.0))
                                {
                                    Console.WriteLine("\nstrat5\n");
                                    bBuy = true;
                                }
                                break;

                            case 6:      //boll up spike + Today < -1.5 + SP500Change < -0.75
                                if (boll_in_range && boll_change_in_range && (scan_result.PercentChange_Today < -1.5) && (scan_result.SP500_PercentChange_Today < -0.75))
                                {
                                    Console.WriteLine("\nstrat6\n");
                                    bBuy = true;
                                }
                                break;

                            case 7:      //strat3 + Today 0.5-2.0 and SP500Change 0.5 - 3.0
                                if ((boll_1 < -1.1) && (boll_0 > -0.9) && (boll_0 < -0.7) && (scan_result.PercentChange_Today > 0.5) &&
                                    (scan_result.PercentChange_Today < 2.0) &&
                                    (scan_result.SP500_PercentChange_Today > 0.5) &&
                                    (scan_result.SP500_PercentChange_Today < 3.0))
                                {
                                    Console.WriteLine("\nstrat7\n");
                                    bBuy = true;
                                }
                                break;

                            default:
                                break;
                            }


                            String signal_name = scan_result.Symbol + "-" + Strategy_Num.ToString() + "-" + DaysToHold.ToString() + "-" + StopLossPercent.ToString();

                            //Check to see if time to sell
                            if (Trade_Status[BarIndex] == "InProgress")
                            {
                                //if ((date_to_process > trig.TradeDate)) // && (date_to_process <= trig.TradeDate.AddDays(trig.DaysToHold)))
                                //{
                                DaysSincePurchase[BarIndex]++;


                                if (DaysSincePurchase[BarIndex] == DaysToHold) //Use input parameter rather than reading from file trig.DaysToHold //Set Exit for end of nth day
                                {
                                    Trade_Status[BarIndex] = "Idle";
                                    ExitLong(BarIndex, NumShares[BarIndex], signal_name, signal_name);
                                    DaysSincePurchase[BarIndex] = -1;
                                }
                                //}
                            }
                            else if (bBuy)
                            {
                                if (scan_result.Close < AmountPerTrade)  //if price is greater than max per trade, then we cannot purchase
                                {
                                    TradeTriggerByDate trig = new TradeTriggerByDate();
                                    trig.Symbol     = scan_result.Symbol;
                                    trig.TradeDate  = scan_result.Date;
                                    trig.DaysToHold = DaysToHold;
                                    trade_triggers.Add(trig);


                                    Trade_Status[BarIndex]      = "InProgress";
                                    DaysSincePurchase[BarIndex] = 0;
                                    //trig.Status = "InProgress";
                                    NumShares[BarIndex] = (int)(AmountPerTrade / scan_result.Close);
                                    EnterLong(BarIndex, NumShares[BarIndex], signal_name);

                                    trade_scanner_results.Add(scan_result);
                                }
                            }
                        }
                        catch
                        {
                            Console.WriteLine("Caught exception");
                        }
                    }
                }
            }

            if (bFinishedProcessingAllData == true)
            {
                uid            = rnd.Next(100000, 999999).ToString();
                results_folder = results_folder + "_" + uid + "\\";
                DirectoryInfo di2 = Directory.CreateDirectory(results_folder);
                WriteTradeScannerResults();
                //WriteTradeTriggers();
                WriteSP500HistoricalData();
            }
        }