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

简介:

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

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

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

在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
{
    /// <summary>
    /// Realtime event object for holding information on the event time and callback.
    /// 实时时间对象:保存时间相关的的事件信息和回调
    /// </summary>
    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
        *********************************************************/
        /// <summary>
        /// Flag indicating the event has been triggered
        /// 事件是否已经触发了的标志
        /// </summary>
        public bool Triggered
        {
            get { return _triggered; }
        }

        /******************************************************** 
        * CONSTRUCTOR METHODS
        *********************************************************/
        /// <summary>
        /// Setup new event to fire at a specific time. Managed by a RealTimeHandler thread.
        /// 设置一个新的事件,在特定的时间触发,由RealTimeHandler线程管理
        /// </summary>
        /// <param name="triggerTime">Time of day to trigger this event</param>
        /// <param name="callback">Action to run when the time passes.</param>
        /// <param name="logging">Enable logging the realtime events</param>
        /// <seealso cref="IRealTimeHandler"/>
        public RealTimeEvent(DateTime triggerTime, Action callback, bool logging = false)
        {
            _triggered = false;
            _triggerTime = triggerTime;
            _callback = callback;
            _logging = logging;
        }

        /******************************************************** 
        * CLASS METHODS:
        *********************************************************/
        /// <summary>
        /// Scan this event to see if this real time event has been triggered.
        /// 扫描,检查该事件是否已经被触发  
        /// </summary>
        /// <param name="time">Current real or simulation time 当前时间(真实交易或者模拟交易的时间)</param>
        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);
                }
            }
        }

        /// <summary>
        /// Reset the triggered flag.
        /// </summary>
        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
{
    /// <summary>
    /// Real time event handler, trigger functions at regular or pretimed intervals
    /// 实时事件处理,周期性触发回调方法,或者在设定的时间触发
    /// </summary>
    public interface IRealTimeHandler
    {
        /******************************************************** 
        * INTERFACE PROPERTIES
        *********************************************************/
        /// <summary>
        /// The real time handlers internal record of current time used to scan the events.
        /// 现在的时间
        /// </summary>
        DateTime Time 
        { 
            get;
        }

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

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

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

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

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

        /// <summary>
        /// Add a new event to the processing list
        /// 增加事件
        /// </summary>
        /// <param name="newEvent">Event information</param>
        void AddEvent(RealTimeEvent newEvent);
        
        /// <summary>
        /// Trigger a scan of the events.
        /// 触发事件
        /// </summary>
        void ScanEvents();

        /// <summary>
        /// Reset all the event flags for a new day.
        /// 重置所有事件标志,为新的一天
        /// </summary>
        /// <remarks>Realtime events are setup as a timespan hours since </remarks>
        void ResetEvents();

        /// <summary>
        /// Clear all the events in the list.
        /// </summary>
        void ClearEvents();

        /// <summary>
        /// Set the current time for the event scanner (so we can use same code for backtesting and live events)
        /// 设置现在的时间
        /// </summary>
        /// <param name="time">Current real or backtest time.</param>
        void SetTime(DateTime time);

        /// <summary>
        /// Trigger and exit signal to terminate real time event scanner.
        /// 退出该线程
        /// </summary>
        void Exit();
    }
}



相关文章
|
5月前
|
机器人 程序员 C++
Scratch3.0——助力新进程序员理解程序(四、事件)
Scratch3.0——助力新进程序员理解程序(四、事件)
65 0
|
5月前
|
BI
SAP ABAP 显式增强技术之 New BAdI 的技术原理介绍试读版
SAP ABAP 显式增强技术之 New BAdI 的技术原理介绍试读版
|
2月前
|
前端开发 Java UED
JSF 面向组件开发究竟藏着何种奥秘?带你探寻可复用 UI 组件设计的神秘之路
【8月更文挑战第31天】在现代软件开发中,高效与可维护性至关重要。JavaServer Faces(JSF)框架通过其面向组件的开发模式,提供了构建复杂用户界面的强大工具,特别适用于设计可复用的 UI 组件。通过合理设计组件的功能与外观,可以显著提高开发效率并降低维护成本。本文以一个具体的 `MessageComponent` 示例展示了如何创建可复用的 JSF 组件,并介绍了如何在 JSF 页面中使用这些组件。结合其他技术如 PrimeFaces 和 Bootstrap,可以进一步丰富组件库,提升用户体验。
44 0
|
XML 移动开发 前端开发
这16种原生函数和属性的区别,你真的知道吗? 精心收集,高级前端必备知识,快快打包带走
原生内置了很多API, 作用类似,却也有差千差万别,了解其区别,掌握前端基础,是修炼上层,成为前端高级工程师的必备知识,让我们一起来分类归纳,一起成长吧。
189 0
这16种原生函数和属性的区别,你真的知道吗? 精心收集,高级前端必备知识,快快打包带走
|
存储 缓存 API
iOS网络编程之一——iOS网络框架简介
iOS网络编程之一——iOS网络框架简介
242 0
iOS网络编程之一——iOS网络框架简介
|
SQL 数据库 Windows
艾伟:基于.NET平台的Windows编程实战(五)—— 问卷管理功能的实现
本系列文章导航 基于.NET平台的Windows编程实战(一)——前言 基于.NET平台的Windows编程实战(二)—— 需求分析与数据库设计 基于.NET平台的Windows编程实战(四)—— 数据库操作类的编写 基于.
726 0
|
SQL 数据库 Windows
艾伟_转载:基于.NET平台的Windows编程实战(五)—— 问卷管理功能的实现
本系列文章导航 基于.NET平台的Windows编程实战(一)——前言 基于.NET平台的Windows编程实战(二)—— 需求分析与数据库设计 基于.NET平台的Windows编程实战(四)—— 数据库操作类的编写 基于.
1088 0
|
程序员 开发者
程序员也是弱势群体?——从WePhone开发者事件说起
作为一名不爱凑热闹的人,今天一直在持续关注一个热点事件——WePhone开发者自杀,即使前几天热议的孕妇跳楼新闻我都不太关注,但是这个事件却让我深深的震撼,花了几个小时在微博上搜索了相关的信息,去了解事情的始末,也去看了网络上对此事件的一些评判,太多的杂音让个人的产生一些想法和大家一起探讨。
896 0
下一篇
无影云桌面