/// <summary> /// constructor /// </summary> /// <param name="strategy">strategy has to be initiated by property setter</param> /// <param name="tickfiles"></param> public BacktestEngine(StrategyBase strategy, List<string> tickfiles) { _tickfiles = tickfiles; _strategy = strategy; _playthread.DoWork += new DoWorkEventHandler(Play); _playthread.WorkerReportsProgress = false; _playthread.WorkerSupportsCancellation = true; _playthread.RunWorkerCompleted += new RunWorkerCompletedEventHandler(PlayComplete); Reset(); }
/// <summary> /// return /// </summary> public StrategySetterWindow(StrategyBase strategy) { InitializeComponent(); _StrategyPropertyGrid.DataContext = strategy; }
public bool LoadStrategy() { Microsoft.Win32.OpenFileDialog openFileDialog1 = new Microsoft.Win32.OpenFileDialog(); openFileDialog1.InitialDirectory = _rootpath+_config.StrategyPath; openFileDialog1.Filter = "dll files (*.dll)|*.dll|All files (*.*)|*.*"; openFileDialog1.Title = "Select strategy dll"; openFileDialog1.RestoreDirectory = true; Nullable<bool> result = openFileDialog1.ShowDialog(); if (result == true) { string f = openFileDialog1.FileName; if (Util.isStrategyFile(f)) { // Select specifc strategy in the dll BackTestWindow.UI.StrategySelectorWindow strategyselector = new BackTestWindow.UI.StrategySelectorWindow(f); Nullable<bool> sresult = strategyselector.ShowDialog(); if (sresult == true) { Reset(); // load first strategy, ignore multi-selection _dllname = f; _strategyname = strategyselector.StrategySelected[0]; _strategy = Util.GetSingleStrategyFromDLL(_strategyname, _dllname); _strategy.Reset(); _strategy.SetIdTracker(_idtracker); // set strategy parameters BackTestWindow.UI.StrategySetterWindow strategysetter = new BackTestWindow.UI.StrategySetterWindow(_strategy); sresult = strategysetter.ShowDialog(); // ignore sresult. User can assume default parameters. // status DllStatus(_strategyname); // set up optimizable variable list _optimvariablelist = OptimizeEngine.GetOptimizeable(_dllname, _strategyname); OptimizerStatusUpdates("OptimVariable"); } } } bool success = CheckPrerequisite(); return success; }
public void Reset(bool reloadstrategy) { try { _tradelist.Clear(); _positionlist.Clear(); if (_createnewengine == false) // already have an egine { if (_backtestengine != null) // could be optimize engine { try { _backtestengine.Stop(); _backtestengine.Reset(); } catch { } } } _ticktable.Clear(); _indicatortable.Clear(); _positiontable.Clear(); _ordertable.Clear(); _filltable.Clear(); _resultstable.Clear(); if (reloadstrategy) { _strategy = null; _strategyname = ""; _dllname = ""; DllStatus(""); if (_createnewengine == false) // already have an egine UnbindBacktestEngine(ref _backtestengine); _createnewengine = true; } } catch (Exception ex) { Status("An error occured, try again."); Debug("reset error: " + ex.Message + ex.StackTrace); } }
void UnBindStrategy(ref StrategyBase tmp) { // handle all the outgoing events from the strategy tmp.SendDebugEvent -= _strategy_GotDebug; tmp.SendOrderEvent -= _strategy_SendOrder; tmp.SendCancelEvent -= _strategy_CancelOrderSource; tmp.SendBasketEvent -= _strategy_SendBasket; tmp.SendReqHistBarEvent -= _strategy_SendReqHistBar; tmp.SendChartLabelEvent -= _strategy_SendChartLabel; tmp.SendIndicatorsEvent -= _strategy_SendIndicators; }
public OptimizeEngine(StrategyBase s, List <string> tickfiles) : base(s, tickfiles) { }
public OptimizeEngine(StrategyBase s, List<string> tickfiles) : base(s, tickfiles) { }
/// <summary> /// change parameter of a strategy /// </summary> bool ChangeValue(ref StrategyBase s, decimal v) { try { var t = s.GetType(); foreach (var pi in t.GetProperties()) if (pi.Name == OptimizeName) { if (pi.PropertyType == typeof(int)) { pi.SetValue(s, (int)v, null); } else pi.SetValue(s, v, null); return true; } } catch (Exception ex) { Debug("error setting parameter " + OptimizeName + " on strategy: " + s.FullName + " to: " + v + " err: " + ex.Message + ex.StackTrace); } return false; }
void UnbindStrategy(ref StrategyBase s) { try { s.SendOrderEvent -= _strategy_SendOrder; s.SendCancelEvent -= _strategy_SendCancel; s.SendDebugEvent -= _strategy_SendDebugEvent; s.SendIndicatorsEvent -= _strategy_SendIndicatorsEvent; s.SendChartLabelEvent -= _strategy_SendChartLabelEvent; s.SendBasketEvent -= _strategy_SendBasketEvent; } catch { } }
void BindStrategy(ref StrategyBase s) { s.SendOrderEvent += _strategy_SendOrder; s.SendCancelEvent += _strategy_SendCancel; s.SendDebugEvent += _strategy_SendDebugEvent; s.SendIndicatorsEvent += _strategy_SendIndicatorsEvent; s.SendChartLabelEvent += _strategy_SendChartLabelEvent; s.SendBasketEvent += _strategy_SendBasketEvent; }