Ejemplo n.º 1
0
 def __init__(self):
     self.event_engine = EventEngine()
     self.feed_list = []
     self.portfolio_list = []
     self.strategy_list = []
     self.order_list = []
     self.context = {}
     self.dateList = []
     self.clock = ClockEngine(EventEngine(), tradedate)
     self.Market = MarketEngine(EventEngine())
     self.strategy = strategy(EventEngine())
Ejemplo n.º 2
0
    def __init__(self):
        """初始化事件引擎"""
        # 事件队列
        self.queue = Queue()

        # 事件引擎开关
        self.__active = False

        # 事件引擎处理线程
        self.__thread = Thread(target=self.__run, name="EventEngine.__thread")

        # 事件字典,key 为时间, value 为对应监听事件函数的列表
        self.__handlers = defaultdict(list)
        self.context = {}
        self.clock = ClockEvent("clock", self.context["date"])

        self.marketevent = MarketEvent()

        self.portfolio = None
        self.MarketEngine = MarketEngine(EventEngine(),
                                         ClockEngine(EventEngine()))
        self.strategy_list = []
Ejemplo n.º 3
0
# coding: utf-8
from WindPy import w

from EventEngine import *


import time
w.start()

from tools.getTushareData import *
w.start()
tradedate=w.tdays("2018-01-01", "2018-04-07", "").Data[0]
date=w.tdays("2018-01-01", "2018-04-01", "Days=Alldays").Data[0]
clockevent=ClockEvent("Clock",tradedate)
clock=ClockEngine(EventEngine(),clockevent)

from tools.getTushareData import *

def clockhandel(Event):
    date=Event.data.strftime("%Y-%m-%d")
    bar=tusharebar(date).getOneBar("600008.SH")
    print("传递bar")
    print(bar)

Event=Event( 'clock_time',date[1])
clockhandel(aa)


clock.register(clockhandel)
clock.start()
Ejemplo n.º 4
0
class go(object):
    def __init__(self):
        self.event_engine = EventEngine()
        self.feed_list = []
        self.portfolio_list = []
        self.strategy_list = []
        self.order_list = []
        self.context = {}
        self.dateList = []
        self.clock = ClockEngine(EventEngine(), tradedate)
        self.Market = MarketEngine(EventEngine())
        self.strategy = strategy(EventEngine())

    def strategy_listen_event(self, strategy, _type="listen"):
        """
        所有策略要监听的事件都绑定到这里
        :param strategy: Strategy()
        :param _type: "listen" OR "unlisten"
        :return:
        """
        func = {
            "listen": self.event_engine.register,
            "unlisten": self.event_engine.unregister,
        }.get(_type)

        # 行情引擎的事件
        for quotation_engine in self.quotation_engines:
            func(quotation_engine.EventType, strategy.run)

        # 时钟事件
        func(ClockEngine.EventType, strategy.clock)

    def run(self):
        # run_once function
        self.initialization()
        # Declare the components with respective parameters

        self.event_engine.start()

        while True:
            eventengine.put(event)

        while True:
            try:

                event = self.event_engine.get(False)
            except queue.Empty:
                break

            else:
                if event.type is 'Market':
                    strategy.calculate_signals(event)
                    port.update_timeindex(event)

                elif event.type is 'Signal':
                    port.update_signal(event)

                elif event.type is 'Order':
                    broker.execute_order(event)

                elif event.type is 'Fill':
                    port.update_fill(event)

    def initialization(self):
        self.portfolio = portfolio()

    def setContext(self, context={}):

        ##__________________________________________

        context["capital"] = 10000000  # 回测的初始资金
        context["securities"] = ['000001.SZ']  # 回测标的
        context["start_date"] = "2018-1-1"  # 回测开始时间
        context["end_date"] = "2018-6-1"  # 回测结束时间
        context["period"] = 'd'  # 'd' 代表日, 'm'代表分钟   表示行情数据的频率
        context["pos"] = 1  # 这里可以传一些自己定义的变量  这个表示的是组合1
        context["benchmark"] = '000906.SH'
        ##___________________________________________
        self.context = context

################### In Loop #######################

    def _check_pending_order(self):
        for f in self.feed_list:  # 判断属于哪个feed_list
            self.fill.check_trade_list(f)
            self.fill.check_order_list(f)

    def _pass_to_market(self, marketevent):
        """因为Strategy模块用到的是marketevent,所以通过marketevent传进去"""
        m = marketevent
        m.fill = self.fill
        self.portfolio.fill = self.fill
        self.broker.fill = self.fill
        m.target = self.target

    def _check_finish_backtest(self, feed_list):
        # if finish, sum(backtest) = 0 + 0 + 0 = 0 -> False
        backtest = [i.continue_backtest for i in feed_list]
        return not sum(backtest)
Ejemplo n.º 5
0
        for _ in range(int(self.PushInterval) + 1):
            time.sleep(1)


if __name__ == '__main__':
    from WindPy import w
    from EventEngine import Event, EventEngine, ClockEvent
    from ClockEngine import ClockEngine
    from FeedEngine import MarketEngine
    w.start()
    tradedate = w.tdays("2018-01-01", "2018-04-07", "").Data[0]
    clocks = ClockEvent("clock", tradedate)

    def clockhandel(Event):
        date = Event.data.strftime("%Y-%m-%d")
        bar = tusharebar(date).getOneBar("600008.SH")
        print("传递bar")
        print(bar)

    M = MarketEngine(EventEngine(), ClockEngine(EventEngine(), clocks))
    import queue

    events = queue.Queue()

    class test:
        def __init__(self):
            pass

        def aa(self):
            events.put(1)