Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # scrape technical indicators data from trade.oanda.com (FOREX brokerage)
- # PLEASE NOTE: Using time.sleep is not the final intention of this code, but rather to implement implicit/explicit waits!
- from selenium import webdriver
- from selenium.webdriver.common.keys import Keys
- from selenium.webdriver.common.action_chains import ActionChains
- from selenium.webdriver.common.keys import Keys
- import time
- import settings
- settings.init()
- class indicatorScraper():
- def __init__(self, symbol, indicator, granularity):
- self.symbol = symbol.replace("_", "")
- self.indicator = indicator
- self.granularity = granularity
- chromedriver = '/Users/rdamaj/Desktop/chromedriver'
- self.driver = webdriver.Chrome(chromedriver)
- self.actions = ActionChains(self.driver)
- def stop(self):
- self.driver.quit()
- def value(self):
- # login to the brokerage
- self.driver.get('http://trade.oanda.com')
- self.driver.set_window_size(1265, 631)
- username = self.driver.find_element_by_id("username")
- password = self.driver.find_element_by_id("password")
- username.send_keys("rdamaj")
- password.send_keys("password")
- self.driver.find_element_by_id("loginButton").click()
- time.sleep(8)
- # switch to the Iframe containing the candlestick chart to be analyzed
- self.actions.move_to_element_with_offset(self.driver.find_element_by_tag_name('body'), 0,0)
- self.actions.move_by_offset(385, 115).click().perform()
- tvdiv = self.driver.find_element_by_id("tv_chart_container")
- theframe = tvdiv.find_element_by_xpath("//iframe")
- time.sleep(2)
- self.actions.move_to_element_with_offset(self.driver.find_element_by_tag_name('body'), 0,0)
- self.driver.switch_to.frame(theframe)
- # change the timeframe of the chart
- time.sleep(6)
- if self.granularity == "W":
- timeframe = self.driver.find_element_by_xpath('//div[text()="1 week"]')
- if self.granularity == "D":
- timeframe = self.driver.find_element_by_xpath('//div[text()="1 day"]')
- if self.granularity == "H4":
- timeframe = self.driver.find_element_by_xpath('//div[text()="4 hours"]')
- if self.granularity == "H1":
- timeframe = self.driver.find_element_by_xpath('//div[text()="1 hour"]')
- if self.granularity == "M15":
- timeframe = self.driver.find_element_by_xpath('//div[text()="15 minutes"]')
- # reset the action chain in order to prevent NoSuchElementException
- self.actions = ActionChains(self.driver)
- self.actions.move_to_element_with_offset(timeframe, 0,0).click().perform()
- # change the currency pair to pull data from
- start = self.driver.find_element_by_class_name("chart-widget")
- self.actions.move_to_element_with_offset(start, 0,0)
- pairname = self.driver.find_element_by_class_name("input-3lfOzLDc-")
- pairname.send_keys(self.symbol)
- pairname.send_keys(Keys.RETURN);
- # find the widget containing indicators, parse the data to pull only what is needed/numeric
- time.sleep(5)
- start = self.driver.find_element_by_class_name("chart-widget")
- self.actions.move_to_element_with_offset(start, 0,0)
- possibles = self.driver.find_elements_by_class_name("pane-legend-line")
- datalist = list()
- for p in possibles:
- try:
- val = float(p.text)
- datalist.append(val)
- #print(str(val))
- except ValueError:
- ok = 1
- #parse the data based on the specific indicator we want
- parse(datalist)
- def parse(self, data):
- if self.indicator == 'stoch':
- stochastic = [0, 0]
- stochastic[0] = data[7]
- stochastic[1] = data[8]
- print('stoch ' + str(stochastic))
- return stochastic
- if self.indicator == 'rsi':
- rsival = data[9]
- print(str(rsival))
- return rsival
- if self.indicator == 'ema':
- emas = [0, 0, 0]
- emas[0] = data[4]
- emas[1] = data[5]
- emas[2] = data[6]
- print(self.symbol + " ema10 " + str(emas[0]) + " ema20 " + str(emas[1]) + " ema50 " + str(emas[2]))
- return emas
- # test code on USD_CAD pair, grabbing the relative strength index indicator
- i1 = indicatorScraper('USD_CAD', 'rsi' , 'W')
- i1.value()
- i1.stop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement