def __init__(self, host='localhost', port=4001, client_id=101, is_use_gateway=False, evaluation_time_secs=20, resample_interval_secs='30s', moving_window_period=dt.timedelta(hours=1)): self.moving_window_period = moving_window_period self.chart = Chart() self.ib_util = IBUtil() # Store parameters for this model self.strategy_params = StrategyParameters(evaluation_time_secs, resample_interval_secs) self.stocks_data = {} # Dictionary storing StockData objects. self.symbols = None # List of current symbols self.account_code = "" self.prices = None # Store last prices in a DataFrame self.trade_qty = 0 self.order_id = 0 self.lock = threading.Lock() # Use ibConnection() for TWS, or create connection for API Gateway self.conn = ibConnection() if is_use_gateway else \ Connection.create(host=host, port=port, clientId=client_id) self.__register_data_handlers(self.__on_tick_event, self.__event_handler)
def __init__(self, host='localhost', port=4001, client_id=130, is_use_gateway=False, moving_window_period=dt.timedelta(seconds=60), test=False): logging.basicConfig(format='%(asctime)s %(message)s') self.test_logger = logging.getLogger('hftModelLogger') self.test_logger.setLevel(logging.INFO) self.test = test self.tz = pytz.timezone('Singapore') self.moving_window_period = moving_window_period self.ib_util = IBUtil() self.symbols = None # List of current symbols self.account_code = "" self.prices = None # Store last prices in a DataFrame self.ohlc = None # I need another store for minute data (I think) self.buffer = list() self.trade_qty = 0 self.traffic_light = Event() self.ohlc_ok = Lock() self.executor = concurrent.futures.ThreadPoolExecutor(max_workers=6) self.timekeeper = None self.parser = None self.execution = None self.strategy = None self.handler = None self.data_path = os.path.normpath( os.path.join(os.path.curdir, "data.csv")) self.ohlc_path = os.path.normpath( os.path.join(os.path.curdir, "ohlc.csv")) self.flag = None self.event_market_on = Event() self.ml = MLcall() self.last_trim = None self.last_ml_call = None self.cur_mean = None self.cur_sd = None # Use ibConnection() for TWS, or create connection for API Gateway self.conn = Connection.create(host=host, port=port, clientId=client_id) if not self.test: self.handler = ExecutionHandler(self.conn) #third handler should register properly si Dieu veut if self.test: self.__register_data_handlers(self.null_handler, self.__event_handler, self.null_handler) else: self.__register_data_handlers(self.handler.on_tick_event, self.__event_handler, self.handler._reply_handler) if self.test: self.order_template = self.create_contract(settings.SYMBOL, settings.SECURITY, settings.EXCHANGE, settings.EXPIRY, settings.CURRENCY) else: self.order_template = self.handler.create_contract( settings.SYMBOL, settings.SECURITY, settings.EXCHANGE, settings.EXPIRY, settings.CURRENCY) self.signal = None self.state = None