def export_completed_crypto_orders(dir_path, file_name=None): """Write all completed crypto 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 crypto_orders_{current date} :type file_name: Optional[str] """ file_path = create_absolute_csv(dir_path, file_name, 'crypto') all_orders = orders.get_all_crypto_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_url'] is None: try: fees = order['fees'] except KeyError: fees = 0.0 csv_writer.writerow([ crypto.get_crypto_quote_from_id(order['currency_pair_id'], 'symbol'), order['last_transaction_at'], order['type'], order['side'], fees, order['quantity'], order['average_price'] ]) f.close()
def order_buy_crypto_by_price(symbol, amountInDollars, priceType='ask_price', timeInForce='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 amountInDollars: The amount in dollars of the crypto you want to buy. :type amountInDollars: float :param priceType: The type of price to get. Can be 'ask_price', 'bid_price', or 'mark_price' :type priceType: str :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 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 = round( float( crypto.get_crypto_quote_from_id(crypto_info['id'], info=priceType)), 8) # turn the money amount into decimal number of shares try: shares = round(amountInDollars / float(price), 8) except: 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': timeInForce, 'type': 'market' } url = urls.order_crypto() data = helper.request_post(url, payload, json=True) return (data)
def order_sell_crypto_by_quantity(symbol, quantity, priceType='ask_price', timeInForce='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 sell. :type quantity: float :param priceType: The type of price to get. Can be 'ask_price', 'bid_price', or 'mark_price' :type priceType: str :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 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 = round( float( crypto.get_crypto_quote_from_id(crypto_info['id'], info=priceType)), 8) print("pice is ", price) payload = { 'account_id': crypto.load_crypto_profile(info="id"), 'currency_pair_id': crypto_info['id'], 'price': price, 'quantity': quantity, 'ref_id': str(uuid4()), 'side': 'sell', 'time_in_force': timeInForce, 'type': 'market' } url = urls.order_crypto() data = helper.request_post(url, payload, json=True) return (data)