Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1.  
  2.  
  3. import time
  4. from bs4 import BeautifulSoup
  5. from timehelper import TimeHelper
  6. from selenium import webdriver
  7. from time import gmtime, strftime
  8. import datetime
  9. import time
  10. from selenium.webdriver.common.keys import Keys
  11. from selenium.webdriver.common.by import By
  12. from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
  13. from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
  14. def wait_and_click(browser, path):
  15. WebDriverWait(browser, 10).until(
  16. EC.element_to_be_clickable((By.XPATH, path))).click()
  17. CHART_URL = "https://www.tradingview.com/chart/QNZJQdNS/" # EOSUSDT Binance
  18. from selenium.webdriver.chrome.options import Options
  19.  
  20.  
  21. # next are only on rpi
  22.  
  23.  
  24. # This line defines your Chromium exe file location.
  25.  
  26. # PURPOSE: Obtain latest trades from trades list for the trade chart
  27. # NOTES:
  28. # 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
  29. # For scrolling down the trades table, nothing of what Selenium ActionChain offers worked. It was necessary to emulate Windows events.
  30. # Return table element HTML
  31. # UPDATE. 30.9.2018:
  32. # 1. Scrape trading pair close price from the graph view
  33.  
  34.  
  35. def scrollToElement(driver, elem):
  36. driver.execute_script("arguments[0].scrollIntoView();", elem)
  37.  
  38. def printTable(rows, driver, table):
  39. roo = []
  40. #rows = list(driver.find_elements_by_xpath('//*[@id="bottom-area"]/div[4]/div[2]/div/div/div/div/table/tbody'))
  41. try:
  42. for i in range(2000):
  43. rows = (driver.find_elements_by_xpath('//*[@id="bottom-area"]/div[4]/div[2]/div/div/div/div/table/tbody'))
  44. roo.append(rows[0].text)
  45. roo.append(rows[1].text)
  46. roo.append(rows[2].text)
  47. roo.append(rows[3].text)
  48. if roo[-1] in roo:
  49.  
  50. scrollToElement(driver, rows[-1])
  51. elif roo[-1] == rows[0].text:
  52. print ('done')
  53. break
  54.  
  55. except:
  56. for i in range(40):
  57.  
  58. print ('bottom')
  59. rows = (driver.find_elements_by_xpath('//*[@id="bottom-area"]/div[4]/div[2]/div/div/div/div/table/tbody'))
  60. roo.append(rows[0].text)
  61. roo.append(rows[1].text)
  62. roo.append(rows[2].text)
  63. roo.append(rows[3].text)
  64. if roo[-1] in roo:
  65. scrollToElement(driver, rows[-1])
  66. elif roo[-1] == rows[0].text:
  67. print ('done')
  68. print (roo)
  69. break
  70. print (len(roo))
  71. print (roo)
  72.  
  73. def ObtainLastTrades():
  74.  
  75. d = webdriver.Chrome('/usr/lib/chromium-browser/chromedriver')
  76.  
  77. d.get("https://www.tradingview.com/chart/QNZJQdNS/")
  78.  
  79. # click strategy tester button
  80. xpath = '//*/div[@data-name="backtesting"]'
  81. e = WebDriverWait(d, 600).until(
  82. EC.visibility_of_element_located((By.XPATH, xpath)))
  83. e.click()
  84.  
  85. # click list of trades button
  86. xpath = '//*/ul[@class="report-tabs"]/li[3]'
  87. wait_and_click(d, xpath)
  88. rows = (d.find_elements_by_xpath('//*[@id="bottom-area"]/div[4]/div[2]/div/div/div/div/table/tbody'))
  89. table = d.find_element_by_class_name('backtesting-content-wrapper')
  90. count = 0
  91. printTable(rows, d, table)
  92.  
  93. ObtainLastTrades()
  94. # move cursor to the table
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement