Advertisement
Guest User

Untitled

a guest
Feb 15th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. # scrape technical indicators data from trade.oanda.com (FOREX brokerage)
  2. # PLEASE NOTE: Using time.sleep is not the final intention of this code, but rather to implement implicit/explicit waits!
  3.  
  4. from selenium import webdriver
  5. from selenium.webdriver.common.keys import Keys
  6. from selenium.webdriver.common.action_chains import ActionChains
  7. from selenium.webdriver.common.keys import Keys
  8. import time
  9. import settings
  10. settings.init()
  11.  
  12. class indicatorScraper():
  13.  
  14. def __init__(self, symbol, indicator, granularity):
  15. self.symbol = symbol.replace("_", "")
  16. self.indicator = indicator
  17. self.granularity = granularity
  18. chromedriver = '/Users/rdamaj/Desktop/chromedriver'
  19. self.driver = webdriver.Chrome(chromedriver)
  20. self.actions = ActionChains(self.driver)
  21.  
  22. def stop(self):
  23. self.driver.quit()
  24.  
  25. def value(self):
  26. # login to the brokerage
  27. self.driver.get('http://trade.oanda.com')
  28. self.driver.set_window_size(1265, 631)
  29. username = self.driver.find_element_by_id("username")
  30. password = self.driver.find_element_by_id("password")
  31. username.send_keys("rdamaj")
  32. password.send_keys("password")
  33. self.driver.find_element_by_id("loginButton").click()
  34. time.sleep(8)
  35.  
  36. # switch to the Iframe containing the candlestick chart to be analyzed
  37. self.actions.move_to_element_with_offset(self.driver.find_element_by_tag_name('body'), 0,0)
  38. self.actions.move_by_offset(385, 115).click().perform()
  39. tvdiv = self.driver.find_element_by_id("tv_chart_container")
  40. theframe = tvdiv.find_element_by_xpath("//iframe")
  41. time.sleep(2)
  42. self.actions.move_to_element_with_offset(self.driver.find_element_by_tag_name('body'), 0,0)
  43. self.driver.switch_to.frame(theframe)
  44.  
  45. # change the timeframe of the chart
  46. time.sleep(6)
  47. if self.granularity == "W":
  48. timeframe = self.driver.find_element_by_xpath('//div[text()="1 week"]')
  49. if self.granularity == "D":
  50. timeframe = self.driver.find_element_by_xpath('//div[text()="1 day"]')
  51. if self.granularity == "H4":
  52. timeframe = self.driver.find_element_by_xpath('//div[text()="4 hours"]')
  53. if self.granularity == "H1":
  54. timeframe = self.driver.find_element_by_xpath('//div[text()="1 hour"]')
  55. if self.granularity == "M15":
  56. timeframe = self.driver.find_element_by_xpath('//div[text()="15 minutes"]')
  57.  
  58. # reset the action chain in order to prevent NoSuchElementException
  59. self.actions = ActionChains(self.driver)
  60. self.actions.move_to_element_with_offset(timeframe, 0,0).click().perform()
  61.  
  62. # change the currency pair to pull data from
  63. start = self.driver.find_element_by_class_name("chart-widget")
  64. self.actions.move_to_element_with_offset(start, 0,0)
  65. pairname = self.driver.find_element_by_class_name("input-3lfOzLDc-")
  66. pairname.send_keys(self.symbol)
  67. pairname.send_keys(Keys.RETURN);
  68.  
  69. # find the widget containing indicators, parse the data to pull only what is needed/numeric
  70. time.sleep(5)
  71. start = self.driver.find_element_by_class_name("chart-widget")
  72. self.actions.move_to_element_with_offset(start, 0,0)
  73. possibles = self.driver.find_elements_by_class_name("pane-legend-line")
  74. datalist = list()
  75. for p in possibles:
  76. try:
  77. val = float(p.text)
  78. datalist.append(val)
  79. #print(str(val))
  80. except ValueError:
  81. ok = 1
  82.  
  83. #parse the data based on the specific indicator we want
  84. parse(datalist)
  85.  
  86. def parse(self, data):
  87. if self.indicator == 'stoch':
  88. stochastic = [0, 0]
  89. stochastic[0] = data[7]
  90. stochastic[1] = data[8]
  91.  
  92. print('stoch ' + str(stochastic))
  93. return stochastic
  94.  
  95. if self.indicator == 'rsi':
  96. rsival = data[9]
  97. print(str(rsival))
  98. return rsival
  99.  
  100. if self.indicator == 'ema':
  101. emas = [0, 0, 0]
  102. emas[0] = data[4]
  103. emas[1] = data[5]
  104. emas[2] = data[6]
  105. print(self.symbol + " ema10 " + str(emas[0]) + " ema20 " + str(emas[1]) + " ema50 " + str(emas[2]))
  106. return emas
  107.  
  108. # test code on USD_CAD pair, grabbing the relative strength index indicator
  109. i1 = indicatorScraper('USD_CAD', 'rsi' , 'W')
  110. i1.value()
  111. i1.stop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement