public MixtureEval(Mixture mixture, double fitness) : base(mixture.Database) { MixtureId = mixture.Id; Fitness = fitness; Database.Store(this); }
public static List <Day> Backtest(Database db, Mixture mixture, string symbol, DateTime startDate, DateTime endDate, double initialEquity, double marginFactor, double tradeCost) { var preloadStart = startDate.AddYears(-1); var allBars = new DataSeries <Bar>(symbol, db.QueryAll <DbBar>(x => x.Symbol == symbol, "Timestamp") .Select(DataPreprocessing.DbBarToBar)); var bars = DataPreprocessing.TrimToWindow(allBars, DataPreprocessing.GetWindow(preloadStart, endDate, allBars)); var data = DataPreprocessing.LoadTrainingSet(db, symbol, preloadStart, endDate, Signals.Null); var predictor = new MixturePredictor(mixture, data); Debug.Assert(bars.Length == data.Input.ColumnCount); var backtestStartIndex = bars.ToList().FindIndex(x => x.Timestamp >= startDate); // preload for (int t = 0; t < backtestStartIndex; t++) { predictor.Predict(t); } int totalPredictions = 0; int correctPredictions = 0; // backtest var account = new VAccount(initialEquity, marginFactor, tradeCost); for (int t = backtestStartIndex + 1; t < bars.Length; t++) { var prediction = predictor.Predict(t - 1); Debug.Assert(prediction != 0); var wasCorrect = ActOnSignal(bars[t - 1], bars[t], prediction, account, symbol); totalPredictions++; if (wasCorrect) { correctPredictions++; } } double correctPct = (double)correctPredictions / totalPredictions; return(null); }
static MixtureEval EvaluateMixture(Mixture m, DataSet data) { using (var mixturePredictor = new MixturePredictor(m, data)) return(new MixtureEval(m, ComputeFitness(mixturePredictor, data))); }