def test_create_outgoing(self): """Create an outgoing record.""" Category.create(category_id='001', description='Testing Stock') Project.create(project_id='001', project_description="Testing") Role.create(role_name='Admin') User.create(first_name='Jay', second_name='Palm', phone='9783978223', role='Admin', username='******', password='******', email='*****@*****.**') create_stock('001', 'Testing stock', 1, '001', 9.99) create_outgoing_stock(stock='001', project_id='001', date="2015-07-22", quantity='7', username='******') OutgoingStock.get(OutgoingStock.stock == '001').delete_instance() Stock.get(Stock.category == '001').delete_instance() User.get(User.username == 'JayPalm').delete_instance() Role.get(Role.role_name == 'Admin').delete_instance() Project.get(Project.project_id == '001').delete_instance() Category.get(Category.category_id == '001').delete_instance()
def get_historical(stock_symbol): now = datetime.datetime.now() # url = f'http://chart.finance.yahoo.com/table.csv?s=aapl&a=5&b=18&c=2001&d=0&e=10&f=2017&g=d&ignore=.csv' url = 'http://chart.finance.yahoo.com/table.csv?s={}&a=1&b=1&c=1900&d={}&e={}&f={}&g=d&ignore=.csv'.format( stock_symbol, now.month - 1, now.day, now.year) print(url) response = urlopen(url) data = response.read() csv_text = data.decode('utf-8') try: stock = Stock.create(name=stock_symbol, symbol=stock_symbol) except: stock = Stock.get(Stock.symbol == stock_symbol) with io.StringIO(csv_text) as csvfile: reader = csv.DictReader(csvfile) for row in reader: try: StockPrice.create( stock=stock, date=row["Date"], open=price_str_to_int(row["Open"]), high=price_str_to_int(row["High"]), low=price_str_to_int(row["Low"]), close=price_str_to_int(row["Close"]), volume=int(row["Volume"]), adj_close=price_str_to_int(row["Adj Close"]), ) except: print("skip " + row["Date"])
def add_existing_stock(pk_id, to_add): """Increase quantity for a specified record by pk_id. pk_id: primary key id """ stock = Stock.get( Stock.id == pk_id ) current_quantity = stock.quantity new_quantity = current_quantity + to_add query = Stock.update( quantity=new_quantity ).where( Stock.id == pk_id ) return query.execute()
def delete_stock(pk_id): """Delete a specified record/stock. pk_id: primary key, or stock to be deleted. """ return Stock.get(Stock.id == pk_id).delete_instance()
def get_ticker(ticker_code): stock = Stock.get(Stock.code == ticker_code.upper()) return stock
def order(stock_id=None, quantity=""): quantity = int(quantity) if quantity != "" and quantity > 0: # validate the quantity given stock = Stock.get(Stock.id == stock_id) orders = ( Order.select(fn.sum(Order.quantity)).where(Order.stock == stock.id).scalar() ) # Get the current number of orders if orders == None: # I don't want a TypeError below orders = 0 if quantity <= ( stock.minimum_quantity - orders ): # verify if quantity is less than or equal to the needed orders price = quantity * stock.price # calculate the amount the buyer has to pay Order.make_order(buyer=1, stock=stock, quantity=quantity, price=price) if ( stock.minimum_quantity == Order.select(fn.sum(Order.quantity)).where(Order.stock == stock.id).scalar() ): # check if the target has been met stock.bought = True # update stock and set it to saved stock.save() orders = Stock.get(Stock.id == stock_id).orders for order in orders: order.ready = True order.save() # replace the old stock with a fresh one with no orders yet new_stock = Stock.enter_stock( product=stock.product, first_description=stock.first_description, second_description=stock.second_description, third_description=stock.third_description, unit=stock.unit, quantity=stock.quantity, minimum_quantity=stock.minimum_quantity, brand=stock.brand, supplier=stock.supplier, price=stock.price, ) # send SMS's to shippers notifying them of ready deliveries for order in stock.orders: shippers = Courier.select() for shipper in shippers: # Account Sid and Auth Token from twilio.com/user/account account_sid = "AC7c8362f62f825e5184fe40e25958623d" auth_token = "834c6ea95160009d251b3f06893768b2" client = TwilioRestClient(account_sid, auth_token) message = client.messages.create( body="Shipping: {} to {}. Reply with your price".format( order.stock.supplier.address, order.buyer.address ), to="+{}".format(str(shipper.phone)), from_="+14782885892", ) print(message.sid) return redirect(url_for("index"))
def is_trade_day(date): staple_stock = Stock.get(Stock.symbol == "AAPL") return StockPrice.filter(StockPrice.stock == staple_stock, StockPrice.date == date).count() > 0
def get_stock_price(symbol, date): stock = Stock.get(Stock.symbol == symbol) stock_price = StockPrice.get(StockPrice.stock == stock, StockPrice.date == date) return stock_price