/// <summary> /// Initializes this alpha handler to accept insights from the specified algorithm /// </summary> /// <param name="job">The algorithm job</param> /// <param name="algorithm">The algorithm instance</param> /// <param name="messagingHandler">Handler used for sending insights</param> /// <param name="api">Api instance</param> public virtual void Initialize(AlgorithmNodePacket job, IAlgorithm algorithm, IMessagingHandler messagingHandler, IApi api) { // initializing these properties just in case, doesn't hurt to have them populated Job = job; Algorithm = algorithm; MessagingHandler = messagingHandler; _fitnessScore = new FitnessScoreManager(); _insights = new List <Insight>(); _securityValuesProvider = new AlgorithmSecurityValuesProvider(algorithm); InsightManager = CreateInsightManager(); var statistics = new StatisticsInsightManagerExtension(algorithm); RuntimeStatistics = statistics.Statistics; InsightManager.AddExtension(statistics); AddInsightManagerCustomExtensions(statistics); // when insight is generated, take snapshot of securities and place in queue for insight manager to process on alpha thread algorithm.InsightsGenerated += (algo, collection) => { lock (_insights) { _insights.AddRange(collection.Insights); } }; }
/// <summary> /// Initializes this alpha handler to accept insights from the specified algorithm /// </summary> /// <param name="job">The algorithm job</param> /// <param name="algorithm">The algorithm instance</param> /// <param name="messagingHandler">Handler used for sending insights</param> /// <param name="api">Api instance</param> public virtual void Initialize(AlgorithmNodePacket job, IAlgorithm algorithm, IMessagingHandler messagingHandler, IApi api) { // initializing these properties just in case, doens't hurt to have them populated Job = job; Algorithm = algorithm; MessagingHandler = messagingHandler; _fitnessScore = new FitnessScoreManager(); _securityValuesProvider = new AlgorithmSecurityValuesProvider(algorithm); InsightManager = CreateInsightManager(); // send scored insights to messaging handler InsightManager.AddExtension(CreateAlphaResultPacketSender()); var statistics = new StatisticsInsightManagerExtension(algorithm); RuntimeStatistics = statistics.Statistics; InsightManager.AddExtension(statistics); _charting = new ChartingInsightManagerExtension(algorithm, statistics); InsightManager.AddExtension(_charting); // when insight is generated, take snapshot of securities and place in queue for insight manager to process on alpha thread algorithm.InsightsGenerated += (algo, collection) => InsightManager.Step(collection.DateTimeUtc, CreateSecurityValuesSnapshot(), collection); }
public void ValueIsCalculatedCorrectly() { var algorithm = new QCAlgorithm(); var initialDate = new DateTime(2018, 1, 1); algorithm.SetStartDate(initialDate); algorithm.SubscriptionManager.SetDataManager(new DataManagerStub(algorithm)); algorithm.AddEquity("SPY"); var fitnessScore = new FitnessScoreManager(); fitnessScore.Initialize(algorithm); algorithm.SetDateTime(initialDate.AddDays(1)); IncreaseCashAmount(algorithm, 0.05); IncreaseSalesVolumeAmount(algorithm); fitnessScore.UpdateScores(); var score = fitnessScore.FitnessScore; // FitnessScore: 1 * (5 + 5) Assert.AreEqual(1m, score); algorithm.SetDateTime(initialDate.AddDays(1)); IncreaseCashAmount(algorithm, -0.20); fitnessScore.UpdateScores(); algorithm.SetDateTime(initialDate.AddDays(1)); IncreaseCashAmount(algorithm, -0.20); // FitnessScore: 0.333 * (-3.299 + -5) fitnessScore.UpdateScores(); score = fitnessScore.FitnessScore; Assert.AreEqual(0.028m, score); }
public void ZeroStartingPortfolioValueAlgorithm() { var algorithm = new QCAlgorithm(); var initialDate = new DateTime(2018, 1, 1); algorithm.SetStartDate(initialDate); algorithm.SetCash(0); var fitnessScore = new FitnessScoreManager(); fitnessScore.Initialize(algorithm); decimal score = 0; for (var i = 0; i < 10; i++) { algorithm.SetDateTime(initialDate.AddDays(i)); fitnessScore.UpdateScores(); score = fitnessScore.FitnessScore; } Assert.AreEqual(0m, score); }
/// <summary> /// Initializes this alpha handler to accept insights from the specified algorithm /// </summary> /// <param name="job">The algorithm job</param> /// <param name="algorithm">The algorithm instance</param> /// <param name="messagingHandler">Handler used for sending insights</param> /// <param name="api">Api instance</param> /// <param name="transactionHandler">Algorithms transaction handler</param> public virtual void Initialize(AlgorithmNodePacket job, IAlgorithm algorithm, IMessagingHandler messagingHandler, IApi api, ITransactionHandler transactionHandler) { // initializing these properties just in case, doesn't hurt to have them populated Job = job; Algorithm = algorithm; MessagingHandler = messagingHandler; _fitnessScore = new FitnessScoreManager(); _insights = new List <Insight>(); _securityValuesProvider = new AlgorithmSecurityValuesProvider(algorithm); InsightManager = CreateInsightManager(); var statistics = new StatisticsInsightManagerExtension(algorithm); RuntimeStatistics = statistics.Statistics; InsightManager.AddExtension(statistics); AddInsightManagerCustomExtensions(statistics); var baseDirectory = Config.Get("results-destination-folder", Directory.GetCurrentDirectory()); var directory = Path.Combine(baseDirectory, AlgorithmId); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } _alphaResultsPath = Path.Combine(directory, "alpha-results.json"); // when insight is generated, take snapshot of securities and place in queue for insight manager to process on alpha thread algorithm.InsightsGenerated += (algo, collection) => { lock (_insights) { _insights.AddRange(collection.Insights); } }; }
public void ExtremePerformanceAlgorithm(double returnFactor) { var algorithm = new QCAlgorithm(); var initialDate = new DateTime(2018, 1, 1); algorithm.SetStartDate(initialDate); algorithm.SubscriptionManager.SetDataManager(new DataManagerStub(algorithm)); algorithm.AddEquity("SPY"); var fitnessScore = new FitnessScoreManager(); fitnessScore.Initialize(algorithm); decimal score = 0; for (var i = 0; i < 10; i++) { algorithm.SetDateTime(initialDate.AddDays(i)); fitnessScore.UpdateScores(); score = fitnessScore.FitnessScore; IncreaseCashAmount(algorithm, returnFactor); IncreaseSalesVolumeAmount(algorithm); } Assert.AreEqual(returnFactor < 1 ? 0.174m : 1m, score); }
public void SigmoidalScaleWorks(decimal input, decimal expectedResult) { var result = FitnessScoreManager.SigmoidalScale(input); Assert.AreEqual(expectedResult, Math.Round(result, 2)); }