This document introduces how to build a simple trading bot using Python. It outlines communicating with exchanges via APIs, performing trades, analyzing data, and testing without real transactions. The bot queries balances, orders, and market data, applies trading algorithms, and logs statistics. Mock connections allow testing bot decisions against scenario files to validate functionality without financial risk. Overall, Python provides libraries to easily create initial trading bots for learning, even if profits are unlikely.
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Introduction to trader bots with Python
1. Introduction to trader bots
with Python
Thomas Aglassinger
http://roskakori.at
@Taglassinger
https://github.com/roskakori/talks/tree/master/pygraz/traderbot
2. Overview
● Outline a simple trading bot
● Example usages for several Python modules
(most part of the standard library)
● (Probably) Python beginner friendly
4. Limitations
● You won't get rich (but it's fun nevertheless)
● Code does not work anymore due API update
● Masterexchange is going to shut down soon
● Terms of service
6. Overview
● Uses https://masterxchange.com/api.php
(defunct after 2015-11-15)
● Communicate using HTTPS and JSON
● Public queries available to anyone (e.g. current
bids)
● Private queries requiring a personal token
bound to you account (e.g. orders)
7. Query trades
● Query the last 500 trades for maidsafe coins:
https://masterxchange.com/api/v2/trades.php?currency=maid
● Result:
[{"tradeid":"31242","price":"0.00990000","amount":"0.151
10800","date":"1446399439","market":"msc_btc"},
{"tradeid":"31241","price":"0.00990000","amount":"0.099
89200","date":"1446319270","market":"msc_btc"},
{"tradeid":"31240","price":"0.00562223","amount":"0.037
79028","date":"1446309947","market":"msc_btc"}, ...]
8. Print the top 3 trades
import json
import requests
def _without_utf8_bom(text):
return text[3:] if text.startswith('xefxbbxbf') else text
query = requests.get(
'https://masterxchange.com/api/v2/trades.php',
headers={'User-Agent': 'demobot/0.1'},
params={'currency': 'maid'}
)
print('query.status_code =', query.status_code)
if query.status_code < 300:
query_text = _without_utf8_bom(query.text)
print('query_text = %r...' % query_text[:40])
trades = json.loads(query_text)
print('trades =', trades[:3])
10. Configuring the API key
● Private queries require an API key.
● Simple way to manage: configparser
● Store key in a config file
● Read it during startup
12. Read the API key from the config
import configparser
config = configparser.ConfigParser()
config.read('demobot.cfg')
api_key = config.get('demobot', 'api_key')
19. Processing monetary values
● Use decimal instead of float
http://floating-point-gui.de/
● Python has a decimal module:
https://docs.python.org/3/library/decimal.html
● json and configparser only support float
→ convert after reading and before writing
● Bitcoin uses 8 digits after decimal separator
20. Use formatting for decimals
>>> from decimal import Decimal
>>> print(Decimal('0.00000001'))
1E-8
>>> print('%.8f' % Decimal('0.00000001'))
0.00000001
21. Modes of operation
● Advise: only suggest to sell or buy → user has to manually initiate
transactions
● Lower risk for “stupid” transactions
● Might miss opportunities due slow reaction time
● Helpful when trying out a hopefully improved trading algorithm
● Action: automatically sell and buy on market conditions deemed
favorable
● Can react quickly to changes
● Possibility for epic fail on buggy trading algorithms
● Recommendation: reduce risk (but also opportunities) by limiting
amount traded per transaction and hour, stop loss limits etc.
22. Basic bot loop
1.Update own balances
2.Update open orders on the market
3.Apply trading algorithm and decide next action
4.Possibly buy or sell
5.Wait some time
6.Repeat
24. Some simple trading algorithms
● Spread between 2 different but interchangeable items;
e.g. Team Fortress 2's keys and earbuds:
http://icrontic.com/article/tf2-black-market-explained
● Delayed correlation between two items; e.g. stocks for
Coca Cola and Pepsi:
http://www.investopedia.com/university/guide-pairs-trading/pairs-trading-correlation.asp
● Wait for slips from sellers, buy “unusually” cheap items
and resell for “normal” price;
article about such a bot (violating terms of service):
http://diablo3story.blogspot.com.au/2014/07/a-diablo-3-story.html
26. Tracking statistics
● Collect statistics in database
● To debug bot decisions
● To improve trading algorithm
● To monitor market conditions
27. Sqlite
● Robust and stable
● Efficient for single client use
● Easy to set up
● Included with Python:
https://docs.python.org/3/library/sqlite3.html
● Rather creative type system
● “Real” instead of “decimal”
● “int” for timestamp instead of “datetime” type
● Type anarchy concerning comparison
● http://www.sqlite.org/datatype3.html
28. Create a statistics database
def _create_database(self, database_path):
_log.info('connect to database %r', database_path)
result = sqlite3.connect(database_path)
with closing(result.cursor()) as cursor:
cursor.execute("""
create table if not exists balances (
action char(4) not null,
balance_time int not null,
btc real not null,
maid real not null,
price_per_maid real not null,
transferred int not null
)
""")
cursor.execute("""
create index if not exists
idx_balance_time on balances (balance_time)
""")
result.commit()
return result
30. Logging
● More detailed tracking of trading decisions than
database
● But no easy structured analysis
● Use logging Module
https://docs.python.org/3/library/logging.html
● Use RotatingFileHandler
https://docs.python.org/3/library/logging.handler
s.html#rotatingfilehandler
31. Example logging configuration 1/2
# Logging configuration as described in
# <https://docs.python.org/3/howto/logging-cookbook.html>.
[loggers]
keys=root,demobot
[handlers]
keys=console,file
[formatters]
keys=default
[logger_root]
level=DEBUG
handlers=console,file
[logger_demobot]
level=DEBUG
handlers=console,file
qualname=demobot
propagate=0
34. Testing challenges
● Network communication is slow
● Many sites have limits on transactions per
second
● Testing actual orders requires money
35. Mock-up connections with scenarios
● Scenarios are simple text file containing
expected queries and JSON results
● The test case makes a decisions that results in
a query, parses the scenarios JSON result, and
makes the next decision and query
● Scenarios can be maintained by domain
experts
36. Example scenario file
# got maid, no open orders
# the bot should create a maid-order for 500maid and 0.10000001btc/maid
private/balances
{
"balances": {
"total":{"btc":0.9,"maid":500},
"available" {"btc":0,"maid":0}
},
"error_message":"", "error_code":0
}
private/openedOrders
{
"open_orders": [],
"error_message":"", "error_code":0
}
orderbook
[
{"market":"maid_btc","type":"sell","amount":"120.00000000","price":"0.20000000","date_created":"1401077847"},
{"market":"maid_btc","type":"buy","amount":"270.00000000","price":"0.10000000","date_created":"1418566454"}
]
private/createOrder
37. Scenario implementation
● Bot constructor gets a connection
● Class HttpConnection → query() returns JSON
from Masterexchange
● Class ScenarioConnection → query() checks
that function matches next line in scenario file
and if so returns next JSON from it
Audience feedback: try Gherkin!
https://pypi.python.org/pypi/gherkin3