예제 #1
0
        public override void Run()
        {
            MulticoreOptimizer optimizer = new MulticoreOptimizer();

            OptimizationUniverse universe = new OptimizationUniverse();

            for (int length1 = 2; length1 < 14; length1++)
            {
                for (int length2 = length1 + 1; length2 < 28; length2++)
                {
                    OptimizationParameterSet parameter = new OptimizationParameterSet();

                    parameter.Add("Length1", length1);
                    parameter.Add("Length2", length2);
                    parameter.Add("Bar", (long)60);

                    universe.Add(parameter);
                }
            }

            strategy = new SMACrossover(framework, "strategy ");

            Instrument instrument1 = InstrumentManager.Instruments["AAPL"];
            Instrument instrument2 = InstrumentManager.Instruments["MSFT"];

            InstrumentList instruments = new InstrumentList();

            instruments.Add(instrument1);
            instruments.Add(instrument2);

            DataSimulator.DateTime1 = new DateTime(2013, 12, 01);
            DataSimulator.DateTime2 = new DateTime(2013, 12, 31);

            optimizer.Optimize(strategy, instruments, universe, 100);
        }
예제 #2
0
		public OptimizationParameterSet Optimize(Strategy strategy, InstrumentList instruments, OptimizationUniverse universe, int bunch = -1)
		{
			this.event_count = 0L;
			this.watch.Start();
			int num;
			if (bunch == -1)
			{
				num = universe.Count;
			}
			else
			{
				num = bunch;
			}
			int num2 = 0;
			while (num2 + num < universe.Count)
			{
				this.Optimize(strategy, instruments, universe, num2, num);
				num2 += num;
			}
			this.Optimize(strategy, instruments, universe, num2, universe.Count - num2);
			int index = 0;
			for (int i = 1; i < universe.Count; i++)
			{
				if (universe[i].Objective > universe[index].Objective)
				{
					index = i;
				}
			}
			Console.WriteLine(string.Concat(new object[]
			{
				"Best Objective ",
				universe[index],
				" Objective = ",
				universe[index].Objective
			}));
			Console.WriteLine("Optimization done");
			this.watch.Stop();
			Console.WriteLine(string.Concat(new object[]
			{
				"Processed ",
				this.event_count,
				" events in ",
				this.watch.ElapsedMilliseconds,
				" msec - ",
				(double)this.event_count / (double)this.watch.ElapsedMilliseconds * 1000.0,
				" event/sec"
			}));
			return universe[index];
		}
예제 #3
0
 public OptimizationParameterSet Optimize(FastQuant.Strategy strategy, OptimizationUniverse universe = null)
 {
     return(this.framework.OptimizationManager.Optimize(strategy, universe));
 }
예제 #4
0
		private void Optimize(Strategy strategy, InstrumentList instruments, OptimizationUniverse universe, int index, int n)
		{
			Framework[] array = new Framework[n];
			Strategy[] array2 = new Strategy[n];
			for (int i = 0; i < n; i++)
			{
				if (i == 0)
				{
					array[i] = strategy.framework;
				}
				else
				{
					array[i] = new Framework("framework " + i, array[i - 1].EventBus, strategy.framework.InstrumentServer, null);
				}
				array2[i] = (Strategy)Activator.CreateInstance(strategy.GetType(), new object[]
				{
					array[i],
					"strategy " + i
				});
			}
			for (int j = 0; j < n; j++)
			{
				OptimizationParameterSet optimizationParameterSet = universe[index + j];
				foreach (OptimizationParameter current in optimizationParameterSet)
				{
					if (current.Name == "Bar")
					{
						using (IEnumerator<Instrument> enumerator2 = instruments.GetEnumerator())
						{
							while (enumerator2.MoveNext())
							{
								Instrument current2 = enumerator2.Current;
								array[j].EventManager.BarFactory.Add(current2, BarType.Time, (long)current.Value);
							}
							continue;
						}
					}
					FieldInfo field = array2[j].GetType().GetField(current.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetField | BindingFlags.SetField);
					if (field != null)
					{
						if (field.FieldType == typeof(int))
						{
							field.SetValue(array2[j], (int)current.Value);
						}
						else
						{
							field.SetValue(array2[j], current.Value);
						}
					}
					else
					{
						Console.WriteLine("Optimizer::Optimize Can not set field with name " + current.Name);
					}
				}
			}
			for (int k = 0; k < n; k++)
			{
				foreach (Instrument current3 in instruments)
				{
					array2[k].AddInstrument(current3);
				}
			}
			for (int l = n - 1; l >= 0; l--)
			{
				array[l].StrategyManager.StartStrategy(array2[l], StrategyMode.Backtest);
			}
			bool flag;
			do
			{
				flag = true;
				for (int m = 0; m < n; m++)
				{
					if (array2[m].Status != StrategyStatus.Stopped)
					{
						flag = false;
					}
				}
				Thread.Sleep(10);
			}
			while (!flag);
			for (int num = 0; num < n; num++)
			{
				universe[num].Objective = array2[num].Objective();
				Console.WriteLine(universe[num] + " Objective = " + universe[num].Objective);
			}
			for (int num2 = 0; num2 < n; num2++)
			{
				this.event_count += array[num2].eventManager.EventCount;
			}
			for (int num3 = 0; num3 < n; num3++)
			{
				array[num3] = null;
				array2[num3] = null;
			}
			strategy.framework.Clear();
		}
예제 #5
0
        public override void Run()
        {
            MulticoreOptimizer optimizer = new MulticoreOptimizer();

            OptimizationUniverse universe = new OptimizationUniverse();

            for (int length1 = 2; length1 < 14; length1++)
                for (int length2 = length1 + 1; length2 < 28; length2++)
                {
                    OptimizationParameterSet parameter = new OptimizationParameterSet();

                    parameter.Add("Length1", length1);
                    parameter.Add("Length2", length2);
                    parameter.Add("Bar", (long)60);

                    universe.Add(parameter);
                }

            strategy = new SMACrossover(framework, "strategy ");

            Instrument instrument1 = InstrumentManager.Instruments["AAPL"];
            Instrument instrument2 = InstrumentManager.Instruments["MSFT"];

            InstrumentList instruments = new InstrumentList();

            instruments.Add(instrument1);
            instruments.Add(instrument2);

            DataSimulator.DateTime1 = new DateTime(2013, 12, 01);
            DataSimulator.DateTime2 = new DateTime(2013, 12, 31);

            optimizer.Optimize(strategy, instruments, universe, 100);
        }
예제 #6
0
        public override void Run()
        {
            MulticoreOptimizer optimizer = new MulticoreOptimizer(this.framework);


            OptimizationUniverse universe = new OptimizationUniverse();

            for (decimal Delta = 1; Delta < 8; Delta++)
            {
                OptimizationParameterSet parameter = new OptimizationParameterSet();

                parameter.Add("Delta", Delta);

                universe.Add(parameter);
            }
            Instrument spreadInsturment = InstrumentManager.Get("*NG 01-15 vs *NG 02-15");

            // Add spread instrument if needed.
            if (spreadInsturment == null)
            {
                spreadInsturment = new Instrument(InstrumentType.Future, "*NG 01-15 vs *NG 02-15");

                InstrumentManager.Add(spreadInsturment);
            }

            spreadInsturment.Legs.Clear();

            // Add legs for spread instrument if needed.
            if (spreadInsturment.Legs.Count == 0)
            {
                spreadInsturment.Legs.Add(new Leg(InstrumentManager.Get("*NG 01-15"), 1));
                spreadInsturment.Legs.Add(new Leg(InstrumentManager.Get("*NG 02-15"), 1));
            }

            // Main strategy.
            strategy = new Strategy(framework, "SpreadTrading");

            // Create BuySide strategy and add trading instrument.
            MyStrategy buySide = new MyStrategy(framework, "BuySide");

            buySide.Instruments.Add(spreadInsturment);

            // Create SellSide strategy.
            SpreadSellSide sellSide = new SpreadSellSide(framework, "SellSide");

            //sellSide.Global[SpreadSellSide.barSizeCode] = barSize;

            // Set SellSide as data and execution provider for BuySide strategy.
            buySide.DataProvider      = sellSide;
            buySide.ExecutionProvider = sellSide;

            // Add strategies to main.
            strategy.AddStrategy(buySide);
            strategy.AddStrategy(sellSide);

            // Set DataSimulator's dates.
            DataSimulator.DateTime1 = new DateTime(2014, 11, 18);            // 1 day before real start
            DataSimulator.DateTime2 = new DateTime(2014, 12, 30);            // 1 day after real end

            InstrumentList instruments = new InstrumentList();

            instruments.Add(spreadInsturment);

            strategy.AddInstruments(instruments);

            //You can choose an optimization method from the "Optimizers" window
            //and observe the optimization results in the "Optimization Results" window.

            //Optimization can use either of two ways to declare parameters:

            //1. Optimization using [OptimizationParameter] atrributes from Strategy

            //Optimize(strategy);

            //2. Optimization via OptimizationUniverse
            optimizer.Optimize(strategy, universe);
        }