Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async #165

Merged
merged 2 commits into from
Dec 18, 2023
Merged

Async #165

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.6.6] - 2023-12-18
### Added
- [interfaces] async api

## [1.6.5] - 2023-10-27
### Added
- [get_service] config param
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# OctoBot-Services [1.6.5](https://github.com/Drakkar-Software/OctoBot-Services/tree/master/docs/CHANGELOG.md)
# OctoBot-Services [1.6.6](https://github.com/Drakkar-Software/OctoBot-Services/tree/master/docs/CHANGELOG.md)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/31a1caa6e5384d80bf890dba5c9b5e4b)](https://app.codacy.com/gh/Drakkar-Software/OctoBot-Services?utm_source=github.com&utm_medium=referral&utm_content=Drakkar-Software/OctoBot-Services&utm_campaign=Badge_Grade_Dashboard)
[![PyPI](https://img.shields.io/pypi/v/OctoBot-Services.svg)](https://pypi.python.org/pypi/OctoBot-Services/)
[![Github-Action-CI](https://github.com/Drakkar-Software/OctoBot-Services/workflows/OctoBot-Services-CI/badge.svg)](https://github.com/Drakkar-Software/OctoBot-Services/actions)
Expand Down
2 changes: 1 addition & 1 deletion octobot_services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
# License along with this library.

PROJECT_NAME = "OctoBot-Services"
VERSION = "1.6.5" # major.minor.revision
VERSION = "1.6.6" # major.minor.revision
12 changes: 12 additions & 0 deletions octobot_services/interfaces/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@
get_all_open_orders,
cancel_orders,
cancel_all_open_orders,
async_cancel_orders,
async_cancel_all_open_orders,
has_trader,
has_real_and_or_simulated_traders,
sell_all_currencies,
sell_all,
async_sell_all_currencies,
async_sell_all,
set_enable_trading,
get_total_paid_fees,
get_trades_history,
Expand All @@ -64,10 +68,12 @@
get_global_portfolio_currencies_amounts,
get_global_portfolio_currencies_values,
trigger_portfolios_refresh,
async_trigger_portfolios_refresh,
get_global_profitability,
get_reference_market,
get_all_positions,
close_positions,
async_close_positions,
)
from octobot_services.interfaces.web import (
AbstractWebInterface,
Expand All @@ -90,10 +96,14 @@
"get_all_open_orders",
"cancel_orders",
"cancel_all_open_orders",
"async_cancel_orders",
"async_cancel_all_open_orders",
"has_trader",
"has_real_and_or_simulated_traders",
"sell_all_currencies",
"sell_all",
"async_sell_all_currencies",
"async_sell_all",
"set_enable_trading",
"get_total_paid_fees",
"get_trades_history",
Expand All @@ -106,6 +116,7 @@
"get_global_portfolio_currencies_amounts",
"get_global_portfolio_currencies_values",
"trigger_portfolios_refresh",
"async_trigger_portfolios_refresh",
"get_global_profitability",
"get_reference_market",
"AbstractBotInterface",
Expand All @@ -115,4 +126,5 @@
"NO_CURRENCIES_MESSAGE",
"get_all_positions",
"close_positions",
"async_close_positions",
]
20 changes: 10 additions & 10 deletions octobot_services/interfaces/bots/abstract_bot_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,10 @@ def get_command_fees(markdown=False):
return result_str

@staticmethod
def get_command_sell_all_currencies():
async def get_command_sell_all_currencies():
try:
interfaces.cancel_all_open_orders()
nb_created_orders = len(interfaces.sell_all_currencies())
await interfaces.async_cancel_all_open_orders()
nb_created_orders = len(await interfaces.async_sell_all_currencies())
if nb_created_orders:
return f"Currencies sold in {nb_created_orders} order{'s' if nb_created_orders > 1 else ''}."
else:
Expand All @@ -223,10 +223,10 @@ def get_command_sell_all_currencies():
return f"An error occurred: {e.__class__.__name__}"

@staticmethod
def get_command_sell_all(currency):
async def get_command_sell_all(currency):
try:
interfaces.cancel_all_open_orders(currency)
nb_created_orders = len(interfaces.sell_all(currency))
await interfaces.async_cancel_all_open_orders(currency)
nb_created_orders = len(await interfaces.async_sell_all(currency))
if nb_created_orders:
return f"{currency} sold in {nb_created_orders} order{'s' if nb_created_orders > 1 else ''}."
else:
Expand Down Expand Up @@ -338,8 +338,8 @@ def get_command_start(markdown=False):
return "Hello, I'm OctoBot, type /help to know my skills."

@staticmethod
def set_command_portfolios_refresh():
return interfaces.trigger_portfolios_refresh()
async def set_command_portfolios_refresh():
return await interfaces.async_trigger_portfolios_refresh()

@staticmethod
def set_command_risk(new_risk):
Expand All @@ -356,8 +356,8 @@ def set_command_risk(new_risk):
def set_command_stop():
interfaces.get_bot_api().stop_bot()

def set_command_pause(self):
interfaces.cancel_all_open_orders()
async def set_command_pause(self):
await interfaces.async_cancel_all_open_orders()
interfaces.set_enable_trading(False)
self.paused = True

Expand Down
12 changes: 12 additions & 0 deletions octobot_services/interfaces/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,21 @@
get_all_open_orders,
cancel_orders,
cancel_all_open_orders,
async_cancel_orders,
async_cancel_all_open_orders,
)
from octobot_services.interfaces.util.position import (
get_all_positions,
close_positions,
async_close_positions,
)
from octobot_services.interfaces.util.trader import (
has_trader,
has_real_and_or_simulated_traders,
sell_all_currencies,
sell_all,
async_sell_all_currencies,
async_sell_all,
set_enable_trading,
get_total_paid_fees,
get_trades_history,
Expand All @@ -65,6 +70,7 @@
get_global_portfolio_currencies_amounts,
get_global_portfolio_currencies_values,
trigger_portfolios_refresh,
async_trigger_portfolios_refresh,
)
from octobot_services.interfaces.util.profitability import (
get_global_profitability,
Expand All @@ -86,10 +92,14 @@
"get_all_open_orders",
"cancel_orders",
"cancel_all_open_orders",
"async_cancel_orders",
"async_cancel_all_open_orders",
"has_trader",
"has_real_and_or_simulated_traders",
"sell_all_currencies",
"sell_all",
"async_sell_all_currencies",
"async_sell_all",
"set_enable_trading",
"get_total_paid_fees",
"get_trades_history",
Expand All @@ -102,8 +112,10 @@
"get_global_portfolio_currencies_amounts",
"get_global_portfolio_currencies_values",
"trigger_portfolios_refresh",
"async_trigger_portfolios_refresh",
"get_global_profitability",
"get_reference_market",
"get_all_positions",
"close_positions",
"async_close_positions",
]
20 changes: 14 additions & 6 deletions octobot_services/interfaces/util/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,20 @@ def get_all_open_orders():


def cancel_orders(order_ids):
return interfaces.run_in_bot_main_loop(async_cancel_orders(order_ids))


async def async_cancel_orders(order_ids):
removed_count = 0
if order_ids:
for order_id in order_ids:
for exchange_manager in interfaces.get_exchange_managers():
if trading_api.is_trader_existing_and_enabled(exchange_manager):
try:
removed_count += 1 if interfaces.run_in_bot_main_loop(
trading_api.cancel_order_with_id(exchange_manager, order_id, wait_for_cancelling=False)
removed_count += 1 if (
await trading_api.cancel_order_with_id(
exchange_manager, order_id, wait_for_cancelling=False
)
) else 0
except (trading_errors.OrderCancelError, trading_errors.UnexpectedExchangeSideOrderStateError) \
as err:
Expand All @@ -52,11 +58,13 @@ def cancel_orders(order_ids):


def cancel_all_open_orders(currency=None):
return interfaces.run_in_bot_main_loop(async_cancel_all_open_orders(currency=currency))


async def async_cancel_all_open_orders(currency=None):
for exchange_manager in interfaces.get_exchange_managers():
if trading_api.is_trader_existing_and_enabled(exchange_manager):
if currency is None:
interfaces.run_in_bot_main_loop(
trading_api.cancel_all_open_orders(exchange_manager))
await trading_api.cancel_all_open_orders(exchange_manager)
else:
interfaces.run_in_bot_main_loop(
trading_api.cancel_all_open_orders_with_currency(exchange_manager, currency))
await trading_api.cancel_all_open_orders_with_currency(exchange_manager, currency)
6 changes: 5 additions & 1 deletion octobot_services/interfaces/util/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,15 @@ def get_global_portfolio_currencies_values() -> dict:


def trigger_portfolios_refresh():
return interfaces.run_in_bot_main_loop(async_trigger_portfolios_refresh())


async def async_trigger_portfolios_refresh():
at_least_one = False
for exchange_manager in interfaces.get_exchange_managers():
if trading_api.is_trader_existing_and_enabled(exchange_manager):
at_least_one = True
interfaces.run_in_bot_main_loop(trading_api.refresh_real_trader_portfolio(exchange_manager))
await trading_api.refresh_real_trader_portfolio(exchange_manager)

if not at_least_one:
raise RuntimeError("no real trader to update.")
8 changes: 6 additions & 2 deletions octobot_services/interfaces/util/position.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,17 @@ def get_all_positions():


def close_positions(positions_descs):
return interfaces.run_in_bot_main_loop(async_close_positions(positions_descs))


async def async_close_positions(positions_descs):
removed_count = 0
if positions_descs:
for positions_desc in positions_descs:
for exchange_manager in interfaces.get_exchange_managers():
if trading_api.is_trader_existing_and_enabled(exchange_manager):
removed_count += 1 if interfaces.run_in_bot_main_loop(
trading_api.close_position(
removed_count += 1 if (
await trading_api.close_position(
exchange_manager,
positions_desc["symbol"],
trading_enums.PositionSide(positions_desc["side"]),
Expand Down
14 changes: 10 additions & 4 deletions octobot_services/interfaces/util/trader.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,26 @@ def has_real_and_or_simulated_traders():


def sell_all_currencies():
return interfaces.run_in_bot_main_loop(async_sell_all_currencies())


async def async_sell_all_currencies():
orders = []
for exchange_manager in interfaces.get_exchange_managers():
if trading_api.is_trader_existing_and_enabled(exchange_manager):
orders += interfaces.run_in_bot_main_loop(
trading_api.sell_all_everything_for_reference_market(exchange_manager))
orders += await trading_api.sell_all_everything_for_reference_market(exchange_manager)
return orders


def sell_all(currency):
return interfaces.run_in_bot_main_loop(async_sell_all(currency))


async def async_sell_all(currency):
orders = []
for exchange_manager in interfaces.get_exchange_managers():
if trading_api.is_trader_existing_and_enabled(exchange_manager):
orders += interfaces.run_in_bot_main_loop(
trading_api.sell_currency_for_reference_market(exchange_manager, currency))
orders += await trading_api.sell_currency_for_reference_market(exchange_manager, currency)
return orders


Expand Down