from config_execution_api import ticker_1 from config_execution_api import ticker_2 from config_execution_api import session_public from config_execution_api import timeframe from config_execution_api import kline_limit from func_calcultions import extract_close_prices import datetime import time from exceptions import FailedRequestError, InvalidRequestError from requests.exceptions import ConnectionError, Timeout, RequestException from json.decoder import JSONDecodeError # Get trade liquidity for ticker def get_ticker_trade_liquidity(ticker): exception = True try: # Get trades history trades = session_public.public_trading_records( symbol=ticker, limit=50 ) print("\n") print('-------------------------------------------------------------------') print("Get Ticker Trade Liquidity") print(trades) print('-------------------------------------------------------------------') print("\n") # Get the list for calculating the average liquidity quantity_list = [] if "result" in trades.keys() and len(trades["result"])>0: for trade in trades["result"]: quantity_list.append(trade["qty"]) # Return output if len(quantity_list) > 0: avg_liq = sum(quantity_list) / len(quantity_list) res_trades_price = float(trades["result"][0]["price"]) exception = False return (avg_liq, res_trades_price, exception) except (ConnectionError, Timeout, RequestException, JSONDecodeError, FailedRequestError, InvalidRequestError) as e: print(e) return (0, 0, exception) # Get start times (TF 15,30,60,240,D) def get_timestamps(): time_start_date = 0 time_next_date = 0 now = datetime.datetime.now() if timeframe == 60: time_start_date = now - datetime.timedelta(hours=kline_limit) time_next_date = now + datetime.timedelta(seconds=30) if timeframe == "D": time_start_date = now - datetime.timedelta(days=kline_limit) time_next_date = now + datetime.timedelta(minutes=1) if timeframe == 15: time_start_date = now - datetime.timedelta(minutes=kline_limit*15) time_next_date = now + datetime.timedelta(seconds=30) if timeframe == 30: time_start_date = now - datetime.timedelta(minutes=kline_limit*30) time_next_date = now + datetime.timedelta(seconds=30) if timeframe == 240: time_start_date = now - datetime.timedelta(hours=kline_limit*4) time_next_date = now + datetime.timedelta(seconds=30) time_start_seconds = int(time_start_date.timestamp()) time_now_seconds = int(now.timestamp()) time_next_seconds = int(time_next_date.timestamp()) return (time_start_seconds, time_now_seconds, time_next_seconds) # Get historical prices (klines) def get_price_klines(ticker): exception = True try: # Get prices time_start_seconds, _, _ = get_timestamps() prices = session_public.query_mark_price_kline( symbol=ticker, interval=timeframe, limit=kline_limit, from_time=time_start_seconds ) exception = False # Manage API calls time.sleep(0.1) print("\n") print('-------------------------------------------------------------------') print("Get Price Klines") print(prices) print('-------------------------------------------------------------------') print("\n") # Return prices output if len(prices["result"]) != kline_limit: return ([], exception) return (prices["result"], exception) except (ConnectionError, Timeout, RequestException, JSONDecodeError, FailedRequestError, InvalidRequestError) as e: print(e) return([], exception) # Get latest klines def get_latest_klines(): exception = False series_1 = [] series_2 = [] prices_1, exception = get_price_klines(ticker_1) if exception: return ([],[],exception) prices_2, exception = get_price_klines(ticker_2) if exception: return ([],[],exception) if len(prices_1) > 0: series_1 = extract_close_prices(prices_1) if len(prices_2) > 0: series_2 = extract_close_prices(prices_2) return (series_1, series_2, exception)