Step 4 - Trading Bot in Live Account
Open the recipe for a walk-through on how to code this
🦉
step 4. Trading Bot in Real Account
Open Recipe
Description
This API module is designed to help trading automation over different exchanges, especially in cryptocurrency exchanges, where this frame could be expanded to most mainstream exchanges.
Currently, we have supported the following exchanges:
- Binance
- Coinbase Pro
- Binance US
- Okx
- Kraken
- Kucoin
- Gate
- Bittrex
- Gemini
Set your API key
api_keys = {
"Binance": {
"api_key": "<your api key>",
"api_secret": "<your api key>"
},
"Kraken": {
"api_key": "<your api key>",
"api_secret": "<your api key>"
},
"Kucoin": {
"api_key": "<your api key>",
"api_secret": "<your api key>",
"passphrase": "<your api key>"
},
"Gate": {
"api_key": "<your api key>",
"api_secret": "<your api key>",
},
"BinanceUS": {
"api_key": "<your api key>",
"api_secret": "<your api key>",
},
"Gemini": {
"api_key": "<your api key>",
"api_secret": "<your api key>",
},
"CoinbasePro": {
"api_key": "<your api key>",
"api_secret": "<your api key>",
"passphrase": "<your api key>"
},
'Bittrex': {
'api_key': '<your api key>',
'api_secret': '<your api key>'
},
}
Practice: Get the exchange information
import TradingBot as tb
# bot = tb.Bot(exchange='Binance')
# print(bot.get_ExchangeInfo())
# bot = tb.Bot(exchange='BinanceUS')
# print(bot.get_ExchangeInfo())
bot = tb.Bot(exchange='Kraken')
print(bot.get_ExchangeInfo())
# bot = tb.Bot(exchange='Kucoin')
# print(bot.get_ExchangeInfo())
# bot = tb.Bot(exchange='Gate')
# print(bot.get_ExchangeInfo())
# bot = tb.Bot(exchange='Gemini')
# print(bot.get_ExchangeInfo())
# bot = tb.Bot(exchange='CoinbasePro')
# print(bot.get_ExchangeInfo())
# bot = tb.Bot(exchange='Bittrex')
# print(bot.get_ExchangeInfo())
altname | wsname | aclass_base | base | aclass_quote | quote | lot | pair_decimals | lot_decimals | lot_multiplier | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 1INCHEUR | 1INCH/EUR | currency | 1INCH | currency | ZEUR | unit | 3 | 8 | 1 |
1 | 1INCHUSD | 1INCH/USD | currency | 1INCH | currency | ZUSD | unit | 3 | 8 | 1 |
2 | AAVEAUD | AAVE/AUD | currency | AAVE | currency | ZAUD | unit | 2 | 8 | 1 |
3 | AAVEETH | AAVE/ETH | currency | AAVE | currency | XETH | unit | 4 | 8 | 1 |
4 | AAVEEUR | AAVE/EUR | currency | AAVE | currency | ZEUR | unit | 2 | 8 | 1 |
Practice: Get ticker info
# bot = tb.Bot(exchange='Binance')
# symbol = 'BTCUSDT' # for Binance
# print(bot.get_TickerInfo(symbol))
# bot = tb.Bot(exchange='BinanceUS')
# symbol = 'BTCUSDT' # for BinanceUS
# print(bot.get_TickerInfo(symbol))
bot = tb.Bot(exchange='Gate')
symbol = 'BTC_USDT' # for Gate
print(bot.get_TickerInfo(symbol))
# bot = tb.Bot(exchange='Kraken')
# symbol = 'XBTUSD' # for Kraken
# print(bot.get_TickerInfo(symbol))
# bot = tb.Bot(exchange='Kucoin')
# symbol = 'BTC-USDT' # for Kucoin
# print(bot.get_TickerInfo(symbol))
# bot = tb.Bot(exchange='Gemini')
# symbol = 'btcusd' # for Gemini
# print(bot.get_TickerInfo(symbol))
# bot = tb.Bot(exchange='CoinbasePro')
# symbol = 'BTC-USD' # for CoinbasePro
# print(bot.get_TickerInfo(symbol))
# bot = tb.Bot(exchange='Bittrex')
# symbol = 'BTC-USD' # for Bittrex
# print(bot.get_TickerInfo(symbol))
currency_pair | last | lowest_ask | highest_bid | change_percentage | base_volume | quote_volume | high_24h | low_24h | |
---|---|---|---|---|---|---|---|---|---|
0 | BTC_USDT | 23559.03 | 23559.03 | 23559.02 | 2.25 | 6148.612962762 | 144155966.35896 | 24173.5 | 22666 |
Practice: Get account info
# exchange = 'Kucoin'
# bot = tb.Bot(exchange=exchange, api_keys=api_keys[exchange])
# print(bot.get_AccountInfo())
# exchange = 'BinanceUS'
# bot = tb.Bot(exchange=exchange, api_keys=api_keys[exchange])
# print(bot.get_AccountInfo())
# exchange = 'Binance'
# bot = tb.Bot(exchange=exchange, api_keys=api_keys[exchange])
# print(bot.get_AccountInfo())
exchange = 'Kraken'
bot = tb.Bot(exchange=exchange, api_keys=api_keys[exchange])
print(bot.get_AccountInfo())
# exchange = 'Gate'
# bot = tb.Bot(exchange=exchange, api_keys=api_keys[exchange])
# print(bot.get_AccountInfo())
# exchange = 'Bittrex'
# bot = tb.Bot(exchange=exchange, api_keys=api_keys[exchange])
# print(bot.get_AccountInfo())
# exchange = 'CoinbasePro'
# bot = tb.Bot(exchange=exchange, api_keys=api_keys[exchange])
# print(bot.get_AccountInfo())
# exchange = 'Gemini'
# bot = tb.Bot(exchange=exchange, api_keys=api_keys[exchange])
# print(bot.get_AccountInfo())
Balance | |
---|---|
USDC | 800100.0000 |
ZUSD | 774984.4626 |
MATIC | 42112.0000 |
DOT | 5936.0000 |
UNI | 5509.1800 |
Practice: Create an order
# #! Kraken
# bot = tb.Bot(api_keys = api_keys['Kraken'])
# commands = {
# "public_params":{
# "symbol": "BCHUSD",
# "side":"buy",
# "type":"limit",
# "quantity": "1",
# "price":"50",
# "stopPrice":"",
# },
# "private_params":{
# }
# }
# print(bot.bot_status())
# print(bot.switch_exchange('Kraken'))
# print(bot.bot_status())
# bot.new_Order(commands)
# #! Kucoin
# bot = tb.Bot('Kucoin', api_keys['Kucoin'])
# commands = {
# "public_params":{
# "symbol": "BTC-USDT",
# "side":"buy",
# "type":"limit",
# "quantity": "0.001",
# "price":"12000",
# "stopPrice":"",
# },
# "private_params":{
# "cancelAfter":"",
# }
# }
# print(bot.bot_status())
# bot.new_Order(commands)
# # ! Gate
# bot = tb.Bot('Gate', api_keys['Gate'])
# commands = {
# "public_params":{
# "symbol": "BTC_USDT",
# "side":"buy",
# "type":"limit",
# "quantity": "0.001",
# "price":"12000",
# "stopPrice":"", # The Gate does not support stopPrice
# },
# "private_params":{
# }
# }
# print(bot.bot_status())
# bot.new_Order(commands)
# #! Binance
# bot = tb.Bot('Binance', api_keys['Binance'])
# commands = {
# "public_params":{
# "symbol": "DOGEUSDT",
# "side":"sell",
# "type":"limit",
# "quantity": "100",
# "price":"0.1",
# "stopPrice":"",
# },
# "private_params":{
# "timeInForce": "GTC",
# }
# }
# bot.new_Order(commands)
# #! BinanceUS
# bot = tb.Bot('BinanceUS', api_keys['BinanceUS'])
# commands = {
# "public_params":{
# "symbol": "DOGEUSDT",
# "side":"sell",
# "type":"limit",
# "quantity": "100",
# "price":"0.1",
# "stopPrice":"",
# },
# "private_params":{
# "timeInForce": "GTC",
# }
# }
# bot.new_Order(commands)
# #!Bittrex
# bot = tb.Bot('Bittrex', api_keys['Bittrex'])
# commands = {
# "public_params":{
# "symbol": "1INCH-USDT",
# "side":"BUY",
# "type":"LIMIT",
# "quantity": 10,
# "price":1,
# "stopPrice":"",
# },
# "private_params":{
# "timeInForce": "GOOD_TIL_CANCELLED",
# }
# }
# bot.new_Order(commands)
# #! CoinbasePro
# bot = tb.Bot('CoinbasePro', api_keys['CoinbasePro'])
# commands = {
# "public_params":{
# "symbol": "ETH-USDT",
# "side":"BUY",
# "type":"LIMIT",
# "quantity": 1200,
# "price":1,
# "stopPrice":"",
# },
# "private_params":{
# "timeInForce": "GTC",
# }
# }
# bot.new_Order(commands)
# # ! Gemini
# bot = tb.Bot('Gemini', api_keys['Gemini'])
# commands = {
# "public_params":{
# "symbol": "btcusd",
# "side":"buy",
# "type":"exchange limit",
# "quantity": '0.01',
# "price":'20000',
# "stopPrice":"",
# },
# "private_params":{
# }
# }
# bot.new_Order(commands)
Practice: Get open orders info
# exchange = 'Kucoin'
# bot = tb.Bot(exchange=exchange, api_keys=api_keys[exchange])
# print(bot.get_OpenOrderInfo())
# exchange = 'Gate'
# bot = tb.Bot(exchange=exchange, api_keys=api_keys[exchange])
# print(bot.get_OpenOrderInfo())
exchange = 'Kraken'
bot = tb.Bot(exchange=exchange, api_keys=api_keys[exchange])
print(bot.get_OpenOrderInfo())
# exchange = 'Binance'
# bot = tb.Bot(exchange=exchange, api_keys=api_keys[exchange])
# print(bot.get_OpenOrderInfo())
# exchange = 'BinanceUS'
# bot = tb.Bot(exchange=exchange, api_keys=api_keys[exchange])
# print(bot.get_OpenOrderInfo())
# exchange = 'Bittrex'
# bot = tb.Bot(exchange=exchange, api_keys=api_keys[exchange])
# print(bot.get_OpenOrderInfo())
# exchange = 'CoinbasePro'
# bot = tb.Bot(exchange=exchange, api_keys=api_keys[exchange])
# print(bot.get_OpenOrderInfo())
# exchange = 'Gemini'
# bot = tb.Bot(exchange=exchange, api_keys=api_keys[exchange])
# print(bot.get_OpenOrderInfo())
OrderID | stopprice | limitprice | misc | oflags | |
---|---|---|---|---|---|
0 | ON6CLT-GDZYI-W7YOVV | 0.00000 | 0.00000 | fciq | |
1 | OFLB7Q-YFAUF-U735F2 | 0.000000 | 0.000000 | fciq | |
2 | OMP2G7-FFWZO-GBJ4ZH | 0.00000 | 0.00000 | fciq | |
3 | O5WQIS-2MQY7-ONDU4B | 0.00000 | 0.00000 | fciq | |
4 | OFYZZ7-J3I42-KJJF2X | 0.00000 | 0.00000 | fciq |
Practice: Cancel an order
exchange = 'Gate'
bot = tb.Bot(exchange=exchange, api_keys=api_keys[exchange])
orderid = '12345123'
print(bot.make_CancelOrder(orderid))
# exchange = 'Binance'
# bot = tb.Bot(exchange=exchange, api_keys=api_keys[exchange])
# orderid = '12345123'
# symbol = 'BTCUSDT'
# print(bot.make_CancelOrder(orderid,symbol))
# exchange = 'BinanceUS'
# bot = tb.Bot(exchange=exchange, api_keys=api_keys[exchange])
# orderid = '12345123'
# symbol = 'BTCUSDT'
# print(bot.make_CancelOrder(orderid,symbol))
# exchange = 'Kraken'
# bot = tb.Bot(exchange=exchange, api_keys=api_keys[exchange])
# orderid = '12345123'
# print(bot.make_CancelOrder(orderid))
# exchange = 'Kucoin'
# bot = tb.Bot(exchange=exchange, api_keys=api_keys[exchange])
# orderid = '12345123'
# print(bot.make_CancelOrder(orderid))
# exchange = 'CoinbasePro'
# bot = tb.Bot(exchange=exchange, api_keys=api_keys[exchange])
# orderid = '12345123'
# print(bot.make_CancelOrder(orderid))
# exchange = 'Bittrex'
# bot = tb.Bot(exchange=exchange, api_keys=api_keys[exchange])
# orderid = '12345123'
# print(bot.make_CancelOrder(orderid))
# exchange = 'Gemini'
# bot = tb.Bot(exchange=exchange, api_keys=api_keys[exchange])
# orderid = '12345123'
# print(bot.make_CancelOrder(orderid))
Order Cancelled: {'label': 'ORDER_NOT_FOUND', 'message': 'not found order info id:12345123 count:0'}
True
Put them together and implement them!
Now, with the Trading Bot, we have the capabilities to do the followings:
- Obtain the price of
BTC
- Monitor our account balance.
- Create and cancel the order.
Amazing! The remaining task is simple, code your trading logic as we did during the backtesting process:
if we do not hold position:
if it is a buy signal:
we buy!!! --> Long position
else it is a sell signal:
we sell!!! --> Short position
else if we hold the short position:
we closed our short position
else if we hold the long position:
we closed our long position
To the Moon and Beyond!
Updated over 1 year ago