Esempio n. 1
0
    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
                    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)
Esempio n. 2
0
    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 = {}
Esempio n. 3
0
 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)