Skip to content

Commit

Permalink
Add account cleaning jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Wu Fei authored and Carlos Wu Fei committed Sep 28, 2023
1 parent 0036d9d commit e47442f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
21 changes: 12 additions & 9 deletions api/account/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,34 +337,37 @@ async def get_balance_series(self, end_date, start_date):
)
return resp

def clean_balance_assets(self):
async def clean_balance_assets(self):
"""
Check if there are many small assets (0.000.. BTC)
if there are more than 5 (number of bots)
transfer to BNB
"""
balance = self.get_raw_balance()
data = json.loads(balance.body)["data"]
data = self.signed_request(url=self.account_url)
assets = []
for item in data:
if item["asset"] not in ["USDT", "NFT", "BNB"]:
assets.append(item["asset"])
for item in data["balances"]:
if item["asset"] not in ["USDT", "NFT", "BNB"] and float(item["free"]) > 0:
assets.append(item["free"])

if len(assets) > 5:
self.transfer_dust(assets)
resp = json_response_message("Sucessfully cleaned balance.")
else:
resp = json_response_error("Amount of assets in balance is low. Transfer not performed.")
resp = json_response_error("Amount of assets in balance is low. Transfer not needed.")

return resp

def disable_isolated_accounts(self):
async def disable_isolated_accounts(self):
"""
Check and disable isolated accounts
"""
info = self.signed_request(url=self.isolated_account_url, payload={})
for item in info["assets"]:
if float(item["liquidatePrice"]) == 0:
self.disable_isolated_margin_account(item["symbol"])
resp = json_response_message("Sucessfully finished disabling isolated margin accounts.")
msg = "Sucessfully finished disabling isolated margin accounts."
else:
msg = "Disabling isolated margin account not required yet."

resp = json_response_message(msg)
return resp
15 changes: 12 additions & 3 deletions api/market_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import time

from apscheduler.schedulers.background import BackgroundScheduler
from api.account.routes import disable_isolated
from account.routes import disable_isolated
from streaming.streaming_controller import StreamingController
from account.assets import Assets
from websocket import (
Expand All @@ -26,7 +26,7 @@
assets = Assets()

scheduler.add_job(
func=assets.store_balance(),
func=assets.store_balance,
trigger="cron",
timezone="Europe/London",
hour=1,
Expand All @@ -35,14 +35,23 @@
)

scheduler.add_job(
func=assets.disable_isolated_accounts(),
func=assets.disable_isolated_accounts,
trigger="cron",
timezone="Europe/London",
hour=2,
minute=0,
id="disable_isolated_accounts",
)

scheduler.add_job(
func=assets.clean_balance_assets,
trigger="cron",
timezone="Europe/London",
hour=3,
minute=0,
id="clean_balance_assets",
)

scheduler.start()
atexit.register(lambda: scheduler.shutdown(wait=False))

Expand Down

0 comments on commit e47442f

Please sign in to comment.