Skip to content

Commit

Permalink
balance.py new feature
Browse files Browse the repository at this point in the history
Added in balance.py module a new function to retrieve market value at specific point of time or range
  • Loading branch information
JustForDev2020 authored May 24, 2020
1 parent d3bb642 commit 2154576
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 30 deletions.
4 changes: 2 additions & 2 deletions app/BinanceAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def get_trades(self, market, limit=50):
params = {"symbol": market, "limit": limit}
return self._get_no_sign(path, params)

def get_kline(self, market):
def get_klines(self, market, interval, startTime, endTime):
path = "%s/klines" % self.BASE_URL
params = {"symbol": market}
params = {"symbol": market, "interval":interval, "startTime":startTime, "endTime":endTime}
return self._get_no_sign(path, params)

def get_ticker(self, market):
Expand Down
84 changes: 56 additions & 28 deletions balance.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

sys.path.insert(0, './app')

from BinanceAPI import BinanceAPI

import config
from datetime import timedelta, datetime
from BinanceAPI import BinanceAPI

class Binance:

Expand All @@ -22,51 +22,61 @@ def balances(self):
if float(balance['locked']) > 0 or float(balance['free']) > 0:
print('%s: %s' % (balance['asset'], balance['free']))

def balance(self, asset="BTC"):
def balance(self, asset='BTC'):
balances = self.client.get_account()

balances['balances'] = {item['asset']: item for item in balances['balances']}

print(balances['balances'][asset]['free'])

def orders(self, symbol, limit):
orders = self.client.get_open_orders(symbol, limit)
print(orders)

def tickers(self):

return self.client.get_all_tickers()

def server_time(self):

return self.client.get_server_time()

def openorders(self):

return self.client.get_open_orders()

def profits(self, asset='BTC'):

coins = self.client.get_products()

for coin in coins['data']:

if coin['quoteAsset'] == asset:

orders = self.client.get_order_books(coin['symbol'], 5)

if len(orders['bids'])>0 and len(orders['asks'])>0:

orders = self.client.get_order_books(coin['symbol'], 5)
if len(orders['bids'])>0 and len(orders['asks'])>0:
lastBid = float(orders['bids'][0][0]) #last buy price (bid)
lastAsk = float(orders['asks'][0][0]) #last sell price (ask)

if lastBid!=0:
profit = (lastAsk - lastBid) / lastBid * 100
else:
profit = 0

print('%6.2f%% profit : %s (bid: %.8f / ask: %.8f)' % (profit, coin['symbol'], lastBid, lastAsk))

else:
print('---.--%% profit : %s (No bid/ask info retrieved)' % (coin['symbol']))


def market_value(self, symbol, kline_size, dateS, dateF="" ):
dateS=datetime.strptime(dateS, "%d/%m/%Y %H:%M:%S")

if dateF!="":
dateF=datetime.strptime(dateF, "%d/%m/%Y %H:%M:%S")
else:
dateF=dateS + timedelta(seconds=59)

klines = self.client.get_klines(symbol, kline_size, int(dateS.timestamp()*1000), int(dateF.timestamp()*1000))

if len(klines)>0:
for kline in klines:
print('[%s] Open: %s High: %s Low: %s Close: %s' % (datetime.fromtimestamp(kline[0]/1000), kline[1], kline[2], kline[3], kline[4]))

return

try:

while True:
Expand All @@ -77,40 +87,58 @@ def profits(self, asset='BTC'):
print('2 >> Scan profits')
print('3 >> List balances')
print('4 >> Check balance')
print('------------------')
print('-----------------------------')
print('5 >> Market value (specific)')
print('6 >> Market value (range)')
print('-----------------------------')
print('0 >> Exit')
print('\nEnter option number:')

option = input()

if option=='1':
print('Enter symbol: (i.e. XVGBTC)')

print('Enter pair: (i.e. XVGBTC)')
symbol = input()

# Orders
print('%s Orders' % (symbol))

m.orders(symbol, 10)

elif option=='2':
print('Enter Asset (i.e. BTC, ETH, BNB)')

asset = input()

print('Enter asset (i.e. BTC, ETH, BNB)')
asset = input()
print('Profits scanning...')

m.profits(asset)

elif option=='3':
m.balances()

elif option=='4':
print('Enter asset: (i.e. BTC)')
symbol = input()
print('%s balance' % (symbol))

m.balance(symbol)

elif option=='5':
print('Enter pair: (i.e. BTCUSDT)')
symbol = input()
print('Enter date/time: (dd/mm/yyyy hh:mm:ss)')
dateS = input()

print('%s balance' % (symbol))
klines=m.market_value(symbol,"1m", dateS)

m.balance(symbol)
elif option=='6':
print('Enter pair: (i.e. BTCUSDT)')
symbol = input()
print('Enter start date/time: (dd/mm/yyyy hh:mm:ss)')
dateS = input()
print('Enter end date/time: (dd/mm/yyyy hh:mm:ss)')
dateF = input()
print('Enter interval as in exchange (i.e. 5m, 1d):')
interval = input()

klines=m.market_value(symbol, interval, dateS, dateF)

elif option=='0':
break
Expand Down

0 comments on commit 2154576

Please sign in to comment.