def __init__(self, instruments, resolution, \ tick_window=1, bar_window=100, timezone="UTC", preload=None, \ continuous=True, blotter=None, **kwargs): self.name = str(self.__class__).split('.')[-1].split("'")[0] # assign algo params self.bars = pd.DataFrame() self.ticks = pd.DataFrame() self.quotes = {} self.tick_count = 0 self.tick_bar_count = 0 self.bar_count = 0 self.bar_hash = 0 self.tick_window = tick_window if tick_window > 0 else 1 if "V" in resolution: self.tick_window = 1000 self.bar_window = bar_window if bar_window > 0 else 100 self.resolution = resolution.replace("MIN", "T") self.timezone = timezone self.preload = preload self.continuous = continuous self.backtest = args.backtest self.backtest_start = args.start self.backtest_end = args.end # ----------------------------------- self.sms_numbers = [] if args.sms is None else args.sms self.trade_log_dir = args.log self.blotter_name = args.blotter if args.blotter is not None else blotter self.record_output = args.output # ----------------------------------- # load blotter settings && initilize Blotter self.load_blotter_args(args.blotter) self.blotter = Blotter(**self.blotter_args) # ----------------------------------- # initiate broker/order manager super().__init__(instruments, ibclient=int(args.ibclient), \ ibport=int(args.ibport), ibserver=str(args.ibserver)) # ----------------------------------- # signal collector self.signals = {} for sym in self.symbols: self.signals[sym] = pd.DataFrame() # ----------------------------------- # initilize output file self.record_ts = None if self.record_output: self.datastore = tools.DataStore(args.output) # ----------------------------------- # initiate strategy self.on_start()
def __init__(self, instruments=[], resolution="1T", tick_window=1, bar_window=100, timezone="UTC", preload=None, continuous=True, blotter=None, sms=[], log=None, backtest=False, start=None, end=None, data=None, output=None, ibclient=998, ibport=4001, ibserver="localhost", **kwargs): # detect algo name self.name = str(self.__class__).split('.')[-1].split("'")[0] # initilize algo logger self.log_algo = logging.getLogger(__name__) # initilize strategy logger tools.createLogger(self.name) self.log = logging.getLogger(self.name) # override args with any (non-default) command-line args self.args = {arg: val for arg, val in locals().items( ) if arg not in ('__class__', 'self', 'kwargs')} self.args.update(kwargs) self.args.update(self.load_cli_args()) # assign algo params self.bars = pd.DataFrame() self.ticks = pd.DataFrame() self.quotes = {} self.books = {} self.tick_count = 0 self.tick_bar_count = 0 self.bar_count = 0 self.bar_hashes = {} self.tick_window = tick_window if tick_window > 0 else 1 if "V" in resolution: self.tick_window = 1000 self.bar_window = bar_window if bar_window > 0 else 100 self.resolution = resolution.upper().replace("MIN", "T") self.timezone = timezone self.preload = preload self.continuous = continuous self.backtest = self.args["backtest"] self.backtest_start = self.args["start"] self.backtest_end = self.args["end"] self.backtest_csv = self.args["data"] # ----------------------------------- self.sms_numbers = self.args["sms"] self.trade_log_dir = self.args["log"] self.blotter_name = self.args["blotter"] self.record_output = self.args["output"] # ----------------------------------- # initiate broker/order manager super().__init__(instruments, **{arg: val for arg, val in self.args.items() if arg in ( 'ibport', 'ibclient', 'ibhost')}) # ----------------------------------- # signal collector self.signals = {} for sym in self.symbols: self.signals[sym] = pd.DataFrame() # ----------------------------------- # initilize output file self.record_ts = None if self.record_output: self.datastore = tools.DataStore(self.args["output"]) # --------------------------------------- # add stale ticks for more accurate time--based bars if not self.backtest and self.resolution[-1] not in ("K", "V"): self.bar_timer = asynctools.RecurringTask( self.add_stale_tick, interval_sec=1, init_sec=1, daemon=True) # --------------------------------------- # sanity checks for backtesting mode if self.backtest: if self.record_output is None: self.log_algo.error("Must provide an output file for Backtest mode") sys.exit(0) if self.backtest_start is None: self.log_algo.error("Must provide start date for Backtest mode") sys.exit(0) if self.backtest_end is None: self.backtest_end = datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') if self.backtest_csv is not None: if not os.path.exists(self.backtest_csv): self.log_algo.error("CSV directory cannot be found (%s)" % self.backtest_csv) sys.exit(0) elif self.backtest_csv.endswith("/"): self.backtest_csv = self.backtest_csv[:-1] else: self.backtest_start = None self.backtest_end = None self.backtest_csv = None # be aware of thread count self.threads = asynctools.multitasking.getPool(__name__)['threads']
def __init__(self, instruments, resolution, tick_window=1, bar_window=100, timezone="UTC", preload=None, continuous=True, blotter=None, force_resolution=False, **kwargs): self.name = str(self.__class__).split('.')[-1].split("'")[0] # ---------------------------------------------------- # default args self.args = kwargs.copy() cli_args = self.load_cli_args() # override kwargs args with cli args for arg in cli_args: if arg not in self.args or ( arg in self.args and cli_args[arg] is not None ): self.args[arg] = cli_args[arg] # fix flag args (no value) for arg in ["backtest"]: if arg in kwargs and "--"+str(arg) not in sys.argv: self.args[arg] = kwargs["backtest"] # ---------------------------------------------------- # assign algo params self.bars = pd.DataFrame() self.ticks = pd.DataFrame() self.quotes = {} self.books = {} self.tick_count = 0 self.tick_bar_count = 0 self.bar_count = 0 self.bar_hash = 0 self.tick_window = tick_window if tick_window > 0 else 1 if "V" in resolution: self.tick_window = 1000 self.bar_window = bar_window if bar_window > 0 else 100 self.resolution = resolution.replace("MIN", "T") self.timezone = timezone self.preload = preload self.continuous = continuous self.backtest = self.args["backtest"] self.backtest_start = self.args["start"] self.backtest_end = self.args["end"] # ----------------------------------- self.sms_numbers = [] if self.args["sms"] is None else self.args["sms"] self.trade_log_dir = self.args["log"] self.blotter_name = self.args["blotter"] if self.args["blotter"] is not None else blotter self.record_output = self.args["output"] # ----------------------------------- # load blotter settings && initilize Blotter self.load_blotter_args(self.args["blotter"]) self.blotter = Blotter(**self.blotter_args) # ----------------------------------- # initiate broker/order manager super().__init__(instruments, ibclient=int(self.args["ibclient"]), ibport=int(self.args["ibport"]), ibserver=str(self.args["ibserver"])) # ----------------------------------- # signal collector self.signals = {} for sym in self.symbols: self.signals[sym] = pd.DataFrame() # ----------------------------------- # initilize output file self.record_ts = None if self.record_output: self.datastore = tools.DataStore(self.args["output"]) # ----------------------------------- # initiate strategy self.on_start() # --------------------------------------- # add stale ticks to allow for interval-based bars if force_resolution and self.resolution[-1] not in ("K", "V"): self.bar_timer = tools.RecurringTask( self.add_stale_tick, interval_sec=1, init_sec=1, daemon=True)