def export_completed_stock_orders(dir_path, file_name=None): """Write all completed orders to a csv file :param dir_path: Absolute or relative path to the directory the file will be written. :type dir_path: str :param file_name: An optional argument for the name of the file. If not defined, filename will be stock_orders_{current date} :type file_name: Optional[str] """ file_path = create_absolute_csv(dir_path, file_name, 'stock') all_orders = orders.get_all_stock_orders() with open(file_path, 'w', newline='') as f: csv_writer = writer(f) csv_writer.writerow([ 'symbol', 'date', 'order_type', 'side', 'fees', 'quantity', 'average_price' ]) for order in all_orders: if order['state'] == 'filled' and order['cancel'] is None: csv_writer.writerow([ stocks.get_symbol_by_url(order['instrument']), order['last_transaction_at'], order['type'], order['side'], order['fees'], order['quantity'], order['average_price'] ]) f.close()
def get_top_movers(info=None): """Returns a list of the Top 20 movers on Robin Hood. :param info: Will filter the results to get a specific value. :type info: Optional[str] :returns: Returns a list of dictionaries of key/value pairs for each mover. If info parameter is provided, \ a list of strings is returned where the strings are the value of the key that matches info. :Dictionary Keys: * ask_price * ask_size * bid_price * bid_size * last_trade_price * last_extended_hours_trade_price * previous_close * adjusted_previous_close * previous_close_date * symbol * trading_halted * has_traded * last_trade_price_source * updated_at * instrument """ url = urls.movers_top() data = helper.request_get(url, 'regular') data = helper.filter(data, 'instruments') symbols = [stocks.get_symbol_by_url(x) for x in data] data = stocks.get_quotes(symbols) return(helper.filter(data, info))
def export_completed_stock_orders(file_path): """Write all completed orders to a csv file :param file_path: absolute path to the location the file will be written. :type file_path: str """ all_orders = orders.get_all_stock_orders() with open( f"{file_path}stock_orders_{dt.date.today().strftime('%b-%d-%Y')}.csv", 'w', newline='') as f: writer = csv.writer(f) writer.writerow([ 'symbol', 'date', 'order_type', 'side', 'fees', 'quantity', 'average_price' ]) for order in all_orders: if order['state'] == 'filled' and order['cancel'] is None: writer.writerow([ stocks.get_symbol_by_url(order['instrument']), order['last_transaction_at'], order['type'], order['side'], order['fees'], order['quantity'], order['average_price'] ]) f.close()
def get_top_movers(info=None): """Returns a list of the Top 20 movers on Robin Hood. :type info: Optional[str] :returns: Returns a list of dictionaries of key/value pairs for each mover. If info parameter is provided, \ a list of strings is returned where the strings are the value of the key that matches info. """ url = urls.movers_top() data = helper.request_get(url, 'regular') data = helper.filter(data, 'instruments') symbols = [stocks.get_symbol_by_url(x) for x in data] data = stocks.get_quotes(symbols) return(helper.filter(data, info))
def get_all_stocks_from_market_tag(tag, info=None): """Returns all the stock quote information that matches a tag category. :param tag: The category to filter for. Examples include 'biopharmaceutical', 'upcoming-earnings', 'most-popular-under-25', and 'technology'. :type tag: str :param info: Will filter the results to get a specific value. :type info: Optional[str] :returns: Returns a list of dictionaries of key/value pairs for each mover. If info parameter is provided, \ a list of strings is returned where the strings are the value of the key that matches info. :Dictionary Keys: * ask_price * ask_size * bid_price * bid_size * last_trade_price * last_extended_hours_trade_price * previous_close * adjusted_previous_close * previous_close_date * symbol * trading_halted * has_traded * last_trade_price_source * updated_at * instrument """ url = urls.market_category(tag) data = helper.request_get(url, 'regular') data = helper.filter(data, 'instruments') if not data: print('ERROR: "{}" is not a valid tag'.format(tag), file=helper.get_output()) return [None] symbols = [stocks.get_symbol_by_url(x) for x in data] data = stocks.get_quotes(symbols) return (helper.filter(data, info))