def handle_requests(user_id): requests = Exchange.query.filter(Exchange.status != 'approved'). \ filter(Exchange.lender_id == user_id).all() if not requests: abort( make_response( jsonify({ 'message': 'No requests for this user OR user does not exist', 'success': False }))) for req in requests: req.status = 'approved' try: Exchange.update(req) except Exception as e: db.session.rollback() abort(404, e) return jsonify({ "success": True, "message": "Your requests are approved" })
def update(): EXCHANGE_URL = 'http://www.brou.com.uy/web/guest/institucional/cotizaciones' html = urlopen(EXCHANGE_URL).read() htmldoc = htmlParseDoc(html, 'utf8') CURRENCIES = ('dolar', 'euro', 'argentino', 'real') # texto en celdas hermanas a la que tiene el nombre de la divisa: EXCHANGE_XPATH = \ '//td[normalize-space(text())="%s"]/following-sibling::td/text()' for currency_slug in CURRENCIES: try: currency = Currency.objects.get(slug=currency_slug) params = {'currency': currency, 'date': date.today()} exchange = Exchange.objects.filter(**params) if exchange.count(): exchange = exchange[0] else: exchange = Exchange(**params) exchange.buy, exchange.sale = [ td.get_content().replace(',', '.') for td in \ htmldoc.xpathEval2(EXCHANGE_XPATH % currency.name)] exchange.save() except Exception, e: print "ERROR:", currency_slug, e
def ensure_exchange_exists(exchange_name): exchange = Exchange.query.filter(Exchange.name == exchange_name).first() if not exchange: exchange = Exchange(name=exchange_name) db_session.add(exchange) db_session.commit() return exchange
def test_exchange_commits(self): exchange = Exchange( 'NMS', 'National Market System', 'USD', 'USA', '19,223 billion') db.session.add(exchange) db.session.commit() # assert exchange is in db self.assertIn(exchange, db.session)
def test_exchange_returns(self): exchange = Exchange( 'NMS', 'National Market System', 'USD', 'USA', '19,223 billion') db.session.add(exchange) db.session.commit() # assert it retuns correct data self.assertEqual('<Exchange \'NMS\'>', str(exchange.query.first()))
def request_book(valid, user_id): if valid: try: new_request = request.get_json() except Exception as e: abort(400, str(e)) #todo ---- if all(k in new_request for k in ('lender_id', 'book_id')): exchange = Exchange( requester_id=user_id, # TODO - requester_id later to be picked from db using session user name lender_id=new_request['lender_id'], book_id=new_request['book_id'], status='pending for approval') try: exchange.insert() except sqlalchemy.exc.IntegrityError as e: abort( make_response( jsonify({ 'message': 'provided user or book does not exist' }))) except Exception as e: abort(404, str(e)) result = { "success": True, "message": 'request submitted successfully' } return jsonify(result) else: abort( make_response( jsonify({ 'success': False, 'message': 'You seem to have missed some data: Provide ' 'requester_id(user_id of the requester), lender_id(book owner:user_id), ' })))
def gift_request(self, request): user = request.user c_id = self.cleaned_data.get('category') category = Category.objects.get(id=c_id) link = self.cleaned_data.get('link') description = self.cleaned_data.get('description') receiver = Profile.objects.get(id=user.id) gift = Exchange(category=category, link=link, description=description, receiver=receiver, request_date=datetime.datetime.now()) if gift is not None: print "gift desc: ", gift.description print "gift receiver: ", gift.receiver print "gift link: ", gift.link print "gift category: ", gift.category gift.save() return gift
def get_new_data(): """ Returns only new data as dict based on previous data import job. """ data = get_exchange_rates() last_imported_date = Exchange.get_latest().date index = get_index(data, last_imported_date) if index: return data[index] return []
def get_exchange(): for page_id in range(1, 2): try: url = CK_API_URL.format('exchanges') response = get_request(url) for exchange_data in response: try: return DB.open().query(Exchange).filter(Exchange.api_url == exchange_data['api']).one() except: obj = get_request(exchange_data['api']) if 'id' in obj: exchange = Exchange() exchange.parse_json(obj) DB.save(exchange) if exchange: print('SAVED EXCHANGE ID {}'.format(exchange.id)) print('+++++++++++++++++') except: pass
def exchange_with_max_lots(): new_exchange = Exchange( name="BTC/USD", description="BTC price in USD, 5 seconds to bet, 5 to wait", bet_and_lock_seconds=5, max_spin_seconds=5, round_decided_threshold=2, max_bet_amount=1000000, ) db.session.add(new_exchange) db.session.commit() yield new_exchange
def test_exchange_relations(self): currency = Currency('US Dollar', 'USD', "USA", "NMS", 1) location = Location('USA', 'US', 'Washington DC', '16.77 trillion USD', 'USD', 'National Market System') exchange = Exchange( 'NMS', 'National Market System', 'USD', 'USA', '19,223 billion') db.session.add(exchange) db.session.add(currency) db.session.add(location) db.session.commit() # assert it retuns correct data self.assertEqual('USD', str(exchange.query.first().currency)) self.assertEqual('USA', str(exchange.query.first().location))
def test_currency_relations(self): currency = Currency('US Dollar', 'USD', "USA", "NMS", 1) exchange = Exchange( 'National Market System', 'NMS', 'USD', 'USA', '19,223 billion') location = location = Location( 'USA', 'US', 'Washington DC', '16.77 trillion USD', 'USD', 'National Market System') db.session.add(currency) db.session.add(exchange) db.session.add(location) db.session.commit() # assert currency is on other models self.assertEqual('NMS', str(currency.query.first().exchanges)) self.assertEqual('USA', str(currency.query.first().locations))
def save_snapshot(exchange_name, instrument_name, bids_sizes, bids_prices, asks_sizes, asks_prices, ts): exchange = Exchange.get(Exchange.name == exchange_name) db_instrument = Instrument.get((Instrument.name == instrument_name) & (Instrument.exchange_name == exchange.name)) base = db_instrument.base quote = db_instrument.quote kind = db_instrument.kind snap = OrderbookRecord(base=base, quote=quote, exchange=exchange, ask_sizes=asks_sizes, ask_prices=asks_prices, bid_sizes=bids_sizes, bid_prices=bids_prices, timestamp=ts, kind=kind) return snap.save()
def day_loop(begin_dt): # 关注标的 focus_symbols = Symbol.FOCUS_SYMBOLS # 交易所 exchange = Exchange() exchange_cursor = exchange.collection.find() for e_document in exchange_cursor: exchange_name = e_document["name"] for focus_symbol in focus_symbols: error_log.error("{} {} start".format(exchange_name, begin_dt)) # 查询trade数据 trade = Trade(exchange_name, focus_symbol) kline = Kline(exchange_name, focus_symbol) # 计算 klines = calculate(trade, kline, begin_dt) # 存储 insert(kline, begin_dt, klines)
def test_company_relations(self): currency = Currency('US Dollar', 'USD', "USA", "NMS", 1) location = Location('USA', 'US', 'Washington DC', '16.77 trillion USD', 'USD', 'National Market System') exchange = Exchange( 'NMS', 'National Market System', 'USD', 'USA', '19,223 billion') company = Company( 'CASS', 'Cass Information Systems, Inc', 'NMS', 'USD', 'USA', '51,29', '50.82', '+2.42%', '59.09', '52.06', '2.07', '0.00', '51.20-52.22', '+4,24%', '+3.80%', '22222', '27743', '585.15M') db.session.add(exchange) db.session.add(currency) db.session.add(location) db.session.add(company) db.session.commit() # assert it retuns correct data self.assertEqual('USA', str(company.query.first().location)) self.assertEqual('NMS', str(company.query.first().exchange)) self.assertEqual('USD', str(company.query.first().currency))
def testExchangeNotFoundInvalidRequest(self, mock_get_exchange): """ Test response for a valid request Ensure: if exchange not found in database, call source to get one. """ mock_get_exchange.return_value = Exchange(source=self.eur, target=self.brl, value=Decimal(4, 20)) url = reverse('api:exchange_detail', args={ 'source': 'XXX', 'target': 'DDD' }) factory = APIRequestFactory() factory = APIRequestFactory() view = ExchangeDetail.as_view() # Make an authenticated request to the view... request = factory.get(url) force_authenticate(request, user=self.user) response = view(request, 'XXX', 'DDD') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) # ensures not parser called assert not mock_get_exchange.called
from local_models import Service import umsgpack import copy target_exchange, target_instrument = sys.argv[1], sys.argv[2] #orderbook_service_details = db_service_interface.find_service('OrderbookFeeder', instrument=target_instrument, exchange=target_exchange) orderbook_service_details = Service.get( (Service.name == 'OrderbookFeeder') & (Service.exchange == target_exchange) & (Service.instrument == target_instrument)) exchange_name, instrument, shm_path = target_exchange, target_instrument, orderbook_service_details.address obh_a = RtOrderbookReader(shm_path) exchange = Exchange.get(Exchange.name == target_exchange) db_instrument = Instrument.get((Instrument.name == target_instrument) & (Instrument.exchange_name == exchange.name)) base = db_instrument.base quote = db_instrument.quote close_db_conn() last_bids, last_asks = None, None same_count = 0 def snapshot_orderbook(obh): global last_bids global last_asks global same_count
def insert_data(session, data, metadata): """ Insert data into db :param data: df with columns date high low open close volume ajd_close :param metadata: df with single row name ticker security exchange """ if not metadata: # updating print('No metadata') else: # new data exchange = Exchange(name=metadata['exchange']) security = Security(type=metadata['security']) mapped_ticker = Ticker( ticker=metadata['ticker'], name=metadata['name'], exchange=exchange, security=security) # TODO create a mapping object data = data.to_dict(orient='records') # list of dicts # print('Debugging - mapped_ticker') # debugging # print(mapped_ticker) # debugging # print('Debugging - Data length') # debugging # print(len(data)) # debugging # print('Debugging - Data') # debugging # print(data) price_list = list() for item in data: # merge metadata to data date = item['date'] high = item['high'] low = item['low'] open = item['open'] close = item['close'] volume = item['volume'] adj_close = item['adj_close'] # data_point = Price(date=date, # open=open, # high=high, # low=low, # close=close, # adj_close=adj_close, # ticker=mapped_ticker) data_point = { 'date': date, 'open': open, 'high': high, 'low': low, 'close': close, 'adj_close': adj_close, 'ticker': mapped_ticker } price_list.append(data_point) # print('Debugging - printing data_point') # debugging # print(data_point) # debugging # print('Debugging - price_list') # debugging # print(price_list) # debugging # print(f'Inserting data into DB') # debugging session.bulk_insert_mappings(Price, price_list)