程序员的量化交易之路(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();
    }
}
时间: 2024-10-12 10:11:38

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

程序员的量化交易之路(1)----规划开篇

其实,一直对量化交易有一定的理解和情节.早在中大读研究生的时候实验室师兄,已经去了中国平安核心投资团队,做高频交易研究的国源师兄的影响,就开始对金融世界产生了浓厚的兴趣.看了丁磊编著的<量化投资--策略与技术>和艾琳.奥尔德里奇的<高频交易>,反复的看,但是都入不了味,现在回过头来想,一个连股都不炒的人怎么可能入味呢.对一些金融的基本概念都不懂. 2013年7月出社会工作后,在10月份确立目标.需要炒股,而且需要一个深入的理解金融的世界.所以确定去考一个证券从业考试,选了证券基础和

程序员的量化交易之路(2)----Esper文档学习之技术概览(1)

转载请注明出处:http://blog.csdn.net/minimicall/ 在接下来的20个工作日中,我将坚持翻译或者略翻译Esper的官方文档. 为什么需要学习Esper,因为我们需要理解复合事件处理 Complex Event Processing (CEP).在量化交易系统中,CEP是必不可少的.它负责处理海量的实时事件. 关于CEP更多知识,大家可以翻阅网络相关资料.我这里集中在学习开源的CEP系统,Esper.. 今天开始第一篇:技术概览. 1. CEP和事件序列分析 Esper

程序员的量化交易之路(13)--Cointrader类图(1)

转载须注明出处:http://blog.csdn.net/minimicall?viewmode=contents, htpp://cloudtrader.top 今天开始正式切入到Cointrader的源码分析学习中,其主页为:https://github.com/timolson/cointrader. 它是基于Esper的一个比特币云交易托管平台.和我想做的事情比较相近.而且虽然现在没什么功能,但代码量相对少,对于学习非常好. 下面是它的一个类图.: 后面我们会根据这个类图一步步的剖析整个

程序员的量化交易之路(19)--Cointrader之Bar实体(7)

转载需注明出处:http://blog.csdn.net/minimicall,http://cloudtrader.top 1. 代码 package org.cryptocoinpartners.schema; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; public class Bar extends Event { private long times

程序员的量化交易之路(20)--Cointrader之Assert实体(8)

转载需说明出处:http://blog.csdn.net/minimicall, http://cloudtrade.top 任何可交易的都可以称之为Assert,资产.其类代码如下: package org.cryptocoinpartners.schema; import javax.persistence.Basic; import javax.persistence.Cacheable; import javax.persistence.Entity; import javax.pers

程序员的量化交易之路(24)--Cointrader之RemoteEvent远程事件实体(11)

转载需注明出处:http://blog.csdn.net/minimicall,http://cloudtrader.top/ 在量化交易系统中,有些事件是远端传来的,比如股票的价格数据等.所以,在这一节我们定义了一个远端事件实体. 它是一个基类,并不单独生成数据表.具体代码如下: package org.cryptocoinpartners.schema; import javax.annotation.Nullable; import javax.persistence.Basic; imp

程序员的量化交易之路(36)--Lean之数据读取SubscriptionDataReader4

转载需注明出处:http://blog.csdn.net/minimicall?viewmode=contents,http://cloudtrade.top 数据读取需要定义一个读者.直接见下面代码: namespace QuantConnect.Lean.Engine.DataFeeds { /******************************************************** * CLASS DEFINITIONS ***********************

程序员的量化交易之路(29)--Cointrader之Tick实体(16)

转载需注明出处:http://blog.csdn.net/minimicall,http://cloudtrade.top Tick:什么是Tick,在交易平台中非常常见,其实就 单笔交易时某只证券的基本数据. 我们通过代码来学习吧: package org.cryptocoinpartners.schema; import javax.annotation.Nullable; import javax.persistence.Entity; import javax.persistence.M

程序员的量化交易之路(35)--Lean之DataFeed数据槽3

转载需注明出处:http://blog.csdn.net/minimicall,http://cloudtrade.top/ Lean引擎的模块划分非常的规范.其中DataFeed是数据槽,就是供应数据的模块. 1. IDataFeed 接口 模块的接口为: namespace QuantConnect.Lean.Engine.DataFeeds { /// <summary> /// Datafeed interface for creating custom datafeed source