unicorn_binance_trailing_stop_loss package

Submodules

unicorn_binance_trailing_stop_loss.manager module

class unicorn_binance_trailing_stop_loss.manager.BinanceTrailingStopLossManager(binance_public_key: Optional[str] = None, binance_private_key: Optional[str] = None, borrow_threshold: Optional[str] = None, callback_error: Optional[builtin_function_or_method] = None, callback_finished: Optional[builtin_function_or_method] = None, callback_partially_filled: Optional[builtin_function_or_method] = None, engine: str = 'trail', exchange: str = 'binance.com', keep_threshold: Optional[str] = None, market: Optional[str] = None, print_notifications: bool = False, reset_stop_loss_price: bool = False, send_to_email_address: Optional[str] = None, send_from_email_address: Optional[str] = None, send_from_email_password: Optional[str] = None, send_from_email_server: Optional[str] = None, send_from_email_port: Optional[int] = None, start_engine: bool = True, stop_loss_limit: Optional[str] = None, stop_loss_order_type: Optional[str] = None, stop_loss_price: Optional[float] = None, stop_loss_start_limit: Optional[str] = None, stop_loss_trigger_gap: str = '0.01', telegram_bot_token: Optional[str] = None, telegram_send_to: Optional[str] = None, test: Optional[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: Optional[Union[unicorn_binance_rest_api.manager.BinanceRestApiManager, bool]] = False, ubwa_manager: Optional[Union[unicorn_binance_websocket_api.manager.BinanceWebSocketApiManager, bool]] = False, warn_on_update=True)

Bases: threading.Thread

After starting the engine, a stop/loss order is placed and trailed until it is completely fulfilled. If desired, a notification can be sent via email and Telegram afterwards. Then it calls the function passed with the callback_finished parameter or on error it calls the function passed to callback_error.

Partially filled orders are currently not handled by the engine. If you want to react individually to this event, you can use the function provided to callback_partially_filled.

In addition, there is a smart entry option called jump-in-and-trail. This 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

Parameters
  • binance_public_key (str) – Provide the public Binance key.

  • binance_private_key (str) – Provide the private Binance key.

  • borrow_threshold (str) – Provide the private Binance key.

  • callback_error (function or None) – Callback function used if an error occurs.

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

  • callback_partially_filled (function or None) – Callback function used if stop_loss gets partially filled filled.

  • engine (str) – Option trail (default) for standard trailing stop/loss or jump-in-and-trail to activate smart entry function.

  • exchange (str) – Choose the exchange endpoint: binance.com, binance.com-futures, binance.com-margin, binance.com-isolated_margin

  • keep_threshold (str) – If empty we sell the full balance, use integer or percent values.

  • market (str) – The market to enforce stop/loss.

  • print_notifications (bool) – If True the lib is printing user friendly information to terminal.

  • reset_stop_loss_price (bool) – Reset an existing stop_loss_price and calculate a new one. Only True is True, anything else is False!

  • send_to_email_address (str) – Email address of receiver

  • send_from_email_address (str) – Email address of sender

  • send_from_email_password (str) – Password for SMTP auth

  • send_from_email_server (str) – Hostname or IP of SMTP server

  • send_from_email_port (int) – Port of SMTP server

  • start_engine (bool) – Start the trailing stop loss engine. Default is True

  • stop_loss_limit (str) – The limit is used to calculate the stop_loss_price of the highest given price, use integer or percent values.

  • stop_loss_order_type (str) – Can be limit or market - default is None which leads to a stop of the algorithm.

  • stop_loss_price (float) – Set a price to use for stop/loss, this is valid till it get overwritten with a higher price.

  • stop_loss_start_limit (str) – The trailing stop/loss order is trailed with the distance defined in stop_loss_limit. If you want to use a different value at the start, you can specify it with 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 (str) – Gap between stopPrice and limit order price, use integer or percent values.

  • test (str) – Use this to test specific systems like “notification”, “binance-connectivity” and “streams”. The streams test needs a valid exchange and market. If test is not None the engine will NOT start! It only tests!

  • telegram_bot_token (str) – Token to connect with Telegram API.

  • telegram_send_to (str) – Receiver of the message sent via Telegram.

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

  • 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) Optional[float]

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: Optional[Union[str, float]] = None, limit: Optional[Union[str, float]] = None) Optional[float]

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

Cancel all open stop/loss orders.

Returns

bool

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

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() Union[dict, bool]

Get the exchange info.

Returns

dict or bool

static get_latest_release_info()

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

get_latest_version() Optional[str]

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

get_open_orders(market: Optional[str] = None) Optional[dict]

Get the owning amount of the stop/loss asset.

Returns

dict or None

get_owning_amount(base_asset: Optional[str] = None) Optional[tuple]

Get the owning amount of the stop/loss asset.

Returns

tuple (total, free) or None

get_stop_loss_asset_amount() Optional[float]

Get the current stop/loss asset amount.

Returns

float

get_stop_loss_asset_amount_free() Optional[float]

Get the free current stop/loss asset amount.

Returns

float

get_stop_loss_price() Optional[float]

Get the current stop loss price.

Returns

float

get_stop_loss_trigger_price(stop_loss_price: float = 0.0) Optional[float]

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: Optional[str] = None) Optional[dict]

Get the symbol info of the stop/loss asset.

Returns

dict

get_user_agent()

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

static get_version() str

Get the package/module version :return: str

is_update_available() bool

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

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

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: Optional[dict] = None, stream_buffer_name=False)

Process the received data of the userData stream.

Returns

bool

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

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

Start Stop/Loss with provided settings!

Returns

None

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

Send a notification via email!

Parameters

message (str) – Text to send via email.

Returns

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

Send a notification via telegram!

Parameters

message (str) – Text to send via Telegram.

Returns

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

Set the stop/loss price.

Parameters

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

Returns

bool

start_streams() bool

Procedure to start the web streams

Returns

bool

stop() bool

Stop stop_loss! :)

Returns

bool

stop_manager() bool

Stop stop_loss! :)

Returns

bool

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

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

Calculate and update the stop_loss_quantity!

Parameters
  • total (float) – Total asset amount

  • free (float) – Free asset amount

Returns

float

Module contents