示例#1
0
    def place_order(self,
                    symbol,
                    action,
                    price,
                    size,
                    algo="",
                    algo_param=None,
                    userdata=""):
        if algo_param is None:
            algo_param = dict()

        # this order object is not for TradeApi, but for strategy itself to remember the order
        order = Order.new_order(symbol, action, price, size,
                                self.ctx.trade_date, 0)
        order.entrust_no = self._get_next_num('entrust_no')

        task = Task(self._get_next_task_no(),
                    algo=algo,
                    algo_param=algo_param,
                    data=order,
                    function_name="place_order")
        self.ctx.pm.add_task(task)
        # self.task_id_map[order.task_id].append(order.entrust_no)

        # self.pm.add_order(order)

        e = Event(EVENT_TYPE.PLACE_ORDER)
        e.dic['task'] = task
        self.publish_event(e)
示例#2
0
    def on_task_status(self, ind_dic):
        # print("\nGateway on task ind: ")
        # print(ind_dic)
        ind = TaskInd.create_from_dict(ind_dic)
        ind.task_no = self._task_no_id_map[ind.task_id]

        e = Event(EVENT_TYPE.TASK_STATUS_IND)
        e.dic['ind'] = ind
        self.ctx.instance.put(e)
示例#3
0
    def _generate_on_task_rsp(self, task_no, task_id, msg):
        # this rsp is generated by gateway itself
        rsp = TaskRsp(task_no=task_no, task_id=task_id, msg=msg)
        if rsp.success:
            self._task_no_id_map[task_id] = task_no

        # DEBUG
        print("\nGateway generate task_rsp {}".format(rsp))
        e = Event(EVENT_TYPE.TASK_RSP)
        e.dic['rsp'] = rsp
        self.ctx.instance.put(e)
示例#4
0
    def goal_portfolio(self, positions, algo="", algo_param=None, userdata=""):
        if algo_param is None:
            algo_param = dict()

        task = Task(self._get_next_task_no(),
                    data=positions,
                    algo=algo,
                    algo_param=algo_param,
                    function_name="goal_portfolio")
        self.ctx.pm.add_task(task)

        e = Event(EVENT_TYPE.GOAL_PORTFOLIO)
        e.dic['task'] = task
        self.publish_event(e)
示例#5
0
    def on_order_status(self, ind_dic):
        """
        
        Parameters
        ----------
        ind_dic : dict

        """
        # print("\nGateway on order status: ")
        # print(ind_dic)
        if 'security' in ind_dic:
            ind_dic['symbol'] = ind_dic.pop('security')

        ind = OrderStatusInd.create_from_dict(ind_dic)
        ind.task_no = self._task_no_id_map[ind.task_id]

        e = Event(EVENT_TYPE.ORDER_STATUS_IND)
        e.dic['ind'] = ind
        self.ctx.instance.put(e)
示例#6
0
    def on_trade(self, ind_dic):
        """
        
        Parameters
        ----------
        ind_dic : dict

        """
        # print("\nGateway on trade: ")
        # print(ind_dic)
        if 'security' in ind_dic:
            ind_dic['symbol'] = ind_dic.pop('security')

        ind = Trade.create_from_dict(ind_dic)
        ind.task_no = self._task_no_id_map[ind.task_id]

        e = Event(EVENT_TYPE.TRADE_IND)
        e.dic['ind'] = ind
        self.ctx.instance.put(e)
示例#7
0
    def on_connection_callback(self, connected):
        """
        
        Parameters
        ----------
        connected : bool

        """
        if connected:
            print("TradeAPI connected.")
            event_type = EVENT_TYPE.TRADE_API_CONNECTED
        else:
            print("TradeAPI disconnected.")
            event_type = EVENT_TYPE.TRADE_API_DISCONNECTED
        e = Event(event_type)
        self.ctx.instance.put(e)
示例#8
0
 def mkt_data_callback(self, key, quote):
     e = Event(EVENT_TYPE.MARKET_DATA)
     # print quote
     e.dic = {'quote': quote}
     self.ctx.instance.put(e)
示例#9
0
 def query_trade(self, task_id=-1, userdata=""):
     args = locals()
     e = Event(EVENT_TYPE.QUERY_TRADE)
     e.dic['args'] = args
     self.publish_event(e)
示例#10
0
 def query_order(self, task_id=-1, userdata=""):
     args = locals()
     e = Event(EVENT_TYPE.QUERY_ORDER)
     e.dic['args'] = args
     self.publish_event(e)
示例#11
0
 def query_portfolio_async(self, userdata=""):
     args = locals()
     e = Event(EVENT_TYPE.QUERY_PORTFOLIO)
     e.dic['args'] = args
     self.publish_event(e)
示例#12
0
 def query_position_async(self, mode="all", symbols="", userdata=""):
     args = locals()
     e = Event(EVENT_TYPE.QUERY_POSITION)
     e.dic['args'] = args
     e.dic['securities'] = e.dic.pop['symbols']
     self.publish_event(e)
示例#13
0
 def query_universe_async(self, userdata=""):
     args = locals()
     e = Event(EVENT_TYPE.QUERY_UNIVERSE)
     e.dic['args'] = args
     self.publish_event(e)
示例#14
0
 def query_account_async(self, userdata=""):
     args = locals()
     e = Event(EVENT_TYPE.QUERY_ACCOUNT)
     e.dic['args'] = args
     self.publish_event(e)
示例#15
0
 def cancel_order(self, entrust_no):
     e = Event(EVENT_TYPE.CANCEL_ORDER)
     e.dic['entrust_no'] = entrust_no
     self.publish_event(e)
示例#16
0
 def mkt_data_callback(self, key, quote):
     e = Event(EVENT_TYPE.MARKET_DATA)
     # print quote
     e.dic = {'quote': quote}
     if (self.ctx is not None) and (self.ctx.instance is not None):
         self.ctx.instance.put(e)