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

Fix panic button #74

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 4 additions & 3 deletions btb_manager_telegram/binance_api_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ def send_signed_request(key, secret, base_url, http_method, url_path, payload={}
else:
query_string = f"timestamp={get_timestamp()}"

url = f'{base_url}{url_path}?{query_string}&signature="{hashing(secret, query_string)}'
print(f"{http_method} {url}")
url = (
f"{base_url}{url_path}?{query_string}&signature={hashing(secret, query_string)}"
)
params = {"url": url, "params": {}}
response = dispatch_request(key, http_method)(**params)
return response.json()
return str(response.json())


def get_current_price(ticker, bridge):
Expand Down
75 changes: 53 additions & 22 deletions btb_manager_telegram/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,29 @@ def menu(update: Update, _: CallbackContext) -> int:
return MENU


def start(update: Update, _: CallbackContext) -> int:
logger.info("Started conversation.")

keyboard = [["Begin"]]
message = (
f"Hi *{escape_markdown(update.message.from_user.first_name, version=2)}*\!\n"
f"Welcome to _Binace Trade Bot Manager Telegram_\.\n\n"
f"This Telegram bot was developed by @lorcalhost\.\n"
f"Find out more about the project [here](https://github.com/lorcalhost/BTB-manager-telegram)\.\n\n"
f"If you like the bot please [consider supporting the project 🍻](https://www.buymeacoffee.com/lorcalhost)\."
)
reply_markup = ReplyKeyboardMarkup(
keyboard, one_time_keyboard=True, resize_keyboard=True
)
update.message.reply_text(
message,
reply_markup=reply_markup,
parse_mode="MarkdownV2",
disable_web_page_preview=True,
)
return MENU


def edit_coin(update: Update, _: CallbackContext) -> int:
logger.info(f"Editing coin list. ({update.message.text})")

Expand Down Expand Up @@ -552,9 +575,9 @@ def panic(update: Update, _: CallbackContext) -> int:

# Get last trade
cur.execute(
"""SELECT alt_coin_id, crypto_coin_id FROM trade_history ORDER BY datetime DESC LIMIT 1;"""
"""SELECT alt_coin_id, crypto_coin_id, crypto_starting_balance FROM trade_history ORDER BY datetime DESC LIMIT 1;"""
)
alt_coin_id, crypto_coin_id = cur.fetchone()
alt_coin_id, crypto_coin_id, crypto_amount = cur.fetchone()

# Get Binance api keys and tld
user_cfg_file_path = os.path.join(settings.ROOT_PATH, "user.cfg")
Expand All @@ -565,39 +588,47 @@ def panic(update: Update, _: CallbackContext) -> int:
api_secret_key = config.get("binance_user_config", "api_secret_key")
tld = config.get("binance_user_config", "tld")

if update.message.text != i18n_format("keyboard.stop_sell"):
if update.message.text == i18n_format("keyboard.stop_sell"):
params = {
"symbol": f"{alt_coin_id}{crypto_coin_id}",
"side": "SELL",
"type": "MARKET",
"quantity": crypto_amount,
}
message = send_signed_request(
api_key,
api_secret_key,
f"https://api.binance.{tld}",
"POST",
"/api/v3/order",
payload=params,
message = escape_markdown(
"`"
+ send_signed_request(
api_key,
api_secret_key,
f"https://api.binance.{tld}",
"POST",
"/api/v3/order",
payload=params,
)
+ "`",
version=2,
)

if update.message.text != i18n_format("keyboard.stop_cancel"):
params = {"symbol": f"{alt_coin_id}{crypto_coin_id}"}
message = send_signed_request(
api_key,
api_secret_key,
f"https://api.binance.{tld}",
"DELETE",
"/api/v3/openOrders",
payload=params,
message = escape_markdown(
"`"
+ send_signed_request(
api_key,
api_secret_key,
f"https://api.binance.{tld}",
"DELETE",
"/api/v3/openOrders",
payload=params,
)
+ "`",
version=2,
)

if update.message.text != i18n_format("keyboard.stop_bot"):
if update.message.text == i18n_format("keyboard.stop_bot"):
message = i18n_format("killed_bot")
else:
message = (
f"{i18n_format('exited_no_change')}\n"
f"{i18n_format('update.btb.not_updated')}"
)
message = i18n_format('panic.exited')

reply_text_escape_fun(message, reply_markup=reply_markup, parse_mode="MarkdownV2")
return MENU
Expand Down
2 changes: 1 addition & 1 deletion i18n/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ panic:
limit_sell_price: "Limit sell at price:"
error: "❌ Something went wrong, the panic button is not working at this time."
db_error: "❌ Unable to perform the needed actions on the database"

exited: "👌 Exited without closing position. The panic button was not used."

coin_list:
success: "✔ Successfully edited coin list file to:"
Expand Down