Пример #1
0
 def wait_elvalue(self,locator,text):
     try:
         WebDriverWait(self.driver,15).until(EC.text_to_be_present_in_element_value(locator,text))
         logger.info('had wait for the input value:%s'%text)
     except TimeoutError as e:
         logger.error("can't wait for the input value:%s"%e)
         self.get_windows_img()
 def save_trained_model(self):
     """
     Save the trained neural network under a fixed name specific for this traders.
     """
     save_keras_sequential(self.model, self.RELATIVE_DATA_DIRECTORY,
                           self.get_name())
     logger.info(f"DQL Trader: Saved trained model")
Пример #3
0
 def close(self):
     try:
         self.driver.close()
         logger.info("关闭并退出浏览器")
     except Exception as e:
         logger.error("退出浏览器,失败%s" % e)
         self.get_windows_img()
    def __init__(self, expert_a: IExpert, expert_b: IExpert, load_trained_model: bool = True,
                 train_while_trading: bool = False, color: str = 'black', name: str = 'dql_trader', ):
        """
        Constructor
        Args:
            expert_a: Expert for stock A
            expert_b: Expert for stock B
            load_trained_model: Flag to trigger loading an already trained neural network
            train_while_trading: Flag to trigger on-the-fly training while trading
        """
        # Save experts, training mode and name
        super().__init__(color, name)
        assert expert_a is not None and expert_b is not None
        self.expert_a = expert_a
        self.expert_b = expert_b
        self.train_while_trading = train_while_trading

        # Parameters for neural network
        self.state_size = 2
        self.action_size = 4
        self.hidden_size = 50

        # Parameters for deep Q-learning
        self.learning_rate = 0.001
        self.epsilon = 1.0
        self.epsilon_decay = 0.90
        self.epsilon_min = 0.01
        self.batch_size = 64
        self.min_size_of_memory_before_training = 1000  # should be way bigger than batch_size, but smaller than memory
        self.memory = deque(maxlen=2000)

        # Attributes necessary to remember our last actions and fill our memory with experiences
        self.last_state = None
        self.last_action_a = None
        self.last_action_b = None
        self.last_portfolio_value = None
        self.last_price_a = None
        self.last_price_b = None

        # Create main model, either as trained model (from file) or as untrained model (from scratch)
        self.model = None
        if load_trained_model:
            self.model = load_keras_sequential(self.RELATIVE_DATA_DIRECTORY, self.get_name())
            logger.info(f"DQL Trader: Loaded trained model")
        if self.model is None:  # loading failed or we didn't want to use a trained model
            self.model = Sequential()
            self.model.add(Dense(self.hidden_size * 2, input_dim=self.state_size, activation='relu'))
            self.model.add(Dense(self.hidden_size, activation='relu'))
            self.model.add(Dense(self.action_size, activation='linear'))
            logger.info(f"DQL Trader: Created new untrained model")
        assert self.model is not None
        self.model.compile(loss='mse', optimizer=Adam(lr=self.learning_rate))

        self.vote_map = {
            Vote.SELL : 0,
            Vote.HOLD : 1,
            Vote.BUY : 2,
        }

        self.vote_map_inverse = {v: k for k, v in self.vote_map.items()}
Пример #5
0
 def exit(self):
     try:
         self.click(*self.exit_button)
         logger.info("退出登录成功")
     except Exception as e:
         logger.error("退出失败%s" % e)
         self.get_windows_img()
Пример #6
0
 def window_handles(self, i):
     try:
         self.driver.switch_to.window(self.driver.window_handles[i])
         logger.info("激活窗口成功")
     except Exception as e:
         logger.error("激活窗口失败%s" % e)
         self.get_windows_img()
Пример #7
0
 def forum(self):
     try:
         self.click(*self.forum_link)
         logger.info("点击论坛成功")
     except Exception as e:
         logger.error("点击论坛失败%s" % e)
         self.get_windows_img()
Пример #8
0
 def wait_gonextframe(self, locator):
     try:
         WebDriverWait(self.driver, 15).until(EC.frame_to_be_available_and_switch_to_it(locator))
         logger.info("had wait and changed frame")
     except TimeoutError as e:
         logger.error("can't wait for frame:%s" % e)
         self.get_windows_img()
Пример #9
0
 def scrollby_element(self,selector):
     el = self.find_element(selector)
     try:
         self.driver.excute_script("arguments[0].scrollIntoView(true);",el)
         logger.info("had scroll to the element target ")
     except NameError as e:
         logger.error("can't scoll to the element:%s"%e)
Пример #10
0
 def double_click(self,selector):
     element = self.find_element(selector)
     try:
         ActionChains(self.driver).double_click(element).perform()
         logger.info("double click on the element")
     except NameError as e:
         logger.error("can't double click:%s" % e)
Пример #11
0
    def select_windows(self):
        handles = self.driver.window_handles  #获取全部窗口

        for handle in handles:
            if handle != self.driver.current_window_handle:
                logger.info("switch to second window:%s "% handle)
                self.driver.switch_to_window(handle)
Пример #12
0
 def closeDB(self):
     """
     close database
     :return:
     """
     self.db.close()
     logger.info("Database closed!")
Пример #13
0
 def mouse_stop(self, selector):
     element = self.find_element(selector)
     try:
         ActionChains(self.driver).move_to_element(element).perform()
         logger.info("mouse stop on the element")
     except NameError as e:
         logger.error("can't mouse stop:%s"%e)
Пример #14
0
    def capture_page(self, filename: str = '001.png'):
        try:
            file_path = os.path.join(CAPTURE_PATH, filename)
            self.driver.execute_script("""
                        (function () {
                          var y = 0;
                          var step = 100;
                          window.scroll(0, 0);

                          function f() {
                            if (y < document.body.scrollHeight) {
                              y += step;
                              window.scroll(0, y);
                              setTimeout(f, 50);
                            } else {
                              window.scroll(0, 0);
                              document.title += "scroll-done";
                            }
                          }

                          setTimeout(f, 1000);
                        })();
                        """)
            time.sleep(2)
            self.driver.save_screenshot(filename=file_path)
            logger.info(f'save screenshot success, save path: {file_path}')
        except Exception as e:
            logger.error(f'save screenshot failed, error code: {e}')
Пример #15
0
 def wait_alert(self):
     try:
         WebDriverWait(self.driver,15).until(EC.alert_is_present())
         logger.info("had wait for the alert")
     except TimeoutError as e:
         logger.error("can't wait for alert:%s"%e)
         self.get_windows_img()
Пример #16
0
 def wait_eltext(self,locator,text):  #locator内容需要括号括起来
     try:
         WebDriverWait(self.driver,15).until(EC.text_to_be_present_in_element(locator,text))
         logger.info('had wait for the text loaded:%s'%text)
     except TimeoutError as e:
         logger.error("can't wait for the text loaded:%s"%e)
         self.get_windows_img()
Пример #17
0
 def click_defaul(self):
     try:
         self.click(*self.defaul_link)
         logger.info("成功点击默认版块")
     except Exception as e:
         logger.error("点击默认版块失败%s" % e)
         self.get_windows_img()
Пример #18
0
 def get_value(self):
     try:
         # ss_list=self.find_elements(self.s)
         # per_list=self.find_elements(self.p)
         one_s = self.find_element(*self.s_one).text
         two_s = self.find_element(*self.s_two).text
         three_s = self.find_element(*self.s_three).text
         one_per = self.find_element(*self.per_one).text
         two_per = self.find_element(*self.per_two).text
         three_per = self.find_element(*self.per_three).text
         self.s_list = [one_s, two_s, three_s]
         self.per_list = [one_per, two_per, three_per]
         # print("名称%s"%one_s)
         # s_list=[]
         # p_list=[]
         # self.a = self.find_elements(*self.s)
         # self.b = self.find_elements(*self.p)
         # for j in range(0,len(self.a)):
         #     s_list.append(self.a[j].text)
         # for p in range(0,len(self.b)):
         #     p_list.append(self.b[p].text)
         tit = self.find_element(*self.main_tit).text
         print("标题:", tit)
         for i in range(0, len(self.s_list)):
             print("名称:{}\n比例:{}".format(self.s_list[i], self.per_list[i]))
             logger.info("成功获取标题 名称 比例")
     except Exception as e:
         logger.error("获取标题 名称 比例失败%s" % e)
         self.get_windows_img()
Пример #19
0
 def wait_elements(self,locator):
     try:
         WebDriverWait(self.driver,15).until(EC.presence_of_all_elements_located(locator))
         logger.info("had wait for all elements")
     except TimeoutError as e:
         logger.error("can't wait for elements:%s"%e)
         self.get_windows_img()
Пример #20
0
 def wait_element(self, locator):
     try:
         El = WebDriverWait(self.driver,15).until(EC.presence_of_element_located(locator))
         text = El.text
         logger.info("had wait for the element: %s"%text)
     except TimeoutError as e:
         logger.error("can't wait fort the element:%s"%e)
         self.get_windows_img()
Пример #21
0
 def wait_elclickable(self,locator):
     try:
         El = WebDriverWait(self.driver,15).until(EC.element_to_be_clickable(locator))
         text = El.text
         logger.info("had wait for the elclickable: %s"%text)
     except TimeoutError as e:
         logger.error("can't wait fort the elclickable:%s"%e)
         self.get_windows_img()
Пример #22
0
 def get_haotest_tit(self):
     try:
         res = self.find_element(*self.result).text
         logger.info("成功获取得到的内容")
         return res
     except Exception as e:
         logger.error("获取得到的内容失败%s" % e)
         self.get_windows_img()
Пример #23
0
 def click_vote(self):
     try:
         self.click(*self.ele)
         self.click(*self.sss_vote)
         logger.info("成功点击发起投票")
     except Exception as e:
         logger.error("点击发起投票失败%s" % e)
         self.get_windows_img()
Пример #24
0
 def new_re(self, reply):
     try:
         self.send_keys(reply, *self.reply)
         self.click(*self.reply_button)
         logger.info("普通用户回帖成功")
     except Exception as e:
         logger.error("普通用户回帖失败%s" % e)
         self.get_windows_img()
Пример #25
0
 def get_input_text(self,selector):
     el = self.find_element(selector)
     try:
         input_text = el.get_attribute('value')
         logger.info("the input_text is:%s"% input_text)
         return input_text
     except NameError as e:
         logger.error("failed to get input value:%s"%e)
Пример #26
0
 def click_haotest(self):
     try:
         self.click(*self.hao_cli)
         self.window_handles(2)
         logger.info("成功点击搜索出的内容")
     except Exception as e:
         logger.error("点击搜索出的内容失败%s" % e)
         self.get_windows_img()
Пример #27
0
 def click_man(self):
     try:
         self.click(*self.manager_center)
         self.window_handles(1)
         logger.info("点击管理中心成功")
     except Exception as e:
         logger.error("点击管理中心失败%s" % e)
         self.get_windows_img()
Пример #28
0
 def ex(self):
     try:
         self.current_window_handle()
         self.click(*self.exi)
         logger.info("退出管理中心成功")
     except Exception as e:
         logger.error("退出管理中心失败%s" % e)
         self.get_windows_img()
Пример #29
0
 def get_element_text(self, selector):
     el = self.find_element(selector)
     try:
         element_text = el.text
         logger.info("The element_text is: %s" % element_text)
         return element_text
     except NameError as e:
         logger.error("failed to get text:%s"%e)
Пример #30
0
 def exit(self):
     try:
         self.click(*self.exit_button)
         logger.info("退出登录成功")
         return self.find_element(*self.user).text
     except Exception as e:
         logger.error("退出失败%s" % e)
         self.get_windows_img()