Skip to content

Commit

Permalink
Merge pull request #254 from mraniki/dev
Browse files Browse the repository at this point in the history
minor: cleanup quote/account balance and position
  • Loading branch information
mraniki authored Apr 23, 2023
2 parents 5a6ae5f + 3d77dc2 commit c7187f2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 59 deletions.
11 changes: 8 additions & 3 deletions config/example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ DEX_NAME = 'UNISWAP_V2'
DEX_CHAINID = 1
DEX_WALLET_ADDRESS = ''
DEX_PRIVATE_KEY = ''
DEX_BASE_TRADING_SYMBOL = ''

DEX_TESTMODE = 'False'
DEX_RPC = 'https://rpc.ankr.com/optimism"'
DEX_ROUTER = '0x5c69bee701ef814a2b6a3edd4b1652cb9cc5aa6f' #uniswap v2
DEX_BASE_TRADING_SYMBOL = 'USDC'
DEX_BLOCK_EXPLORER_API = ''
DEX_PROTOCOL = ''
DEX_AMOUNT_TRADING_OPTION = ''
DEX_PROTOCOL = '1inch'
DEX_AMOUNT_TRADING_OPTION = '1'

CEX_NAME = 'binance'
CEX_API = ''
Expand All @@ -32,6 +36,7 @@ CEX_PASSWORD = ''
CEX_TESTMODE = 'True'
CEX_DEFAULTTYPE = 'spot'
CEX_ORDERTYPE = 'MARKET'
CEX_BASE_TRADING_SYMBOL = ''

[DEV]
LOGLEVEL = 'DEBUG'
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ readme = "README.md"
"Issues" = "https://github.com/mraniki/tt/issues"

[tool.poetry.dependencies]
python = "^3.10"
python = ">=3.10"
asyncio = "*"
dynaconf = "*"
apprise = "*"
Expand All @@ -25,7 +25,7 @@ ccxt = "*"
fastapi = "*"
uvicorn = "*"
ping3 = "*"
ttp = "*"
findmyorder = "*"

[tool.poetry.dev-dependencies]
python-semantic-release = "*"
Expand Down
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
python-dotenv>=0.21.1
ccxt
web3>=v6.1.0
web3>=v6.2
apprise
python-telegram-bot>=20.1
telethon
Expand All @@ -11,4 +10,5 @@ fastapi
uvicorn
dxsp>=1.2
ttp
dynaconf
dynaconf
findmyorder
87 changes: 36 additions & 51 deletions src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
##=============== import =============

import logging, sys, json, requests, asyncio
from fastapi import FastAPI
import uvicorn

from config import settings

import findmyorder as fmo
import ccxt
from dxsp import DexSwap

Expand All @@ -23,11 +28,6 @@

#Utils
from ping3 import ping
from ttp import ttp

#API
from fastapi import FastAPI
import uvicorn


#🧐LOGGING
Expand All @@ -51,9 +51,7 @@ async def parse_message(self,msg):
wordlistsize = len(wordlist)
logger.debug(msg=f"parse_message wordlist {wordlist} len {wordlistsize}")
try:
logger.debug(msg="orderparsing NEW")
orderparsing = await order_parsing(msg)
logger.debug(msg="orderparsing COMPLETED")
logger.debug(msg=f"orderparsing {orderparsing}")
except:
pass
Expand Down Expand Up @@ -111,14 +109,9 @@ async def parse_message(self,msg):
logger.warning(msg="Parsing exception")

async def order_parsing(message_to_parse):
logger.info(msg=f"order_parsing V2 with {message}")
logger.info(msg=f"order_parsing V2 with {message_to_parse}")
try:
order_template = """ {{ direction }} {{ symbol }} sl={{ stoploss }} tp={{ takeprofit }} q={{ quantity }} """
parser = ttp(data=message_to_parse, template=order_template)
parser.parse()
result = parser.result(format="json")
logger.debug(msg=f"result {result}")
return result[0]
return fmo.identify(message_to_parse)
except Exception as e:
logger.warning(msg=f"Order parsing error {e}")

Expand Down Expand Up @@ -149,7 +142,7 @@ async def notify(msg):

#💱EXCHANGE
async def load_exchange():
logger.info(msg=f"Setting up exchange")
logger.info(msg="Setting up exchange")
global ex_type
global ex_name
global ex_test_mode
Expand All @@ -170,6 +163,7 @@ async def load_exchange():
ex_test_mode = True
ex_name = settings.CEX_NAME
markets = cex.load_markets()
logger.debug(msg=f"CEX object created {cex}")
ex_type = 'cex'
except Exception as e:
await handle_exception(e)
Expand All @@ -190,13 +184,13 @@ async def load_exchange():
amount_trading_option = settings.DEX_AMOUNT_TRADING_OPTION

try:
dex = DexSwap(chain_id=chain_id,wallet_address=wallet_address,private_key=private_key,block_explorer_api=block_explorer_api) ,
logger.info(msg=f"dexswap object dex {dex}")
dex = DexSwap(chain_id=chain_id,wallet_address=wallet_address,private_key=private_key,block_explorer_api=block_explorer_api)
logger.debug(msg=f"DEX object created{dex}")
ex_type = 'dex'
except Exception as e:
await handle_exception(e)
else:
logger.info(msg=f"no CEX or DEX config found")
logger.warning(msg="no CEX or DEX config found")
return

#📦ORDER
Expand All @@ -207,77 +201,72 @@ async def execute_order(direction,symbol,stoploss,takeprofit,quantity):
response = f"⬇️ {symbol}" if (direction=="SELL") else f"⬆️ {symbol}"
if ex_type == 'dex':
order = execute_order.dex(direction=direction,symbol=symbol,stoploss=stoploss,takeprofit=takeprofit,quantity=quantity)
response+= order['confirmation']
order_confirmation+= order['confirmation']
else:
if (await get_account_balance()=="No Balance"):
await handle_exception("Check your Balance")
return
asset_out_quote = float(cex.fetchTicker(f'{symbol}').get('last'))
totalusdtbal = await get_account_basesymbol_balance() ##cex.fetchBalance()['USDT']['free']
totalusdtbal = await get_base_trading_symbol_balance() ##cex.fetchBalance()['USDT']['free']
amountpercent = (totalusdtbal)*(float(quantity)/100) / asset_out_quote
order = cex.create_order(symbol, price_type, direction, amountpercent)
response+= f"\n➕ Size: {order['amount']}\n⚫️ Entry: {order['price']}\nℹ️ {order['id']}\n🗓️ {order['datetime']}"
return response
order_confirmation+= f"\n➕ Size: {order['amount']}\n⚫️ Entry: {order['price']}\nℹ️ {order['id']}\n🗓️ {order['datetime']}"
return order_confirmation

except Exception as e:
logger.error(msg=f"Exception with order processing {e}")
await handle_exception(e)
await handle_exception(f"Exception with order processing {e}")
return

async def get_quote(symbol):
if ex_type == 'dex':
asset_out_quote = await dex.get_quote(symbol)
response=f"🦄{asset_out_quote} USD\n🖊️{chainId}: {symbol}"
return f"🦄{asset_out_quote} USD\n🖊️{chainId}: {symbol}"
else:
price= cex.fetch_ticker(symbol.upper())['last']
response=f"🏛️ {price} USD"
return response

asset_out_quote = cex.fetch_ticker(symbol.upper())['last']
return f"🏛️ {price} USD"


#🔒PRIVATE
async def get_account_balance():
balance =f"🏦 Balance\n"
try:
if ex_type == 'dex':
bal = get_account_balance.dex()
msg = {bal}
balance += get_account_balance.dex()
else:
bal = cex.fetch_free_balance()
bal = {k: v for k, v in bal.items() if v is not None and v>0}
sbal = "".join(f"{iterator}: {value} \n" for iterator, value in bal.items())
if not sbal:
sbal = "No Balance"
msg = f"{sbal}"
return msg
raw_balance = cex.fetch_free_balance()
filtered_balance = {k: v for k, v in bal.items() if v is not None and v>0}
balance += "".join(f"{iterator}: {value} \n" for iterator, value in filtered_balance.items())
if not balance:
balance += "No Balance"
return balance
except Exception:
return

async def get_account_basesymbol_balance():
async def get_base_trading_symbol_balance():
try:
if ex_type == 'dex':
return dex.get_basecoin_balance()
else:
return cex.fetchBalance()['USDT']['free']
return cex.fetchBalance()[f'{CEX_BASE_TRADING_SYMBOL}']['free']
except Exception:
await handle_exception("Check your balance")

async def get_account_position():
try:
position = f"📊 Position\n"
if ex_type == 'dex':
open_positions = await dex.get_account_position()
else:
positions = cex.fetch_positions()
open_positions = [p for p in positions if p['type'] == 'open']
logger.debug(msg=f"open_positions {open_positions}")
return open_positions or 0
return position+=open_positions
except Exception:
await handle_exception("Error when retrieving position")

async def get_account_margin():
try:
if ex_type == 'dex':
return
else:
return
return
except Exception as e:
await handle_exception(e)
return
Expand Down Expand Up @@ -313,14 +302,10 @@ async def help_command():
return f"Environment: {defaultenv} Ping: {bot_ping}ms\nExchange: {ex_name} Sandbox: {ex_test_mode}\n{bot_menu_help}"

async def account_balance_command():
bal =f"🏦 Balance\n"
bal += await get_account_balance()
return bal
return await get_account_balance()

async def account_position_command():
position = f"📊 Position\n"
position += await get_account_position()
return position
return await get_account_position()

async def quote_command(symbol):
return await get_quote(symbol)
Expand Down

0 comments on commit c7187f2

Please sign in to comment.