unicorn_binance_trailing_stop_loss package

Submodules

unicorn_binance_trailing_stop_loss.manager module

class unicorn_binance_trailing_stop_loss.manager.BinanceTrailingStopLossManager(api_key: str = None, api_secret: str = None, borrow_threshold: str = None, callback_error: BuiltinMethodType | None = None, callback_finished: BuiltinMethodType | None = None, callback_partially_filled: BuiltinMethodType | None = None, disable_colorama: bool = False, engine: str = 'trail', exchange: str = 'binance.com', keep_threshold: str = None, market: str = None, print_notifications: bool = False, reset_stop_loss_price: bool = False, send_to_email_address: str = None, send_from_email_address: str = None, send_from_email_password: str = None, send_from_email_server: str = None, send_from_email_port: int = None, start_engine: bool = True, stop_loss_limit: str = None, stop_loss_order_type: str = None, stop_loss_price: float = None, stop_loss_start_limit: str = None, stop_loss_trigger_gap: str = '0.01', telegram_bot_token: str = None, telegram_send_to: str = None, test: str = None, trading_fee_discount_futures_percent: float = 10.0, trading_fee_discount_margin_percent: float = 25.0, trading_fee_discount_spot_percent: float = 25.0, trading_fee_percent: float = 0.1, trading_fee_use_bnb: bool = False, ubra_manager: BinanceRestApiManager | None = None, ubwa_manager: BinanceWebSocketApiManager | None = None, warn_on_update=True)[source]

Bases: Thread

Manage stop/loss orders on the Binance exchange.

Initialising the class spawns a new thread in which a stop/loss order is placed and trailed until it is completely fulfilled.

Optional support for:
  • Telegram and Email notifications - see send_* and telegram_* parameters.

  • Setting “success” and “error” callback functions - see callback_finished and callback_error parameters.

  • Handling of partial fills - see callback_partially_filled parameter, note that partial fills are

currently not directly handled by the engine. - Smart entry - by setting the engine param to jump-in-and-trail, it offers the possibility to buy spot, future and margin assets with a limit or market order and then to trail a stop/loss order until sold.

Supported exchanges: binance.com, binance.com-testnet, binance.com-futures, binance.com-margin, binance.com-isolated_margin

# Authentication parameters

Parameters:
  • api_key (str) – Provide the Binance API key.

  • api_secret (str) – Provide the Binance API secret.

# Engine Control parameters

Parameters:
  • disable_colorama (bool) – set to True to disable the use of colorama

  • engine – Activate standard trailing stop/loss with trail (default), or activate the experimental smart

entry method by setting jump-in-and-trail :type engine: str :param start_engine: Start the trailing stop loss engine. Default is True :type start_engine: bool :param test: Specify the subsystem to test. Supported options are : “notification”, “binance-connectivity”

and “streams”. The streams test needs a valid exchange and market. Note that this value MUST be None or the engine will NOT start! Only tests will be performed.

# Stop loss parameters

Parameters:

stop_loss_order_type – Order type. Possible values are limit and market. If neither is

set, the algorithm will NOT run. :type stop_loss_order_type: str

Parameters:

stop_loss_price – Price at which the stop/loss order is submitted. Valid until it gets overwritten

by a higher price. :type stop_loss_price: float

Parameters:
  • stop_loss_limit (str) – Stop loss limit in percent or as fixed float value

  • stop_loss_start_limit (str) – The stop/loss order is trailed with the distance defined in stop_loss_limit. For different initial parameters, you can use stop_loss_start_limit. This value will be used instead of the stop_loss_limit value until this value is caught up and then trailed.

  • stop_loss_trigger_gap – Gap between stop price and limit order price, use integer or percent

values. :type stop_loss_trigger_gap: str

Parameters:
  • reset_stop_loss_price (bool) – Reset an existing stop_loss_price and force a new one to be calculated. Only True evaluates to True, anything else is False!

  • keep_threshold – Percentage to keep when selling. Providing % symbol is optional , i.e. “1” is equal to

“1%”. If empty, the full balance is sold. :type keep_threshold: str

Parameters:

borrow_threshold (str) – TODO provide description

# Callback parameters

Parameters:
  • callback_error (function or None) – Callback to call if error occurs.

  • callback_finished (function or None) – Callback to call if stop_loss gets filled.

  • callback_partially_filled (function or None) – Callback to call if stop_loss is only partially filled.

# Exchange & Market parameters

Parameters:
  • exchange (str) – Exchange endpoint to connected to: binance.com, binance.com-futures, binance.com-margin, binance.com-isolated_margin

  • market (str) – Market to enforce stop/loss on, e.g. “ETHBTC”

  • trading_fee_use_bnb (bool) – Whether to use BNB for a discount on trading fees. Default is False. See: https://www.binance.com/en/support/faq/115000583311 for details.

# Logging and Notification parameters

Parameters:
  • current_price (float) – Current market price. Used for logging purposes only, updated automatically from the stream.

  • print_notifications – Print user friendly information to terminal.

  • send_to_email_address (str) – Receiver’s email address

  • send_from_email_address (str) – Sender’s email address

  • send_from_email_password (str) – SMTP auth password

  • send_from_email_server (str) – SMTP server hostname or IP

  • send_from_email_port (int) – SMTP server port

  • telegram_bot_token (str) – Telegram API Token

  • telegram_send_to (str) – Receiver’s UserId (i.e. not username) on Telegram.

# Unicorn Binance Suite parameters

Parameters:
  • warn_on_update (bool) – set to False to disable the update warning

  • ubra_manager (BinanceRestApiManager) – Provide a shared unicorn_binance_rest_api.manager instance

  • ubwa_manager (BinanceWebSocketApiManager) – Provide a shared unicorn_binance_websocket_api.manager instance.

calculate_stop_loss_amount(amount: float) float | None[source]

Calculate the tradeable stop/loss asset amount (= owning and free - trading fee)

Parameters:

amount (float) – The full owning asset amount.

Returns:

float or None

static calculate_stop_loss_price(price: str | float = None, limit: str | float = None) float | None[source]

Calculate the stop/loss price.

Parameters:
  • price (float, str) – Base price used for the calculation

  • limit (float, str) – Stop loss limit in percent or as fixed float value

Returns:

float or None

cancel_open_stop_loss_order() bool[source]

Cancel all open stop/loss orders.

Returns:

bool

create_stop_loss_order(stop_loss_price: float = None, current_price: float = None) bool[source]

Create a stop/loss order!

Parameters:
  • stop_loss_price (float) – Price to set for the SL order.

  • current_price (float) – Current price is optional and only used for logging.

Returns:

bool

get_exchange_info() dict | bool[source]

Get the exchange info.

Returns:

dict or bool

static get_latest_release_info()[source]

Get info about the latest available release :return: dict or False

get_latest_version() str | None[source]

Get the version of the latest available release (cache time 1 hour) :return: str or None

get_open_orders(market: str = None) dict | None[source]

Get the open orders on a given market.

Returns:

dict or None

get_owning_amount(base_asset: str = None) tuple | None[source]

Get the owning amount of the stop/loss asset.

Returns:

tuple (total, free) or None

static get_precision(step_size=None)[source]
get_stop_loss_asset_amount() float | None[source]

Get the current stop/loss asset amount.

Returns:

float

get_stop_loss_asset_amount_free() float | None[source]

Get the free current stop/loss asset amount.

Returns:

float

get_stop_loss_price() float | None[source]

Get the current stop loss price.

Returns:

float

get_stop_loss_trigger_price(stop_loss_price: float = 0.0) float | None[source]

Get the current stop/loss trigger price - if this price gets touched the limit order will get placed in the orderbook.

Returns:

float

get_symbol_info(symbol: str = None) dict | None[source]

Get the symbol info of the stop/loss asset.

Returns:

dict

get_user_agent()[source]

Get the user_agent string “lib name + lib version + python version” :return:

static get_version() str[source]

Get the package/module version :return: str

is_manager_stopping()[source]

Returns True if the manager has a stop request, ‘False’ if not.

Returns:

bool

is_update_available() bool[source]

Is a new release of this package available? :return: bool

process_price_feed_stream(stream_data: dict = None, stream_buffer_name=False) bool[source]

Process the price feed data: Control current price and update stop_loss_price or trigger stop/loss if needed.

Returns:

bool

process_userdata_stream(stream_data: dict = None, stream_buffer_name=False)[source]

Process the received data of the userData stream.

Returns:

bool

static round_decimals_down(number: float, decimals: int = 2) float[source]

Returns a value rounded down to a specific number of decimal places. :param number: The decimal number to round down. :type number: float :param decimals: How many decimals you want to keep. :type decimals: int :return: float

run() None[source]

Start Stop/Loss with provided settings!

Returns:

None

send_email_notification(message: str = None) bool[source]

Send a notification via email!

Parameters:

message (str) – Text to send via email.

Returns:

send_telegram_notification(message: str = None) bool[source]

Send a notification via telegram!

Parameters:

message (str) – Text to send via Telegram.

Returns:

set_stop_loss_price(stop_loss_price: float = None) bool[source]

Set the stop/loss price.

Parameters:

stop_loss_price (float) – Price to set for the SL order.

Returns:

bool

start_streams() bool[source]

Procedure to start the web streams

Returns:

bool

stop() bool[source]

Stop stop_loss! :)

Returns:

bool

stop_manager(close_api_session: bool = True) bool[source]

Stop stop_loss! :)

Returns:

bool

update_stop_loss_asset_amount(total: float = None, free: float = None) tuple[source]

Update the owning asset amount (total, free)!

Parameters:
  • total (float) – Total amount of the stop_loss_asset!

  • free (float) – Free amount of the stop_loss_asset!

Returns:

tuple

update_stop_loss_quantity(total: float = 0.0, free: float = 0.0) float[source]

Calculate and update the stop_loss_quantity!

Parameters:
  • total (float) – Total asset amount

  • free (float) – Free asset amount

Returns:

float

unicorn_binance_trailing_stop_loss.cli module

async unicorn_binance_trailing_stop_loss.cli.cli()[source]

UNICORN Binance Trailing Stop Loss Command Line Interface Documentation

More info: https://oliver-zehentleitner.github.io/unicorn-binance-trailing-stop-loss/cli.html

unicorn_binance_trailing_stop_loss.cli.main()[source]

Module contents