LogoLogo
  • Analytics
    • Create Chart
      • Select Mode
      • Function mode
        • 1. How to import data
          • Select Data
          • Select Time Range
          • Data Setting
        • 2. How to analyze data
          • Data Details
          • Function Setting
          • How to write functions
          • Function Library
          • Function Chart Examples
        • 3. How to visualize data
          • Line & Bar Chart
          • Doughnut Chart
          • Number Chart
          • Table Chart
          • Color Setting
      • Python mode
        • 1. How to preview data
        • 2. How to analyze data
          • Python Setting
          • How to write Python
          • Python Chart Examples
        • 3. How to visualize data
          • Line & Bar Chart
          • Table Chart
    • Publish Chart
    • Chart Examples
    • Fork Chart
  • Terminal
    • Create Bot Strategy
      • 0. How to write code
      • 1. How to preview data
    • Fork Strategy
    • Strategy Examples
    • 🔜Deploy Bot
  • Workspace
    • Manage Strategy
      • Strategy Published
      • Strategy saved as draft
    • 🔜Manage Bot
  • Account Setting
    • Change Password
    • 🔜Register Exchange Account
    • 🔜Register OTP
    • 🔜Register Payment
  • library
    • 📍Function Library
    • 📍Python Library
      • Private API
        • Binance
      • Public API
  • FAQ
    • Data
      • Dataset
        • data list
      • What type of data can I use?
      • How customizable are the data?
    • Security
      • Is UnBlinked safe?
    • Account
      • I forgot my OTP device
      • I forgot password
Powered by GitBook

Copyright © 2023, UnBlinked Co., Ltd. | All rights reserved.

On this page
  • Functions
  • 1. Get the Time-series Data
  • 2. Get the Time-series Data in DataFrame
  • Terminal-only Functions
  • 1. Stop Bot
  1. library

Python Library

PreviousFunction LibraryNextPrivate API

Last updated 1 year ago

Functions

They are the functions available in both , and .

1. Get the Time-series Data

get_raw_data(table_name: str, symbol: str, start_time: str, end_time: str) -> list

get_raw_data is a function to get the data you can find in the . It returns the data as a list of dictionaries.

Arguments

  • table_name : (string) The name of the table you want to get. It can be seen right next to the data alias at data preview. Check the to see the list of available table_name.

  • symbol : (string) A category in the table_name. For instance, if the table_name = binance_candlestick_1h and the symbol = BTCUSDT, then it would return 1 hour interval candlestick data of BTCUSDT in the Binance spot market.

  • start_time : (string)

  • end_time : (string) The start_time and end_time are the time range of the data to get. They are strings of time in yyyy-MM-dd HH:mm:ss format. You can set them by strftime('%Y-%m-%d %H:%M:%S') method of datetime.datetime.

Returns

It returns the data in a list of the dictionaries. In the dictionary, every numeric value is decimal.Decimal type, and time value is datetime.datetime.

# Basic ways to get the data
btc_price = get_raw_data("binance_candlestick_1d", 
    "BTCUSDT", 
    "2022-01-01 00:00:00",
    "2022-01-08 00:00:00")
    
# Using datetime.strftime. 2hours ago ~ now.
# Format %F %T can do the same as %Y-%m-%d %H:%M:%S
xrp_price = get_raw_data("binance_um_futures_candlestick_5min", 
    "XPRBUSDT", 
    (datetime.datetime.now() - datetime.timedelta(hours=2)).strftime('%F %T'),
    datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))

# The example of the result
'''
[
    {
        "time" : `object of datetime.datetime type`,
        "close" : `object of decimal.Decimal type`,
        "high" : `object of decimal.Decimal type`,
        "low" : `object of decimal.Decimal type`,
        "close" : `object of decimal.Decimal type`,
        ...
    },
    
    ...
]
'''


2. Get the Time-series Data in DataFrame

get_raw_data_in_df(table_name: str, symbol: str, start_time: str, end_time: str) -> pandas.DataFrame

get_raw_data_in_df is a function that works same as get_raw_data, but it refines the result into pandas.DataFrame type, and converts decimal.Decimal type values in it into float.

It is useful to apply methods from pandas such as ewm to the data.

Arguments

  • table_name : (string)

  • symbol : (string)

  • start_time : (string)

  • end_time : (string)

Returns

It returns the data in pandas.DataFrame object. In each entry, every numeric value is float, and the time value is datetime.datetime.

# The list of the arguments is same as of get_raw_data.
btc_price = get_raw_data_in_df("binance_candlestick_1d", 
    "BTCUSDT", 
    "2022-01-01 00:00:00", 
    "2022-01-08 00:00:00")


Terminal-only Functions

1. Stop Bot

shutdown(message: str|None = None) -> None

It stops the bot that the code is being run. You can call this function when you want to stop your bot in a certain condition.

Do not wrap this function by try ... except, as it is implemented by raising ShutdownException.

Arguments

  • message : (string) The message to record to the log when the function is called

Returns

None

# abstract on how the shutdown() is defined.
def shutdown(message:str=None):
    print(f"Bot shutdown. {message}")
    raise ShutdownException

# Example of using `shutdown()` function

# 1. stop the bot when USDT in the balance is more than 50 or less then 30.
balance = privateAPI.get_balance('binance')
usdt_val = balance['USDT']['amount'] if 'USDT' in balance else 0

if usdt_val > 50 or usdt_val < 30:
    shutdown()
...

# 2. You shouldn't wrap it by try ... except.
try:
    shutdown() # shutdown() doesn't work in this way!
except:
    ...


Terminal-only function can be used only in the , which means that they are not available in the analytics.

📍
terminal
analytic/python mode
terminal
data preview
data list
R1's table_name is binance_um_futures_candlestick_1d.