def init(self, bars: List[Bar], account: Account, symbol: Symbol, unique_id: str = ""): '''init open position etc.''' self.symbol = symbol self.unique_id = unique_id if unique_id is not None: base = 'openPositions/' try: os.makedirs(base) except Exception: pass try: with open(base + self._get_pos_file(), 'r') as file: data = json.load(file) self.last_time = data["last_time"] for pos_json in data["positions"]: pos: Position = Position.from_json(pos_json) self.open_positions[pos.id] = pos if "moduleData" in data.keys(): if str(bars[0].tstamp) in data["moduleData"].keys(): moduleData = data['moduleData'][str(bars[0].tstamp)] ExitModule.set_data_from_json(bars[0], moduleData) if str(bars[1].tstamp) in data["moduleData"].keys(): moduleData = data['moduleData'][str(bars[1].tstamp)] ExitModule.set_data_from_json(bars[1], moduleData) self.logger.info("done loading " + str( len(self.open_positions)) + " positions from " + self._get_pos_file() + " last time " + str( self.last_time)) except Exception as e: self.logger.warn("Error loading open positions: " + str(e)) self.open_positions = {} # init positions from existing orders self.sync_positions_with_open_orders(bars, account)
def read_open_positions(self,bars: List[Bar]): if self.unique_id is not None: base = 'openPositions/' try: os.makedirs(base) except Exception: pass try: with open(base + self._get_pos_file(), 'r') as file: data = json.load(file) self.last_time = data["last_time"] if "max_equity" in data.keys(): self.max_equity= data["max_equity"] self.time_of_max_equity= data["time_of_max_equity"] for pos_json in data["positions"]: pos: Position = Position.from_json(pos_json) self.open_positions[pos.id] = pos if "moduleData" in data.keys(): if str(bars[0].tstamp) in data["moduleData"].keys(): moduleData = data['moduleData'][str(bars[0].tstamp)] ExitModule.set_data_from_json(bars[0], moduleData) if str(bars[1].tstamp) in data["moduleData"].keys(): moduleData = data['moduleData'][str(bars[1].tstamp)] ExitModule.set_data_from_json(bars[1], moduleData) self.logger.info("done loading " + str( len(self.open_positions)) + " positions from " + self._get_pos_file() + " last time " + str( self.last_time)) except Exception as e: self.logger.warn("Error loading open positions: " + str(e)) self.open_positions = {}
def write_dashboard(dashboardFile): result = {} for thread in activeThreads: try: engine: LiveTrading = thread.bot if engine.alive: bot = engine.bot result[engine.id] = { 'alive': engine.alive, "last_time": bot.last_time, "last_tick": str(bot.last_tick_time), "last_tick_tstamp": bot.last_tick_time.timestamp() if bot.last_tick_time is not None else None, "equity": engine.account.equity, "risk_reference": bot.risk_reference, "max_equity": bot.max_equity, "time_of_max_equity": bot.time_of_max_equity } data = result[engine.id] data['positions'] = [] for pos in engine.bot.open_positions: data['positions'].append( engine.bot.open_positions[pos].to_json()) data['moduleData'] = {} data['moduleData'][ engine.bars[0].tstamp] = ExitModule.get_data_for_json( engine.bars[0]) data['moduleData'][ engine.bars[1].tstamp] = ExitModule.get_data_for_json( engine.bars[1]) else: result[engine.id] = {"alive": False} except Exception as e: logger.error("exception in writing dashboard: " + traceback.format_exc()) thread.bot.alive = False try: os.makedirs(os.path.dirname(dashboardFile)) except Exception: pass with open(dashboardFile, 'w') as file: json.dump(result, file, sort_keys=False, indent=4)
def fill_openpositions(self, data, bars: List[Bar]): self.last_time = data["last_time"] if "last_tick_tstamp" in data: self.last_tick_time = datetime.fromtimestamp(data["last_tick_tstamp"]) if "max_equity" in data.keys(): self.max_equity = data["max_equity"] self.time_of_max_equity = data["time_of_max_equity"] for pos_json in data["positions"]: pos: Position = Position.from_json(pos_json) self.open_positions[pos.id] = pos if "moduleData" in data.keys(): for idx in range(5): if str(bars[idx].tstamp) in data["moduleData"].keys(): moduleData = data['moduleData'][str(bars[idx].tstamp)] ExitModule.set_data_from_json(bars[idx], moduleData)
def save_open_positions(self, bars: List[Bar]): if self.unique_id is None: return base = 'openPositions/' try: os.makedirs(base) except Exception: pass with open(base + self._get_pos_file(), 'w') as file: pos_json = [] for pos in self.open_positions: pos_json.append(self.open_positions[pos].to_json()) moduleData = {} for idx in range(5): moduleData[bars[idx].tstamp] = ExitModule.get_data_for_json( bars[idx]) data = { "last_time": self.last_time, "last_tick": str(self.last_tick_time), "positions": pos_json, "moduleData": moduleData, "risk_reference": self.risk_reference, "max_equity": self.max_equity, "time_of_max_equity": self.time_of_max_equity } json.dump(data, file, sort_keys=False, indent=4)
def write_dashboard(dashboardFile): result = {} for thread in activeThreads: try: bot: LiveTrading = thread.bot if bot.alive: result[bot.id] = { 'alive': bot.alive, "last_time": bot.bot.last_time, "last_tick": str(bot.bot.last_tick_time) } data = result[bot.id] data['positions'] = [] for pos in bot.bot.open_positions: data['positions'].append( bot.bot.open_positions[pos].to_json()) data['moduleData'] = {} data['moduleData'][ bot.bars[0].tstamp] = ExitModule.get_data_for_json( bot.bars[0]) data['moduleData'][ bot.bars[1].tstamp] = ExitModule.get_data_for_json( bot.bars[1]) else: result[bot.id] = {"alive": False} except Exception as e: logger.error("exception in writing dashboard: " + traceback.format_exc()) thread.bot.alive = False try: os.makedirs(os.path.dirname(dashboardFile)) except Exception: pass with open(dashboardFile, 'w') as file: json.dump(result, file, sort_keys=False, indent=4)
def save_open_positions(self, bars: List[Bar]): if self.unique_id is None: return base = 'openPositions/' try: os.makedirs(base) except Exception: pass try: pos_json = [] for pos in self.open_positions: pos_json.append(self.open_positions[pos].to_json()) moduleData = {} for idx in range(5): moduleData[bars[idx].tstamp] = ExitModule.get_data_for_json( bars[idx]) data = { "last_time": self.last_time, "last_tick_tstamp": self.last_tick_time.timestamp(), "last_tick": str(self.last_tick_time), "positions": pos_json, "moduleData": moduleData, "risk_reference": self.risk_reference, "max_equity": self.max_equity, "time_of_max_equity": self.time_of_max_equity } string = json.dumps(data, sort_keys=False, indent=4) with open(base + self._get_pos_file(), 'w') as file: file.write(string) # backup if no error happened: this is need to have a backup of the status # for manual intervention when something breaks massivly with open(base + self._get_pos_file(self.openPositionRolling), 'w') as file: file.write(string) self.openPositionRolling += 1 if self.openPositionRolling > 10: self.openPositionRolling = 1 except Exception as e: self.logger.error("Error saving positions " + str(e)) raise e