1+ """
2+ The Purpose of the RoibalBot Python Program is to create an automated trading bot (functionality) on Binance
3+ Utilized Python-Binance ( https://github.com/sammchardy/python-binance )
4+
5+ Created 4/14/2018 by Joaquin Roibal
6+ V 0.01 - Updated 4/20/2018
7+
8+ Licensed under MIT License
9+
10+ Instructional Youtube Video:
11+
12+ Did you enjoy the functionality of this bot? Tips always appreciated.
13+
14+ BTC:
15+ ETH:
16+
17+ NOTE: All Subsequent Version of Program must contain this message, unmodified, in it's entirety
18+ Copyright (c) 2018 by Joaquin Roibal
19+ """
20+
21+ from binance .client import Client
22+ import time
23+ import matplotlib
24+ from binance .enums import *
25+ import save_historical_data_Roibal
26+ from BinanceKeys import BinanceKey1
27+
28+
29+ api_key = BinanceKey1 ['api_key' ]
30+ api_secret = BinanceKey1 ['api_secret' ]
31+
32+ client = Client (api_key , api_secret )
33+
34+
35+
36+ def run ():
37+ # get system status
38+ #Create List of Crypto Pairs to Watch
39+ list_of_symbols = ['BTCUSDT' , 'ETHUSDT' , 'BNBUSDT' ,'BNBBTC' , 'ETHBTC' , 'LTCBTC' ]
40+ time_horizon = "Short"
41+ Risk = "High"
42+
43+ #Get Status of Exchange & Account
44+ try :
45+ status = client .get_system_status ()
46+ print ("\n Exchange Status: " , status )
47+
48+ #Account Withdrawal History Info
49+ withdraws = client .get_withdraw_history ()
50+ print ("\n Client Withdraw History: " , withdraws )
51+
52+ #get Exchange Info
53+ info = client .get_exchange_info ()
54+ print ("\n Exchange Info (Limits): " , info )
55+ except ():
56+ pass
57+
58+ # place a test market buy order, to place an actual order use the create_order function
59+ # if '1000 ms ahead of server time' error encountered, visit https://github.com/sammchardy/python-binance/issues/249
60+ try :
61+ order = client .create_test_order (
62+ symbol = 'BNBBTC' ,
63+ side = Client .SIDE_BUY ,
64+ type = Client .ORDER_TYPE_MARKET ,
65+ quantity = 100 )
66+ except :
67+ print ("\n \n \n ATTENTION: NON-VALID CONNECTION WITH BINANCE \n \n \n " )
68+
69+ #Get Info about Coins in Watch List
70+ coin_prices (list_of_symbols )
71+ coin_tickers (list_of_symbols )
72+ for symbol in list_of_symbols :
73+ market_depth (symbol )
74+
75+ #get recent trades
76+ trades = client .get_recent_trades (symbol = 'BNBBTC' )
77+ print ("\n Recent Trades: " , trades )
78+ print ("Local Time: " , time .localtime ())
79+ print ("Recent Trades Time: " , convert_time_binance (trades [0 ]['time' ]))
80+
81+ #get historical trades
82+ try :
83+ hist_trades = client .get_historical_trades (symbol = 'BNBBTC' )
84+ print ("\n Historical Trades: " , hist_trades )
85+ except :
86+ print ('\n \n \n ATTENTION: NON VALID CONNECTION WITH BINANCE \n \n \n ' )
87+
88+ #get aggregate trades
89+ agg_trades = client .get_aggregate_trades (symbol = 'BNBBTC' )
90+ print ("\n Aggregate Trades: " , agg_trades )
91+
92+
93+ def convert_time_binance (gt ):
94+ #Converts from Binance Time Format (milliseconds) to time-struct
95+ #From Binance-Trader Comment Section Code
96+ #gt = client.get_server_time()
97+ print ("Binance Time: " , gt )
98+ print (time .localtime ())
99+ aa = str (gt )
100+ bb = aa .replace ("{'serverTime': " ,"" )
101+ aa = bb .replace ("}" ,"" )
102+ gg = int (aa )
103+ ff = gg - 10799260
104+ uu = ff / 1000
105+ yy = int (uu )
106+ tt = time .localtime (yy )
107+ #print(tt)
108+ return tt
109+
110+
111+ def market_depth (sym , num_entries = 10 ):
112+ #Get market depth
113+ #Retrieve and format market depth (order book) including time-stamp
114+ i = 0 #Used as a counter for number of entries
115+ print ("Order Book: " , convert_time_binance (client .get_server_time ()))
116+ depth = client .get_order_book (symbol = sym )
117+ print ("\n " , sym , "\n Depth ASKS:\n " )
118+ print ("Price Amount" )
119+ for ask in depth ['asks' ]:
120+ if i < num_entries :
121+ print (ask )
122+ i += 1
123+ j = 0 #Secondary Counter for Bids
124+ print ("\n " , sym , "\n Depth BIDS:\n " )
125+ print ("Price Amount" )
126+ for bid in depth ['bids' ]:
127+ if j < num_entries :
128+ print (bid )
129+ j += 1
130+
131+
132+ def coin_prices (watch_list ):
133+ #Will print to screen, prices of coins on 'watch list'
134+ #returns all prices
135+ prices = client .get_all_tickers ()
136+ print ("\n Selected (watch list) Ticker Prices: " )
137+ for price in prices :
138+ if price ['symbol' ] in watch_list :
139+ print (price )
140+ return prices
141+
142+
143+ def coin_tickers (watch_list ):
144+ # Prints to screen tickers for 'watch list' coins
145+ # Returns list of all price tickers
146+ tickers = client .get_orderbook_tickers ()
147+ print ("\n Watch List Order Tickers: \n " )
148+ for tick in tickers :
149+ if tick ['symbol' ] in watch_list :
150+ print (tick )
151+ return tickers
152+
153+ def portfolio_management (deposit = '10000' , withdraw = 0 , portfolio_amt = '0' , portfolio_type = 'USDT' , test_acct = 'True' ):
154+ """The Portfolio Management Function will be used to track profit/loss of Portfolio in Any Particular Currency (Default: USDT)"""
155+ #Maintain Portfolio Statistics (Total Profit/Loss) in a file
156+ pass
157+
158+ def Bollinger_Bands ():
159+ #This Function will calculate Bollinger Bands for Given Time Period
160+ #EDIT: Will use Crypto-Signal for this functionality
161+ #https://github.com/CryptoSignal/crypto-signal
162+ pass
163+
164+ def buy_sell_bot ():
165+ pass
166+
167+
168+
169+ #Place Limit Order
170+ """
171+ order = client.order_limit_buy(
172+ symbol='BNBBTC',
173+ quantity=100,
174+ price='0.00001')
175+
176+ order = client.order_limit_sell(
177+ symbol='BNBBTC',
178+ quantity=100,
179+ price='0.00001')
180+ """
181+
182+
183+
184+
185+ """
186+ #trade aggregator (generator)
187+ agg_trades = client.aggregate_trade_iter(symbol='ETHBTC', start_str='30 minutes ago UTC')
188+ # iterate over the trade iterator
189+ for trade in agg_trades:
190+ pass
191+ #print(trade)
192+ # do something with the trade data
193+
194+ # convert the iterator to a list
195+ # note: generators can only be iterated over once so we need to call it again
196+ agg_trades = client.aggregate_trade_iter(symbol='ETHBTC', start_str='30 minutes ago UTC')
197+ agg_trade_list = list(agg_trades)
198+
199+ # fetch 30 minute klines for the last month of 2017
200+ klines = client.get_historical_klines("ETHBTC", Client.KLINE_INTERVAL_30MINUTE, "1 Dec, 2017", "1 Jan, 2018")
201+ #for kline in klines:
202+ #print(kline)
203+ """
204+
205+ #place an order on Binance
206+ """
207+ order = client.create_order(
208+ symbol='BNBBTC',
209+ side=SIDE_BUY,
210+ type=ORDER_TYPE_LIMIT,
211+ timeInForce=TIME_IN_FORCE_GTC,
212+ quantity=100,
213+ price='0.00001')
214+ """
215+
216+ if __name__ == "__main__" :
217+ run ()
0 commit comments