Ejemplo n.º 1
0
 def SendDataEvent(self, type):
     """
     将有新数据产生这一时间发送至事件处理引擎
     :return:
     """
     DataEvent = Event(type=type)
     data = {}
     data['time'] = self.__time
     data['open'] = self.__open
     data['high'] = self.__high
     data['low'] = self.__low
     data['volume'] = self.__volume
     DataEvent.dict['data'] = data
     DataEvent.dict['magic'] = self.__magic
     self.__eventEngine.SendEvent(DataEvent)
Ejemplo n.º 2
0
    def order_begin(self, event):

        print(
            '===================Trading_Event_Driven: order begin=================='
        )
        event = Event('order_begin')
        self.eventmanager.SendEvent(event)
Ejemplo n.º 3
0
def mytest():
    data_dict = {'contract_list': ['j1901', 'j1905']}
    data_dict = {
        'id': 'j',
        'instrument': 'j1901',
        'periods': '30min',
        'time_begin': '2018-03-01 21:00:00',
        'time_end': '2018-08-31 23:29:00'
    }
    stra_dict = {
        'name': 'jma',
        'slowLength': 21,
        'slowphase': 101,
        'fastLength': 10,
        'fastphase': 101
    }
    orde_dict = {}
    trad_dict = {}
    posi_dict = {}

    data = trading_data(data_dict)
    jma = strategy_jma(stra_dict)
    order = order_jma(posi_dict)

    eventmanger = EventManager()
    eventmanger.AddEventLister('start', data.data_begin)
    eventmanger.AddEventLister('data_begin', jma.strategy_begin)
    eventmanger.AddEventLister('strategy_begin', order.order_begin)
    eventmanger.AddEventLister('order_begin', data.data_receive)
    """
    eventmanger.AddEventLister('td_data_public', jma.strategy_receive)
    eventmanger.AddEventLister('td_data_receive', data.data_public)
    eventmanger.AddEventLister('td_strategy_public', order.trading_order_receive)
    eventmanger.AddEventLister('td_strategy_receive', jma.trading_strategy_public)
    eventmanger.AddEventLister('td_order_public', data.trading_data_receive)
    eventmanger.AddEventLister('td_order_receive', order.trading_order_public)
    eventmanger.AddEventLister('td_end', order.trading_order_end)
    """
    eventmanger.Start()

    data.data_add_eventmanager(eventmanger)
    jma.strategy_add_eventmanager(eventmanger)
    order.order_add_eventmanager(eventmanger)

    event = Event('start')
    event.dict['instrument'] = ['j1901']
    data.data_init(event)
Ejemplo n.º 4
0
 def strategy_public(self, event):
     event = Event('strategy_public')
     event.dict['num'] = self.event['num']
     event.dict['sum'] = self.event['sum']
     event.dict['marketdata'] = self.event['marketdata']
     event.dict['order'] = self.event['order']
     event.dict['position'] = self.event['position']
     event.dict['account'] = self.event['account']
     print('%s of %s strategy 发送........' %
           (str(self.event['num']), str(self.event['sum'])))
     self.eventmanager.SendEvent(event)
Ejemplo n.º 5
0
 def data_receive(self, event):
     ## 收到backtest事件之后,要继续加第几根K线
     self.event['num'] = event.dict['num'] + 1
     self.event['sum'] = event.dict['sum']
     self.event['position'] = event.dict['position']
     self.event['account'] = event.dict['account']
     print('%s of %s data 收到........' %
           (str(self.event['num']), str(self.event['sum'])))
     event = Event('data_receive')
     # data_receive 发送至 datapublic
     self.eventmanager.SendEvent(event)
Ejemplo n.º 6
0
 def data_begin(self, event):
     # 读取本地数据
     data = self.minute_select(self.data['instrument'])
     data = pd.DataFrame(
         data, columns=['时间', '开盘价', '最高价', '最低价', '收盘价', '成交量', '持仓量'])
     data.set_index(data['时间'], inplace=True)
     # 时间筛选
     data = data[(data.index >= self.data['time_begin'])]
     data = data[(data.index <= self.data['time_end'])]
     # k线合成
     data = self.minute_combine(data, self.data['periods'])
     # 本地数据准备
     self.data['data'] = data
     print(
         '===================Trading_Event_Driven: data begin==================='
     )
     # 本地数据准备好了之后,就开始进行实盘事件驱动
     event = Event('data_begin')
     ly_ctpmd(contract=event.dict['instrument'])
     event.dict['data'] = data
     self.eventmanager.SendEvent(event)
Ejemplo n.º 7
0
 def strategy_receive(self, event):
     self.event['num'] = event.dict['num']
     self.event['sum'] = event.dict['sum']
     self.event['marketdata'] = event.dict['marketdata']
     self.event['position'] = event.dict['position']
     self.event['account'] = event.dict['account']
     print('%s of %s strategy 收到........' %
           (str(self.event['num']), str(self.event['sum'])))
     self.strategy_to_order()
     event = Event('strategy_receive')
     # strategy_receive event绑定 strategy_public
     self.eventmanager.SendEvent(event)
Ejemplo n.º 8
0
 def backtest_public(self, event):
     event = Event('backtest_public')
     event.dict['num'] = self.event['num']
     event.dict['sum'] = self.event['sum']
     event.dict['position'] = self.event['position']
     event.dict['account'] = self.event['account']
     if self.event['num'] != self.event['sum'] - 1:
         print('%s of %s backtest 发送........' %
               (str(self.event['num']), str(self.event['sum'])))
         self.eventmanager.SendEvent(event)
     else:
         event = Event('end')
         print('This is end. Hold your breathe and count to ten.')
         self.eventmanager.SendEvent(event)
Ejemplo n.º 9
0
    def backtest_receive(self, event):
        self.event['num'] = event.dict['num']
        self.event['sum'] = event.dict['sum']
        self.event['marketdata'] = event.dict['marketdata']
        self.event['order'] = event.dict['order']

        self.process_control(event.dict['marketdata'], event.dict['order'])
        self.event['position'] = self.backtest['position']
        self.event['account'] = self.backtest['account']
        print('%s of %s backtest 收到........' %
              (str(self.event['num']), str(self.event['sum'])))
        event = Event('backtest_receive')
        self.eventmanager.SendEvent(event)
Ejemplo n.º 10
0
 def data_begin(self, event):
     event = Event('data_public')
     event.dict['num'] = self.event['num']
     event.dict['sum'] = self.event['sum']
     event.dict['position'] = None
     event.dict['account'] = None
     md = pd.DataFrame(self.data_source['data'].iloc[[0], :])
     md.index = [0]
     event.dict['marketdata'] = md
     print('%s of %s data 发送........' %
           (str(self.event['num']), str(self.event['sum'])))
     # data_public 输送至strategy_receive
     self.eventmanager.SendEvent(event)
Ejemplo n.º 11
0
 def strategy_begin(self, event):
     data = event.dict['data']
     # 将数据转成所需要的类型
     data = data['收盘价'].tolist()
     # 将数据转换成策略所需要的类型
     jmafast = jmavalue(self.event['slowLength'], self.event['slowphase'])
     jmaslow = jmavalue(self.event['fastLength'], self.event['fastphase'])
     for i in range(len(data)):
         d = data[i]
         jmafast.jma_calculate(d)
         jmaslow.jma_calculate(d)
     print(
         '===================Trading_Event_Driven: strategy begin==============='
     )
     event = Event('strategy_begin')
     self.eventmanager.SendEvent(event)
Ejemplo n.º 12
0
 def data_public(self, event):
     event = Event('data_public')
     event.dict['num'] = self.event['num']
     event.dict['sum'] = self.event['sum']
     #####################################
     md = pd.DataFrame(
         self.data_source['data'].iloc[[self.event['num']], :])
     md.index = [0]
     #####多个品种同时推送,market就应该改为dict
     event.dict['marketdata'] = md
     event.dict['position'] = self.event['position']
     event.dict['account'] = self.event['account']
     print('%s of %s data 发送........' %
           (str(self.event['num']), str(self.event['sum'])))
     # data_public 输送至strategy_receive
     self.eventmanager.SendEvent(event)
Ejemplo n.º 13
0
 def SendEvent(self, type=None, orderinfo=None):
     OrderEvent = Event(type=type)
     OrderEvent.dict = orderinfo
     self.__eventEngine.SendEvent(OrderEvent)
Ejemplo n.º 14
0
 def order_receive(self, event):
     event = Event('strategy_receive')
     self.eventmanager.SendEvent(event)
Ejemplo n.º 15
0
 def data_init(self, event):
     event = Event('start')
     self.eventmanager.SendEvent(event)
Ejemplo n.º 16
0
 def init(self):
     event = Event('begin')
     print('EventDriven_Backtest开始!!!!!')
     # begin  Addlistener data_begin
     self.eventmanager.SendEvent(event)
Ejemplo n.º 17
0
 def data_public(self, event):
     # 数据循环发布
     event = Event('data_public')
     self.eventmanager.SendEvent(event)
Ejemplo n.º 18
0
    def strategy_receive(self, event):

        # 收到数据后,先进行策略_to_order的处理
        self.strategy_to_order()
        event = Event('strategy_receive')
        self.eventmanager.SendEvent(event)
Ejemplo n.º 19
0
 def strategy_public(self):
     event = Event('strategy_public')
     self.eventmanager.SendEvent(event)
Ejemplo n.º 20
0
 def data_receive(self, event):
     # order处理之后,数据可以进一步
     print('初始化全部完成,数据循环播报')
     event = Event('data_receive')
     self.eventmanager.SendEvent(event)