def check_instrument(self, title): log.print_green('checking instruments...') for i in self.instruments_dict.keys(): if i.lower() in title.lower(): log.print_green('Found: ', i.lower()) return i.lower() return 'not found'
def place_order(self, data, i): ''' Funtion to place an order with ST and TP parameters ''' log.print_green('Placing an order...') print data try: self._instr = self.instruments_dict.get( data.get('instrument').upper())[0].get('instrument') self._unit = self.instruments_dict.get( data.get('instrument').upper())[3].get('tradeUnit') for x in range(1, (i + 1)): self._action = data.get('trade')[x].keys()[0] if not data.get('trade')[x][self._action]['TP']: self._tp = 0 else: self._tp = data.get('trade')[x][self._action]['TP'] self._sl = data.get('trade')[x][self._action]['SL'] log.print_warning(self._instr, self._unit, self._action, self._tp, self._sl) self.ordr = order.MyOanda(self._instr, self._unit, self._action, self._sl, self._tp) try: self._art_data.add_oanda(self.ordr.create_order()) except OandaError: log.print_error('Placing a order failed') return except AttributeError, TypeError: log.print_error('AttributeError, data NoneType') return
def do_collect_info(self, art, pageurl): #self.save_to_yaml('page', str(art)) self._art_data = {} #internal variable to hold article data log.print_green("Let's start with ", art.find("h1").text.lower()) self._art_data = tradeClass() self._art_data.title = art.find("h1").text.lower().encode('ascii', 'ignore') self._art_data.add_time(art.find("time").get("datetime")) self._art_data.ID = art.get("id") self._art_data.page_adr = pageurl self._art_data.instrument = self.check_instrument(self._art_data.title) for p in art.find('div').find_all('p'): #.text.encode('ascii', 'ignore') self._art_data.description += p.text.encode('ascii', 'ignore') log.print_warning('###### description ##### ', self._art_data.description, self._art_data.ID) self.find_tp_sl(self._art_data.description) # find take profit and sl values self._art_data.author = art.find('section', {'class' : 'autor'}).find('div', {'class' : 'about'}).find('h1').text.encode('ascii', 'ignore') #self._art_data.add_trade(self.art_data['action'], self._art_data.takestop) #log.print_warning(art.find('div').find('p').text.encode('ascii', 'ignore')) self._art_data.do_all_data() print 'take stop', self._art_data.takestop self.do_trade(self._art_data.takestop) if self._art_data.trade and (self._art_data.instrument != 'not found') : self.place_order(self._art_data.all_data, len(self._art_data.trade)) #log.print_warning('trade: ', self._art_data.trade) #log.print_warning('len: ', len(self._art_data.trade)) self._art_data.do_all_data() self.trades.update({self._art_data.ID : self._art_data.all_data}) #self.save_to_yaml(self._art_data.ID, self._art_data.all_data) #self.save_to_yaml_all(self.trades) self.db.insert(self._art_data.all_data) #insert all data to database self.trades = {} return
def place_order(self, data, i): ''' Funtion to place an order with ST and TP parameters ''' log.print_green('Placing an order...') print data try: self._instr = self.instruments_dict.get(data.get('instrument').upper())[0].get('instrument') self._unit = self.instruments_dict.get(data.get('instrument').upper())[3].get('tradeUnit') for x in range(1,(i+1)): self._action = data.get('trade')[x].keys()[0] if not data.get('trade')[x][self._action]['TP']: self._tp = 0 else: self._tp = data.get('trade')[x][self._action]['TP'] self._sl = data.get('trade')[x][self._action]['SL'] log.print_warning(self._instr, self._unit, self._action, self._tp, self._sl) self.ordr = order.MyOanda(self._instr, self._unit, self._action, self._sl, self._tp) try: self._art_data.add_oanda(self.ordr.create_order()) except OandaError: log.print_error('Placing a order failed') return except AttributeError, TypeError: log.print_error('AttributeError, data NoneType') return
def check_instrument(self, title): log.print_green('checking instruments...') for i in self.instruments_dict.keys(): print title print 'i.lower() ', i.lower() if i.lower() in title: print '2: ', title print '2: i.lower() ', i.lower() log.print_green('Found: ', i.lower()) return i.lower()
def import_test_page(self): ''' Importing test_page from mainpage.yaml file ''' try: with open(self.test_page, 'r') as f: self.test_page_cont = f.read() log.print_green('Test page imported!') except: log.print_error('Test page import failed')
def import_instruments(self): ''' Importing list of indtrument from instruments.yaml file ''' try: with open(instruments, 'r') as f: self.instruments_dict = yaml.load(f) log.print_green('Instruments imported!') except: log.print_error('Instruments import failed')
def check_artid(self, page, option): self.soup = BeautifulSoup(self.test_page_cont, "lxml") self.art = self.soup.find('article', option) if self.ask_database('time', self.art.get("id")): log.print_green('Found something with has same articleID in database already...') self.check_timestamp(self.art.get("id"), self.art) # checking if article is an update, or old one return else: log.print_warning('Found something new!') self.do_soup(page, option)
def find_tp_sl(self, info): print('finding TP and SL...') for action in trade_action.keys(): for t in trade_action.get(action): try: if t in info.lower(): self._tmp = self.find_digits(t, info.lower()) self._art_takestop.update({action : self._tmp}) except ValueError: pass log.print_green(self._art_takestop)
def find_tp_sl(self, info): log.print_green('finding TP and SL...') for action in trade_action.keys(): for t in trade_action.get(action): try: if t in info.lower(): self._tmp = self.find_digits(t, info.lower()) self._art_data.add_takestop(action, self._tmp) except ValueError: pass log.print_green('takestop: ', self._art_data.takestop)
def save_to_yaml_all(self, data): self._time = time.ctime().split(' ') self._time.remove('') self._dir_name = "./%s-%s/" % (self._time[2], self._time[1]) self._file_name = 'all_trade_ideas.yaml' try: with open(self._file_name, "ab") as f: yaml.dump(data, f) log.print_green('All data has been saved to: ', self._file_name) except IOError: log.print_error('Error during saving: ', self._file_name)
def do_update(self, new_time, artID): ''' do update for article ''' log.print_green("Let's check what has been updated") self._old_time = self.ask_database('time', artID) self._tmp = self._old_time print 'przed: ', self._tmp self._tmp2 = '{}'.format(len(self._old_time) + 1) print 'tmp2: ', self._tmp2 self._tmp.update({self._tmp2: new_time}) print 'po: ', self._tmp self.update_database('time', self._tmp, artID) return
def do_update(self, new_time, artID): ''' do update for article ''' log.print_green("Let's check what has been updated") self._old_time = self.ask_database('time', artID) self._tmp = self._old_time print 'przed: ', self._tmp self._tmp2 = '{}'.format(len(self._old_time)+1) print 'tmp2: ', self._tmp2 self._tmp.update({self._tmp2 : new_time}) print 'po: ', self._tmp self.update_database('time', self._tmp, artID) return
def check_timestamp(self, artID, content): ''' compare timestamp of last entry in time key ''' #log.print_green('I am going to compare timestamps') #self._old_time = self.all_trade_ideas.get(artID).get('time')[len(self.all_trade_ideas.get(artID).get('time'))] self._old_time = self.ask_database('time', artID) #print artID #print self._old_time #print len(self._old_time) #self._old_time = self._old_time.values() self._new_time = content.find("time").get("datetime") if self._new_time in self._old_time.values(): print('No updates') return else: log.print_green('Update of article has been detected!') return self.do_update(self._new_time, artID)
def do_collect_info(self, art, pageurl): #self.save_to_yaml('page', str(art)) self._art_data = {} #internal variable to hold article data log.print_green("Let's start with ", art.find("h1").text.lower()) self._art_data = tradeClass() self._art_data.title = art.find("h1").text.lower().encode( 'ascii', 'ignore') self._art_data.add_time(art.find("time").get("datetime")) self._art_data.ID = art.get("id") self._art_data.page_adr = pageurl self._art_data.instrument = self.check_instrument(self._art_data.title) for p in art.find('div').find_all( 'p'): #.text.encode('ascii', 'ignore') self._art_data.description += p.text.encode('ascii', 'ignore') log.print_warning('###### description ##### ', self._art_data.description, self._art_data.ID) self.find_tp_sl( self._art_data.description) # find take profit and sl values self._art_data.author = art.find('section', { 'class': 'autor' }).find('div', { 'class': 'about' }).find('h1').text.encode('ascii', 'ignore') #self._art_data.add_trade(self.art_data['action'], self._art_data.takestop) #log.print_warning(art.find('div').find('p').text.encode('ascii', 'ignore')) self._art_data.do_all_data() print 'take stop', self._art_data.takestop self.do_trade(self._art_data.takestop) if self._art_data.trade and (self._art_data.instrument != 'not found'): self.place_order(self._art_data.all_data, len(self._art_data.trade)) #log.print_warning('trade: ', self._art_data.trade) #log.print_warning('len: ', len(self._art_data.trade)) self._art_data.do_all_data() self.trades.update({self._art_data.ID: self._art_data.all_data}) #self.save_to_yaml(self._art_data.ID, self._art_data.all_data) #self.save_to_yaml_all(self.trades) self.db.insert(self._art_data.all_data) #insert all data to database self.trades = {} return
def do_trade(self, value): log.print_green('Do trade with value: ', value) try: for i in range(0,max(len(value.get('SL')), len(value.get('TP')))): log.print_green(i) try: self._temp = {} self._temp.update({'SL' : value.get('SL')[0], 'TP': value.get('TP')[i]}) log.print_green('temp1: ', self._temp) self._art_data.add_trade(self.art_data['action'], self._temp) except IndexError: self._temp = {} log.print_green('temp2: ', self._temp) self._temp.update({'SL' : value.get('SL')[i], 'TP': value.get('TP')[0]}) self._art_data.add_trade(self.art_data['action'], self._temp) except TypeError: self._art_data.trade = ''
def do_trade(self, value): log.print_green('Do trade with value: ', value) try: for i in range(0, max(len(value.get('SL')), len(value.get('TP')))): try: self._temp = {} self._temp.update({ 'SL': value.get('SL', '0')[0], 'TP': value.get('TP', '0')[i] }) log.print_green('temp1: ', self._temp) self._art_data.add_trade(self.art_data['action'], self._temp) except IndexError: self._temp = {} log.print_green('temp2: ', self._temp) self._temp.update({ 'SL': value.get('SL', '0')[i], 'TP': value.get('TP', '0')[0] }) self._art_data.add_trade(self.art_data['action'], self._temp) except TypeError: self._art_data.trade = ''
def update_database(self, what, onwhat, artID): self._result = self.db.update(what, onwhat, artID) if self._result: log.print_green(what, onwhat, artID, ' Update OK!') else: return