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_loss(symbol, quantity, stop_price, time_in_force='gtc'): """Submits a stop order to be turned into a market 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 stop_price: The price to trigger the market order. :type stop_price: float :param time_in_force: 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 time_in_force: 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() latest_price = helper.round_price(stocks.get_latest_price(symbol)[0]) stop_price = helper.round_price(stop_price) except AttributeError as msg: print(msg) return None if latest_price > stop_price: print('Error: stop_price 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': stop_price, 'quantity': quantity, 'ref_id': str(uuid4()), 'type': 'market', 'stop_price': stop_price, 'time_in_force': time_in_force, 'trigger': 'stop', 'side': 'buy' } url = urls.orders() data = helper.request_post(url, payload) return data
def order_buy_crypto_by_price(symbol, amount_in_dollars, price_type='ask_price', time_in_force='gtc'): """Submits a market order for a crypto by specifying the amount in dollars that you want to trade. Good for share fractions up to 8 decimal places. :param symbol: The crypto ticker of the crypto to trade. :type symbol: str :param amount_in_dollars: The amount in dollars of the crypto you want to buy. :type amount_in_dollars: float :param price_type: The type of price to get. Can be 'ask_price', 'bid_price', or 'mark_price' :type price_type: str :param time_in_force: 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 time_in_force: Optional[str] :returns: Dictionary that contains information regarding the selling of options, \ 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 crypto_info = crypto.get_crypto_info(symbol) price = helper.round_price( crypto.get_crypto_quote_from_id(crypto_info['id'], info=price_type)) # turn the money amount into decimal number of shares try: shares = round(amount_in_dollars / price, 8) except Exception as e: print(repr(e)) shares = 0 payload = { 'mimeType': 'application/json', 'account_id': crypto.load_crypto_profile(info="id"), 'currency_pair_id': crypto_info['id'], 'price': price, 'quantity': shares, 'ref_id': str(uuid4()), 'side': 'buy', 'time_in_force': time_in_force, 'type': 'market' } url = urls.order_crypto() data = helper.request_post(url, payload, json=True) return data
def order_sell_market(symbol, quantity, time_in_force='gtc', extended_hours='false'): """Submits a market order to be executed immediately. :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 time_in_force: 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 time_in_force: Optional[str] :param extended_hours: Premium users only. Allows trading during extended hours. Should be true or false. :type extended_hours: 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() 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': helper.round_price(stocks.get_latest_price(symbol)[0]), 'quantity': quantity, 'ref_id': str(uuid4()), 'type': 'market', 'stop_price': None, 'time_in_force': time_in_force, 'trigger': 'immediate', 'side': 'sell', 'extended_hours': extended_hours } url = urls.orders() data = helper.request_post(url, payload) 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 order_buy_crypto_by_quantity(symbol, quantity, price_type='ask_price', time_in_force='gtc'): """Submits a market order for a crypto by specifying the decimal amount of shares to buy. Good for share fractions up to 8 decimal places. :param symbol: The crypto ticker of the crypto to trade. :type symbol: str :param quantity: The decimal amount of shares to buy. :type quantity: float :param price_type: The type of price to get. Can be 'ask_price', 'bid_price', or 'mark_price' :type price_type: str :param time_in_force: 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 time_in_force: Optional[str] :returns: Dictionary that contains information regarding the selling of options, \ such as the order id, the state of order (queued,confired,filled, failed, canceled, etc.), \ the price, and the quantity. """ crypto_info = crypto.get_crypto_info(symbol) price = helper.round_price( crypto.get_crypto_quote_from_id(crypto_info['id'], info=price_type)) payload = { 'account_id': crypto.load_crypto_profile(info="id"), 'currency_pair_id': crypto_info['id'], 'price': price, 'quantity': quantity, 'ref_id': str(uuid4()), 'side': 'buy', 'time_in_force': time_in_force, 'type': 'market' } url = urls.order_crypto() data = helper.request_post(url, payload, json=True) return data