def delete_symbols_from_watchlist(inputSymbols, name="My First List"): """Deletes multiple stock tickers from a watchlist. :param inputSymbols: May be a single stock ticker or a list of stock tickers. :type inputSymbols: str or list :param name: The name of the watchlist to delete data from. :type name: Optional[str] :returns: Returns result of the delete request. """ symbols = helper.inputs_to_set(inputSymbols) ids = stocks.get_instruments_by_symbols(symbols, info='id') data = [] #Get id of requested watchlist all_watchlists = get_all_watchlists() watchlist_id = '' for wl in all_watchlists['results']: if wl['display_name'] == name: watchlist_id = wl['id'] for id in ids: payload = { watchlist_id: [{ "object_type": "instrument", "object_id": id, "operation": "delete" }] } url = urls.watchlists(name, True) data.append(helper.request_post(url, payload, json=True)) return (data)
def order(symbol, quantity, orderType, trigger, side, limitPrice = None, stopPrice = None, timeInForce = 'gtc', extendedHours = False): """A generic order function. All parameters must be supplied. :param symbol: The stock ticker of the stock to sell. :type symbol: str :param quantity: The number of stocks to sell. :type quantity: int :param orderType: Either 'market' or 'limit' :type orderType: str :param trigger: Either 'immediate' or 'stop' :type trigger: str :param side: Either 'buy' or 'sell' :type side: str :param limitPrice: The price to trigger the market order. :type limitPrice: float :param stopPrice: The price to trigger the limit or market order. :type stopPrice: float :param timeInForce: Changes how long the order will be in effect for. 'gtc' = good until cancelled. \ 'gfd' = good for the day. 'ioc' = immediate or cancel. 'opg' execute at opening. :type timeInForce: str :param extendedHours: Premium users only. Allows trading during extended hours. Should be true or false. :type extendedHours: Optional[str] :returns: Dictionary that contains information regarding the purchase or selling of stocks, \ such as the order id, the state of order (queued,confired,filled, failed, canceled, etc.), \ the price, and the quantity. """ try: symbol = symbol.upper().strip() except AttributeError as message: print(message) return None if stopPrice: stopPrice = helper.round_price(stopPrice) if limitPrice: limitPrice = helper.round_price(limitPrice) else: limitPrice = helper.round_price(stocks.get_latest_price(symbol)[0]) payload = { 'account': profiles.load_account_profile(info='url'), 'instrument': stocks.get_instruments_by_symbols(symbol, info='url')[0], 'symbol': symbol, 'price': limitPrice, 'quantity': quantity, 'ref_id': str(uuid4()), 'type': orderType, 'stop_price': stopPrice, 'time_in_force': timeInForce, 'trigger': trigger, 'side': side, 'extended_hours': extendedHours } url = urls.orders() data = helper.request_post(url, payload) return(data)
def order_sell_stop_limit(symbol, quantity, limitPrice, stopPrice, timeInForce='gtc', extendedHours=False): """Submits a stop order to be turned into a limit order once a certain stop price is reached. :param symbol: The stock ticker of the stock to sell. :type symbol: str :param quantity: The number of stocks to sell. :type quantity: int :param limitPrice: The price to trigger the market order. :type limitPrice: float :param stopPrice: The price to trigger the limit order. :type stopPrice: float :param timeInForce: Changes how long the order will be in effect for. 'gtc' = good until cancelled. \ 'gfd' = good for the day. 'ioc' = immediate or cancel. 'opg' execute at opening. :type timeInForce: Optional[str] :param extendedHours: Premium users only. Allows trading during extended hours. Should be true or false. :type extendedHours: Optional[str] :returns: Dictionary that contains information regarding the selling of stocks, \ such as the order id, the state of order (queued,confired,filled, failed, canceled, etc.), \ the price, and the quantity. """ try: symbol = symbol.upper().strip() latestPrice = helper.round_price(stocks.get_latest_price(symbol)[0]) stopPrice = helper.round_price(stopPrice) limitPrice = helper.round_price(limitPrice) except AttributeError as message: print(message) return None if (latestPrice < stopPrice): print('Error: stopPrice must be below the current price.') return (None) payload = { 'account': profiles.load_account_profile(info='url'), 'instrument': stocks.get_instruments_by_symbols(symbol, info='url')[0], 'symbol': symbol, 'price': limitPrice, 'quantity': quantity, 'ref_id': str(uuid4()), 'type': 'limit', 'stop_price': stopPrice, 'time_in_force': timeInForce, 'trigger': 'stop', 'side': 'sell', 'extended_hours': extendedHours } url = urls.orders() data = helper.request_post(url, payload) return (data)
def order_buy_stop_limit(symbol,quantity,limitPrice,stopPrice,timeInForce='gtc'): """Submits a stop order to be turned into a limit order once a certain stop price is reached. :param symbol: The stock ticker of the stock to purchase. :type symbol: str :param quantity: The number of stocks to buy. :type quantity: int :param limitPrice: The price to trigger the market order. :type limitPrice: float :param stopPrice: The price to trigger the limit order. :type stopPrice: float :param timeInForce: Changes how long the order will be in effect for. 'gtc' = good until cancelled. \ 'gfd' = good for the day. 'ioc' = immediate or cancel. 'opg' execute at opening. :type timeInForce: Optional[str] :returns: Dictionary that contains information regarding the purchase of stocks, \ such as the order id, the state of order (queued,confired,filled, failed, canceled, etc.), \ the price, and the quantity. """ try: symbol = symbol.upper().strip() latestPrice = float(stocks.get_latest_price(symbol)[0]) stopPrice = float(stopPrice) limitPrice = float(limitPrice) except AttributeError as message: print(message) return None if (latestPrice > stopPrice): print('Error: stopPrice must be above the current price.') return(None) payload = { 'account': profiles.load_account_profile(info='url'), 'instrument': stocks.get_instruments_by_symbols(symbol,info='url')[0], 'symbol': symbol, 'price': limitPrice, 'quantity': quantity, 'ref_id': helper.get_device_token(), 'type': 'limit', 'stop_price': stopPrice, 'time_in_force': timeInForce, 'trigger': 'stop', 'side': 'buy' } url = urls.orders() data = helper.request_post(url,payload) return(data)
def order_buy_market(symbol, quantity, timeInForce='gtc', extendedHours='false'): """Submits a market order to be executed immediately. :param symbol: The stock ticker of the stock to purchase. :type symbol: str :param quantity: The number of stocks to buy. :type quantity: int :param timeInForce: Changes how long the order will be in effect for. 'gtc' = good until cancelled. \ 'gfd' = good for the day. 'ioc' = immediate or cancel. 'opg' execute at opening. :type timeInForce: Optional[str] :param extendedHours: Premium users only. Allows trading during extended hours. Should be true or false. :type extendedHours: str :returns: Dictionary that contains information regarding the purchase of stocks, \ such as the order id, the state of order (queued,confired,filled, failed, canceled, etc.), \ the price, and the quantity. """ try: symbol = symbol.upper().strip() except AttributeError as message: print(message) return None payload = { 'account': profiles.load_account_profile(info='url'), 'instrument': stocks.get_instruments_by_symbols(symbol, info='url')[0], 'symbol': symbol, 'price': float(stocks.get_latest_price(symbol)[0]), 'quantity': quantity, 'ref_id': str(uuid4()), 'type': 'market', 'stop_price': None, 'time_in_force': timeInForce, 'trigger': 'immediate', 'side': 'buy', "extended_hours": extendedHours } url = urls.orders() data = helper.request_post(url, payload) return (data)
def delete_symbols_from_watchlist(inputSymbols, name='Default'): """Deletes multiple stock tickers from a watchlist. :param inputSymbols: May be a single stock ticker or a list of stock tickers. :type inputSymbols: str or list :param name: The name of the watchlist to delete data from. :type name: Optional[str] :returns: Returns result of the delete request. """ symbols = helper.inputs_to_set(inputSymbols) ids = stocks.get_instruments_by_symbols(symbols, info='id') data = [] for id in ids: url = urls.watchlist_delete(name, id) data.append(helper.request_delete(url)) return (data)
def order_sell_limit(symbol,quantity,limitPrice,timeInForce='gtc'): """Submits a limit order to be executed once a certain price is reached. :param symbol: The stock ticker of the stock to sell. :type symbol: str :param quantity: The number of stocks to sell. :type quantity: int :param limitPrice: The price to trigger the sell order. :type limitPrice: float :param timeInForce: Changes how long the order will be in effect for. 'gtc' = good until cancelled. \ 'gfd' = good for the day. 'ioc' = immediate or cancel. 'opg' execute at opening. :type timeInForce: Optional[str] :returns: Dictionary that contains information regarding the selling of stocks, \ such as the order id, the state of order (queued,confired,filled, failed, canceled, etc.), \ the price, and the quantity. """ try: symbol = symbol.upper().strip() limitPrice = helper.round_price(limitPrice) except AttributeError as message: print(message) return None payload = { 'account': profiles.load_account_profile(info='url'), 'instrument': stocks.get_instruments_by_symbols(symbol,info='url')[0], 'symbol': symbol, 'price': limitPrice, 'quantity': quantity, 'ref_id': str(uuid4()), 'type': 'limit', 'stop_price': None, 'time_in_force': timeInForce, 'trigger': 'immediate', 'side': 'sell' } url = urls.orders() data = helper.request_post(url,payload) return(data)
def find_orders(**arguments): """Returns a list of orders that match the keyword parameters. :param arguments: Variable length of keyword arguments. EX. find_orders(symbol='FB',cancel=None,quantity=1) :type arguments: str :returns: Returns a list of orders. """ url = urls.orders() data = helper.request_get(url, 'pagination') if (len(arguments) == 0): return (data) for item in data: item['quantity'] = str(int(float(item['quantity']))) if 'symbol' in arguments.keys(): arguments['instrument'] = stocks.get_instruments_by_symbols( arguments['symbol'], info='url')[0] del arguments['symbol'] if 'quantity' in arguments.keys(): arguments['quantity'] = str(arguments['quantity']) stop = len(arguments.keys()) - 1 list_of_orders = [] for item in data: for i, (key, value) in enumerate(arguments.items()): if key not in item: print(helper.error_argument_not_key_in_dictionary(key)) return ([None]) if value != item[key]: break if i == stop: list_of_orders.append(item) return (list_of_orders)