Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- from bs4 import BeautifulSoup
- from timehelper import TimeHelper
- from selenium import webdriver
- from time import gmtime, strftime
- import datetime
- import time
- from selenium.webdriver.common.keys import Keys
- from selenium.webdriver.common.by import By
- from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
- from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
- def wait_and_click(browser, path):
- WebDriverWait(browser, 10).until(
- EC.element_to_be_clickable((By.XPATH, path))).click()
- CHART_URL = "https://www.tradingview.com/chart/QNZJQdNS/" # EOSUSDT Binance
- from selenium.webdriver.chrome.options import Options
- # next are only on rpi
- # This line defines your Chromium exe file location.
- # PURPOSE: Obtain latest trades from trades list for the trade chart
- # NOTES:
- # Trades list is dynamicaly generated table thus it is necessary to scroll it down to the last row in order to read active/last trade
- # For scrolling down the trades table, nothing of what Selenium ActionChain offers worked. It was necessary to emulate Windows events.
- # Return table element HTML
- # UPDATE. 30.9.2018:
- # 1. Scrape trading pair close price from the graph view
- def scrollToElement(driver, elem):
- driver.execute_script("arguments[0].scrollIntoView();", elem)
- def printTable(rows, driver, table):
- roo = []
- #rows = list(driver.find_elements_by_xpath('//*[@id="bottom-area"]/div[4]/div[2]/div/div/div/div/table/tbody'))
- try:
- for i in range(2000):
- rows = (driver.find_elements_by_xpath('//*[@id="bottom-area"]/div[4]/div[2]/div/div/div/div/table/tbody'))
- roo.append(rows[0].text)
- roo.append(rows[1].text)
- roo.append(rows[2].text)
- roo.append(rows[3].text)
- if roo[-1] in roo:
- scrollToElement(driver, rows[-1])
- elif roo[-1] == rows[0].text:
- print ('done')
- break
- except:
- for i in range(40):
- print ('bottom')
- rows = (driver.find_elements_by_xpath('//*[@id="bottom-area"]/div[4]/div[2]/div/div/div/div/table/tbody'))
- roo.append(rows[0].text)
- roo.append(rows[1].text)
- roo.append(rows[2].text)
- roo.append(rows[3].text)
- if roo[-1] in roo:
- scrollToElement(driver, rows[-1])
- elif roo[-1] == rows[0].text:
- print ('done')
- print (roo)
- break
- print (len(roo))
- print (roo)
- def ObtainLastTrades():
- d = webdriver.Chrome('/usr/lib/chromium-browser/chromedriver')
- d.get("https://www.tradingview.com/chart/QNZJQdNS/")
- # click strategy tester button
- xpath = '//*/div[@data-name="backtesting"]'
- e = WebDriverWait(d, 600).until(
- EC.visibility_of_element_located((By.XPATH, xpath)))
- e.click()
- # click list of trades button
- xpath = '//*/ul[@class="report-tabs"]/li[3]'
- wait_and_click(d, xpath)
- rows = (d.find_elements_by_xpath('//*[@id="bottom-area"]/div[4]/div[2]/div/div/div/div/table/tbody'))
- table = d.find_element_by_class_name('backtesting-content-wrapper')
- count = 0
- printTable(rows, d, table)
- ObtainLastTrades()
- # move cursor to the table
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement