<commit_before># -*- coding: utf-8 -*-

import time

from shipane_sdk.client import *
from shipane_sdk.models import *
from shipane_sdk.support import *


class BaseStrategyManagerFactory(object):
    def __init__(self):
        self._config = self._create_config()

    def create(self, id):
        traders = self._create_traders(id)
        return StrategyManager(id, self._create_logger(), self._config, traders)

    def _get_context(self):
        pass

    def _create_traders(self, id):
        traders = OrderedDict()
        for trader_id, trader_config in self._config.build_trader_configs(id).items():
            trader = self._create_trader(trader_config)
            traders[trader_id] = trader
        return traders

    def _create_trader(self, trader_config):
        return StrategyTrader(self._create_logger(), trader_config, self._get_context())

    def _create_logger(self):
        pass

    def _create_config(self):
        return StrategyConfig(self._get_context())


class BaseStrategyContext(object):
    def get_portfolio(self):
        pass

    def convert_order(self, quant_order):
        pass

    def has_open_orders(self):
        pass

    def cancel_open_orders(self):
        pass

    def cancel_order(self, quant_order):
        pass

    def read_file(self, path):
        pass

    def is_sim_trade(self):
        pass

    def is_backtest(self):
        pass


class BaseLogger(object):
    def debug(self, msg, *args, **kwargs):
        pass

    def info(self, msg, *args, **kwargs):
        pass

    def warning(self, msg, *args, **kwargs):
        pass

    def error(self, msg, *args, **kwargs):
        pass

    def exception(self, msg, *args, **kwargs):
        pass


class StrategyManager(object):
    THEMATIC_BREAK = '-' * 50

    def __init__(self, id, logger, config, traders):
        self._id = id
        self._logger = logger
        self._config = config
        self._traders = traders

    @property
    def id(self):
        return self._id

    @property
    def traders(self):
        return self._traders

    def purchase_new_stocks(self):
        for trader in self._traders.values():
            try:
                trader.purchase_new_stocks()
            except:
                self._logger.exception('[%s] 打新失败', trader.id)

    def execute(self, order=None, **kwargs):
        if order is None and not kwargs:
            return
        for trader in self._traders.values():
            try:
                trader.execute(order, **kwargs)
            except:
                self._logger.exception('[%s] 下单失败', trader.id)

    def cancel(self, order):
        for trader in self._traders.values():
            try:
                trader.cancel(order)
            except:
                self._logger.exception('[%s] 撤单失败', trader.id)

    def sync(self):
        stop_watch = StopWatch()
        stop_watch.start()
        self._logger.info("[%s] 开始同步", self._id)
        self._refresh()
        for id, trader in self._traders.items():
            trader.sync()
        stop_watch.stop()
        self._logger.info("[%s] 结束同步，总耗时[%s]", self._id, stop_watch.short_summary())
        self._logger.info(self.THEMATIC_BREAK)

    def _refresh(self):
        self._config.reload()
        trader_configs = self._config.build_trader_configs(self._id)
        for id, trader in self._traders.items():
            trader.set_config(trader_configs[id])


class StrategyTrader(object):
    def __init__(self, logger, config, strategy_context):
        self._logger = logger
        self._config = config
        self._strategy_context = strategy_context
        self._shipane_client = Client(self._logger, **config['client'])
        self._order_id_map = {}
        self._expire_before = datetime.datetime.combine(datetime.date.today(), datetime.time.min)
        self._last_sync_portfolio_fingerprint = {}

    @property
    def id(self):
        return self._config['id']

    @property
    def client(self):<commit_msg>Fix sync not performed issue when portfolio becomes empty<commit_after># -*- coding: utf-8 -*-

import time

from shipane_sdk.client import *
from shipane_sdk.models import *
from shipane_sdk.support import *


class BaseStrategyManagerFactory(object):
    def __init__(self):
        self._config = self._create_config()

    def create(self, id):
        traders = self._create_traders(id)
        return StrategyManager(id, self._create_logger(), self._config, traders)

    def _get_context(self):
        pass

    def _create_traders(self, id):
        traders = OrderedDict()
        for trader_id, trader_config in self._config.build_trader_configs(id).items():
            trader = self._create_trader(trader_config)
            traders[trader_id] = trader
        return traders

    def _create_trader(self, trader_config):
        return StrategyTrader(self._create_logger(), trader_config, self._get_context())

    def _create_logger(self):
        pass

    def _create_config(self):
        return StrategyConfig(self._get_context())


class BaseStrategyContext(object):
    def get_portfolio(self):
        pass

    def convert_order(self, quant_order):
        pass

    def has_open_orders(self):
        pass

    def cancel_open_orders(self):
        pass

    def cancel_order(self, quant_order):
        pass

    def read_file(self, path):
        pass

    def is_sim_trade(self):
        pass

    def is_backtest(self):
        pass


class BaseLogger(object):
    def debug(self, msg, *args, **kwargs):
        pass

    def info(self, msg, *args, **kwargs):
        pass

    def warning(self, msg, *args, **kwargs):
        pass

    def error(self, msg, *args, **kwargs):
        pass

    def exception(self, msg, *args, **kwargs):
        pass


class StrategyManager(object):
    THEMATIC_BREAK = '-' * 50

    def __init__(self, id, logger, config, traders):
        self._id = id
        self._logger = logger
        self._config = config
        self._traders = traders

    @property
    def id(self):
        return self._id

    @property
    def traders(self):
        return self._traders

    def purchase_new_stocks(self):
        for trader in self._traders.values():
            try:
                trader.purchase_new_stocks()
            except:
                self._logger.exception('[%s] 打新失败', trader.id)

    def execute(self, order=None, **kwargs):
        if order is None and not kwargs:
            return
        for trader in self._traders.values():
            try:
                trader.execute(order, **kwargs)
            except:
                self._logger.exception('[%s] 下单失败', trader.id)

    def cancel(self, order):
        for trader in self._traders.values():
            try:
                trader.cancel(order)
            except:
                self._logger.exception('[%s] 撤单失败', trader.id)

    def sync(self):
        stop_watch = StopWatch()
        stop_watch.start()
        self._logger.info("[%s] 开始同步", self._id)
        self._refresh()
        for id, trader in self._traders.items():
            trader.sync()
        stop_watch.stop()
        self._logger.info("[%s] 结束同步，总耗时[%s]", self._id, stop_watch.short_summary())
        self._logger.info(self.THEMATIC_BREAK)

    def _refresh(self):
        self._config.reload()
        trader_configs = self._config.build_trader_configs(self._id)
        for id, trader in self._traders.items():
            trader.set_config(trader_configs[id])


class StrategyTrader(object):
    def __init__(self, logger, config, strategy_context):
        self._logger = logger
        self._config = config
        self._strategy_context = strategy_context
        self._shipane_client = Client(self._logger, **config['client'])
        self._order_id_map = {}
        self._expire_before = datetime.datetime.combine(datetime.date.today(), datetime.time.min)
        self._last_sync_portfolio_fingerprint = None

    @property
    def id(self):
        return self._config['id']

    @property
    def client(self):
{'fraction_non_alphanumeric': 0.10776505724240916, 'fraction_numerical': 0.0007466401194624191, 'mean_word_length': 2.3390112172829247, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '93283', 'n_tokens_mistral': 2497, 'n_tokens_neox': 2404, 'n_words': 530}