程序员的量化交易之路(38)--Lean之实时事件处理接口IRealTimeHandler和RealTimeEvent6

简介:

转载需注明出处:http://blog.csdn.net/minimicall?viewmode=contents

这节开始我们要开始说明另外一个模块:实时事件处理模块。

这个模块的工作是什么呢。它就是用来设置一些在特定时间需要执行的任务。比如,每天开盘的时候,你可以做一个什么动作,比如每天收盘的时候你也可以做一个动作。当然还有更为广泛的运用。

在Lean中,是开启一个单独的线程来处理这种定时任务的。

实时事件:RealTimeEvent

实时事件处理接口:IRealTimeHandler

下面我们通过代码来说明,说明都在注释里面。废话少说:

/*
 * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
 * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
*/

using System;
using QuantConnect.Logging;

namespace QuantConnect.Lean.Engine.RealTime
{
    /// 
    /// Realtime event object for holding information on the event time and callback.
    /// 实时时间对象:保存时间相关的的事件信息和回调
    /// 
    public class RealTimeEvent
    {
        /******************************************************** 
        * CLASS VARIABLES
        *********************************************************/
        // Trigger Timing
        private readonly DateTime _triggerTime;//触发时间
        private readonly Action _callback;//回调方法(没有参数、返回值)
        private readonly bool _logging;//是否写日志

        // Trigger Action
        private bool _triggered;//是否已经触发

        /******************************************************** 
        * CLASS PROPERTIES
        *********************************************************/
        /// 
        /// Flag indicating the event has been triggered
        /// 事件是否已经触发了的标志
        /// 
        public bool Triggered
        {
            get { return _triggered; }
        }

        /******************************************************** 
        * CONSTRUCTOR METHODS
        *********************************************************/
        /// 
        /// Setup new event to fire at a specific time. Managed by a RealTimeHandler thread.
        /// 设置一个新的事件,在特定的时间触发,由RealTimeHandler线程管理
        /// 
        /// 
Time of day to trigger this event /// Action to run when the time passes. /// Enable logging the realtime events /// public RealTimeEvent(DateTime triggerTime, Action callback, bool logging = false) { _triggered = false; _triggerTime = triggerTime; _callback = callback; _logging = logging; } /******************************************************** * CLASS METHODS: *********************************************************/ /// /// Scan this event to see if this real time event has been triggered. /// 扫描,检查该事件是否已经被触发 /// /// Current real or simulation time 当前时间(真实交易或者模拟交易的时间) public void Scan(DateTime time) { if (_triggered) {//如果已经触发过,就直接返回,不需要返回了 return; } //When the time passes the trigger time, trigger the event. //如果当前时间比设定的触发时间更晚了,那么就需要触发该事件 if (time > _triggerTime) { _triggered = true;//标志为已经触发 try { if (_logging) { Log.Trace("RealTimeEvent.Scan(): Eventhandler Called: " + time.ToString("u")); } _callback();//调用回调函数 } catch (Exception err) { Log.Error("RealTimeEvent.Scan(): Error in callback: " + err.Message); } } } /// /// Reset the triggered flag. /// public void Reset() { _triggered = false; } } }


实时事件处理接口:

/*
 * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
 * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
*/

using System;
using System.Collections.Generic;
using QuantConnect.Packets;

namespace QuantConnect.Lean.Engine.RealTime
{
    /// 
    /// Real time event handler, trigger functions at regular or pretimed intervals
    /// 实时事件处理,周期性触发回调方法,或者在设定的时间触发
    /// 
    public interface IRealTimeHandler
    {
        /******************************************************** 
        * INTERFACE PROPERTIES
        *********************************************************/
        /// 
        /// The real time handlers internal record of current time used to scan the events.
        /// 现在的时间
        /// 
        DateTime Time 
        { 
            get;
        }

        /// 
        /// List of events we're monitoring.
        /// 我们监控的事件列表 
        /// 
        List Events
        {
            get;
        }

        /// 
        /// Thread status flag.
        /// 本线程是否活跃
        ///  
        bool IsActive
        {
            get;
        }

        /// 
        /// Data for the Market Open Hours Today
        /// 市场交易时间
        /// 
        Dictionary MarketToday
        {
            get;
        }

        /******************************************************** 
        * INTERFACE METHODS
        *********************************************************/
        /// 
        /// Main entry point to scan and trigger the realtime events.
        /// 线程入口
        /// 
        void Run();

        /// 
        /// Given a list of events, set it up for this day.
        /// 
        void SetupEvents(DateTime day);

        /// 
        /// Add a new event to the processing list
        /// 增加事件
        /// 
        /// 
Event information void AddEvent(RealTimeEvent newEvent); /// /// Trigger a scan of the events. /// 触发事件 /// void ScanEvents(); /// /// Reset all the event flags for a new day. /// 重置所有事件标志,为新的一天 /// /// Realtime events are setup as a timespan hours since void ResetEvents(); /// /// Clear all the events in the list. /// void ClearEvents(); /// /// Set the current time for the event scanner (so we can use same code for backtesting and live events) /// 设置现在的时间 /// /// Current real or backtest time. void SetTime(DateTime time); /// /// Trigger and exit signal to terminate real time event scanner. /// 退出该线程 /// void Exit(); } }


相关文章
|
Windows
『Consul』Consul数据持久化配置并且注册为Windows服务
📣读完这篇文章里你能收获到 - Consul数据持久化配置并且注册为Windows服务
1916 0
『Consul』Consul数据持久化配置并且注册为Windows服务
|
存储 文件存储 对象存储
S3存储服务间数据同步工具Rclone迁移教程
目前大多项目我们都会使用各种存储服务,例如oss、cos、minio等。当然,因各种原因,可能需要在不同存储服务间进行数据迁移工作,所以今天就给大家介绍一个比较通用的数据迁移工具Rclone。
S3存储服务间数据同步工具Rclone迁移教程
|
7月前
|
人工智能 弹性计算 安全
2026年阿里云部署OpenClaw(Clawdbot)一键接入Telegram教程
2026年,OpenClaw(前身为Clawdbot、Moltbot)凭借开源轻量化架构、强大的自动化任务执行能力及跨平台适配特性,成为个人、跨境团队及中小企业搭建专属AI智能代理的首选工具。其核心优势在于无需复杂开发,即可快速对接全球主流即时通信平台,实现“聊天式指挥AI干活”,而Telegram作为全球流行的即时通讯工具,凭借多终端同步、无广告干扰、消息加密传输、跨境通信稳定的特点,成为OpenClaw跨平台对接的核心场景之一,尤其适配海外团队协作、跨境社群运营、全球远程办公等需求。
1319 2
|
传感器 数据挖掘
为你的数据选择合适的分布:8个实用的概率分布应用场景和选择指南
面对真实数据不知该用哪种分布?本文精炼总结8个实战必备概率分布,涵盖使用场景、避坑指南与代码实现。从二元事件到计数、等待时间、概率建模,再到小样本处理,教你快速选择并验证合适分布,用对模型显著提升分析准确性。
869 10
|
存储 NoSQL 区块链
开源:LMDB 操作工具:lmcmd
本文介绍了 LMDB(一种高效的键值存储数据库)和基于 Python 开发的命令行工具 `lmcmd`。由于 LMDB 使用二进制文件存储,管理和调试不便,因此开发了 `lmcmd`,提供了类似 Redis 的命令行操作界面,支持数据库操作、数据导入导出和查找等功能。文章涵盖了 `lmcmd` 的安装、连接数据库和常用命令(如 `set`、`get`、`export` 等)示例。最后强调了开源工具的价值,鼓励用户反馈和改进。
753 1
|
弹性计算 网络协议 安全
下一代互联网IPv6规模部署和应用
本文介绍了IPv6在云计算场景下的规模部署与应用创新,强调其作为互联网演进的必然趋势及网络强国建设的基础支撑作用。文章从企业上云部署IPv6、云上IPv6网络底座构建、双栈方案全景图、专有云IPv6改造、政务云和金融客户的实践案例等方面展开讨论,详细阐述了IPv6在不同场景下的技术要求和服务能力。最后展望了IPv6与AI结合的未来发展方向,旨在推动IPv6的全面应用和技术创新。
|
存储 监控 安全
公司电脑监控软件深度对比:软件功能、易用性和成本效益分析
随着企业对信息安全和员工效率管理的日益重视,公司电脑监控软件逐渐成为企业管理工具箱中的必备品。本文将深入探讨三款行业领先的监控软件——WorkWin、OsMonitor和ManageEngine Desktop Central,分析它们在功能、易用性和成本效益等方面的优劣,为企业管理提供参考。
763 1
|
机器学习/深度学习 分布式计算 大数据
一文读懂Apache Beam:统一的大数据处理模型与工具
【4月更文挑战第8天】Apache Beam是开源的统一大数据处理模型,提供抽象化编程模型,支持批处理和流处理。它提倡"一次编写,到处运行",可在多种引擎(如Spark、Dataflow、Flink)上运行。Beam的核心特性包括抽象化概念(PCollection、PTransform和PipelineRunner)、灵活性(支持多种数据源和转换)和高效执行。它广泛应用在ETL、实时流处理、机器学习和大数据仓库场景,助力开发者轻松应对数据处理挑战。
3577 1
|
缓存 测试技术 开发工具
内存泄漏检测工具Valgrind:C++代码问题检测的利器(二)
内存泄漏检测工具Valgrind:C++代码问题检测的利器
957 0

热门文章

最新文章