Exemplo n.º 1
0
    def init(
        self,
        host: str,
        proxy_host: str = "",
        proxy_port: int = 0,
        ping_interval: int = 60,
        header: dict = None,
        log_path: Optional[str] = None,
    ):
        """
        :param host:
        :param proxy_host:
        :param proxy_port:
        :param header:
        :param ping_interval: unit: seconds, type: int
        :param log_path: optional. file to save log.
        """
        self.host = host
        self.ping_interval = ping_interval  # seconds
        if log_path is not None:
            self.logger = get_file_logger(log_path)
            self.logger.setLevel(logging.DEBUG)

        if header:
            self.header = header

        if proxy_host and proxy_port:
            self.proxy_host = proxy_host
            self.proxy_port = proxy_port
Exemplo n.º 2
0
 def save_output(self):
     """保存输出日志"""
     logger = get_file_logger(
         os.path.join(self.backtest_log_path, "output.log"))
     logger.setLevel(logging.INFO)
     for i in self.output_list:
         logger.info(i)
Exemplo n.º 3
0
    def save_trade_data(self):
        """保存策略实盘数据
        tips: 实盘时调用"""
        if self.instance_name is None:
            return
        if not os.path.exists(self.save_path):
            os.makedirs(self.save_path)

        if len(self.order_list) != 0:
            csv_add_rows(data_list=self.order_list,
                         header=[
                             "datetime", "direction", "exchange",
                             "gateway_name", "offset", "orderid", "price",
                             "status", "symbol", "time", "traded", "type",
                             "volume", "vt_orderid", "vt_symbol"
                         ],
                         csv_path=os.path.join(self.save_path, "orders.csv"))
            self.order_list = []

        if len(self.trade_list) != 0:
            csv_add_rows(data_list=self.trade_list,
                         header=[
                             "datetime", "direction", "exchange",
                             "gateway_name", "offset", "orderid", "price",
                             "symbol", "time", "tradeid", "volume",
                             "vt_orderid", "vt_symbol", "vt_tradeid"
                         ],
                         csv_path=os.path.join(self.save_path, "trades.csv"))
            self.trade_list = []

        if len(self.output_list) != 0:
            logger = get_file_logger(os.path.join(self.save_path,
                                                  "output.log"))
            logger.setLevel(logging.INFO)
            for output in self.output_list:
                logger.info(output)
            self.output_list = []

        if len(self.daily_close_dict) != 0:
            daily_close_file = os.path.join(self.save_path, "daily_close.json")
            pre_close_dict = load_json(daily_close_file)
            pre_close_dict.update(self.daily_close_dict)
            save_json(daily_close_file, pre_close_dict)

        list_dict = self.KLine_chart_dict.list_dict
        if len(list_dict["datetime"]) != 0:
            KLine_list = []
            for i in range(len(list_dict["datetime"])):
                row = []
                for field in self.KLine_chart_dict.all_field:
                    row.append(list_dict[field][i])
                KLine_list.append(row)
            csv_add_rows(data_list=KLine_list,
                         header=self.KLine_chart_dict.all_field,
                         csv_path=os.path.join(self.save_path,
                                               "KLineChart.csv"))
Exemplo n.º 4
0
    def init(
        self,
        url_base: str,
        proxy_host: str = "",
        proxy_port: int = 0,
        log_path: Optional[str] = None,
    ):
        """
        Init rest client with url_base which is the API root address.
        e.g. 'https://www.bitmex.com/api/v1/'
        :param url_base:
        :param proxy_host:
        :param proxy_port:
        :param log_path: optional. file to save log.
        """
        self.url_base = url_base

        if log_path is not None:
            self.logger = get_file_logger(log_path)
            self.logger.setLevel(logging.DEBUG)

        if proxy_host and proxy_port:
            proxy = f"{proxy_host}:{proxy_port}"
            self.proxies = {"http": proxy, "https": proxy}