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 >= 2 /*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; 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]; 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_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; default: break; } String signal_name = scan_result.Symbol + "-" + Strategy_Num.ToString() + "-" + DaysToHold.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); } } } } if (bFinishedProcessingAllData == true) { uid = rnd.Next(100000, 999999).ToString(); WriteTradeScannerResults(); WriteTradeTriggers(); } }
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 && boll_declining_3dys) { bBuy = true; } break; case 2: //boll up spike if (boll_in_range && boll_change_in_range) { bBuy = true; } break; case 3: //boll crossing -1 threshold, but not too far if ((boll_1 < -1.1) && nt.InRange(boll_0, -0.9, -0.7)) { 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)) { bBuy = true; } break; case 5: //strat3 + Today 0.75-1.25 and SP500Change 0.5 - 1.0 if ((boll_1 < -1.1) && nt.InRange(boll_0, -0.9, -0.7) && nt.InRange(scan_result.PercentChange_Today, 0.75, 1.25) && nt.InRange(scan_result.SP500_PercentChange_Today, 0.5, 1.0)) { 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)) { bBuy = true; } break; case 7: //strat3 + Today 0.5-2.0 and SP500Change 0.5 - 3.0 if ((boll_1 < -1.1) && nt.InRange(boll_0, -0.9, -0.7) && nt.InRange(scan_result.PercentChange_Today, 0.5, 2.0) && nt.InRange(scan_result.SP500_PercentChange_Today, 0.5, 3.0)) { bBuy = true; } break; case 8: //strat7 + 5-day indicator change -1.5 to -0.5 if ((boll_1 < -1.1) && nt.InRange(boll_0, -0.9, -0.7) && nt.InRange(scan_result.PercentChange_Today, 0.5, 2.0) && nt.InRange(scan_result.SP500_PercentChange_Today, 0.5, 3.0) && nt.InRange(scan_result.Indicator_Change_5Day, -1.5, -0.5)) { bBuy = true; } break; case 9: //strat7 but use 0.0 for min of %ChangeToday and SP500ChangeToday if ((boll_1 < -1.1) && nt.InRange(boll_0, -0.9, -0.7) && nt.InRange(scan_result.PercentChange_Today, 0.0, 2.0) && nt.InRange(scan_result.SP500_PercentChange_Today, 0.0, 3.0)) { bBuy = true; } break; case 10: //strat7 but remove SP500%Change condition if ((boll_1 < -1.1) && nt.InRange(boll_0, -0.9, -0.7) && nt.InRange(scan_result.PercentChange_Today, 0.5, 2.0)) { bBuy = true; } break; case 11: //strat7 but change boll range max to -0.5 if ((boll_1 < -1.1) && nt.InRange(boll_0, -0.9, -0.5) && nt.InRange(scan_result.PercentChange_Today, 0.5, 2.0) && nt.InRange(scan_result.SP500_PercentChange_Today, 0.5, 3.0)) { bBuy = true; } break; case 12: //strat7 but change boll range min/max to -0.95/-0.75 if ((boll_1 < -1.1) && nt.InRange(boll_0, -0.95, -0.75) && nt.InRange(scan_result.PercentChange_Today, 0.5, 2.0) && nt.InRange(scan_result.SP500_PercentChange_Today, 0.5, 3.0)) { bBuy = true; } break; case 13: //strat12 but also add condition for CompSP500_today > 0.15 if ((boll_1 < -1.1) && nt.InRange(boll_0, -0.95, -0.75) && nt.InRange(scan_result.PercentChange_Today, 0.5, 2.0) && nt.InRange(scan_result.SP500_PercentChange_Today, 0.5, 3.0) && (scan_result.Compare_SP500_Today > 0.15)) { bBuy = true; } break; case 14: //Just testing SP500%change range if (nt.InRange(scan_result.SP500_PercentChange_Today, 0.5, 3.0)) { bBuy = true; } break; case 15: //strat7, but use 0.0 for SP500 min if ((boll_1 < -1.1) && nt.InRange(boll_0, -0.9, -0.7) && nt.InRange(scan_result.PercentChange_Today, 0.5, 2.0) && nt.InRange(scan_result.SP500_PercentChange_Today, 0.0, 3.0)) { bBuy = true; } break; case 16: //strat7, but use -1.0 for boll_1 min and -0.5 for boll_0 max if ((boll_1 < -1.0) && nt.InRange(boll_0, -0.9, -0.5) && nt.InRange(scan_result.PercentChange_Today, 0.5, 2.0) && nt.InRange(scan_result.SP500_PercentChange_Today, 0.5, 3.0)) { bBuy = true; } break; case 17: //strat7 but but use -1.0 for boll_1 min and -0.5 for boll_0 max, use 0.25 for today min, and remove SP500 change if ((boll_1 < -1.0) && nt.InRange(boll_0, -0.9, -0.5) && nt.InRange(scan_result.PercentChange_Today, 0.25, 2.0)) { bBuy = true; } break; case 18: //strat7 but but use -1.2 for boll_1 min and -0.95 for boll_0 max, use 0.25 for today min, and remove SP500 change if ((boll_1 < -1.2) && nt.InRange(boll_0, -0.95, -0.5) && nt.InRange(scan_result.PercentChange_Today, 0.25, 2.0)) { bBuy = true; } break; case 19: //just dropped to range of -1.0 to -0.8 if ((boll_1 > -0.5) && nt.InRange(boll_0, -1.0, -0.8)) { bBuy = true; } break; case 20: //recovery from below -0.9 if ((boll_1 < -0.9) && nt.InRange(boll_0, -0.7, -0.3)) { 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(); } }
protected override void OnStateChange() { if (State == State.SetDefaults) { if (bFirst) { bFirst = false; } symbol_list = new List <String> { "DUMMY", "^SP500" }; company_name_list = new List <String> { "DUMMY", "S&P 500" }; Trade_Status = new List <String> { "DUMMY", "DUMMY" }; DaysSincePurchase = new List <int> { -1, -1 }; NumShares = new List <int> { -1, -1 }; output_folder = "C:\\temp\\knn\\"; LoadTickers(); //load symbols from file Description = "test"; Name = "Trade_Scanner3"; Calculate = Calculate.OnEachTick; //This will let us get the last Bar TestingStartDate = DateTime.Today; TestingEndDate = DateTime.Today; //Trading params SimulateTrades = true; AmountPerTrade = 500.0f; StopLossPercent = 5; //use int so we can potentially change during optimization DaysToHold = 5; //Allows multiple stocks to be traded on same day EntryHandling = EntryHandling.UniqueEntries; //Custom params uid = ""; indicator = ""; master_symbol_name = ""; bDataLoaded = false; BOLL_param_string = "2;14;-1"; Strategy_Num = 1; // This strategy has been designed to take advantage of performance gains in Strategy Analyzer optimizations // See the Help Guide for additional information IsInstantiatedOnEachOptimizationIteration = true; //IsInstantiatedOnEachOptimizationIteration = false; } else if (State == State.Configure) { //Set our stop loss here SetStopLoss(CalculationMode.Percent, StopLossPercent / 100.0f); //System.Windows.Forms.MessageBox.Show("Configure"); bDataLoaded = false; for (int i = 0; i < symbol_list.Count; i++) { //Don't add the dummy instrument (but we still need a placeholder list which is added below) if (i != 0) { AddDataSeries(symbol_list[i], Data.BarsPeriodType.Day, 1); } } for (int i = 0; i < indicator_list.Count; i++) { master_list_training.Add(new List <List <Indicator_FutureValueChange_Pair> >()); master_list_testing.Add(new List <List <Indicator_FutureValueChange_Pair> >()); for (int j = 0; j < symbol_list.Count; j++) { master_list_training[i].Add(new List <Indicator_FutureValueChange_Pair>()); master_list_testing[i].Add(new List <Indicator_FutureValueChange_Pair>()); } } } else if (State == State.DataLoaded) { //master_symbol_name = Instrument.MasterInstrument.Name; bDataLoaded = true; TrainingStartDate = TestingEndDate.AddYears(-1); TrainingEndDate = TestingEndDate.AddDays(-91); //TestingStartDate = TestingEndDate.AddDays(-90); output_folder_with_date = output_folder + DateTime.Today.ToString("ddMMMyyyy") + "\\"; DirectoryInfo di1 = Directory.CreateDirectory(output_folder_with_date); //3-2d-10%-01Jan2018-01Jul2018 Strategy_Description = Strategy_Num.ToString() + "-" + DaysToHold.ToString() + "d-" + StopLossPercent.ToString() + "%-" + TestingStartDate.ToString("ddMMMyyyy") + "-" + TestingEndDate.ToString("ddMMMyyyy"); results_folder = output_folder_with_date + Strategy_Description; //DirectoryInfo di2 = Directory.CreateDirectory(results_folder); } else if ((bDataLoaded == true) && ((State == State.Transition) || (State == State.Terminated))) //finished processing historical data (and ready for real-time) { } }
protected override void OnBarUpdate() { //if (BarsInProgress == 0) //{ int BarIndex = BarsInProgress; bool bFinishedProcessingAllData = false; if (((CurrentBar + 1) == 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 string symbol_name = symbol_list[BarIndex]; double current_price = Closes[BarIndex][0]; List <TradeTriggerByDate> triggers = trade_triggers[BarIndex]; for (int i = 0; i < triggers.Count; i++) { TradeTriggerByDate trig = triggers[i]; string trade_id_str = symbol_name; string trade_exit_str = "Exit " + trade_id_str; //if (DaysSincePurchase[BarIndex] >= 0) //Trade in-progress if (trig.Status == "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 { trig.Status = "Completed"; Trade_Status[BarIndex] = "Idle"; ExitLong(BarIndex, NumShares[BarIndex], trade_exit_str, trade_id_str); DaysSincePurchase[BarIndex] = -1; } } } //else if ((trig.TradeDate == date_to_process) && (Trade_Status[BarIndex] != "InProgress")) //prevent multiple entries for same symbol else if ((trig.TradeDate == date_to_process) && (Trade_Status[BarIndex] != "InProgress")) //prevent multiple entries for same symbol { if (current_price < AmountPerTrade) //if price is greater than max per trade, then we cannot purchase { Trade_Status[BarIndex] = "InProgress"; DaysSincePurchase[BarIndex] = 0; trig.Status = "InProgress"; NumShares[BarIndex] = (int)(AmountPerTrade / current_price); EnterLong(BarIndex, NumShares[BarIndex], trade_id_str); scanner_results.Add(trig.Symbol + " " + trig.TradeDate.ToShortDateString() + " " + DaysToHold.ToString()); } } } } if (bFinishedProcessingAllData) { WriteStringListToCSV(scanner_results, "c:\\temp\\knn\\scanner_results.txt"); } //} }